Contents
The Arduino Microcontroller
The Arduino Uno is a microcontroller made and designed in Italy. Have a quick look at Adafruit's decription of this and related devices (there are many). A microcontroller is not the same as a //microprocessor//. The Raspberry Pi is a microprocessor. You can run an operating system on the Pi. The Arduino is far simpler; think of it as a programmable chip. You write the program on a computer, compile it (translate it into machine code), upload it onto the Arduino, and the Arduino will attempt to follow your instructions. Because of its simplicity it is not as powerful as something like a Raspberry Pi, but it is far more robust, easy to use, is better able to control and accept input from devices, and is cheaper!
You will not need to set up any software as this has already been done. But for an introduction of the system and software have a look at the Getting Started page on the Adafruit website.
Why use an Arduino Board? Watch the Arduino Movie on Vimeo. (But not while you are in the lab!!!) The Wikipedia page on the Arduino is also quite cool and contains a load of information.
Basics
What's on the board?
We will be using the Arduino Uno. This is the most commonly used board for hobby projects. For a full description of the board, chips, memory, I/O pins, voltages, etc., see the Arduino Uno page.
Getting Started & Troubleshooting
Need help setting up an Arduino board, or something has gone wrong? Have a look at the Getting Started with Arduino page for information.
Foundations
The best way to learn about an Arduino board is to use it. So first try the examples given below. At least the simple ones. Once you've got your hands wet, you may want to know more about it: What is a //sketch//? What are the pins on an Arduino board for? This sort of information can be found on the Foundations page. The most useful bits are:
- Programming Techniques:
Example Sketches
Here are some example projects that introduce you to various aspects of the Arduino. If you are doing an SCM laboratory experiment, you may need to do only a few of these. Make sure you know which ones are necessary!
Blink: (PDF) This is a very simple example. There is nothing to construct here as the Arduino already contains all the components.
Blink 2: (PDF) This example is similar to the previous one, but includes more exercises and more information. Try out the exercises to make sure you understand how the C-language works.
Control a single LED: (PDF) In this example we learn how to control a single LED (light emitting diode). This time we will create our first circuit. You will also use a breadboard here. If you have not seen one before, this is a good place to start.
More on LEDs: (PDF) Use this tutorial to learn more about LEDs and why we need to connect them to resistors. There are some cool projects here, but try them only if you have the components at hand. Also use this tutorial if you are unsure of how to read resistor codes and use breadboards.
Digital Inputs: (PDF) Here we learn how to use button input to control an LED.
A (pseudo-)Theremin: (PDF) This is a device that can be played by waving your hand in front of it. This small project involved sensing light and using this reading to control the pitch of a piezo speaker.
Programming the Arduino and using the Serial Interface: (PDF) These issues are described here in some detail, together with exercises. If you want to be really sure of what you are doing try this exercise. Another version of the Serial Monitor tutorial from Adafruit is here (PDF).
Working with Inputs: (PDF) Learn how to program the Arduino to use inputs from switches. You will have used switches in the Digital Inputs tutorial above, but this one contains much more detailed information and more challenging projects. You could regard this as a more advanced version of the earlier project.
You will find more examples at the Examples page on the arduino.cc site.
Debugging your sketchs
There is no simple recipe for debugging sketches. But a few conventions will help:
- Keep it simple! Don't overcomplicate a sketch.
Comments: These can significantly improve the readibility of your sketch.
Indent: The C-language doesn't force you to indent (unlike Python), but indentation greatly improves the readibility and therefore makes debugging easier. Which one of the following do you find easier to read? This one?
if (abs(speedLeft) < abs(speedRight)) { digitalWrite(pinRled, HIGH); digitalWrite(pinLled, LOW); } else if (abs(speedLeft) > abs(speedRight)) { digitalWrite(pinRled, LOW); digitalWrite(pinLled, HIGH); } else { digitalWrite(pinRled, LOW); digitalWrite(pinLled, LOW); }
Or this one, without indents?
if (abs(speedLeft) < abs(speedRight)){ digitalWrite(pinRled, HIGH); digitalWrite(pinLled, LOW); }else if (abs(speedLeft) > abs(speedRight)){ digitalWrite(pinRled, LOW); digitalWrite(pinLled, HIGH); }else{ digitalWrite(pinRled, LOW); digitalWrite(pinLled, LOW); }
Variable & Function Names: Keep these descriptive. Ideally, each variable and function name will reflect the function of the variable/function. Don't use xxx for the voltage_level. Likewise, the function name sense_levels is far better than my_function.
Sensible Pin Names: You will probably use many pins on the Arduino board. In many of the examples give above, these pin numbers are often //hard-coded// into the sketch, what is, pin numbers are scattered all over the sketch. This may be OK for a short sketch, but is not god practice for a large one. Here it is best to declare pins at the start, and use descriptive names. So int pin_rightLED = 6; tells us exactly what the function of pin 6 is: it controls the right LED. This also makes it easy to modify your circuit: say you move the right LED to pin 7, now all you need do is change one line in your sketch to read int pin_rightLED = 7;. That's easier than modifying lines all over the code, isn't it? See how pins are labeled in the above code example.
Serial Monitor: The Serial Monitor can be used to see what's going on in your sketch and is probably the most powerful way to debug a sketch. This is so important that it has its own page.
Reference Material
The most comprehensive reference for the Arduino is the arduino.cc website. Everything you need is likely here. But here are some specific pages that you will probably need to reference more often:
Have a look at the language reference as this is the first place to go when you get stuck and need to either debug a program (they call them //sketches//) or add a new language feature to your sketch.