My relationship with computers is a love-hate one. They are too complex and fragile to be reliable and always let you down when you most need them. But there is a miniature computer I really love: the Micro Controller Unit (a.k.a. MCU). They are simple and reliable, because they do one thing and they do it well. For us hobbyist the two most well known are Microchip Technologies’ PIC and Atmel’s AVR. I dabbled with PICs before, but they are a pain to program and require special hardware to do so. Enter Arduino. These guys from Italy took Atmel’s AVR chips and put them on a board with an easy to use USB interface and an integrated development environment, which uses a dialect of C. Weehee, could it get any better?
MCUs are just like Legos: add some sensors and an LCD and you have made yourself a weather station. Add some wheels and a motor and you created a robot. The possibilities are endless. But just like Legos my Arduino projects have been build and then taken apart for the next project. This is about to change now. My loop antenna needed a way to switch between the 7, 14 and 28 loop turns and it had to do so remotely. “Well, use relays” you would rightly say and those happen to be one of the easiest things to hook up to an Arduino. Let me show you.
Two wires for power and one control line (here hooked up to the Arduino input/output “pin” number seven) so simple enough. But it won’t do a thing until you tell the Arduino to do something, using a program. For writing such programs there are computer languages, and they are just that: languages. Learn how to interpret a language into your own and you’re good to go. It might scare you off, but then isn’t electronics a kind of language as well? Remember, I honestly can’t call myself a programmer, but with a guide book and some advice from the experts I can go a long way in making something exciting. Don’t believe me? Let’s start with the basics.
Arduino programs are made up of three blocks of code:
1 – a block to declare and define the things you need later
2 – a block to set up those things
3 – a block that does stuff with those things over and over and over again
Can you spot the three blocks in this example?
The first block features only one line: “
int relayPin = 7;“. Let’s put that in plain English: we use pin 7 of the Arduino to connect the relay to, and instead of “pin 7” we now call it “relayPin”. And because “7” is an integer we also declare that.
Every pin on the Arduino board can be used to either get signals in or out, so that’s what we define in the setup block: set the
pinMode of the
Since we set the
relayPin as an output pin we can activate it and that is what happens in the loop block. First we write to the
relayPin and make it active, or
HIGH. Then we have a slight delay of 1000 milliseconds, after which we write to
relayPin and make it inactive, or
LOW. After another 1000 millisecond delay the whole sequence in the loop block starts all over again. After compiling and uploading the program to the Arduino the result looks like this….
It might not seem very useful to have an oscillating relay, so we need other ways to control it. I’ll cover that next time.
In the mean time, Arduinos are pretty cheap and together with a breadboard, a bunch of wires and some LEDs you can already start playing around. At Banggood.com you can get a bare Arduino Uno for US$3.99. This is a knock-off version, but they work equally well. A whole starter kit is only a little more, so it won’t break your bank.
On the internet there are tons of tutorials, YouTube videos and forums to help you with your first Arduino/MCU steps. I’m sure that after the initial steps MCUs won’t be so intimidating anymore. So, try it and have fun.