Today I learned to use an RTC and how to control 3 Relays with it.

 How to use an RTC to control 3 Relays


Today I learned how to control 3 Relays with just an RTC and an Arduino board. This project was one of the most major projects I've done in the last few days. As I'd said in the last blog post about the project that had been done in my Father's office that had been completed, I worked on it a bit further.

If you've read the last blog post, you must know that the project was about controlling the tap of the tree-watering pipe using an app and Wi-Fi. But I took it a step further when I used an RTC. 


What is an RTC? RTC stands for 'Real Time Clock' which is  a specialized electronic component designed to keep track of the current date and time, even when a system is powered off, using a small battery for backup. It is used in devices which need the current value of time.👇

So, what I'd done is I took this RTC module and using an Arduino Board, I controlled 3 Relays so that at a specific time the relay should switch on and thereafter the tap of the pipe. 

This project was one a kind-of hard project. It took a bit of time and trial and errors to write the code. 

So come on...Let's look at the steps!

Step 1: Understanding the Project.

So, first I had to gather some info on 'What is an RTC?' , 'How does it work?', etc.
I searched it up on Google and understood the mechanism of the module.

Then, I went on to the next step

Step 2: Setting an RTC to the current time.

Now came the important part of this project which was setting the time on the RTC to the current time. I took some help of the Internet and some reference projects available on Arduino's Forum and YouTube. I found the code and made the appropriate connections.

Connecting the RTC module to Arduino board.
What I needed:
  • Arduino Uno Board 
  • RTC module
  • Jumper wires x 4
Connections:
  • GND - GND (Arduino)
  • VCC - 3.3V (Arduino)
  • SDA - A4 (Analog Pin 4)
  • SCL - A5 (Analog Pin 5)
The connections were ready and so was the code. I've provided the code below 👇.

#include <Wire.h>
#include <RTClib.h>

RTC_DS3231 rtc;

void setup() {
    Serial.begin(9600);
    if (!rtc.begin()) {
        Serial.println("Couldn't find RTC");
        while (1);
    }

    // Set RTC to current computer time (only run once!)
    rtc.adjust(DateTime(__DATE__, __TIME__));
    Serial.println("RTC Time Set!");
}

void loop() {
    // Print the current time every second
    DateTime now = rtc.now();
   
    Serial.print("Current Date & Time: ");
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.println(now.second(), DEC);

    delay(1000); // Wait 1 second before updating

}

In this code, the most important part is that after uploading the code, immediately
you should comment out this line👉 rtc.adjust(DateTime(__DATE__, __TIME__));.
Which means that you should add 2 // before the line. This helps in keeping the
time constant in the RTC module.

After uploading the code, I tested the RTC if it works correctly. For that, I needed
another code. this code is given below.

#include <Wire.h>
#include <RTClib.h>

RTC_DS3231 rtc;

void setup() {
    Serial.begin(9600);
    if (!rtc.begin()) {
        Serial.println("Couldn't find RTC");
        while (1);
    }
}

void loop() {
    DateTime now = rtc.now();
   
    Serial.print("Current Date & Time: ");
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.println(now.second(), DEC);
   
    delay(1000); // Update every second
}

After this code had been uploaded, I opened the Serial Port to check
the current time.

How does this work? The RTC in which I'd uploaded the code,
immediately the RTC checked my computer's time and set the time in
it itself.

So, when I checked it, the time was shown with seconds too. The RTC had a lag due
to which it was 7 seconds late. But this problem occurs with all of the RTC modules
so it is acceptable.

It looked like this...































The RTC module was set and was ready to be connected to the 3 relays and control
them!!


Step 3: Connecting the RTC module to the 3 relays.

What I needed:
  • RTC Module
  • Arduino Board
  • Relay x 3
  • Breadboard
  • Jumper wires x 15
Connections for RTC and Arduino Board:
  • GND - GND (Arduino)
  • VCC - 3.3V (Arduino)
  • SDA - A4 (Analog Pin 4)
  • SCL - A5 (Analog Pin 5)
Connections for the 3 relays and Arduino Board:

First I took a breadboard and 2 Jumper Wires. Then, inserted the end of the 1st
wire into the -ve row of the breadboard and the other into GND of Arduino. Then,
inserted the end of the 2nd wire into the 5V of the Arduino and the other in the
+ve row of the breadboard.
  • GND of each Relay into the -ve row of the breadboard.
  • VCC of each Relay into the +ve row of the breadboard.
  • In1, In2 and In3 into Digital pins 5, 6 and 7 respectively.
The connections are DONE!😎😎
Step 4: Making the code and uploading it to the Arduino Board.

This step took me a lot of time to make and adjust many times. After a lot of trial
and errors with a lot of adjusting the code it was finally ready!!

I've provided the code below...

#include <Wire.h>
#include <RTClib.h>

RTC_DS3231 rtc;

#define RELAY1 5
#define RELAY2 6
#define RELAY3 7

void setup() {
  Serial.begin(9600);
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1)
      ;
  }
  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);

  // Start with relays off
  digitalWrite(RELAY1, HIGH);
  digitalWrite(RELAY2, HIGH);
  digitalWrite(RELAY3, HIGH);
}

void loop() {
  DateTime now = rtc.now();

  int hour = now.hour();
  int minute = now.minute();

  Serial.print("Time: ");
  Serial.print(hour);
  Serial.print(":");
  Serial.println(minute);

  // Relay 1 ON from 16:37 to 16:38
  if (hour == 16 && minute >= 37 && minute < 38) {
    digitalWrite(RELAY1, LOW);
  } else {
    digitalWrite(RELAY1, HIGH);
  }

  // Relay 2 ON from 16:38 to 16:39
  if (hour == 16 && minute >= 38 && minute < 39) {
    digitalWrite(RELAY2, LOW);
  } else {
    digitalWrite(RELAY2, HIGH);
  }

  // Relay 3 ON from 16:39 to 16:40
  if (hour == 16 && minute >= 39 && minute < 40) {
    digitalWrite(RELAY3, LOW);
  } else {
    digitalWrite(RELAY3, HIGH);
  }

  delay(60000);  // Check time every 60 seconds
}

The highlighted part shows the time from which the relays should
switch on and off. I've set the time according to when I want the
tap to open.


The code was a success and the project was ready!!!😎💪👏


Comments

Popular posts from this blog

Creating a Home Automation Project Using Xiao board and Wi-Fi

Making a Neon LED Sign Board

Creating a Home Automation Project Using MIT App Inventor