A seven-segment display is a form of electronic display devicefor displaying decimal numeralsthat is an alternative to the more complex dot matrix displays.
Seven-segment displays are widely used in digital clocks, electronic meters, basic calculators, and other electronic devices that display numerical information.
What is the different between 4-digits and 1-digit?The different is the number of digits on the display.
The 1 digit, 7-segment display has 10 pins, 7 for the segments, one for the dot, and 2 pins for the power supply (either GND or Vcc depending on the type of the display)
On the other hand, the 4 -digit display does not have 40 pins, rather it has only 12 pins: 7 segments, one dot, and 4 digit selection pins that select which digit will be active to display the character sent by the Arduino on the 7+1 segment pins.
The 7-segment is arranged in two ways: either all the cathodes of the segment are gathered in one pin (GND), and this type is called common cathode, or all the anodes are gathered in one pin (Vcc), and this type is called common anode. Similarly the 7-segment, 4-digit are either common cathode or common anode, hence we have four common pins for the 4 digits.
How does it work?So, How doesit work?
To display a character on a 7-segment display you need to connect the common pin to the appropriate power pin (either GND or Vcc which activates it) and set the required segment pins to the opposite state ( i.e Vcc or GND).
Similarly, to display a set of characters on a 4 digit display, we active them in sequence and set the 7 segment pins to display the corresponding character.(i.e set the digit pin1 to the active state, set all the other digit pins to inactive state, and set the 7 segment pin to display character 1, then set digit pin2 only to active, and display charterer 2 and so on. This happens in a fast rate in order to make the eyes feels as if all four digits are active at the same time. That requires all the 12 pins to be connected to Digital pins on Arduino.
Example:
To display the number 2021 using Arduino, on a common cathode display: set digit1to LOW and the remaining digit pins to HIGH, now display Char 2. Now set digit pin2 to LOW, and the other pins to HIGH, and set the 7 segment pins to Char 0..etc
In this project, we used this method to print any possible character on the display.
Circuit:The used display with 12 pin only.
The resistors are 330 ohm.
Main code :/*
Showing numbers, chars and phrases
A
---
F | | B
| G |
---
E | | C
| |
---
D
*/
#define common_cathode 0
#define common_anode 1
bool mode = common_anode;// my display is common anode
#define pinA 2
#define pinB 3
#define pinC 4
#define pinD 5
#define pinE 7
#define pinF 12
#define pinG 8
#define pinDP 13
#define D1 6
#define D2 9
#define D3 10
#define D4 11
#include "array.h"
#include "functions.h"
void setup() {
// initialize the digital pins as outputs.
pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(pinC, OUTPUT);
pinMode(pinD, OUTPUT);
pinMode(pinE, OUTPUT);
pinMode(pinF, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinDP, OUTPUT);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
printDisplay("2021",1000);// use this function to print a string (has numbers, characters or phrases) when the length of string is 4 or less than 4, the second variable is the time for printing on display
Reset();// use this function to reset the display
delay(1000);
printDisplay("all usable characters [[ 1 2 3 4 5 6 7 8 9 0 a b c d e f g h i j l n o p q r s t u y - _ . [ ] ? ]]",300);// when the length of string is more than 4, the second variable is custom speed for movement
delay(1000);
printDigit('y',D1);//print any char on any digit
delay(1000);
}
Main code explanation:#define common_cathode 0
#define common_anode 1
The defines for display types.
bool mode = common_anode;// my display is common anode
The type of the display, set to your display's type.
#include "array.h"
#include "functions.h"
Include files "array.h" "functions.h" ( download those files from code section of this project, Those files should be in the same folderalong with the code).
#define pinA 2
#define pinB 3
#define pinC 4
#define pinD 5
#define pinE 7
#define pinF 12
#define pinG 8
#define pinDP 13
#define D1 6
#define D2 9
#define D3 10
#define D4 11
Define pin numbers.
void setup() {
// initialize the digital pins as outputs.
pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(pinC, OUTPUT);
pinMode(pinD, OUTPUT);
pinMode(pinE, OUTPUT);
pinMode(pinF, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinDP, OUTPUT);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
Serial.begin(9600);
}
Pin modes and Serial.begin.
Functions:Those functions are defined in file "functions.h".
printDisplay(String Phrase, int Delay)Use this function to print a string (has numbers, characters or phrases).
The first entry of this function(String Phrase) is the string that you want to print.
If the length of "Phrase" is more than 4, the Phrase will move from the start to the ending of it.
The second entry is "Delay".
when the length of "Phrase" is 4 or less than 4, "Delay" is the time for printing on display, for example:
printDisplay("2021",1000);
when the length of "Phrase" is more than 4, "Delay" is custom speed for movement, for example:
printDisplay("all usable characters [[ 1 2 3 4 5 6 7 8 9 0 a b c d e f g h i j l n o p q r s t u y - _ . [ ] ? ]]",300);
Reset()Reset();
Use this function to reset the display.
printDigit(char Chara, int digitPort)Use this function to print a character (any character in array "Char" in "array.h") on any digit.
array.h:#define DP 30
const int charsInArray = 37;
byte digits[]{D1,D2,D3,D4};
byte seg[] {pinA,pinB,pinC,pinD,pinE,pinF,pinG,pinDP};
byte Char[37][9] {
{1,1,1,1,1,1,0,0,'0'},//0
{0,1,1,0,0,0,0,0,'1'},//1
{1,1,0,1,1,0,1,0,'2'},//2
{1,1,1,1,0,0,1,0,'3'},//3
{0,1,1,0,0,1,1,0,'4'},//4
{1,0,1,1,0,1,1,0,'5'},//5
{1,0,1,1,1,1,1,0,'6'},//6
{1,1,1,0,0,0,0,0,'7'},//7
{1,1,1,1,1,1,1,0,'8'},//8
{1,1,1,1,0,1,1,0,'9'}, //9
{1,1,1,0,1,1,1,0,'a'},//A/1
{0,0,1,1,1,1,1,0,'b'},//b/2
{0,0,0,1,1,0,1,0,'c'},//C/3
{0,1,1,1,1,0,1,0,'d'},//d/4
{1,0,0,1,1,1,1,0,'e'},//E/5
{1,0,0,0,1,1,1,0,'f'},//F/6
{1,0,1,1,1,1,0,0,'g'},//G/7
{0,1,1,0,1,1,1,0,'h'},//H/8
{0,1,1,0,0,0,0,0,'i'},//I/9
{0,1,1,1,1,0,0,0,'j'},//J/10
{0,0,0,1,1,1,0,0,'l'},//L/11
{0,0,1,0,1,0,1,0,'n'},//n/12
{0,0,1,1,1,0,1,0,'o'},//o/13
{1,1,0,0,1,1,1,0,'p'},//P/14
{1,1,1,0,0,1,1,0,'q'},//q/15
{0,0,0,0,1,0,1,0,'r'},//r/16
{1,0,1,1,0,1,1,0,'s'},//S/17 looks like number 5
{0,0,0,1,1,1,1,0,'t'},//t/18
{0,1,1,1,1,1,0,0,'u'},//U/19
{0,1,1,1,0,1,1,0,'y'},//y/20
{0,0,0,0,0,0,0,1,'.'},//.
{0,0,0,0,0,0,1,0,'-'},//dash/negative
{0,0,0,1,0,0,0,0,'_'},//underscore
{1,0,0,1,1,1,0,0,'['},//[
{1,1,1,1,0,0,0,0,']'},//]
{1,1,0,0,1,0,1,0,'?'},//?
{0,0,0,0,0,0,0,0,' '}//blank
};
array.h explanation: const int charsInArray = 37;
this is the number of characters in array "Char", so if you want to add a new character, you should change this variable to the new number of characters in "Char".
byte digits[]{D1,D2,D3,D4};
Digit pins.
byte seg[] {pinA,pinB,pinC,pinD,pinE,pinF,pinG,pinDP};
Segment pins.
byte Char[37][9] {
{1,1,1,1,1,1,0,0,'0'},//0
{0,1,1,0,0,0,0,0,'1'},//1
{1,1,0,1,1,0,1,0,'2'},//2
{1,1,1,1,0,0,1,0,'3'},//3
{0,1,1,0,0,1,1,0,'4'},//4
{1,0,1,1,0,1,1,0,'5'},//5
{1,0,1,1,1,1,1,0,'6'},//6
{1,1,1,0,0,0,0,0,'7'},//7
{1,1,1,1,1,1,1,0,'8'},//8
{1,1,1,1,0,1,1,0,'9'}, //9
{1,1,1,0,1,1,1,0,'a'},//A/1
{0,0,1,1,1,1,1,0,'b'},//b/2
{0,0,0,1,1,0,1,0,'c'},//C/3
{0,1,1,1,1,0,1,0,'d'},//d/4
{1,0,0,1,1,1,1,0,'e'},//E/5
{1,0,0,0,1,1,1,0,'f'},//F/6
{1,0,1,1,1,1,0,0,'g'},//G/7
{0,1,1,0,1,1,1,0,'h'},//H/8
{0,1,1,0,0,0,0,0,'i'},//I/9
{0,1,1,1,1,0,0,0,'j'},//J/10
{0,0,0,1,1,1,0,0,'l'},//L/11
{0,0,1,0,1,0,1,0,'n'},//n/12
{0,0,1,1,1,0,1,0,'o'},//o/13
{1,1,0,0,1,1,1,0,'p'},//P/14
{1,1,1,0,0,1,1,0,'q'},//q/15
{0,0,0,0,1,0,1,0,'r'},//r/16
{1,0,1,1,0,1,1,0,'s'},//S/17 looks like number 5
{0,0,0,1,1,1,1,0,'t'},//t/18
{0,1,1,1,1,1,0,0,'u'},//U/19
{0,1,1,1,0,1,1,0,'y'},//y/20
{0,0,0,0,0,0,0,1,'.'},//.
{0,0,0,0,0,0,1,0,'-'},//dash/negative
{0,0,0,1,0,0,0,0,'_'},//underscore
{1,0,0,1,1,1,0,0,'['},//[
{1,1,1,1,0,0,0,0,']'},//]
{1,1,0,0,1,0,1,0,'?'},//?
{0,0,0,0,0,0,0,0,' '}//blank
};
"Char", the code for every char(you should not delete any thing of the values).
functions.hvoid Reset()
{ digitalWrite(D1, !mode);
digitalWrite(D2, !mode);
digitalWrite(D3, !mode);
digitalWrite(D4, !mode);
for(byte i = 0 ; i < 8 ; i++){
digitalWrite(seg[i],mode);
}
}
void printDigit(char Chara,int digitPort)
{ Reset();
int character = -1;
digitalWrite(digitPort,mode);
for(int i = 0 ; i < charsInArray ; i++){
if(Chara == Char[i][8]){
character = i;
}
}
if (character == -1){
digitalWrite(pinG,!mode);
}else{
for(int i = 0;i<= 7;i++)
{if(mode == common_anode) digitalWrite(seg[i],!Char[character][i]);
else if(mode == common_cathode) digitalWrite(seg[i],Char[character][i]);
}
}
}
void printDisplay(String Phrase,int Delay)
{
char char1 = Phrase.charAt(0);
char char2 = Phrase.charAt(1);
char char3 = Phrase.charAt(2);
char char4 = Phrase.charAt(3);
//char char5 = Phrase.charAt(4);
char char1Num = 0;
char char2Num = 0;
char char3Num = 0;
char char4Num = 0;
int stringLength = Phrase.length();
if(stringLength < 5){
for(int ti = 0 ; ti <= (Delay / 8) ; ti++){
if(1 > stringLength) char1 = ' ';
else char1 = Phrase.charAt(0);
if(2 > stringLength) char2 = ' ';
else char2 = Phrase.charAt(1);
if(3 > stringLength) char3 = ' ';
else char3 = Phrase.charAt(2);
if(4 > stringLength) char4 = ' ';
else char4 = Phrase.charAt(3);
printDigit(char1,D1);
delay(2);
printDigit(char2,D2);
delay(2);
printDigit(char3,D3);
delay(2);
printDigit(char4,D4);
delay(2);
}
}else{
for(int t = 0 ; t <= stringLength ; t++){
for(int ti = 0 ; ti <= (Delay / 8) ; ti++){
/*Reset();
delay(2);*/
printDigit(char1,D1);
delay(2);
printDigit(char2,D2);
delay(2);
printDigit(char3,D3);
delay(2);
printDigit(char4,D4);
delay(2);
}
if(t + 1 > stringLength) char1 = ' ';
else char1 = Phrase.charAt(t);
if((t + 2) > stringLength) char2 = ' ';
else char2 = Phrase.charAt(t + 1);
if((t + 3) > stringLength) char3 = ' ';
else char3 = Phrase.charAt(t + 2);
if((t + 4) > stringLength) char4 = ' ';
else char4 = Phrase.charAt(t + 3);
}
}
}
functions.h explanationvoid Reset()
{ digitalWrite(D1, !mode);
digitalWrite(D2, !mode);
digitalWrite(D3, !mode);
digitalWrite(D4, !mode);
for(byte i = 0 ; i < 8 ; i++){
digitalWrite(seg[i],mode);
}
}
This is reset function ( i'm not sure but the digits in my display needs to turn on the digit to stop printing on it and the opposite to the segment pins, if you set mode to your display's type and the functions didn't work correctly try to remove '!' from the first 4 digitalWrite function, if didn't work check "problems" section in this project)
void printDigit(char Chara,int digitPort)
{ Reset();
int character = -1;
digitalWrite(digitPort,mode);
for(int i = 0 ; i < charsInArray ; i++){
if(Chara == Char[i][8]){
character = i;
}
}
if (character == -1){
digitalWrite(pinG,!mode);
}else{
for(int i = 0;i<= 7;i++)
{if(mode == common_anode) digitalWrite(seg[i],!Char[character][i]);
else if(mode == common_cathode) digitalWrite(seg[i],Char[character][i]);
}
}
}
print a char on the required digit.
for(int i = 0 ; i < charsInArray ; i++){
if(Chara == Char[i][8]){
character = i;
}
}
search for the entered character.
if (character == -1){
digitalWrite(pinG,!mode);
}
if the character is not found.
else{
for(int i = 0;i<= 7;i++)
{if(mode == common_anode) digitalWrite(seg[i],!Char[character][i]);
else if(mode == common_cathode) digitalWrite(seg[i],Char[character][i]);
}
}
if the character found, print it using it's code in array "Char" in "array.h".
void printDisplay(String Phrase,int Delay)
{
char char1 = Phrase.charAt(0);
char char2 = Phrase.charAt(1);
char char3 = Phrase.charAt(2);
char char4 = Phrase.charAt(3);
//char char5 = Phrase.charAt(4);
char char1Num = 0;
char char2Num = 0;
char char3Num = 0;
char char4Num = 0;
int stringLength = Phrase.length();
if(stringLength < 5){
for(i
delay(2);*/nt ti = 0 ; ti <= (Delay / 8) ; ti++){
if(1 > stringLength) char1 = ' ';
else char1 = Phrase.charAt(0);
if(2 > stringLength) char2 = ' ';
else char2 = Phrase.charAt(1);
if(3 > stringLength) char3 = ' ';
else char3 = Phrase.charAt(2);
if(4 > stringLength) char4 = ' ';
else char4 = Phrase.charAt(3);
printDigit(char1,D1);
delay(2);
printDigit(char2,D2);
delay(2);
printDigit(char3,D3);
delay(2);
printDigit(char4,D4);
delay(2);
}
}else{
for(int t = 0 ; t <= stringLength ; t++){
for(int ti = 0 ; ti <= (Delay / 8) ; ti++){
printDigit(char1,D1);
delay(2);
printDigit(char2,D2);
delay(2);
printDigit(char3,D3);
delay(2);
printDigit(char4,D4);
delay(2);
}
if(t + 1 > stringLength) char1 = ' ';
else char1 = Phrase.charAt(t);
if((t + 2) > stringLength) char2 = ' ';
else char2 = Phrase.charAt(t + 1);
if((t + 3) > stringLength) char3 = ' ';
else char3 = Phrase.charAt(t + 2);
if((t + 4) > stringLength) char4 = ' ';
else char4 = Phrase.charAt(t + 3);
}
}
}
print a string.
char char1 = Phrase.charAt(0);
char char2 = Phrase.charAt(1);
char char3 = Phrase.charAt(2);
char char4 = Phrase.charAt(3);
get the first 4 chars of the string.
char char1Num = 0;
char char2Num = 0;
char char3Num = 0;
char char4Num = 0;
get the code of the 4 characters.
int stringLength = Phrase.length();
get the length of "Phrase".
if(stringLength < 5){
for(int ti = 0 ; ti <= (Delay / 8) ; ti++){
if(1 > stringLength) char1 = ' ';
else char1 = Phrase.charAt(0);
if(2 > stringLength) char2 = ' ';
else char2 = Phrase.charAt(1);
if(3 > stringLength) char3 = ' ';
else char3 = Phrase.charAt(2);
if(4 > stringLength) char4 = ' ';
else char4 = Phrase.charAt(3);
printDigit(char1,D1);
delay(2);
printDigit(char2,D2);
delay(2);
printDigit(char3,D3);
delay(2);
printDigit(char4,D4);
delay(2);
}
}
if
the
string is less than 5 digits (to know if the string can fit the display or not)
if(1 > stringLength) char1 = ' ';
else char1 = Phrase.charAt(0);
if(2 > stringLength) char2 = ' ';
else char2 = Phrase.charAt(1);
if(3 > stringLength) char3 = ' ';
else char3 = Phrase.charAt(2);
if(4 > stringLength) char4 = ' ';
else char4 = Phrase.charAt(3);
check if any one of the digits sould be empty(to avoid glitches).
printDigit(char1,D1);
delay(2);
printDigit(char2,D2);
delay(2);
printDigit(char3,D3);
delay(2);
printDigit(char4,D4);
delay(2);
print the string
else{
for(int t = 0 ; t <= stringLength ; t++){
for(int ti = 0 ; ti <= (Delay / 8) ; ti++){
printDigit(char1,D1);
delay(2);
printDigit(char2,D2);
delay(2);
printDigit(char3,D3);
delay(2);
printDigit(char4,D4);
delay(2);
}
if(t + 1 > stringLength) char1 = ' ';
else char1 = Phrase.charAt(t);
if((t + 2) > stringLength) char2 = ' ';
else char2 = Phrase.charAt(t + 1);
if((t + 3) > stringLength) char3 = ' ';
else char3 = Phrase.charAt(t + 2);
if((t + 4) > stringLength) char4 = ' ';
else char4 = Phrase.charAt(t + 3);
}
}
}
if the string is more than 4 digits (make the string move to show the full string because the diplay fits 4 digits only)
for(int ti = 0 ; ti <= (Delay / 8) ; ti++){
printDigit(char1,D1);
delay(2);
printDigit(char2,D2);
delay(2);
printDigit(char3,D3);
delay(2);
printDigit(char4,D4);
delay(2);
}
move the string.
if(t + 1 > stringLength) char1 = ' ';
else char1 = Phrase.charAt(t);
if((t + 2) > stringLength) char2 = ' ';
else char2 = Phrase.charAt(t + 1);
if((t + 3) > stringLength) char3 = ' ';
else char3 = Phrase.charAt(t + 2);
if((t + 4) > stringLength) char4 = ' ';
else char4 = Phrase.charAt(t + 3);
when the movement finishes up start emptying the display.
Problems:1- the display is showing strange figures:
- Check the display mode ( common cathode or common anode )
- If that doesn't solvetheproblem,try switching the states of the digitalwrite() in the file "functions.h"
- If that didn't work either, check the definitions and the values in "array.h"
2-the display is blank
- Check the connections
- Make sure that the display is working by testing each digit of the display by connecting the digit pin to the appropriate power terminal based on the display type ( VCC or GND ) while connecting all segment pins to other power terminal ( VCC or GND ).
Comments