DIGITAL CLOCK USING ATMEGA328 IC

 

    My idea was to create a simple digital clock. As I was familiar with Arduino microcontroller programming, I chose arduino uno for my project. I wanted my digital clock to look like a product. So, I cannot use my arduino uno board as it was big and needed plenty of jumper cables for connections. But I decided to use only the microcontroller chip which is atmega328. I found on the internet that atmega328 IC can be used like the arduino uno with a couple of capacitors and a crystal oscillator.

Stage – 1: Preparing block diagram and circuit diagram



    The block diagram consists of the atmega328 microcontroller. For that, a start button, a reset button, hour setting button and minute setting button are connected as input. All these buttons are 4-pin pushbuttons. And an LED, a 4-digit 7-segment display is used as output. 


    As I was going to use atmega328 microcontroller, I had to figure out the pin configuration of that IC. After that I was looking for the right hardware components. Arduino uno board has a crystal oscillator in it, to produce clock signals for the microcontroller. I had to look for a 16MHz crystal oscillator for armega328 chip. A couple of 22pF capacitors are connected in parallel to the crystal oscillator. A 10k resistor has to be connected between the reset pin of the microcontroller and the positive terminal of the power supply. A pushbutton is connected in parallel between the 10k resistor and the negative terminal, to reset the system. A red color LED is connected to digital pin 6 of the microcontroller with 220 ohms resistor to indicate seconds passing in the clock. A pushbutton is connected to the digital pin 13 of the microcontroller to start the clock after setting the time. Another pushbutton is connected to the digital pin 11 of the microcontroller to set the minute of the clock. Another pushbutton is connected to the digital pin 12 to set the hour of the clock. The 4-digit 7-segment display is connected to the appropriate pins to display hours and minutes. I chose a common cathode display in which all the segments are connected to the positive.

Stage - 2: Programming 4-digit 7-segment display

     

    The main component in the project which is going to show the time is the display. So, I had to learn how to program it first. This display is going to show only numbers. So, I wrote code how to access the different segments to display numbers.

Stage – 3: Code for calculating seconds, minutes and hours

    This was the difficult part of my project. I was trying multiple ways to calculate seconds. If seconds were calculated accurately, then minutes and hours were very easy to calculate. Then I found out that by accessing timers I can calculate time easily. I used millis() function to calculate time. Microcontrollers use milliseconds for time calculation.

Stage – 4: Making the circuit in breadboard with arduino uno board

    The coding part was almost done and it was the time for making the physical circuit. I connected all the components as per the circuit diagram. It worked very well as expected. But I was using the arduino uno board for this. For this project, I had to replace the board with atmega328 chip.



Stage – 5: Testing with atmega328 microcontroller IC

    I had to change certain things for implementing the project with atmega328 IC. I needed to provide the power supply separately. The operating voltage of the IC was 5 volts. But I was able to get only 9-volt batteries. So, I had to step down the 9 volts to 5 volts. For that, I used a 7805 voltage regulator IC. It can take up to 12-volts and give out 5-volts. To make the power supply more stable, I used a 10 micro-Farad capacitor across the positive and negative terminals of the power supply.

          Everything was connected perfectly. But, when I switched on the battery, it did not work. I rechecked each and every connection but still did not work. I tested and changed certain components, but still did not work. After that, it worked with some glitches. Then I found out that the connections were not that strong. I was using an old breadboard and old jumper wires. Then I bought a new breadboard and new hookup wires for the project. Then it worked fine. 


Stage – 6: Final project output

    The final output video of my project is attached below.


The code is also attached.

int A = A0;       //The 'A' segment of the display is connected to analog pin-0(23) of the atmega328 chip

int B = A1;       //The 'B' segment of the display is connected to analog pin-1(24) of the atmega328 chip

int C = A2;       //The 'C' segment of the display is connected to analog pin-2(25) of the atmega328 chip

int D = A3;       //The 'D' segment of the display is connected to analog pin-3(26) of the atmega328 chip

int E = A4;       //The 'E' segment of the display is connected to analog pin-4(27) of the atmega328 chip

int F = A5;       //The 'F' segment of the display is connected to analog pin-5(28) of the atmega328 chip

int G = 13;       //The 'G' segment of the display is connected to digital pin-13(19) of the atmega328 chip

int DP = 8;       //The decimal point of the display is connected to digital pin-8(14) of the atmega328 chip

 

int D1 = 12;      //The first digit of the 4-digit 7-segment display is connected to digital pin-12(18) of the atmega328 chip

int D2 = 11;      //The second digit of the 4-digit 7-segment display is connected to digital pin-11(17) of the atmega328 chip

int D3 = 10;      //The third digit of the 4-digit 7-segment display is connected to digital pin-10(16) of the atmega328 chip

int D4 = 9;       //The fourth digit of the 4-digit 7-segment display is connected to digital pin-9(15) of the atmega328 chip

 

int start_button = 7;       //a pushbutton is connected to digital pin 7(13) to start the digital clock

int hour_button = 6;        //a pushbutton is connected to digital pin 6(12) to change the hour of the digital clock

int minute_button = 5;        //a pushbutton is connected to digital pin 5(11) to change the minute of the digital clock

 

int HH = 0;        //variable HH(hour value) is initialized as 0;

int MM = 0;       //variable MM(minute value) is initialized as 0;

 

int H1 = 0, H2 = 0, M1 = 0, M2 = 0;       //variables H1, H2, M1, M2 are used for storing the hours and minutes of the clock

 

unsigned long previousmillis = 0;       //stores the value of timer

long interval = 1000;       //1000 milliseconds = 1 second, to provide 1 second interval in clock

int seconds = 0;        //the second variable is initialized as 0

 

int led = 4;        // a red colour LED is connected to digital pin 4(6)

 

void setup(){

 

 pinMode(A, OUTPUT);        //pin connected to variable A is declared as output

 pinMode(B, OUTPUT);        //pin connected to variable B is declared as output

 pinMode(C, OUTPUT);        //pin connected to variable C is declared as output

 pinMode(D, OUTPUT);        //pin connected to variable D is declared as output

 pinMode(E, OUTPUT);        //pin connected to variable E is declared as output

 pinMode(F, OUTPUT);        //pin connected to variable F is declared as output

 pinMode(G, OUTPUT);        //pin connected to variable G is declared as output

 pinMode(DP, OUTPUT);       //pin connected to variable DP is declared as output

 

 pinMode(D1, OUTPUT);       //pin connected to variable D1 is declared as output

 pinMode(D2, OUTPUT);       //pin connected to variable D2 is declared as output

 pinMode(D3, OUTPUT);       //pin connected to variable D3 is declared as output

 pinMode(D4, OUTPUT);       //pin connected to variable D4 is declared as output

 

 pinMode(start_button, INPUT);        //pin connected to variable start_button is declared as input

 pinMode(hour_button, INPUT);       //pin connected to variable hour_button is declared as input

 pinMode(minute_button, INPUT);       //pin connected to variable minute_button is declared as input

 

 pinMode(led, OUTPUT);        //pin connected to variable led is declared as output

 

 

 int state = 1;

 int val1, val2, val3;

  while(state){

   val1 = digitalRead(start_button);        //reading the state(HIGH or LOW) of the pushbutton and storing it in the variable val1

    if(val1 == 1){

      state = 0;

    }

   

    val2 = digitalRead(hour_button);        //reading the state(HIGH or LOW) of the pushbutton and storing it in the variable val2

    if(val2 == 1){

      if(HH <= 11){

      HH = HH + 1;

      delay(1000);          //waiting for 1 second

      }

    }

   

    val3 = digitalRead(minute_button);        //reading the state(HIGH or LOW) of the pushbutton and storing it in the variable val3

    if(val3 == 1){

      if(MM <= 58)

      MM = MM + 1;

      delay(1000);        //waiting for 1 second

    }

   H1 = int(HH / 10);       //to get the first digit(tenth digit) of the hour value

   H2 = int(HH % 10);       //to get the last digit(unit digit) of the hour value

   M1 = int(MM / 10);       //to get the first digit(tenth digit) of the minute value

   M2 = int(MM % 10);       //to get the last digit(unit digit) of the minute value

   

    digits(H1);       //calling the function digits

    digitalWrite(D1, LOW);        //making the first digit of the display ON

 digitalWrite(D2, HIGH);

 digitalWrite(D3, HIGH);

 digitalWrite(D4, HIGH);

 digitalWrite(DP, LOW);

    delay(5);

   

    digits(H2);

    digitalWrite(D1, HIGH);

 digitalWrite(D2, LOW);       //making the second digit of the display ON

 digitalWrite(D3, HIGH);

 digitalWrite(D4, HIGH);

 digitalWrite(DP, LOW);

    delay(5);

   

    digits(M1);

    digitalWrite(D1, HIGH);

 digitalWrite(D2, HIGH);

 digitalWrite(D3, LOW);       //making the third digit of the display ON

 digitalWrite(D4, HIGH);

 digitalWrite(DP, LOW);

    delay(5);

   

    digits(M2);

    digitalWrite(D1, HIGH);

 digitalWrite(D2, HIGH);

 digitalWrite(D3, HIGH);

 digitalWrite(D4, LOW);       //making the last digit of the display ON

 digitalWrite(DP, LOW);

    delay(5);       //5 millisoconds delay to provide continous display of the time

  }

}

 

void digits(int num){       //defining the functon digits(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) with integer parameter

  #define ON HIGH       //defining variable ON as HIGH(1)  

  #define OFF LOW       //defining variable OFF as LOW(0)

  switch(num){

    case 0:               //switch-case to display number 0

 digitalWrite(A, ON);

 digitalWrite(B, ON);

 digitalWrite(C, ON);

 digitalWrite(D, ON);

 digitalWrite(E, ON);

 digitalWrite(F, ON);

 digitalWrite(G, OFF);

    break;

   

    case 1:               //switch-case to display number 1

    digitalWrite(A, OFF);

 digitalWrite(B, ON);

 digitalWrite(C, ON);

 digitalWrite(D, OFF);

 digitalWrite(E, OFF);

 digitalWrite(F, OFF);

 digitalWrite(G, OFF);

    break;

   

    case 2:               //switch-case to display number 2

    digitalWrite(A, ON);

 digitalWrite(B, ON);

 digitalWrite(C, OFF);

 digitalWrite(D, ON);

 digitalWrite(E, ON);

 digitalWrite(F, OFF);

 digitalWrite(G, ON);

    break;

   

    case 3:               //switch-case to display number 3

    digitalWrite(A, ON);

 digitalWrite(B, ON);

 digitalWrite(C, ON);

 digitalWrite(D, ON);

 digitalWrite(E, OFF);

 digitalWrite(F, OFF);

 digitalWrite(G, ON);

    break;

   

    case 4:               //switch-case to display number 4

    digitalWrite(A, OFF);

 digitalWrite(B, ON);

 digitalWrite(C, ON);

 digitalWrite(D, OFF);

 digitalWrite(E, OFF);

 digitalWrite(F, ON);

 digitalWrite(G, ON);

    break;

   

    case 5:               //switch-case to display number 5

    digitalWrite(A, ON);

 digitalWrite(B, OFF);

 digitalWrite(C, ON);

 digitalWrite(D, ON);

 digitalWrite(E, OFF);

 digitalWrite(F, ON);

 digitalWrite(G, ON);

    break;

   

    case 6:               //switch-case to display number 6

    digitalWrite(A, ON);

 digitalWrite(B, OFF);

 digitalWrite(C, ON);

 digitalWrite(D, ON);

 digitalWrite(E, ON);

 digitalWrite(F, ON);

 digitalWrite(G, ON);

    break;

   

    case 7:               //switch-case to display number 7

    digitalWrite(A, ON);

 digitalWrite(B, ON);

digitalWrite(C, ON);

digitalWrite(D, OFF);

 digitalWrite(E, OFF);

 digitalWrite(F, OFF);

 digitalWrite(G, OFF);

    break;

   

    case 8:               //switch-case to display number 8

    digitalWrite(A, ON);

 digitalWrite(B, ON);

 digitalWrite(C, ON);

 digitalWrite(D, ON);

 digitalWrite(E, ON);

 digitalWrite(F, ON);

 digitalWrite(G, ON);

    break;

   

    case 9:               //switch-case to display number 9

    digitalWrite(A, ON);

 digitalWrite(B, ON);

 digitalWrite(C, ON);

 digitalWrite(D, ON);

 digitalWrite(E, OFF);

 digitalWrite(F, ON);

 digitalWrite(G, ON);

    break;

  }

}

 

void loop(){

 unsigned long currentmillis = millis();            //millis() function starts timer from 0

  if(currentmillis - previousmillis >= interval){       //condition for the timer to count 1 second

    digitalWrite(led, HIGH);

    seconds = seconds + 1;

    if(seconds > 59){

      seconds = 0;

      MM = MM + 1;

    }

    if(MM > 59){

      MM = 0;

      HH = HH + 1;

    }

    if(HH > 12){

      HH = 1;

    }

   

    previousmillis = currentmillis;

  }

 

  H1 = int(HH / 10);

  H2 = int(HH % 10);

  M1 = int(MM / 10);

  M2 = int(MM % 10);

 

   digits(H1);

    digitalWrite(D1, LOW);

 digitalWrite(D2, HIGH);

 digitalWrite(D3, HIGH);

 digitalWrite(D4, HIGH);

 digitalWrite(DP, LOW);

    delay(5);

   

    digits(H2);

    digitalWrite(D1, HIGH);

 digitalWrite(D2, LOW);

 digitalWrite(D3, HIGH);

 digitalWrite(D4, HIGH);

 digitalWrite(DP, LOW);

    delay(5);

   

    digits(M1);

    digitalWrite(D1, HIGH);

 digitalWrite(D2, HIGH);

 digitalWrite(D3, LOW);

 digitalWrite(D4, HIGH);

 digitalWrite(DP, LOW);

    delay(5);

   

    digits(M2);

    digitalWrite(D1, HIGH);

 digitalWrite(D2, HIGH);

 digitalWrite(D3, HIGH);

 digitalWrite(D4, LOW);

 digitalWrite(DP, LOW);

    delay(5);

 

  digitalWrite(led, LOW);

}

          













Comments