Friday, February 27, 2015

Step by step Arduino blocks - Installment 1 - welcome and blink an LED

In this series I'll be starting from the absolute basics of the Arduino, in each installment I hope to cover either a basic technique such as serial debugging or how to connect some type of device.

The first few will be extremely basic; this one just covers getting up and running and blinking an LED (the standard starting point). Later on I will be interfacing LCD text and perhaps color graphics displays, motors, sensors, etc.

I have ordered a whole raft of very inexpensive sensors off eBay - my hope is to provide you with building blocks that you can use together to enable whatever it is you can dream up.

Here's the companion video for this post:  

What's an Arduino?

Arduino is a very popular and inexpensive way to get into creating electronic gadgets. It's so inexpensive that you might use one simply to blink an LED or make a motor move around, but it's also so powerful that they can, on their own, completely control complex machinery. The hardware and software are both open source.  Basic Arduinos cost as little as $4 and the software to program them is free.

At this point in early 2015, there are undoubtedly hundreds of Arduino variants; though most are general purpose, there are Arduinos designed for specific purposes as varied as decorative clothing lighting or controlling a 3D printer.

When I first started playing with Arduino a few years ago, the "Duemilanove" (2009 in Italian, the year it was introduced) was THE one in general use, so I'll use that for at least the first few lessons.  However, you can use whatever one you want.

Arduino Duemilanove

You'll need an Arduino (see sources at the end) a USB cable to connect it, and the software - available at  Download the version appropriate for your computer and install it.

You also should obtain a breadboard and some connecting wires for it - again, see sources below.  In the following photo, you will see a breadboard, an Arduino, some jumper wires, an LED and a resistor.  The resistor is there to keep the LED from drawing too much power, overheating and burning out.  I'm using a 330 ohm resistor which is about right for running a small LED from 5 volts, but anything from 150 to 500 ohms would work just fine, LEDs are generally pretty tolerant.

The breadboard is really your best friend when building circuits. You simply insert wires into it, and the wire is connected to others via spring clips under the surface.  Each column of 5 holes vertically as seen in the photo above are connected together, but separate from the ones to the left and right.

Building your circuit

Below you see an example of a circuit built on a breadboard.  The red wire on the left goes into a hole, the long wire of the LED goes into a hole in the same column, so they are electrically connected.  The short wire of the LED goes into the next hole, one of the resistor wires connects to that, and the other lead is connected to the black wire in the same way.

Here I am using the Arduino simply to provide power to make the LED light up. You should do this first, before we start writing any software. This will keep you from going crazy later trying to get the LED to blink simply to find out afterward that the LED was in backward or something.  If the LED does not light when you do this, try flipping it around the other way.  Putting an LED in backward does not damage it, it just doesn't work.

As you can see above, the red wire goes into the hole that is labelled in white on the board as "power 5V" and the black one into the one labelled "Gnd" - all these holes do is to provide power for you to use to run things.  Hook up your Arduino like this, then attach the USB cable to give it power.  If you hook everything up as shown the LED will light up.

(your computer will probably load the drivers at this time too assuming this is the first time you've plugged in the board)

Now we're going to make the LED blink.

Start up the Arduino software.  Select File, Examples, 01.Basics, Blink.  You will get a new Arduino window with some software already entered for you.  Here's a quick rundown of what it means.

First, in case you're not familiar with programming, the following is a function:
void functionname(){}
A function is a bit of code that you can run to do something and then return.
The first word there, "void" means what the function returns back to the software that called it.  "Void" means that it doesn't return anything at all - it just does something and finishes.
The second thing is the name of the function.  A couple of functions are required in Arduino, but you can create more functions of your own if you want to.
The parenthesis tell the software what information is given TO the function by the software calling it.  Since they're empty in this case, nothing is given to it, it's just called with no information.
The curly brackets {} is where you put your code.  Anything inside there is what runs when you call the function.

Every Arduino program must contain at least two basic functions: setup and loop.

Setup is run once when the Arduino is first turned on (or reset).  Loop is run over and over forever after setup finishes.

In setup() you will see the following line of code:

  pinMode(13, OUTPUT);

"pinMode" is a function that the Arduino software provide to you that you call to tell it what you want the various pins on the Arduino to do.

The pins on the Arduino can be "rewired" by your program to perform many different tasks - they can be outputs that cause things to happen, they can be inputs that will receive information from sensors that your program can do things with, they can communicate with other smart devices to perform complex tasks.

This line of code tells the Arduino that you want to use pin 13 as a simple digital output pin.

Now look at loop():

void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second

The stuff after the // are simply comments, the computer ignores them.  They're there to help the programmer remember what each line does (or to explain it to someone else). 

These comments are pretty good.  

digitalWrite sends a value to the pin, and delay waits a given number of milliseconds.

So this code sets pin 13 to "HIGH" which means that power is coming out of it (this makes the LED attached to that pin light up).  Then it waits 1000 milliseconds (one second), puts a "LOW" on it (turning off the power and the LED) and waits another second.  Then it leaves the "loop" function, but remember that "loop" runs over and over forever, so it then immediately just goes right back up to the top.

Assuming your Arduino is still attached to the USB (if not, plug it in now), you need to use "Tools/Board" in the Arduino software and pick the kind of board you have, then use "Tools/Serial port" to pick the right serial port.  It's probably the highest numbered one there if you have just plugged in your Arduino for the first time.

Click the round button in the upper left with the right arrow on it (next to the round button with the checkbox).  This should "compile" the software and send it to your Arduino board.

Now take the red wire out of the 5V connector, and move it to pin 13, which is over on the other side of the Arduino board, close to the USB connector (on my Arduino - yours may be different) as shown:

Your LED should now be blinking once per second.

Unfortunately Radio Shack is no longer with us, so you will most likely have to go to the internet to get your parts.  The following are just examples, not requirements.

eBay is a very good and inexpensive source for electronics parts.
Here's a very cheap starter - it includes a few wires, some LEDs and resistors, and Arduino and a small breadboard:

You can go to eBay and search for "arduino kit" and come up with everything from the above small starter to very comprehensive kits with dozens of sensors, motors, actuators, LEDs and displays.  Let your curiosity and budget dictate, but I would start out relatively small unless you really know right away what you're after.  eBay will still be there next month when you have learned out to do all the first set of stuff and are ready to go further.

1 comment:

Karl Loeffler said...

Thanks for Posting John. I plan on buying an Uno (come on, $4...). I've watched dozens of videos on Arduino, and at least I get information from yours...the others drone on for 30 minutes about what you can do with it, then in 60 seconds create a project that took 12 hours to do...

New printer: JGAurora A3

This week I decided I'd had it with all the other printers in my stable.  The CTC is stable and decent but it just bugs me (can't st...