How to Interface a 4 Digit 7 Segment Display with Arduino UNO




Hey guys, so this is my first tutorial on my blog and hopefully, it will be of use to any person reading this. 😄




Components:
1. 3 220 Ohms Resistors
2. Arduino UNO (or any other Arduino model with at least 12 Digital Pins)
3. a 4 Digit 7 Segment LED ( The model I'm using is 3461AS)
4. Couple of Jumper Wires
5. A breadboard.

Step 1: Refer to the Datasheet


Now, I'm using the 3461AS Display, but you can use any other model for the 4 digit Displays. The reason why you should refer to your datasheet first is so that you can determine important information such as:
  • Pin numbers
  • Polarity, whether your component has a COMMON ANODE or COMMON CATHODE
  • Display Colour
  • Dimensions
  • Voltage/Current Limits
Now we only need to focus on the first two pieces of information here. Firstly, in the diagram below you are shown the position of PIN 1. This is helpful because we can then label the rest of the pins on our component, which increase as you move anticlockwise along the component. 

 Source:Silsmart Optoelectronics Co

Next, in the datasheet you may find a diagram similar to the one shown below. What you can infer from this is that pins 12,9,8 and 6, control the first,second, third and fourth digits' displays respectively. The rest of the pins control the individual 7 LEDs on the segment.

The multiplexed digits Source:Silsmart Optoelectronics Co

The important thing is that you find the EXACT component datasheet. I made a mistake to referring to one which had a similar component number and it incorrectly labelled the pin controlling the 4rth digit. I could have saved a lot of time had I referred to the correct datasheet first! :( 

Step 2: Understand the underlying concept 

To be honest, there isn't much theory to learn to make a 7 segment digit work. All you need to know is that each individual LED on the 7 segment is denoted by an alphabet. As shown in the diagram below, which was also given in the datasheet. 
The 7 segment alphabetic denotion. Source:Silsmart Optoelectronics Co
So if you power up the pin for A, the A segment will light up. If you send a LOW signal to DP1, the decimal point won't light up. Now, you need to the link up the pin number on your component to the alphabets! In my component, Pin 11 lights up A, pin 7 lights up B and so on... This is summarized below:

  • A=11
  • B=7
  • C=4 
  • D=2
  • E=1
  • F=10
  • G=5
  • DP (decimal point) = 3

Step 3: Connect the Circuit


According to the datasheet, the voltage supply should be between 1.7 to 2.5 V and current should be around 20 mA. If we go higher, we risk damaging the component. Also, Arduino I/O pins will have a current output of 20 mA (it can go up to 40 mA, though). To be on the safe side, you should connect 4 330 Ohms resistors to the digit pins (12 ,9,8,6), however in my experiment I connect 3. (I know it's unsafe to do so, but it didn't cause me any problems though.)

Messy wires.


The image below shows the circuitry I followed.


In this circuit, I used a 9V battery to power up the Arduino.


Step 4: Programming


There are several header files written which cater to 7 segment Displays. These make our lives easier because you don't have individually program for each number or letter every single time you want to code. One such library was uploaded by DeanIsMe on github, and can be easily imported into your Arduino code.

In order to import the library, go to Sketch--> Include Library--> Manage Libraries


Then, search "SevSeg" in the Library Manager search bar and install the first library as shown below:


Bear in mind the Library manager lags a bit so it may take a while. Once you've installed the file copy the Arduino Code below into your program.


//Header File was download from DeanIsMe github SevSeg repository.
// URL:https://github.com/DeanIsMe/SevSeg
// The rest of the code was mostly written by mischmaschbeyond, but the inspirations were taken
// from the example files in DeanIsMe's repository.
//
//This program counts up from 600 till 610, with a 1 sec interval
//afterwards, it displays "bad", "sad", "rad" one after the other

#include "SevSeg.h"
SevSeg sevseg; //Define your class name
#define MAX_NUMBER_STRINGS 3 // Number of words to display, can increase the number if needed
#define MAX_STRING_SIZE 4 // Since a 4 digit 7 Segment Display is used, we limit it to 4 letters
char name[MAX_NUMBER_STRINGS][MAX_STRING_SIZE]; //defining the 2D char array
byte namepos= 0; //position of letter in words is defined
unsigned long times = millis() - 5000; //timer for displaying word
void setup() {
byte numDigits = 4; // 4 digit 7 Segment Display is used, we define it here
// D1 D2 D3 D4
byte digitPins[] = {2, 5, 6, 9}; //PIN NUMBERS OF the digit pins
//a b c d e f g DP
byte segmentPins[] = {3, 7, 10, 12, 13, 4, 1, 11}; //respective pins for 7 segment led pins
bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
byte hardwareConfig = COMMON_CATHODE; // Need to check against your datasheet whether it is COMMON_ANODE or COMMON_CATHODE
bool updateWithDelays = false; // Default. Recommended
bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros

//The strcpy function copies the word in the name char array
//only 3 words have been defined
strcpy(name[0], "bad");
strcpy(name[1], "sad");
strcpy(name[2], "rad");

//passing the relevant variables to the Sevseg class, refer to the header and .cpp file for more info
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros);
sevseg.setBrightness(30); //brightness can range from 0 to 100
}
void loop() {
//declaring timer that counts in microseconds
// returns the amount of time since the program started
static unsigned long timer = micros();
static int sec= 600; //initial start time
if (micros() - timer >= 1000000) { //10000000 us is 1 second
timer += 1000000; // add 1 sec to variable
sec++; // add 1 sec to variable
if (sec == 610) { // Reset to 0 after counting for 610 seconds.
sec=0;
//calls stop function
stop();
}
sevseg.setNumber(sec, 1); //display the time on the 4 digit array
}
sevseg.refreshDisplay(); // Must run repeatedly
digitalWrite(11,LOW); // Depending on your component, you may have to set the decimal point to LOW/HIGH, to turn it off. Check datasheet
}


//This function will run through the 3 words copied in the char array
void stop(){
while(1){
//the 4 digits leds will refresh after more time has elasped thann the times variable
// 5000 is an arbitray no. can change it to something smaller
if(millis() > (times + 5000)){
sevseg.setChars(name[namepos]); // runs through the rows in the array and passed the word to setChars() function, refer to .cpp file for more info

namepos++; // top up to move to next row
if (namepos >= 3) // Only 3 words were defined, if namepos exceeds it restarts to first word
namepos = 0;
times = millis(); //timer is also reset
}

sevseg.refreshDisplay(); // Must run repeatedly
digitalWrite(11,LOW); // TO turn OFF Decimal Point
delay(1);
}
};

Code Explanation:

The code imports the Sevseg library by importing the header file from DeanIsMe's SevSeg repository.

I have defined that there are 4 digits displays, and written out the arduino digital I/O pins which correspond to the digit and 7 segment pins on the component. These have been defined in the comments. You can change the pin numbers too, depending on your circuitry. Also, since my component was COMMON_CATHODE, I set the hardwareConfig to that, although it may differ depending on your component.

Basically, this code starts to count up from 600 and increases every second. This is displayed on the component. Once the number becomes 610, it starts displaying the three words that I have defined "bad", "sad" and "rad". Now you can change it to something else by altering the strcpy() lines. But keep in mind, you are limited to alphabets that can only be displayed on a 7 segment. So letters with Z's in them won't work. 😒







Step 5: Take some pics and impress your friends!

This is the most important step, guys!!






Sources:
Datasheet: http://images02.cdn86.net/kps01/M00/8C/6D/wKiAiVYpjP2evuZuAAwbJQOHNgQ761.pdf
SevSeg library: https://github.com/DeanIsMe/SevSeg




Alright, since this was my first tutorial please let me know how I could improve my content, would really appreciate it!! 






Comments

Popular posts from this blog

Movie Review: Mary Poppins Returns