Not just Raspberry Pi – playing with an Arduino

I have had an Arduino board sitting in one of my electronics boxes for months. While chatting with an ex colleague yesterday evening, it occurred to me that I had never actually done anything with it. Although it may seem that Arduino and Raspberry Pi are in some sense competitors, I'm seeing more and more projects which use the best features of both devices to make something "more than the sum of the parts". The Gertboard (which I have also never got around to doing anything with yet) has an on-board AVR microcontroller, as do several others. "Zoe", the retro-cased hydroponic garden controller which I saw at the Norwich group also used a combination of the two devices.

All of which adds up to it being high time that I got myself familiar with Arduino use and programming.

Following my feeble attempt at trying to drive an LED from the slice of Pi/O, this time I was determined to make something that I could actually see. I did get out my Saleae logic analyser, though, just to make sure that things were working as I expected.

For this exercise I decided to program the Arduino using my Windows PC. I followed the installation instructions including manually installing the driver, and loaded up an example program (or "sketch" as they seem to be called). This one was a "blink" example which flashed an on-board LED once per second.

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

There's not much to it, it's mostly comments.

The language is very like C, but seems to have a bunch of functions implicitly available, without the need to include header files, which is quite pleasant.

Having seen this working, I wanted to do something with actual I/0. I grabbed my little breadboard, still with the LED from my previous attempt, replaced the resistor with something smaller, and connected it to the first digital output pin (and ground) of the Arduino. Then I changed the sketch to write to pin 1 rather than pin 13.

When I started it up, the LED did not light. I even switched off the room lights to check, but no dice. If you are ahead of me you will probably have spotted my mistake already. It took me a litle longer, despite the fact that it is the kind of thing that I deal with every day at work.

To find the problem I connected the logic analyser which indeed showed me that the output pin was not being switched. By this point I had a glimmer of an idea, so I connected the logic analyser to the first few pins. Soon enough I saw the second pin happily rising and falling. Of course. It's C, the first pin is pin 0. D'oh.

Once I had connected the LED to the correct pin the Arduino happily flashed it for me. But I wanted to do something a bit more interesting. So I fetched two other 30-year-old LEDs from my parts box and wired them to pins 0 and 2. I stuck with the single resistor, wiring the LEDs in a "common cathode" pattern.

Arduino traffic lights

I changed the sketch to:

int red_flag = 1;
int amber_flag = 2;
int green_flag = 4;

void setup() {                
  pinMode(0, OUTPUT);     
  pinMode(1, OUTPUT);     
  pinMode(2, OUTPUT);     
}

void show_colour(int flags) {
  if (flags & red_flag) digitalWrite(0, HIGH);
  if (flags & amber_flag) digitalWrite(1, HIGH);
  if (flags & green_flag) digitalWrite(2, HIGH);
  delay(1000);
  digitalWrite(0, LOW);
  digitalWrite(1, LOW);
  digitalWrite(2, LOW);
}

void loop() {
  show_colour(red_flag);
  show_colour(red_flag | amber_flag);
  show_colour(green_flag);
  show_colour(amber_flag);
}

A bit longer. I hope you can work out what it does.

Then I "uploaded" the sketch to the device. The Arduino IDE helpfully tells me that the binary is 1238 bytes. Pretty small. When it started running I was getting roughly the correct output, but the red LED was switching on with the green, rather than with the amber. This baffled me for a bit. I checked with the logic analyser and the signals seemed correct

traffic-logic

Then I got it. I had put the red LED the wrong way round on the breadboard! This had puzzled me because I kind of expected that if the LED was the wrong way round it would just not light up. However, because of the "common cathode" wiring, when another LED was switched on, there was enough voltage across the resistor to power the LED with the Arduino pin low.

Once I had spotted the problem I quickly swapped the red LED round and had a lovely traffic light sequence.

No related content found.

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *