Blog

Arduino 7-segment output

I hit the electronics store the other day - sadly, they didn’t have any opto-isolators, so no MIDI input experiments this time round. But I scored some 74HC595 ICs, for the driving of LED displays and other digital outputs.

Here’s some code to run a common-cathode 7-segment display using the 595 (the Arduino site has a tutorial on how to hook up the 595, send data to it, and daisy-chain multiple ICs). I’ve included a list of which pins to connect to which anodes on the display in the character set file, below. Displays differ in their pinouts, so if you’ll likely need to do some testing to figure out how the pins are arranged.

charset_7seg.h - I spent a few minutes scribbling out a character set, and here it is as an #includeable file. (I put it in lib/targets/libraries/LED7Segment for the compiler to find.)

And here’s a test sketch. It lets you flip through the characters using a potentiometer on analog pin 0, but if you don’t have one handy you could easily adapt it to display the characters one by one.

#include <charset_7seg.h>

// Digital pins connected to 74HC595
int latchPin = 8;
int clockPin = 12;
int dataPin = 11;

// Analog input pin
int potPin = 0;

void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}

void loop() {
int i, j;

// Read the value from the sensor and convert to 7-bit value
i = analogRead(potPin) >> 3;

digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, MSBFIRST, ledCharSet[i]);
digitalWrite(latchPin, 1);
}

Shortcut URL

Comments

Just what I needed, many thanks!!!!!!

Posted by David Darke on 24 May 2009 at 8:03 PM

In your file charset_7seg.h ; you should define the array as “const” so that it doesn’t eat up RAM and is stored in the program address space instead:

const byte ledCharSet[128] = { ... }

Posted by maathieu on 3 June 2009 at 1:44 PM

I just made this yesterday with a 74ls164.

Posted by Sullivan Brophy on 21 January 2010 at 6:40 PM

i got a problem using this librarie. i use arduino017 under OS X.4.

error message wile compiling:
/Volumes/USER/simonjuif/Documents/Arduino/libraries/
charset_7seg/charset_7seg.h:29: error: ‘byte’ does not name a type

anyone an idea? thanks

Posted by pest23 on 28 January 2010 at 9:52 AM

Thanks for charset_7seg.h. I just started playing with Arduino & electronics and I’ve used it in the example code in one of my blog entries. It describes how to do the same thing with common anode displays by building an “active-low” circuit, inverting the bits, etc…

Link

Posted by Ole on 13 October 2010 at 1:27 PM

Commenting is not available in this weblog entry.