A working program on a Nano, driving a relay module loses the ability to drive the relay module when an RTC is added to control the times of switching.
That does not sound good. You could post code, schematics, and images of your project so others can help but until then I and others can only sympathize with you.
I have gotten around the problem by adding transistor to drive the relay module but I am curious as to why the nano behaved that way. I will have a go at drawing out the connection diagram and add the code. Maybe I will see what I have done wrong if I do that anyway. It is just a simple light controlled timer to switch a light on and off as the daylight levels change, but with a random switch off time around 23:20 ish.
I would suspect that your relay module is pretty much a relay on a breakout board with connectors to the outside world. But then again, a quick google search brought up many relay modules that have on-board electronics to drive the relay from 5V logic.
Are there any other components on the relay board other than the relay and the connectors?
Can you provide a link to the relay board you bought, or upload a photo of it?
The MCU just does not have the ability to drive the relay coil.
Have you placed a reversed biased diode across the coil to prevent Vspike?
Hi
post an image or a link of your relay module.
The module has a driver transistor and a spike diode built onto the board and it works with other projects, even a simpler version of this with no RTC, just a light sensor. the problem is the D2 pin or any other pin I tried does not give out anything more than 2.8v when it tries to run my program. It works with the simple program without the RTC. I tried reversing the output to see if it could sink enough current using a reversible relay module, that is one that works either with a high input or by moving a jumper, a low input. The Nano could still not sink enough current in either state. Disconnecting the RTC makes no difference. It seems to be the software causing the problem.
#include <Wire.h>
#include "RTClib.h"
#define ledPin 2
#define sensorPin 0
long randNumber;
long wait = 0;
int H =0;
int M =0;
int Switchon = 0;
RTC_DS1307 RTC;
void setup() {
randomSeed(analogRead(0));
while(!Serial);
Serial.begin(9600);
if(!RTC.begin()){
Serial.print("RTC is NOT running!");
RTC.adjust(DateTime(F(__DATE__),F(__TIME__)));
pinMode(ledPin,OUTPUT);
}
}
void loop() {
int light = analogRead(sensorPin); // checking light level
DateTime now = RTC.now();
randNumber = random(20); // generate a random number
H = now.hour(); // capture the real time
M = now.minute();
Serial.print(H); // Show what is happening
Serial.print(":");
Serial.print(M);
Serial.println();
Serial.println();
Serial.print("Randdom number =");
Serial.println(randNumber);
Serial.println();
Serial.print("Light level =");
Serial.println(light);
Serial.println();
Serial.print("Switchon =");
Serial.println(Switchon);
Serial.println();
if(H>=7 && H<23) Switchon=1; //Set time limits for on times
else (Switchon =0);
if(Switchon == 1) digitalWrite(ledPin,HIGH);
if(light > 50) digitalWrite(ledPin,LOW);
if(light <5 && Switchon ==1) digitalWrite(ledPin,HIGH);
wait=(randNumber * 60000); // generate a random number of minutes before switching off for the night
if(Switchon==0) delay(wait);
else delay(5000);
if(Switchon ==0) digitalWrite(ledPin,LOW); // if it is night time, switch off the light
}
Yes, a drawing typo. Missed that did it in a hurry.
Hi
There are basically 2 types of relay modules on the market.
One has a photo coupler between the relay circuit and the micro controller circuit, and the other has no photo coupler.
Yours looks like it's the second type.
Below are the schematics of both models so that you can understand what happens with your project.
Without photo coupler.
With photo coupler.

Thanks, that is useful, but the relay operates as per the manufacturer's spec. it works and others I have, all work fine on other programs, in this instance it is the Arduino that is not putting out a full digital HIGH on the data pins. I have run the same program with two different makes and models of Arduino and they all do the same thing, both Uno and Nano. The output voltages only achieves 2.8v which is not enough to operate the relay. My get around circuit works, so it is not a significant problem, I was just wondering why this code causes that problem. Also if anyone else had come across the same problem.
Did you test the same program without the relay attached?
If it was specific to just the D2 pin, then I would be thinking that particular pin might have been used by the RTC library for the RTC interrupt.
Strange that it works without the RTC - did you mean the RTC hardware or the RTC library?
Yes, and measured the pin voltage, still around 2.8
Only the hardware. I am beginning to think it is the RTC library causing the problem. that will be running in the background with or without the hardware and the relay works without the RTC library on a simple light sensing program.




