DS3231

sprinkfitter
Posts: 11
Joined: Tue Nov 21, 2017 1:15 am

DS3231

Postby sprinkfitter » Thu Nov 23, 2017 12:20 am

I have a Ds3231 on my esp32 by i2c. I want to put 2 relays and control them by my DS3231. My clock displays the correct time by serial port. How do I set up tripping my relays by the clock time? Starting to like this chip a lot. Thank You for all the help

Sprinkfitter

sprinkfitter
Posts: 11
Joined: Tue Nov 21, 2017 1:15 am

Re: DS3231

Postby sprinkfitter » Thu Nov 23, 2017 3:48 pm

Code: Select all

#include "Wire.h"

#define SDA_PIN 21
#define SCL_PIN 22
#define DS3231_I2C_ADDRESS 0x68

const int relay1= 2;//Digital pin that the Relay is connected
const int relay2= 5;//Digital pin that the Relay is connected

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return( (val/16*10) + (val%16) );
}
void setup()
{
  Wire.begin();
  Serial.begin(9600);
pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);

digitalWrite(relay1,LOW); //OFF
digitalWrite(relay2,LOW); //OFF

  // set the initial time here:
  //DS3231 seconds, minutes, hours, day, date, month, year
  //setDS3231time(30,30,18,4,22,11,17);
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
void displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  &year);
  // send it to the serial monitor
  Serial.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  Serial.print(":");
  if (minute<10)
  {
    Serial.print("0");
  }
  Serial.print(minute, DEC);
  Serial.print(":");
  if (second<10)
  {
    Serial.print("0");
  }
  Serial.print(second, DEC);
  Serial.print(" ");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print(" Day of week: ");
  switch(dayOfWeek){
  case 1:
    Serial.println("Sunday");
    break;
  case 2:
    Serial.println("Monday");
    break;
  case 3:
    Serial.println("Tuesday");
    break;
  case 4:
    Serial.println("Wednesday");
    break;
  case 5:
    Serial.println("Thursday");
    break;
  case 6:
    Serial.println("Friday");
    break;
  case 7:
    Serial.println("Saturday");
    break;
  }
}
void loop()
{
  displayTime(); // display the real-time clock data on the Serial Monitor,
  delay(1000); // every second

digitalWrite(relay1,HIGH); //turns the relay OFF
delay(3000); //waits for a second
digitalWrite(relay1,LOW); //turns the relay ON
delay(3000); //waits for a second

digitalWrite(relay2,HIGH); //turns the relay OFF
delay(3000); //waits for a second
digitalWrite(relay2,LOW); //turns the relay ON
delay(3000); //waits for a second
}

sprinkfitter
Posts: 11
Joined: Tue Nov 21, 2017 1:15 am

Re: DS3231

Postby sprinkfitter » Sat Nov 25, 2017 8:06 pm

New guy here. Have I posted this in the wrong forum?? Sorry just looking for some help. Thanks to all!!!

Sprinkfitter

ESP_Sprite
Posts: 1218
Joined: Thu Nov 26, 2015 4:08 am

Re: DS3231

Postby ESP_Sprite » Sat Nov 25, 2017 10:57 pm

You're probably not getting any response because you're asking a really broad question without expaining what you want and/or already tried yourself. Can you at the very least tell us which methods you already tried and why they didn't work?

sprinkfitter
Posts: 11
Joined: Tue Nov 21, 2017 1:15 am

Re: DS3231

Postby sprinkfitter » Sun Nov 26, 2017 12:14 am

Sorry very new to the forum. Very new to the board and very new to the Ardinuo. IDE. I have search the web looking for a library that works with the rtc and esp32. I want to trip these relays by time like (24 hour) On at 6 hour off at 12 hour for the first relay and on 13 and off 20 for the second relay. Very new been using relay with delay and that's it. Thanks for taking the time to read my post

Sprinkfitter

ESP_Sprite
Posts: 1218
Joined: Thu Nov 26, 2015 4:08 am

Re: DS3231

Postby ESP_Sprite » Sun Nov 26, 2017 4:14 pm

Okay, so you now have a function that can return the current time. (I assume it works fine.) Could you wager a guess on how you could make an alarm out of this? As in, without coding anything, just describing the process, what would you need to do?

sprinkfitter
Posts: 11
Joined: Tue Nov 21, 2017 1:15 am

Re: DS3231

Postby sprinkfitter » Sun Nov 26, 2017 8:11 pm

I have been working to call the byte hours and minutes

Code: Select all

#include "Wire.h"

#define SDA_PIN 21
#define SCL_PIN 22
#define DS3231_I2C_ADDRESS 0x68


const int relay1 = 2; //Digital pin that the Relay is connected
const int relay2 = 5; //Digital pin that the Relay is connected
const int OnHour1 = 12;
const int OnMin1 = 25;
const int OffHour1 = 12;
const int OffMin1 = 25;


// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val / 10 * 16) + (val % 10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val / 16 * 10) + (val % 16) );
}
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);

  digitalWrite(relay1, LOW); //OFF
  digitalWrite(relay2, LOW); //OFF

  // set the initial time here:
  //DS3231 seconds, minutes, hours, day, date, month, year
  //setDS3231time(30,30,18,4,22,11,17);
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
                   dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3231time(byte *second,
                    byte *minute,
                    byte *hour,
                    byte *dayOfWeek,
                    byte *dayOfMonth,
                    byte *month,
                    byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
void displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
                 &year);
  // send it to the serial monitor
  Serial.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  Serial.print(":");
  if (minute < 10)
  {
    Serial.print("0");
  }
  Serial.print(minute, DEC);
  Serial.print(":");
  if (second < 10)
  {
    Serial.print("0");
  }
  Serial.print(second, DEC);
  Serial.print(" ");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print(" Day of week: ");
  switch (dayOfWeek) {
    case 1:
      Serial.println("Sunday");
      break;
    case 2:
      Serial.println("Monday");
      break;
    case 3:
      Serial.println("Tuesday");
      break;
    case 4:
      Serial.println("Wednesday");
      break;
    case 5:
      Serial.println("Thursday");
      break;
    case 6:
      Serial.println("Friday");
      break;
    case 7:
      Serial.println("Saturday");
      break;
  }
}
void loop()
{
  displayTime(); // display the real-time clock data on the Serial Monitor,
  delay(1000); // every second


  if (readDS3231time (byte hour) == Onhour1 && readDS3231time (byte min) == OnMin1)
   {digitalWrite (Relay1, HIGH);}
   else if (readDS3231time (byte hour) == OffHour1 && readDS3231time (byte min) == OffMin)
   {digitalWrite (Relay1, HIGH);}

}

ESP_Sprite
Posts: 1218
Joined: Thu Nov 26, 2015 4:08 am

Re: DS3231

Postby ESP_Sprite » Mon Nov 27, 2017 9:50 am

Your idea is good, but the way you structure your code is bad. I suggest you copy the beginning of displayTime, and then check the hours and minutes variable after that.

sprinkfitter
Posts: 11
Joined: Tue Nov 21, 2017 1:15 am

Re: DS3231

Postby sprinkfitter » Mon Nov 27, 2017 8:30 pm

I got this code to compile but get fatal error Failed to connect to esp32 Time out waiting for packet header. I have uploaded a sketch to check if Esp32 was working. It work fine for two different sketches.

Code: Select all

#include "Wire.h"

#define SDA_PIN 21
#define SCL_PIN 22
#define DS3231_I2C_ADDRESS 0x68


const int relay1 = 2; //Digital pin that the Relay is connected
const int relay2 = 5; //Digital pin that the Relay is connected
const int OnHour1 = 15;
const int OnMin1 = 6;
const int OffHour1 = 15;
const int OffMin1 = 8;


// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val / 10 * 16) + (val % 10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val / 16 * 10) + (val % 16) );
}
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);

  digitalWrite(relay1, LOW); //OFF
  digitalWrite(relay2, LOW); //OFF

  // set the initial time here:
  //DS3231 seconds, minutes, hours, day, date, month, year
  //setDS3231time(30,30,18,4,22,11,17);
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
                   dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3231time(byte *second,
                    byte *minute,
                    byte *hour,
                    byte *dayOfWeek,
                    byte *dayOfMonth,
                    byte *month,
                    byte *year)

{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
void displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
                 &year);
  // send it to the serial monitor
  Serial.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  Serial.print(":");
  if (minute < 10)
  {
    Serial.print("0");
  }
  Serial.print(minute, DEC);
  Serial.print(":");
  if (second < 10)
  {
    Serial.print("0");
  }
  Serial.print(second, DEC);
  Serial.print(" ");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print(" Day of week: ");
  switch (dayOfWeek) {
    case 1:
      Serial.println("Sunday");
      break;
    case 2:
      Serial.println("Monday");
      break;
    case 3:
      Serial.println("Tuesday");
      break;
    case 4:
      Serial.println("Wednesday");
      break;
    case 5:
      Serial.println("Thursday");
      break;
    case 6:
      Serial.println("Friday");
      break;
    case 7:
      Serial.println("Saturday");
      break;
  }
}
void loop()
{
  displayTime(); // display the real-time clock data on the Serial Monitor,
  delay(1000); // every second

  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
                 &year);
  if (hour == OnHour1 && minute == OnMin1) {
    digitalWrite (relay1, HIGH);
  }
  else if (hour == OffHour1 && minute == OffMin1) {
    digitalWrite (relay1, LOW);
  }

}





Who is online

Users browsing this forum: No registered users and 5 guests