Simple Arduino 7 segment display example

It appears that there are many different ways to interface a seven segment LED display to the Arduino; the humble 4511, 74HC595 shift register and a multitude of fancy (and expensive) serial ICs such as the MAX7219.

I decided to use a 4511 driver IC for this purpose as it was the only suitable chip I had lying around. It saves a few pins over driving the display directly from the Arduino and also works from a wider voltage range so can be used for driving large display modules.

LED display in action

What you need

  • Arduino board
  • 4511 LED decoder/driver IC
  • Common cathode 7 segment LED display
  • 7x 390 ohm resistors
  • 1x 150k resistor
  • 1x 10uF capacitor
  • Breadboard or other prototyping platform
  • Wire

Making it

This circuit will fit onto a Protoshield kitted with a mini breadboard. I highly recommend you consider the Protoshield if you don’t have one for your Arduino; it’s a very tidy platform for whipping up simple circuits like this.

Example schematic

Note that C1 and R8 are optional; these cause the 4511 to blank the display for a second or two at switch on. This is to prevent the display producing garbage while the Arduino loads its bootloader. If you omit them, connect pin 4 to 5v.

It doesn’t really matter what output pins you connect the Arduino to on the 4511 as long as you configure it in the code.

Remember to connect pins 8 and 16 on the 4511 to 0v and 5v respectively.

Code

Here’s some source code to drive it. It displays a simple count from 0-9 in an endless loop.

The display() function can be reused for other projects.

// Pin matching from 4511 to Aduino
const int LedA = 5;
const int LedB = 2;
const int LedC = 3;
const int LedD = 4;

int count = 0;

void setup()
{
  // Configure the pins connected to the 4511 as outputs
  pinMode(LedA, OUTPUT);
  pinMode(LedB, OUTPUT);
  pinMode(LedC, OUTPUT);
  pinMode(LedD, OUTPUT);
}

void loop()
{
  Display(count);  // Displays the current count value

  // Count from 0 to 9 every half second
  count = count + 1;

  if (count > 9)
  {
     count = 0;
  }

  delay(500);
}

// This function outputs the supplied value to the display.
void Display(int n)
{
  // Start with a range check - remember we can only display
  // from 0 to 9! :)

  if ( n > 9 )
  {
    n = 9;
  }

  if ( n < 0 )
  {
    n = 0;
  }

  // The rest from here is basic decimal to binary conversion

  if ( n / 8 == 1 )
  {
    digitalWrite(LedD, HIGH);
  }
  else
  {
    digitalWrite(LedD, LOW);
  }

  n = n % 8;

  if ( n / 4 == 1 )
  {
    digitalWrite(LedC, HIGH);
  }
  else
  {
    digitalWrite(LedC, LOW);
  }

  n = n % 4;

  if ( n / 2 == 1 )
  {
    digitalWrite(LedB, HIGH);
  }
  else
  {
    digitalWrite(LedB, LOW);
  }
  n = n % 2;

  if ( n == 1 )
  {
    digitalWrite(LedA, HIGH);
  }
  else
  {
    digitalWrite(LedA, LOW);
  }
}

Next step: Multiplexing multiple displays.

4 Responses to Simple Arduino 7 segment display example

  1. Pingback: Multiplexing 7 segment LED displays with Arduino « My Blog

  2. Ivan Seiler says:

    thanks for code, I’m just starting with Arduino and it helps a lot, Ivan

  3. Rick Dennis says:

    Thanks! Your blog was very useful for getting me started using the 4511. I was messing around with the code and found another way to write the Display function.

    void Display(int n)
    {
    digitalWrite(LedD, ((n >> 3) & 1) ? HIGH : LOW);
    digitalWrite(LedC, ((n >> 2) & 1) ? HIGH : LOW);
    digitalWrite(LedB, ((n >> 1) & 1) ? HIGH : LOW);
    digitalWrite(LedA, (n & 1) ? HIGH : LOW);
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: