In this tutorial, you will learn how to use a 4-digit 7-segment display.
When using 1-digit 7-segment display, please notice that if it is common anode, the common anode pin connects to the power source; if it is common cathode, the common cathode pin connects to the GND.
When using 4-digit 7-segment display, the common anode or common cathode pin is used to control which digit is displayed. Even though there is only one digit working, the principle of Persistence of Vision enables you to see all numbers displayed because each the scanning speed is so fast that you can hardly notice the intervals.
(1) x 830 tie-points breadboard
(1)x 74HC595 IC
(1) x 4 Digit 7-Segment Display
(4) x 220 ohm resistors
(23) x M-M wires (Male to Male jumper wires)
Four Digital Seven Segment Display
You can click the blue text link to download the program file to your local device, and double-click the file to open it after the download is complete. Please note: Before opening the file, ensure that you have installed the Arduino IDE development environment and completed the installation of relevant components such as the board support package and driver corresponding to the UNO R3. If you have any questions about this operation process, you can refer to the "part 1" chapter of the document for detailed guidance.
Click the Serial Monitor button to turn on the serial monitor. The basics about the serial monitor are introduced in details in tutorial 4 in part 2.
int latch=9; //74HC595 pin 9 STCP
int clock=10; //74HC595 pin 10 SHCP
int data=8; //74HC595 pin 8 DSunsigned char table[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c
,0x39,0x5e,0x79,0x71,0x00};//Display digital data
Pin Definitions and Display Table:
int latch=9;:Arduino digital pin 9 connected to STCP of 74HC595 (latch pin)int clock=10;:Arduino digital pin 10 connected to SHCP of 74HC595 (clock pin)int data=8;:Arduino digital pin 8 connected to DS of 74HC595 (data pin)unsigned char table[]:Array of hex values representing 7-segment display patterns for digits 0-15 and blank
void setup() {
pinMode(latch,OUTPUT);
pinMode(clock,OUTPUT);
pinMode(data,OUTPUT);
}
Setup Function:
pinMode(latch,OUTPUT);:Sets the latch pin as outputpinMode(clock,OUTPUT);:Sets the clock pin as outputpinMode(data,OUTPUT);:Sets the data pin as output/* The most common method of using 74CH595
- latch->LOW : Begin transmitting signals.
- shiftOut(dataPin, clockPin, bitOrder, value)
- dataPin: the pin on which to output each bit. Allowed data types: int.
- clockPin: the pin to toggle once the dataPin has been set to the correct value. Allowed data types: int.
- bitOrder: which order to shift out the bits; either MSBFIRST or LSBFIRST. (Most Significant Bit First, or, Least Significant Bit First).
- value: the data to shift out. Allowed data types: byte.
- latch->HIch : The end of the transmission signal.
*/
void Display(unsigned char num)
{digitalWrite(latch,LOW);
shiftOut(data,clock,MSBFIRST,table[num]);
digitalWrite(latch,HIGH);}
Display() Function:
void Display(unsigned char num):Function to display a number on the 7-segment displaydigitalWrite(latch,LOW);:Sets latch pin low to prepare for data transfershiftOut(data,clock,MSBFIRST,table[num]);:Shifts out the display pattern
data:Pin to send data throughclock:Pin to clock the dataMSBFIRST:Send most significant bit firsttable[num]:Display pattern for the specified numberdigitalWrite(latch,HIGH);:Sets latch pin high to update the displayvoid loop() {
Display(1);
delay(500);
Display(2);
delay(500);
Display(3);
.........
}
Loop Function:
Display(n);:Displays the number ndelay(500);:Delays 500 milliseconds between each numberNow that you have completed the basic examples, you may want to control each digit individually and show different numbers on each display.
From the pinout diagram of the 4-digit 7-segment display, you can see that seg1–seg4 control whether each digit is turned ON or OFF.Since we are using a common-cathode 4-digit 7-segment display, all four digits will light up if seg1–seg4 are connected directly to GND.
To control one digit at a time, you must connect these seg pins to digital output pins on the Arduino UNO, so the Arduino can turn each digit ON or OFF.
example:
int seg1= 4;
int seg2= 11;
int seg3= 12;
int seg4= 13;
Assign the seg pins to UNO pins, and set them as outputs in setup():
pinMode(seg1,OUTPUT);
pinMode(seg2,OUTPUT);
pinMode(seg3,OUTPUT);
pinMode(seg4,OUTPUT);
At this point, you can already turn each digit ON or OFF independently.
Next, we will make each digit show a different number. The idea is simple:Turn ON only one digit, display a number on it, and keep all other digits OFF.
Based on the code you already have, add this function:
void lightOneDigit(int digitIndex, int num) {
// Step 1: Turn OFF all digits to prevent ghosting
digitalWrite(seg1, HIGH);
digitalWrite(seg2, HIGH);
digitalWrite(seg3, HIGH);
digitalWrite(seg4, HIGH);// Step 2: Send the segment code for the number you want to display
Display(num);// Step 3: Turn ON only the selected digit
digitalWrite(digitIndex, LOW);
}
To display a number on a specific digit, just call:
lightOneDigit(seg1, 0);
This means:
In the same way:
By calling these functions quickly one after another in loop(), you can make all four digits show different numbers at the same time.