Exercise 20 - Connecting a LED display

1.      INTRODUCTION


In this exercise, we are going to use a four digit seven segment display. If you want to activate the segments one by one, it will be necessary to have many INPUTS, but in this case, the display has the segments (cathodes) associated to digits (anodes). This means that when digit pin are activated the segments actuate only in this digit. You can look this in the schematic:


As you can see, the display has common anode. This means that segments are activated by LOW level.

It is necessary to say on Arduino where you want the number, this is similar to demultiplex. In summary, you have 7*4 = 28 segments, but the display only has twelve terminals. You must select the digits that you want to turn on and next select the digit where you want to display. You can create four digit number because Arduino can change between instructions very fast.


2.      HOW TO CONNECT?

Materials:

·         Arduino UNO board

·         Protoboard

·         Some wire Jumpers

·         Some 220 ohms resistors

·         LED display


In this exercise dot pin is not used.

Connect the digit activator D1…D4 terminals to 13…10 pins on the Arduino board. Also, connect A… G terminal to 9…3 pins with a 220- ohm in each one. Another option is to connect a 1KΩ resistor in digit activator terminals, but the 220-ohm option is safer.


EXERCISE_A (Testing_4_digits_7_segments_display)



3.      PROGRAMMING

This program is a counter to test a 4 digits 7 segments display. The display used has common anode.

//Digit pins

int d1 = 13;

int d2 = 12;

int d3 = 11;

int d4 = 10;

//Segment pins

int a = 9;

int b = 8;

int c = 7;

int d = 6;

int e = 5;

int f = 4;

int g = 3;

//Counter variables

long n = 0;

int x = 100;

int del = 45;

 

//Set all as OUTPUT

 

void setup() {

  pinMode(a, OUTPUT);

  pinMode(b, OUTPUT);

  pinMode(c, OUTPUT);

  pinMode(d, OUTPUT);

  pinMode(e, OUTPUT);

  pinMode(f, OUTPUT);

  pinMode(g, OUTPUT);

  pinMode(d1, OUTPUT);

  pinMode(d2, OUTPUT);

  pinMode(d3, OUTPUT);

  pinMode(d4, OUTPUT);

  Serial.begin(9600);

 

}

On loop, we profit the % operator, which in Arduino returns the remainder of when one integer is divided by another. Thanks to it, variable n can be separated in individual digits.

void loop() {

  clearLEDs();

  SelectDigit(1);

  SelectNumber((n/x/1000)%10);

  delayMicroseconds(del);

  

  clearLEDs();

  SelectDigit(2);

  SelectNumber((n/x/100)%10);

  delayMicroseconds(del);

  

  clearLEDs();

  SelectDigit(3);

  SelectNumber((n/x/10)%10);

  delayMicroseconds(del);

  

  clearLEDs();

  SelectDigit(4);

  SelectNumber(n/x%10);

  

You can uncomment the serial commands to see how actuate the previous SelectNumber().

 

//  Serial.print((n/x/1000)%10);

//  Serial.print("|||");

//  Serial.print((n/x/100)%10);

//  Serial.print("|||");

//  Serial.print((n/x/10)%10);

//  Serial.print("|||");

//  Serial.print(n/x%10);

//  Serial.print("|||");

//  Serial.println(n);

  

  delayMicroseconds(del);

  

  n++;

  

  if (digitalRead(13) == HIGH)

  {

    n = 0;

  }

}

 

 

To simplify loop, you can create functions out of it.

This function does no return anything. It is used to select the digit where you want to write. You must introduce the number of digits.

void SelectDigit(int x)

{

  digitalWrite(d1, LOW);

  digitalWrite(d2, LOW);

  digitalWrite(d3, LOW);

  digitalWrite(d4, LOW);

 

  switch (x)

  {

    case 1: digitalWrite(d1, HIGH); break;

    case 2: digitalWrite(d2, HIGH); break;

    case 3: digitalWrite(d3, HIGH); break;

    default: digitalWrite(d4, HIGH); break;

  }

}

 

 

SelectNumber() neither return anything. It is used to select the number, which you want to write. You must introduce the number.

 

void SelectNumber(int x)

{

  switch (x)

  {

    default: zero(); break;

    case 1: one(); break;

    case 2: two(); break;

    case 3: three(); break;

    case 4: four(); break;

    case 5: five(); break;

    case 6: six(); break;

    case 7: seven(); break;

    case 8: eight(); break;

    case 9: nine(); break;

  }

}

 

This function turns off all of segments.

 

void clearLEDs()

{

  digitalWrite(a, HIGH);

  digitalWrite(b, HIGH);

  digitalWrite(c, HIGH);

  digitalWrite(d, HIGH);

  digitalWrite(e, HIGH);

  digitalWrite(f, HIGH);

  digitalWrite(g, HIGH);

}

 

The next functions declare the segments, which must be activated to create numbers in display

 

void zero()

{

  digitalWrite(a, LOW);

  digitalWrite(b, LOW);

  digitalWrite(c, LOW);

  digitalWrite(d, LOW);

  digitalWrite(e, LOW);

  digitalWrite(f, LOW);

  digitalWrite(g, HIGH);

}

 

void one()

{

  digitalWrite(a, HIGH);

  digitalWrite(b, LOW);

  digitalWrite(c, LOW);

  digitalWrite(d, HIGH);

  digitalWrite(e, HIGH);

  digitalWrite(f, HIGH);

  digitalWrite(g, HIGH);

}

 

void two()

{

  digitalWrite(a, LOW);

  digitalWrite(b, LOW);

  digitalWrite(c, HIGH);

  digitalWrite(d, LOW);

  digitalWrite(e, LOW);

  digitalWrite(f, HIGH);

  digitalWrite(g, LOW);

}

 

void three()

{

  digitalWrite(a, LOW);

  digitalWrite(b, LOW);

  digitalWrite(c, LOW);

  digitalWrite(d, LOW);

  digitalWrite(e, HIGH);

  digitalWrite(f, HIGH);

  digitalWrite(g, LOW);

}

 

void four()

{

  digitalWrite(a, HIGH);

  digitalWrite(b, LOW);

  digitalWrite(c, LOW);

  digitalWrite(d, HIGH);

  digitalWrite(e, HIGH);

  digitalWrite(f, LOW);

  digitalWrite(g, LOW);

}

 

void five()

{

  digitalWrite(a, LOW);

  digitalWrite(b, HIGH);

  digitalWrite(c, LOW);

  digitalWrite(d, LOW);

  digitalWrite(e, HIGH);

  digitalWrite(f, LOW);

  digitalWrite(g, LOW);

}

 

void six()

{

  digitalWrite(a, HIGH);

  digitalWrite(b, HIGH);

  digitalWrite(c, LOW);

  digitalWrite(d, LOW);

  digitalWrite(e, LOW);

  digitalWrite(f, LOW);

  digitalWrite(g, LOW);

}

 

void seven()

{

  digitalWrite(a, LOW);

  digitalWrite(b, LOW);

  digitalWrite(c, LOW);

  digitalWrite(d, HIGH);

  digitalWrite(e, HIGH);

  digitalWrite(f, HIGH);

  digitalWrite(g, HIGH);

}

 

void eight()

{

  digitalWrite(a, LOW);

  digitalWrite(b, LOW);

  digitalWrite(c, LOW);

  digitalWrite(d, LOW);

  digitalWrite(e, LOW);

  digitalWrite(f, LOW);

  digitalWrite(g, LOW);

}

 

void nine()

{

  digitalWrite(a, LOW);

  digitalWrite(b, LOW);

  digitalWrite(c, LOW);

  digitalWrite(d, HIGH);

  digitalWrite(e, HIGH);

  digitalWrite(f, LOW);

  digitalWrite(g, LOW);

}

//Code based on Jimmacle code who posted it in instructables.com


Last modified: Tuesday, 21 January 2020, 10:24 AM