[AVR] How to connect and drive 7 Segment LED with Arduino

Hardware issues, electronic components, schemas, Arduino, STM32, Robots, Sensors
Post Reply
Administrator
Site Admin
Posts: 81
Joined: 26-Feb-2014, 17:54

[AVR] How to connect and drive 7 Segment LED with Arduino

Post by Administrator » 17-Mar-2015, 21:54

Hello.

A seven-segment display is a device for displaying decimal numerals. Such LED-displays are widely used in digital clocks and other electronic devices for displaying numerical information. The 7-segment display is a simple device. It is actually composed of seven elements and a dot (8 LEDs, Light-emitting diodes). We light different segments to make numerical digits.
LED1x7Segments.jpg
LED1x7Segments.jpg (11.59 KiB) Viewed 41987 times
It is possible to buy such LED here: DealExtreme or BangGood

Each segment in the display shares the same cathode (or anode, depends on module) connection points, labeled A, B, C, D, E, F, G, H (dot). This reduces the wiring required when developing projects that need multiple digit displays. Common cathode is (negative) connection point (GND).
7seg_pinouts.png
7seg_pinouts.png (7.21 KiB) Viewed 41987 times
Many LED displays require a current-limiting resistor when powered from a 5 V logic pin. The value of the resistor is typically 220 Ohms, sometimes 470 Ohms. The higher the value, the more the current that is limited, and the less bright the LED segments. Connect the resister between common cathode and GND pin of microcontroller. My LED-display SMA42056 can work without a resistor. But the current that flows in the circuit can damage Arduino port pins or the LED.
Always read datasheet for your device!
LED1x7SegmentsArduino.jpg
LED1x7SegmentsArduino.jpg (34.79 KiB) Viewed 41987 times
DO NOT USE pins 0 and 1 of ARDUINO for driving LEDs.

Arduino code is very simple.
1) Declare PIN numbers used by the LED.
2) Cofigure PINs to work in digital OUTPUT mode.
3) To display a digit turn on (HIGH) required segments (diodes).

Code: Select all

int segA = 3; // top
int segB = 4; // right-top
int segC = 5; // right-bottom
int segD = 6; // bottom
int segE = 7; // left-bottom
int segF = 8; // left-top
int segG = 9; // middle
int segH = 10;// dot

void setup() {
    // put your setup code here, to run once:
    pinMode(segA, OUTPUT);
    pinMode(segB, OUTPUT);
    pinMode(segC, OUTPUT);
    pinMode(segD, OUTPUT);
    pinMode(segE, OUTPUT);
    pinMode(segF, OUTPUT);
    pinMode(segG, OUTPUT);
    pinMode(segH, OUTPUT);
}

//-----------------------------------------------------//
static int nTicks = 0;

void loop()
{
    delay(1000);
    drawDigit(nTicks % 10);
    nTicks++;
}


//-----------------------------------------------------//
void drawDigit(int n)
{
    // All segments OFF
    digitalWrite(segA, LOW);
    digitalWrite(segB, LOW);
    digitalWrite(segC, LOW);
    digitalWrite(segD, LOW);
    digitalWrite(segE, LOW);
    digitalWrite(segF, LOW);
    digitalWrite(segG, LOW);
    digitalWrite(segH, LOW);

    // Turn ON required segments
    switch( n )
    {
      case 0:
        digitalWrite(segA, HIGH);
        digitalWrite(segB, HIGH);
        digitalWrite(segC, HIGH);
        digitalWrite(segD, HIGH);
        digitalWrite(segE, HIGH);
        digitalWrite(segF, HIGH);
        break;
      case 1:
        digitalWrite(segB, HIGH);
        digitalWrite(segC, HIGH);
        break;
      case 2:
        digitalWrite(segA, HIGH);
        digitalWrite(segB, HIGH);
        digitalWrite(segD, HIGH);
        digitalWrite(segE, HIGH);
        digitalWrite(segG, HIGH);
        break;
      case 3:
        digitalWrite(segA, HIGH);
        digitalWrite(segB, HIGH);
        digitalWrite(segC, HIGH);
        digitalWrite(segD, HIGH);
        digitalWrite(segG, HIGH);
        break;
      case 4:
        digitalWrite(segB, HIGH);
        digitalWrite(segC, HIGH);
        digitalWrite(segF, HIGH);
        digitalWrite(segG, HIGH);
        break;
      case 5:
        digitalWrite(segA, HIGH);
        digitalWrite(segC, HIGH);
        digitalWrite(segD, HIGH);
        digitalWrite(segF, HIGH);
        digitalWrite(segG, HIGH);
        break;
      case 6:
        digitalWrite(segA, HIGH);
        digitalWrite(segC, HIGH);
        digitalWrite(segD, HIGH);
        digitalWrite(segE, HIGH);
        digitalWrite(segF, HIGH);
        digitalWrite(segG, HIGH);
        break;
      case 7:
        digitalWrite(segA, HIGH);
        digitalWrite(segB, HIGH);
        digitalWrite(segC, HIGH);
        break;
      case 8:
        digitalWrite(segA, HIGH);
        digitalWrite(segB, HIGH);
        digitalWrite(segC, HIGH);
        digitalWrite(segD, HIGH);
        digitalWrite(segE, HIGH);
        digitalWrite(segF, HIGH);
        digitalWrite(segG, HIGH);
        break;
      case 9:
        digitalWrite(segA, HIGH);
        digitalWrite(segB, HIGH);
        digitalWrite(segC, HIGH);
        digitalWrite(segD, HIGH);
        digitalWrite(segF, HIGH);
        digitalWrite(segG, HIGH);
        break;
       
    }
}
 
More effective code (fast and short) is here:

Code: Select all

void drawDigitFast(int n)
{
    const byte aPins[8] = {
        segA, segB, segC, segD, segE, segF, segG, segH 
    };
    const byte aSegments[10][8] = {
        //  A     B     C     D     E     F     G    H
        { HIGH, HIGH, HIGH, HIGH, HIGH, HIGH,  LOW, LOW }, // 0
        {  LOW, HIGH, HIGH,  LOW,  LOW,  LOW,  LOW, LOW }, // 1
        { HIGH, HIGH,  LOW, HIGH, HIGH,  LOW, HIGH, LOW }, // 2
        { HIGH, HIGH, HIGH, HIGH,  LOW,  LOW, HIGH, LOW }, // 3
        {  LOW, HIGH, HIGH,  LOW,  LOW, HIGH, HIGH, LOW }, // 4
        { HIGH,  LOW, HIGH, HIGH,  LOW, HIGH, HIGH, LOW }, // 5
        { HIGH,  LOW, HIGH, HIGH, HIGH, HIGH, HIGH, LOW }, // 6
        { HIGH, HIGH, HIGH,  LOW,  LOW,  LOW, LOW,  LOW }, // 7
        { HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW }, // 8
        { HIGH, HIGH, HIGH, HIGH,  LOW, HIGH, HIGH, LOW }, // 9
    };
     for( int i = 0; i < 8; i++ )
     {
        digitalWrite( aPins[i], aSegments[n][i] );
     }
} 
The Result:


P.S. I would not recommend to use 7 segment LED in any projects because it uses too many PINs (7+). Instead, it is a good idea to use simple and cheap GREEN or RED LEDs (diode) to indicate the status of your device (2 wires).

The source code is available at GitHub repository.

Post Reply