Tuesday, December 29, 2009

The First 10 Things Everyone Does with their New Arduino

A lot of people got Arduinos for Christmas (and holiday) presents. Welcome to the world of Open Source Hardware, DIY electronic tinkering, and learning physical computing and programming... it's a long journey, and I started on it about 2 years ago, and it's been a pretty fun adventure.

As an aside, this is my favorite week of the year, because I have just enough time off from the day job to do some serious hacking. And that's what I'll be doing over the next couple days, getting ready for some serious fun in January.

I don't usually write beginner's guides, because I'm not that good at it, and also because I try to do harder and harder stuff with my Arduino every time, and try to push the limits. There are lots of Arduino tutorials and startup and setup guides, but most of them assume the read has tons of wires, sensors, and - most importantly - a few years of programming experience and are comfortable doing some DIY tinkering. Few of them assume you just have an Arduino, just the Arduino, and only the Arduino... which is what lots of people get and give as Christmas presents (if this past couple of weeks is any indication!)...

So this is my top ten list of fun things to do with a newly-acquired Arduino - I do quite a bit of tinkering with the Arduino, Arduino MEGA, and Illuminato Genesis boards, in fact these days I carry them in my travel bag pretty much everywhere I go, just to play around. The Arduino is like my swiss army knife of electronics, and the Illuminato Genesis is my favorite board for when I want to hack devices, reverse engineer protocols, or do generally "hackier" things.

1 - Make it blink

This one is pretty obvious... just download the Arduino IDE (or the Antipasto Arduino Aardvark IDE for Windows or Mac if you want to play around with some extra features), and then download this code into the main section, run it, and download it to the Arduino board.

2 - Make it blink faster (or slower)

This is almost universally the first thing anyone tries to do immediately after the blinky sketch is up and running... get comfortable with the settings by screwing around with the blink settings, by tampering around with the delay and timer settings. Whenever I'm teaching someone or showing someone else how to use an Arduino, I always suggest changing the timing, or copying and pasting the digitalWrite() and delay() lines several times to get comfortable with the programming language, and to see your code reflected in real time instantly...

3 - Make a function to make the Arduino blink

This is almost like the step above, except that now you're getting comfortable with putting your blink inside a real C function wrapper. The major difference is that I made the void myBlink( void) function. As soon as people get the hang of functions, usually a light goes off in their heads as they figure out the power of modern programming languages: code modularity (a fancy term for little chunks of reusable code):

4 - Send stuff over the serial port from the Arduino to your PC

You can blink an LED, and you can put code inside functions. That means you've now mastered about 90% of the hardest parts of using an Arduino. Now, you want to have the Arduino communicate with the computer. This is a tiny little program that sets a variable, called "counter", sets it to the number 0, then prints a little message "Hello over Serial" from the Arduino to the PC, and then loops the counter from 1 to 25, printing the number over the serial connection. Just type this code into the Arduino IDE window, download it to the Arduino, and then open up the Serial Monitor (that's one of the little blue buttons underneath the menu bar):

5 - Merge the LED blinking with the Serial

I think it's pretty cool how quickly things can add up when you have the basics down. This is basically the same thing as the previous sketches, only added together. Every time the counter reaches past 25 and resets to 0, it calls the blink-LED function, which is the same one we wrote up above - so the functions really are re-usable...

6 - Send something from the computer to the Arduino... and back...

Now we want to send something from the PC to the Arduino. Put this code onto the Arduino, then open the Serial Monitor again, and this time in the little box next to the send button that shows up, type a character, and press the Send button. The character will get echoed back. For instance if you type and send "a", you'll see the "> a" come back. This is literally the same thing as the serial read example except with no comments, which get in the way. By the way, I also encourage anyone new to Arduino to type this out by hand, instead of just copying and pasting, because it seems to give a better idea for what each line of code does.

7 - Combine the Serial sending with blinking

This time, we'll add the code function for the LED blinking, and a couple lines of code that basically say, if the character we sent from the PC to the Arduino is the letter "a", then blink the LED light in the myBlink() function:

8 - Put everything together in one big mega program - count, blink, echo, and blink again

This one's pretty self-explanatory. Now, we've just added all the components together. All the variables get defined up at the top, the serial connection gets initialized in the setup() function, the counter cycles from 1-25 over the serial connection, blinks every time it reaches the top, checks to see if the PC sent a character, if we did, the Arduino will echo the character back to the PC, and if that character is the letter "a", the Arduino will blink again.

9 - Copy and paste the following code:

The Arduino is all about copying and pasting code. This is hugely helpful, because it speeds up hacking, but it's kind of like how word-checker is destroying my abaility to spell words :-) If you always just copy and paste stuff, it's no fun because half of feeling comfortable programming and hacking with the Arduino is feeling comfortable pulling code out of thin air, and experimenting with the syntax... but that's just my opinion!

int counter = 0;
int ledPin = 13;
int incomingByte = 0;

void setup() {
Serial.begin(9600); delay(100);

void loop()
if(counter == 25) { counter = 0; myBlink(); };

if (Serial.available() > 0) {
incomingByte = Serial.read();
Serial.print("> ");
if (incomingByte == 'a') { myBlink(); };


void myBlink( void) {
digitalWrite(ledPin, HIGH); delay(500);
digitalWrite(ledPin, LOW); delay(500);

10 - Meet people on the web who use Arduinos

Arduino is about the interweb, and forums, and chat rooms, and blogs, and twitters, and youtube instructables and vimeo facebook wallposts and all kinds of "social media" that I have no idea how to use, but am trying to learn.

It's also about being an open community, and instead of so many types of technologies and new things that are inaccessible and hard to learn, Arduino is about sharing and hacking with other people. So in that spirit, please email me, or twitter me, or whatever!

2010 is going to be a year filled with Arduino hacking for me, and my goal is to meet as many other fellow hackers as possible, and to show off more cool Arduino hacks :-)

inthebitz at gmail


Unknown said...

This is a good list of basic ardweenie functions. I'd almost forgot about wrapping functions outside of the main loop. Thanks.

Nick said...

Really, this is a good post! Always wanted to learn how to extend beyond the "loop" function (kinda like Gianteye).

Anyways, thanks!

Matt said...

@gianteye - thanks :) wrapping outside functions is a pretty nifty trick that c guys pick up instantly, but takes a while if you're not used to it...

@mintybot, thanks :-)

oh, and thanks to everyone who emailed - i'll probably need to do an update one of these days too

Unknown said...

These practice projects are great additions to the blinking LED starter project for us beginners. Thanks for sharing!

acidblue said...

Holy cow !You can wrap a function?
Pretty cool, I'll have to try that

Jan said...

Thank you for these. Got my first arduino today and tried out your list and learned alot. Even had to fix a prob with the com port...

Glutnix said...

Good post! I've been teaching code for a long time now, and I wholeheartedly agree with your suggestions about typing everything out yourself at first.

Because I've got code-confidence from doing PHP and other exciting database stuff, and a little experience with Processing, learning Arduino and re-learning the electronics basics didn't take long. Here's my first non-trivial project I did with the Arduino: a reaction game!


I really had fun making it, and it was easier than I thought! Now I need ideas for my next project!