The aim of this article demonstrate how the Arduino can run on 7-segment display. We used a SA56-11GWA which is a single display with common anode, it means that the anode is common to all digits, and to the point.
A display consists of 8 LEDs, 7 form the figure and 1 point decimal.A conversion between decimal and what we intend to show the binary information that you receive is called the BCD (Binary Coded Decimal).
In terms of hardware, each LED work individually, so each of the segments has to lead a limiting resistor, in this case, each digit has an operating voltage of 2.2V and consumes 20mA. If the voltage is 5volts, we need the resistor fall 5-2.2 = 3.8V, by Ohm's Law we can calculate, R = V res / I led R = 3.8 / 0.020 = 190 Ω +/- 20% (let's use a resistor with 220Ω). This applies to this display SA56-11GWA used as an example, other values may be different, it is useful to calculate the required resistance value.
Decimal | The | A 3 | A 2 | A 1 |
0 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 0 | 1 |
2 | 0 | 0 | 1 | 0 |
3 | 0 | 0 | 1 | 1 |
4 | 0 | 1 | 0 | 0 |
5 | 0 | 1 | 0 | 1 |
6 | 0 | 1 | 1 | 0 |
7 | 0 | 1 | 1 | 1 |
8 | 1 | 0 | 0 | 0 |
9 | 1 | 0 | 0 | 1 |
The first thing we have to do is convert our decimal number to binary, we will use examples only 0-9 this first phase
decToBcd byte (byte val)
{
return ((val / 10 * 8) + (val% 10));
}
This makes our decimal value to binary, for example, assuming that have numbers with more than two digits, 27 will be 0010 0111 (2 - 7 BCD). If we have only one figure 2 for example, will have 0010.
This allows us to act on an IC decoder (4511) and the use of only four BCD outputs Arduino, so can convert a result of a count value, or for any input digit.
/ *
Example of a counter from 0 to 9 that adds each
Since there is an entry in A0
author: Arturo Guadalupi
* /
// Declaration of pins used in the 4511 entries
The const int = 2;
const int b = 3;
const int c = 4;
const int D = 5;
void setup () {
pinMode (A0, INPUT);
pinMode (A, OUTPUT); // LSB
pinMode (B, OUTPUT);
pinMode (C, OUTPUT);
pinMode (D, OUTPUT); // MSB
}
int count = 0; // Used for the variable number
void loop () {
if (digitalRead (A0) == LOW) // if the button is pressed on the A0 input
{
count ++;
delay (200); // The delay prevents multiple counts
if (count == 10) // 0-9 counter
count = 0;
to_BCD (); // Convert to binary
}
if (count == 10)
count = 0;
}
to_BCD void ()
{
if (count == 0) // write 0000
{
digitalWrite (A, LOW);
digitalWrite (B, LOW);
digitalWrite (C, LOW);
digitalWrite (D, LOW);
}
if (count == 1) // write 0001
{
digitalWrite (A, HIGH);
digitalWrite (B, LOW);
digitalWrite (C, LOW);
digitalWrite (D, LOW);
}
if (count == 2) // write 0010
{
digitalWrite (A, LOW);
digitalWrite (B, HIGH);
digitalWrite (C, LOW);
digitalWrite (D, LOW);
}
if (count == 3) // write 0011
{
digitalWrite (A, HIGH);
digitalWrite (B, HIGH);
digitalWrite (C, LOW);
digitalWrite (D, LOW);
}
if (count == 4) // write 0100
{
digitalWrite (A, LOW);
digitalWrite (B, LOW);
digitalWrite (C, HIGH);
digitalWrite (D, LOW);
}
if (count == 5) // write 0101
{
digitalWrite (A, HIGH);
digitalWrite (B, LOW);
digitalWrite (C, HIGH);
digitalWrite (D, LOW);
}
if (count == 6) // write 0110
{
digitalWrite (A, LOW);
digitalWrite (B, HIGH);
digitalWrite (C, HIGH);
digitalWrite (D, LOW);
}
if (count == 7) // write 0111
{
digitalWrite (A, HIGH);
digitalWrite (B, HIGH);
digitalWrite (C, HIGH);
digitalWrite (D, LOW);
}
if (count == 8) // write 1000
{
digitalWrite (A, LOW);
digitalWrite (B, LOW);
digitalWrite (C, LOW);
digitalWrite (D, HIGH);
}
if (count == 9) // write 1001
{
digitalWrite (A, HIGH);
digitalWrite (B, LOW);
digitalWrite (C, LOW);
digitalWrite (D, HIGH);
}
}
Convert decimal to binary (8bit)
It is possible to directly transfer the binary value for the exits, here is an example of cyclic counter zero to nine
ledpins int [] = {4,5,6,7,8,9,10,11};
byte digits [] = {B11111100, B01100000, B11011010, B11110010, B01100110, B10110110,
B10111110, B11100000, B11111110, B11110110};
void setup ()
{
for (int i = 0; i <8; i ++)
{
pinMode (ledpins [i] OUTPUT);
digitalWrite (ledpins [i], HIGH);
}
}
void loop ()
{
for (int i = 0; i <10; i ++)
{
displayDigit (i);
delay (1000);
AllOff ();
delay (500);
}
}
displayDigit void (int digit)
{
for (int i = 0; i <8; i ++)
{
digitalWrite (ledpins [i] ,! bitRead (digits [Scan], 7-i));
}
}
void AllOff ()
{
for (int i = 0; i <8; i ++)
{
digitalWrite (ledpins [i], HIGH);
}
}
In this case not used the BCD outputs but a direct binary code in 8 bits (one for each connected LED)
We used a level 1 (HIGH) when the segment lights and 0 (LOW) when deleted. The last digit is always a 0 represents the decimal point that is not being used.
The AllOff () procedure off all digits reversing the common pin.
displayDigit () connects the right thread for each digit. The function bitread reads the bits of the binary number, starting with the rightmost bit.That's why the 7-i is used. When using the digitalWrite () statement, we can write 1 when we want to HIGH and LOW 0 when we want. This is the opposite of how our binary numbers are working. The operator! inverts the value. via electronica-pt.com