Need Help - Binary clock with arduino nano + DS3231

Dear All,
I am new user of this forum, and newbee in arduino programming,

I try to crete a binary clock using this code :

#include <Wire.h>
#include "DS3231.h"

//DS3232 I2C Address
#define RTC_ADDRESS 0x68

//DS3232 Register Addresses
#define RTC_SECONDS 0x00
#define RTC_MINUTES 0x01
#define RTC_HOURS 0x02

//Times keeping variables
byte seconds;  //00-59
byte minutes;  //00-59
byte hours;    //00-23



//Buttons pins
const int minutesButton = A0;
const int hoursButton = A1;

//LED pins
const int hoursPin10 = 0;
const int hoursPin20 = 1;

const int hoursPin1 = 2;
const int hoursPin2 = 3;
const int hoursPin4 = 4;
const int hoursPin8 = 5;

const int minutesPin10 = 6;
const int minutesPin20 = 7;
const int minutesPin40 = 8;

const int minutesPin1 = 9;
const int minutesPin2 = 10;
const int minutesPin4 = 11;
const int minutesPin8 = 12;



void setup() {
  //Set Button pins mode
  pinMode(minutesButton, INPUT);
  pinMode(hoursButton, INPUT);

  //Set LED pins mode
  pinMode(hoursPin10, OUTPUT);
  pinMode(hoursPin20, OUTPUT);
  pinMode(hoursPin1, OUTPUT);
  pinMode(hoursPin2, OUTPUT);
  pinMode(hoursPin4, OUTPUT);
  pinMode(hoursPin8, OUTPUT);
  pinMode(minutesPin10, OUTPUT);
  pinMode(minutesPin20, OUTPUT);
  pinMode(minutesPin40, OUTPUT);
  pinMode(minutesPin1, OUTPUT);
  pinMode(minutesPin2, OUTPUT);
  pinMode(minutesPin4, OUTPUT);
  pinMode(minutesPin8, OUTPUT);

  //Start the RTC communication
  Wire.begin();

  //Initialize and set time
  seconds = 00;
  minutes = 00;
  hours = 00;
}



void loop() {
  checkInputs();
  getTime();
  displayLights();
  delay(500);
}



//----- Input Function -----

void checkInputs() {
  if (digitalRead(minutesButton) == HIGH) {
    minutes++;
    seconds = 00;
    if (minutes == 60) {
      minutes = 00;
    }
    setTime();
  }

  if (digitalRead(hoursButton) == HIGH) {
    hours++;
    seconds = 00;
    if (hours == 24) {
      hours = 00;
    }
    setTime();
  }
}



//----- RTC Functions -----

void setTime() {
  Wire.beginTransmission(RTC_ADDRESS);
  Wire.write(0);
  Wire.write(decToBcd(seconds));
  Wire.write(decToBcd(minutes));
  Wire.write(decToBcd(hours));
  Wire.endTransmission();
}

void getTime() {
  Wire.beginTransmission(RTC_ADDRESS);
  Wire.write(0x00);
  Wire.endTransmission();

  Wire.requestFrom(RTC_ADDRESS, 3);
  seconds = bcdToDec(Wire.read());
  minutes = bcdToDec(Wire.read());
  hours = bcdToDec(Wire.read() & 0b00111111);
}




//----- Utility functions ----

byte decToBcd(byte val) {
  return ((val / 10 * 16) + (val % 10));
}

byte bcdToDec(byte val) {
  return ((val / 16 * 10) + (val % 16));
}



//----- Display Function -----

void displayLights() {
  if (hours >= 20) {
    digitalWrite(hoursPin20, HIGH);
    digitalWrite(hoursPin10, LOW);
  } else if (hours >= 10) {
    digitalWrite(hoursPin20, LOW);
    digitalWrite(hoursPin10, HIGH);
  } else {
    digitalWrite(hoursPin20, LOW);
    digitalWrite(hoursPin10, LOW);
  }

  int hoursUnit = hours % 10;
  if (hoursUnit >= 8) {
    digitalWrite(hoursPin8, HIGH);
    digitalWrite(hoursPin4, LOW);
    digitalWrite(hoursPin2, LOW);
  } else if (hoursUnit >= 6) {
    digitalWrite(hoursPin8, LOW);
    digitalWrite(hoursPin4, HIGH);
    digitalWrite(hoursPin2, HIGH);
  } else if (hoursUnit >= 4) {
    digitalWrite(hoursPin8, LOW);
    digitalWrite(hoursPin4, HIGH);
    digitalWrite(hoursPin2, LOW);
  } else if (hoursUnit >= 2) {
    digitalWrite(hoursPin8, LOW);
    digitalWrite(hoursPin4, LOW);
    digitalWrite(hoursPin2, HIGH);
  } else {
    digitalWrite(hoursPin8, LOW);
    digitalWrite(hoursPin4, LOW);
    digitalWrite(hoursPin2, LOW);
  }
  if (hoursUnit % 2 == 1) {
    digitalWrite(hoursPin1, HIGH);
  } else {
    digitalWrite(hoursPin1, LOW);
  }

  if (minutes >= 40) {
    digitalWrite(minutesPin40, HIGH);
    digitalWrite(minutesPin20, LOW);
    if (minutes >= 50) {
      digitalWrite(minutesPin10, HIGH);
    } else {
      digitalWrite(minutesPin10, LOW);
    }
  } else if (minutes >= 20) {
    digitalWrite(minutesPin40, LOW);
    digitalWrite(minutesPin20, HIGH);
    if (minutes >= 30) {
      digitalWrite(minutesPin10, HIGH);
    } else {
      digitalWrite(minutesPin10, LOW);
    }
  } else if (minutes >= 10) {
    digitalWrite(minutesPin40, LOW);
    digitalWrite(minutesPin20, LOW);
    digitalWrite(minutesPin10, HIGH);
  } else {
    digitalWrite(minutesPin40, LOW);
    digitalWrite(minutesPin20, LOW);
    digitalWrite(minutesPin10, LOW);
  }

  int minutesUnit = minutes % 10;
  if (minutesUnit >= 8) {
    digitalWrite(minutesPin8, HIGH);
    digitalWrite(minutesPin4, LOW);
    digitalWrite(minutesPin2, LOW);
  } else if (minutesUnit >= 6) {
    digitalWrite(minutesPin8, LOW);
    digitalWrite(minutesPin4, HIGH);
    digitalWrite(minutesPin2, HIGH);
  } else if (minutesUnit >= 4) {
    digitalWrite(minutesPin8, LOW);
    digitalWrite(minutesPin4, HIGH);
    digitalWrite(minutesPin2, LOW);
  } else if (minutesUnit >= 2) {
    digitalWrite(minutesPin8, LOW);
    digitalWrite(minutesPin4, LOW);
    digitalWrite(minutesPin2, HIGH);
  } else {
    digitalWrite(minutesPin8, LOW);
    digitalWrite(minutesPin4, LOW);
    digitalWrite(minutesPin2, LOW);
  }
  if (minutesUnit % 2 == 1) {
    digitalWrite(minutesPin1, HIGH);
  } else {
    digitalWrite(minutesPin1, LOW);
  }
}

a little bit modify from this website :

Issue is.. it doesn't work well !

I used prototype board, and tried to test 1 - 2 - 4 - 8 minutes.
I obtained a display "loop" with only

2 minutes led light
then
1 + 2 minutes led light
then
2 minutes led light

and this pattern repeat.

please could you help me to find the issue?

Other issue, is I can't set up the clock by using buttons, when I tried to put 5V into A0 input, sometimes nothing happen, and sometime the 1 minute led blink every second

Thank you very much !

Benoit

Welcome to the forum

You started a topic in the Uncategorised category of the forum

Your topic has been moved to a relevant category. Please be careful in future when deciding where to start new topics

1 Like

Run the I2C scanner and let us know what you get. Also post an annotated schematic showing exactly how you have wired it, show all connections, power, ground, power sources and note any wires over 25cm/10".

Hello gilshutz,

Thanks for answer !

I made my connexions as the pucture below, for the moment I only connected "blue LEDs" and DS3231.

No wire above 25cm are used.

For the i2C scanner, I downloaded library in arduino IDE, but I don't know how to use it, sorry, I am really new in this world, I am currently trying to learn a to perform the I2C scanner using google search.

thanks!

I used this code, and injected it in my arduino system :

#include "Wire.h"
 
void setup(){
    Wire.begin();    
    Serial.begin(9600);
} 
 
void loop(){
    byte error, address;
    int nDevices;
 
    Serial.println("Scanning...");
 
    nDevices = 0;
    for(address = 8; address < 127; address++ ){
        Wire.beginTransmission(address);
        error = Wire.endTransmission();
 
        if (error == 0){
            Serial.print("I2C device found at address 0x");
            if (address < 16)
                Serial.print("0");
            Serial.print(address,HEX);
            Serial.println(" !");
 
            nDevices++;
        }
        else if (error == 4) {
            Serial.print("Unknow error at address 0x");
            if (address < 16)
                Serial.print("0");
            Serial.println(address,HEX);
        } 
    }
    if (nDevices == 0)
        Serial.println("No I2C devices found\n");
    else
        Serial.println("done\n");
 
    delay(5000);
}

After that, i used arduino, IDE, and select, "Tools" , then, "serial monitor"

I hope I did well.

here is a capture of the obtained informations :

Here is a schematic of what i exactly did :

Power source is USB from my computer

please find the current project and code in this simulation (which not work too)

The I2C scanner is just a piece of diagnostic software that you run on the Arduino. Load it the same as you would your program, compile it, upload it and the terminal will give you the results. I would seriously consider getting a copy of the Arduino Cookbook, and skim it, most if not all of your project is in there.

thanks for these info gilshultz.
So i did well, and i have 2 I2C devices,
on adress 0x57 and 0x68.

Please could we come back to my initial issue, which is the binary clock programm doesn't work and i don't know why ?

thanks a lot :slight_smile:
Benoit

dear all, not a lot of people to help me, do you need more information?

thanks,
Benoit

1 Like

I succeed with this code !

/*
  An open-source binary clock for Arduino.
  Based on the code from by Rob Faludi (http://www.faludi.com)
  Code under (cc) by Lucas Berbesson
  http://creativecommons.org/license/cc-gpl
  review to set time by Benwood
*/
#include <DS3231.h>

// Init the DS3231 using the hardware interface
DS3231  rtc(SDA, SCL);

// Init a Time-data structure
Time  t;
 

int second = 0, minute = 0, hour = 0; //initialise la base de temps
int munit, hunit, minuteTens, hourTens, valm = 0, valh = 0, ledstats, i;

//Attribue les leds sur les differentes sorties
#define LED1 2
#define LED2 3
#define LED3 4
#define LED4 5
#define LED5 6
#define LED6 7
#define LED7 8
#define LED8 9
#define LED9 10
#define LED10 11
#define LED11 12
#define LED12 13
#define LED13 14 //A0

void setup() {
  //set outputs

  // Setup Serial connection
  Serial.begin(115200);
   
  // Initialize the rtc object
  rtc.begin();
  
   //
  //
  //
  //HEURE A REGLER CI DESSOUS
  //
  // 
  //
  //

 
  rtc.setTime(22, 42, 30);     // reglage de l'heure au format 12:00:00 (24hr format)
 

  //
  //
  //
  //
  //
  //
  //
  //
  //
  //


  for (int k = 2; k <= 14; k++) {
    pinMode(k, OUTPUT);
    digitalWrite(k, LOW);

  }
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  rtc.begin();
}

void loop() {
  
  // Send time
  Serial.println(rtc.getTimeStr());
    
  t = rtc.getTime();
  second = t.sec;
  minute = t.min;
  hour = t.hour;


  munit = minute % 10; //sets the variable munit and hunit for the unit digits
  hunit = hour % 10;
  minuteTens = (int)(minute / 10);
  hourTens = (int)(hour / 10);
  //minutes units
  if (munit & 1) {
    digitalWrite(LED1, HIGH);
  } else {
    digitalWrite(LED1, LOW);
  }
  if (munit & 2) {
    digitalWrite(LED2, HIGH);
  } else {
    digitalWrite(LED2, LOW);
  }
  if (munit & 4) {
    digitalWrite(LED3, HIGH);
  } else {
    digitalWrite(LED3, LOW);
  }
  if (munit & 8) {
    digitalWrite(LED4, HIGH);
  } else {
    digitalWrite(LED4, LOW);
  }

  //minutes
  if (minuteTens & 1)  {
    digitalWrite(LED5, HIGH);
  } else {
    digitalWrite(LED5, LOW);
  }
  if (minuteTens & 2)  {
    digitalWrite(LED6, HIGH);
  } else {
    digitalWrite(LED6, LOW);
  }
  if (minuteTens & 4) {
    digitalWrite(LED7, HIGH);
  } else {
    digitalWrite(LED7, LOW);
  }

  //hour units
  if (hunit & 1) {
    digitalWrite(LED8, HIGH);
  } else {
    digitalWrite(LED8, LOW);
  }
  if (hunit & 2) {
    digitalWrite(LED9, HIGH);
  } else {
    digitalWrite(LED9, LOW);
  }
  if (hunit & 4) {
    digitalWrite(LED10, HIGH);
  } else {
    digitalWrite(LED10, LOW);
  }
  if (hunit & 8) {
    digitalWrite(LED11, HIGH);
  } else {
    digitalWrite(LED11, LOW);
  }

  //hour
  if (hourTens & 1)  {
    digitalWrite(LED12, HIGH);
  } else {
    digitalWrite(LED12, LOW);
  }
  if (hourTens & 2)  {
    digitalWrite(LED13, HIGH);
  } else {
    digitalWrite(LED13, LOW);
  }

  valm = digitalRead(A1);    // add one minute when pressed
  if (valm == HIGH) {
    minute++;
    second = 0;
    rtc.setTime(hour, minute, second);
    delay(250);
  }

  valh = digitalRead(A2);    // add one hour when pressed
  if (valh == HIGH) {
    hour++;
    if (hour > 24) {
      hour = 0;
    }
    second = 0;
    rtc.setTime(hour, minute, second);
    delay(250);
  }
  delay(50);
}

I would like to light on all the leds for 1 second at each new hour, can you please help ?

HERE is the good DS3231 library to use

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.