4 Digit Seven Segment Display Digital Clock

by 728848 in Circuits > Arduino

914 Views, 1 Favorites, 0 Comments

4 Digit Seven Segment Display Digital Clock

IMG_3415.jpg

In this project, we will be making a 4 digit digital clock using seven segment displays via multiplexing. This circuit displays time and raises the time by 1 minute every 60 seconds using a coded timer. The circuit utilizes a pushbutton that raises the time by 1 each press to allow the user to set the time to the real time.

Supplies

Multiplexing

circuit1.png

Place your 4 seven segment displays next to each other on the breadboard. Then using wires, connect the same letter pins of each display to each other. For example, jump the A pin from the first display to the second display's A pin, then jump that to the third, then fourth so all of them are connected in series. Then do it with all other pins except decimal point. Now connect pins from the arduino to each pin on the first display using wires so when we digital write to that pin, all same letter pins on all 4 displays will get power since we connected them all together. I'll explain why this is necessary in the next step

Commons

circuit2.png

Place your 4 pnp transistors onto the breadboard ideally close to the displays. Jump ground from the power rail to the collectors. Each transistor corresponds to 1 of the displays, connect the emitter of 1 transistor to 1 of the display's common using a 330 ohm resistor and wire. Do this until all 4 transistors are each connected to their own display's common. Now connect 4 pins on the arduino to the bases of the transistors using wire so when we digital write to them, the corresponding seven segment display will get power to its common. The reason we are not directly connecting the arduino pins to the common pins is because the arduino cannot sustain enough current for all 4 displays so we use the transistors to draw power from the power rail while still controlling it via arduino. Now for the explanation of how multiplexing works, without multiplexing, we would need to allocate an arduino pin for each pin on each display meaning we would need a total of 28 arduino pins for the desired effect which is not practical nor possible with a basic arduino. With multiplexing, we connect all the pins together, digital write the desired pins to create a number, give common to the first display, then digital write a new number whilst removing the common from the previous display and giving common to the next one. This process of turning on and off and switching the displays happens very quickly and is not visible to the human eye allowing the desired effect to be achieved in which 4 solid numbers are visible at once.

Pushbutton

circuit3.png

Place a pushbutton on the breadboard, jump ground onto 1 terminal using the 10k ohm resistor via power rail and jump positive into the opposite terminal also via power rail. Then connect an arduino pin using wire to the adjacent terminal to the positive wire. This allows us to digital read the button as an input.

Code

const int segmentPins[7] = {2, 3, 4, 5, 6, 7, 8}; // replace pin numbers with your own

const int digitPins[4] = {9, 10, 11, 12};

const int button = 13;


int time = 100; // you can also set the initial time using this variable

int buttonState = 0;

long timer = millis();


const byte segmentStates[10][7] = {

 {1, 1, 1, 1, 1, 1, 0}, // 0

 {0, 1, 1, 0, 0, 0, 0}, // 1

 {1, 1, 0, 1, 1, 0, 1}, // 2

 {1, 1, 1, 1, 0, 0, 1}, // 3

 {0, 1, 1, 0, 0, 1, 1}, // 4

 {1, 0, 1, 1, 0, 1, 1}, // 5

 {1, 0, 1, 1, 1, 1, 1}, // 6

 {1, 1, 1, 0, 0, 0, 0}, // 7

 {1, 1, 1, 1, 1, 1, 1}, // 8

 {1, 1, 1, 1, 0, 1, 1}  // 9

};


void setup() {

 for (int i = 0; i < 7; i++) {

  pinMode(segmentPins[i], OUTPUT);

 }

 for (int i = 0; i < 4; i++) {

  pinMode(digitPins[i], OUTPUT);

 }

 pinMode(button, INPUT);

}


void displayDigit(int digit, int number) {

 for (int i = 0; i < 4; i++) {

  digitalWrite(digitPins[i], HIGH);

 }

 for (int i = 0; i < 7; i++) {

  digitalWrite(segmentPins[i], !segmentStates[number][i]);

 }

 digitalWrite(digitPins[digit], LOW);

}


void displayNumber(int number) {

 int digit4 = number / 1000;     

 int digit3 = (number / 100) % 10;  

 int digit2 = (number / 10) % 10;  

 int digit1 = number % 10;

 if(digit4 > 0){

  displayDigit(0, digit4);

  delay(5); 

 }

 displayDigit(1, digit3);

 delay(5);  

 displayDigit(2, digit2);

 delay(5);  

 displayDigit(3, digit1);

 delay(5); 

}


void loop() {

 displayNumber(time);

 if(millis() - timer >= 60000){

 time ++;

 timer = millis();

 }

 if(digitalRead(button) == HIGH && buttonState == 0){

  buttonState = 1;

  time ++;

 }

 if(digitalRead(button) == LOW && buttonState == 1){

  buttonState = 0;

 }

 if((time / 10) % 10 == 6){

  time = time+100-60;

 }

if(time == 1300){

  time = 100;

}

}


After copy and pasting this code either above or from the txt file below, ensuring you changed the pin numbers to your own, your circuit should be fully functional if you followed each step correctly

Downloads