Posts | Comments

Planet Arduino

7-Segment LED

7-Segment LED

Fun with Arduino contest entry submitted by Jeff.

For a while now I’d been interested in things like networked objects and gps/location-awareness, and I think that soon enough we’re going to see a lot of ‘intelligent objects’ out and about in the world.

I love the idea of being able to actually build something like that, but there’s a lot I don’t know about the hardware side of things. What little electronics I studied was long in the past; I’d done a little coding for microcontrollers, but only a little. But there was something about the Arduino that made it seem more accessible.

So my wonderful wife, who is wonderful if I didn’t mention that, bought me the “Arduino Advanced Starter Kit” and the accompanying book “Getting Started with the Arduino” for my birthday. Yay!

I have had enough hardware exposure that nothing in the package was a total mystery, and I understood the concept of breadboarding a circuit, so it only took maybe 15 minutes to get the software installed, a basic circuit built (ok, I stuck a LED onto two pins on the board), and wrote my first ‘hello world’ blinky-light program. The Arduino uses the aptly named Arduino language — it seems to be C with a light wrapper and a library, so it came pretty naturally.

Following along with the exercises in Getting Started, I experimented with the with the various inputs and outputs, got some hands-on-experience building the circuits out. It didn’t take me long at all to start wandering off-script and just building out random ideas that involved an LED and a button.

After a few hours, I thought it would be fun to do something a little more challenging. I went and went and picked up an 7-segment LED (Radio Shack part 276-075). With the pin designations, it was pretty easy to hook the 7 segments up to 7 outputs on the Arduino board.

On the software side, I mapped the segments onto bits – bit 0 maps to anode A, bit 1 to anode B, etc. — so the state of all 7 segments can be represented in a single byte. With the relatively small amount of memory available, compact representations are a good thing. Going the other way, I can look at that byte, and read each of the bits with bitRead() function, then set the LED segment appropriately.

Now that I had a working numeric LED, I needed a value to show on it. Borrowing a simple example from Getting Started, I wired up a photoresistor and had it report it’s state to Arduino’s analog input 0.

Arduino and 7-Segment LED

Arduino and 7-Segment LED

The analog inputs report a value from 0-1023 but my LED can only display 0-9, so I used the map() function to convert the input range to the output range and get the corresponding value back out — then display that. To help stabilize the number and keep it from flickering between values too quickly, I only update the LED if the value has changed, and then hold for a moment.

The full source code is below:

#define PHOTO 0

void setup()
{
  pinMode(0, OUTPUT); // -> Anode A, bit 0 in the definitions
  pinMode(1, OUTPUT); // -> Anode B, bit 1
  pinMode(2, OUTPUT); // -> Anode C, bit 2
  pinMode(3, OUTPUT); // -> Anode D, bit 3
  pinMode(4, OUTPUT); // -> Anode E, bit 4
  pinMode(5, OUTPUT); // -> Anode F, bit 5
  pinMode(6, OUTPUT); // -> Anode G, bit 6
}

// encode the on/off state of the LED segments for the characters
// '0' to '9' into the bits of the bytes
const byte numDef[10] = { 63, 6, 91, 79, 102, 109, 124, 7, 127, 103 };

// keep track of the old value
int oldVal = -1;

void loop()
{
  // grab the input from the photoresistor
  int input = analogRead(PHOTO);

  // convert the input range (0-1023) to the range we can
  // display on the LED (0-9)
  int displayVal = map(input, 0, 1023, 0, 9);

  // if the value has changed, then update the LED and hold for a
  // brief moment to help with the debouncing.
  if (displayVal != oldVal)
  {
    setSegments( numDef[displayVal] );
    delay(250);
  }
}

void setSegments(byte segments)
{
  // for each of the segments of the LED
  for (int s = 0; s < 7; s++)
  {
    int bitVal = bitRead( segments, s ); // grab the bit
    digitalWrite(s, bitVal); // set the segment
  }
}

Jeff also has another great Arduino tutorial for

Using the 555 timer as an external clock for the Arduino

Share/Bookmark

Comments are closed.



  • Newsletter

    Sign up for the PlanetArduino Newsletter, which delivers the most popular articles via e-mail to your inbox every week. Just fill in the information below and submit.

  • PlanetArduino is powered by WordPress. Design by Jasone.it. Valid XHTML   •   Valid CSS
    47 queries. 1,810 seconds.