Panel Cookies
yt_link
insta_link
fb_link
twitter_link

"Nixie" clock


"Nixie" 7 segments clock - CODE
Help me by sharing this post



Download the .zip file below. Open the Arduino (.ino) code on your Arduino IDE and upload it to the Arduino NANO. Make sure you have connections in the schematic below. You could download the code from the link below or also copy and paste the code from below.


Downlaod full code here (last update 01/11/2018)

Download DS3231 library:



7 segments MAX7219 Arduino clock





/*
Code by: ELECTRONOOBS, 16/10/2018
Tutorial webpage: https://www.electronoobs.com/eng_arduino_tut47.php
Scheamtic: https://www.electronoobs.com/eng_arduino_tut47_sch1.php
Youtube channel: https://www.youtube.com/c/ELECTRONOOBS
*/

//We include the DS3231 library for the real time module
#include <DS3231.h>   //Downlaod it here: https://www.electronoobs.com/eng_arduino_ds3231.php
DS3231  rtc(SDA, SCL);          


//Define the I/O
#define MAX7219_Clock 2         //CLK pin for the MAX7219
#define MAX7219_Chip_Select 3   //CS pin for the MAX7219
#define MAX7219_Data_IN 4       //D_in pin for the MAX7219
     
int LED = 8;                    //Pin for the two blinking LEDs
//int SET = 5;                  //Not used any more
int HOUR = 7;                   //Input for set hour button
int MINUTE = 6;                 //Input for set minute button



//Other variables for the code
int Delay=1000;                       //Variable used for the 1 second loop. Each 1s we change the LED state so it will blink    
bool LED_state = false;               //Here we save the LED state each loop, HIGH or LOW
unsigned long previousMillis = 0;     //Variable used to count the 1s loop time
String time_str = "";                 //Here we save the time in a string format from the ral time module
String prev_time_str = "";            //We save the last time value so we could compare it
String hr_1_str = "";                 //We divide the hours and minutes in separate variables first digit for hour
String hr_2_str = "";                 //We divide the hours and minutes in separate variables second digit of hour
String min_1_str = "";                //We divide the hours and minutes in separate variables first digit of minute
String min_2_str = "";                //We divide the hours and minutes in separate variables secodn digit of minute
int hr_1 = 0;                         //first digit for hour
int hr_2 = 0;                         //second digit for hour
int min_1 = 0;                        //first digit for minute
int min_2 = 0;                        //second digit for minute
int set_hour = 0;
int set_minute = 0;



//This function will shift the data to the MAX7219 driver. 
void shift(byte send_to_address, byte send_this_data)
{
  digitalWrite(MAX7219_Chip_Select, LOW);
  shiftOut(MAX7219_Data_IN, MAX7219_Clock, MSBFIRST, send_to_address);
  shiftOut(MAX7219_Data_IN, MAX7219_Clock, MSBFIRST, send_this_data);
  digitalWrite(MAX7219_Chip_Select, HIGH);
}


 
void setup() {
  //Serial.begin(9600);
  rtc.begin();                                  //We start the real time module
  pinMode(MAX7219_Data_IN, OUTPUT);             //Define the data pin for the MAX7219 as output
  pinMode(MAX7219_Chip_Select, OUTPUT);         //Define the cs pin for the MAX7219 as output
  pinMode(MAX7219_Clock, OUTPUT);               //Define the clock pin for the MAX7219 as output
  digitalWrite(MAX7219_Chip_Select, HIGH);      //We set the CS to high, CS is negative enabeled

  pinMode(LED,OUTPUT);                            
  //pinMode(SET,INPUT);                         //Not used any more
  pinMode(HOUR,INPUT);
  pinMode(MINUTE,INPUT);
  //digitalWrite(SET, HIGH);                    //Not used any more
  digitalWrite(HOUR, HIGH); 
  digitalWrite(MINUTE, HIGH);  
  digitalWrite(LED,LED_state);
  delay(200);

  //Setup
  shift(0x0f, 0x00); //display test register - test mode off
  shift(0x0c, 0x01); //shutdown register - normal operation
  shift(0x0b, 0x04); //scan limit register - display digits 0 thru 7
  shift(0x0a, 0x01); //intensity register - Min brightness: 0x01, Max brightness: 0x0f
  shift(0x09, 0xff); //decode mode register - CodeB decode all digits

  shift(0x01, 0xff); //Turn off all segments
  shift(0x02, 0xff); 
  shift(0x03, 0xff); 
  shift(0x04, 0xff);
  delay(100); //Small delay
}

void loop() {
  //This loop will execute each Delay=1000ms, so 1 second
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis >= Delay)
  {
    previousMillis += Delay;          
    time_str = rtc.getTimeStr();      //First,we get the real time, hour, minute, second
    hr_1_str += (char)time_str[0];    //We separate the first digit hour value
    hr_1 = hr_1_str.toInt();          //Pass the hour value from String to int format
    hr_2_str += (char)time_str[1];    //We separate the second digit hour value
    hr_2 = hr_2_str.toInt();          //Pass the hour value from String to int format
    min_1_str += (char)time_str[3];   //We separate the first digit minute value 
    min_1 = min_1_str.toInt();        //Pass the minute value from String to int format
    min_2_str += (char)time_str[4];   //We separate the second digit minute value 
    min_2 = min_2_str.toInt();        //Pass the minute value from String to int format
    LED_state = !LED_state;           //Invert the blinking LED state each loop, so each 1 second.
    digitalWrite(LED,LED_state);      //Turn the LEDs on or off
    if(prev_time_str != time_str)     //If the time is not the same, we print the new value to the 7-seg display
    {
      print_time_to_7_seg(time_str);
    }
    prev_time_str = time_str;         //Save the last time value
   }//end of timer

    hr_1_str = "";                    //Reset the digits String valuew for hours and minutes
    hr_2_str = "";
    min_1_str = "";
    min_2_str = "";

   if(!digitalRead(HOUR))             //If the hour button is pressed, we increase hour by one
   {
     set_hour = hr_1*10+hr_2;         //get total hour value in int format 
     set_minute = min_1*10+min_2;     //get total minuter value in int forma
     set_hour = set_hour + 1;         //Increase hour by 1
     if(set_hour == 24)               //If we get to hour 24, we start from 0
     {
       set_hour = 0;
     }
     rtc.setTime(set_hour, set_minute, 0);     // Write the new time to the RTC module
     //delay(50);
     time_str = rtc.getTimeStr(); 
     print_time_to_7_seg(time_str);            //Print the new time value to the 7-seg dispaly
   }

    //We do the same for the minute push button
   if(!digitalRead(MINUTE))
   {
     set_hour = hr_1*10+hr_2;
     set_minute = min_1*10+min_2;
     set_minute = set_minute + 1;
     if(set_minute == 60)
     {
       set_minute = 1;
     }
     rtc.setTime(set_hour, set_minute, 0);     // Set the time to 12:00:00 (24hr format)
     //delay(50);
     time_str = rtc.getTimeStr(); 
     print_time_to_7_seg(time_str);
   }
}//end of void loop


//This function will shift the numbers to the MAX7219 driver
void print_time_to_7_seg(String the_time)
{

  if(hr_1 == 0)
  {
    shift(0x01, 0); 
  }
  if(hr_1 == 1)
  {
    shift(0x01, 1); 
  }
  if(hr_1 == 2)
  {
    shift(0x01, 2); 
  }
  if(hr_1 == 3)
  {
    shift(0x01, 3); 
  }
  if(hr_1 == 4)
  {
    shift(0x01, 4); 
  }
  if(hr_1== 5)
  {
    shift(0x01, 5); 
  }
  if(hr_1 == 6)
  {
    shift(0x01, 6); 
  }
  if(hr_1 == 7)
  {
    shift(0x01, 7); 
  }
  if(hr_1 == 8)
  {
    shift(0x01, 8); 
  }
  if(hr_1 == 9)
  {
    shift(0x01, 9); 
  }/////////////////////////////////////end of first digit for hour  
  

  if(hr_2 == 0)
  {
    shift(0x02, 0); 
  }
  if(hr_2== 1)
  {
    shift(0x02, 1); 
  }
  if(hr_2 == 2)
  {
    shift(0x02, 2); 
  }
  if(hr_2 == 3)
  {
    shift(0x02, 3); 
  }
  if(hr_2 == 4)
  {
    shift(0x02, 4); 
  }
  if(hr_2 == 5)
  {
    shift(0x02, 5); 
  }
  if(hr_2 == 6)
  {
    shift(0x02, 6); 
  }
  if(hr_2 == 7)
  {
    shift(0x02, 7); 
  }
  if(hr_2 == 8)
  {
    shift(0x02, 8); 
  }
  if(hr_2 == 9)
  {
    shift(0x02, 9); 
  }/////////////////////////////////////end of second digit for hour
    

  if(min_1 == 0)
  {
    shift(0x03, 0); 
  }
  if(min_1 == 1)
  {
    shift(0x03, 1); 
  }
  if(min_1 == 2)
  {
    shift(0x03, 2); 
  }
  if(min_1 == 3)
  {
    shift(0x03, 3); 
  }
  if(min_1 == 4)
  {
    shift(0x03, 4); 
  }
  if(min_1== 5)
  {
    shift(0x03, 5); 
  }
  if(min_1 == 6)
  {
    shift(0x03, 6); 
  }
  if(min_1 == 7)
  {
    shift(0x03, 7); 
  }
  if(min_1 == 8)
  {
    shift(0x03, 8); 
  }
  if(min_1 == 9)
  {
    shift(0x03, 9); 
  }/////////////////////////////////////end of first digit for minute
  

  if(min_2 == 0)
  {
    shift(0x04, 0); 
  }
  if(min_2== 1)
  {
    shift(0x04, 1); 
  }
  if(min_2 == 2)
  {
    shift(0x04, 2); 
  }
  if(min_2 == 3)
  {
    shift(0x04, 3); 
  }
  if(min_2 == 4)
  {
    shift(0x04, 4); 
  }
  if(min_2 == 5)
  {
    shift(0x04, 5); 
  }
  if(min_2 == 6)
  {
    shift(0x04, 6); 
  }
  if(min_2 == 7)
  {
    shift(0x04, 7); 
  }
  if(min_2 == 8)
  {
    shift(0x04, 8); 
  }
  if(min_2 == 9)
  {
    shift(0x04, 9); 
  }/////////////////////////////////////end of first digit for minute
  
}










ADVERTISERS



PCBWAY PCB service





Curso Arduino Online nivel Intermedio