Relay turns on when computer is disconected

Hello all

I am having a problem using a uno with an rtc and relays connected.

When I flash the uno with my sketch and the time is out of spec I expect the lighting relays to remain off they do, until I disconnect the computer from the uno (uno, relays etc being powered from separate 5v psu) then one of the relay randomly turns on and stays on untill i reconnect the computer then it turns off again any ideas would be gladly recieved as im going round in circles!

Recommendation is to describe the situation more clearly...... breaking down your original post into coherent sentences. Eg....first explain what you want to do. Eg.... computer is connected to arduino (relays all initially open).....and then you upload sketch code to arduino, and the relays remain open (all ok). Next -- a relay turns on (ie. closes) when you don't want it to - which only occurs whenever you disconnect the arduino from the computer usb port.

Show your sketch.

Show circuit diagrams so that people can see how you powered your relay. Plus indicate specifications for your relay.

(uno, relays etc being powered from separate 5v psu) then one of the relay randomly turns on and stays on untill i reconnect the computer then it turns off again

So you have a system where some parts are powered up and other parts are not.

That is not the way to design electronics and a sure fire way to get things to misbehave, unless you design it correctly.

Tell us what you have and then maybe we can correct your design.

1x Uno,
1x ds3231RTC,
1x I2c Lcd display
1x Dh22
1x water temp sensor
1x 240v-5v 3.5amp Psu
1x 5v 10amp relay this one is causing trouble
1x 5v 30amp relay

#include <RTClib.h>               // Real time clock
#include <DHT.h>                  // Air Temp Humid Sensor
#include <OneWire.h>              // Sensors (water temp) connected to one wire
#include <DallasTemperature.h>    // Water Temp Sensor
#include <LiquidCrystal_I2C.h>    // Lcd display 
const int lightingPin = 4;
const int bulblightingPin = 9;
const int pumpPin = 6;
RTC_DS3231 rtc;          // Init the DS3231 using the hardware interface (Nano A4=SDA A5=SCL)
unsigned long currentTime;
unsigned long previousTime = 0;
int marker = 0;
int lampState =0;
int pumpState =0;

                        


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Lcd Display ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
                                                                // set the LCD address to 0x3F for a 16 chars 2 line display
                                                                // Set the pins on the I2C chip used for LCD connections:
                                                                // addr, en,rw,rs,d4,d5,d6,d7,bl,blpol           

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Air Temperature & Humidity sensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 #define DHTPIN 2             // what pin we're connected to
 #define DHTTYPE DHT22        // DHT 22  (AM2302)
 DHT dht(DHTPIN, DHTTYPE);    // Initialize DHT sensor
 int chk;
 float hum;                   //Stores humidity value
 float temp;                  //Stores temperature value

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Water Temperature sensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 #define ONE_WIRE_BUS 7
 OneWire oneWire(ONE_WIRE_BUS);          // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
 DallasTemperature sensors(&oneWire);    // Pass our oneWire reference to Dallas Temperature.


 //########################################################################## SETUP ################################################################################################### 
void setup()
{
  Serial.begin(9600);
  rtc.begin();             // Initialize the rtc object
  lcd.begin(16,2);         // initialize the lcd for 16 chars 2 lines
  lcd.backlight();         // Turns backlight LCD on
  sensors.begin();         // water temp sensor
  dht.begin();             // temp humid sensor 
  
  pinMode (lightingPin, OUTPUT);
  digitalWrite (lightingPin, HIGH);
  pinMode (bulblightingPin, OUTPUT);
  digitalWrite (bulblightingPin, HIGH); 
  pinMode (pumpPin, OUTPUT);
  digitalWrite (pumpPin, HIGH);
   
  
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Uncomment to set the date and time ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
 // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));      // Automatic RTC DS3231 time update from computer clock while compiling
  // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));       // This line sets the RTC with an explicit date & time for example to set January 21, 2014 at 3am 
    
    
}

 //########################################################################## LOOP ################################################################################################### 

void loop()
{
 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ OUTPUT REAL TIME CLOCK ON SERIAL ~~~~~~~~~~~~~~~~~~~~~~~~~~
 
   DateTime now = rtc.now();
    
    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.print(now.second(), DEC);
    Serial.println();
 
  

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ lighting RELAY ACTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 
  lampState = (now.hour()*60) + now.minute();         // Hour times it by 60 add the minutes, 18 hour cycle 5am = 300 11pm = 1380
                                                      // 12 hour cycle 6am = 360 12am = 00
                                                      // 14 hour cycle 6am = 360 2am = 1200
                                                    
 if (lampState >= 749 && lampState < 751){
    digitalWrite (lightingPin, HIGH);
    digitalWrite (bulblightingPin, LOW);
    Serial.println("Lights ON");
 }
  else {
    digitalWrite (lightingPin, LOW);
    digitalWrite (bulblightingPin, HIGH);
    Serial.println("Lights OFF");
 }  

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Water Pump Relay Times ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//  pumpState = (now.hour()*60) + now.minute();            // Hour times it by 60 add the minutes Pump run for 1 minute @10pm = 1320 
 
 // if (pumpState >= 1200 && pumpState < 1201){
 //   digitalWrite (pumpPin, LOW);
 //   Serial.println("Water Pump ON");                                 
 // }
 // else { 
 //   digitalWrite (pumpPin, HIGH);
 //   Serial.println("Water Pump OFF");
 // }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Air and Humid Sensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The system is to mainly to control lighting. Also give info on water, air and humidity data via the i2c display

The lighting relays are controlled from via a time interval, from the RTC.

When i first connect the computer to the uno and upload the sketch the relays both switch off as the time interval has not yet come round all good. The serial monitor tells me that the lights are off (correct) and the relays are off (correct) as the time interval has not been reached. If the time interval is reached the relays both activate as expected turn on then off depending on the interval set.

However if i disconnect the usb cable from the uno the 5v10amp relay turns on and stays on! this relay needs the pin to be high to turn it off and low to turn it on. I have hooked an oscilloscope up to pin9 and indeed it is high, however the relay is activated (on) when with the channel High it should be off.

I noticed that the voltage on pin 9 had a ripple so i put a 420mf capacitor across the psu and it smoothed out quite well, I then put a pull up 20k resistor across the signal and 5v, but still the relay remains on.

any ideas would be greatly apreciated

https://www.google.co.uk/search?q=arduino+5v+10ampo+relAY&tbm=isch&source=iu&ictx=1&fir=89QG9J5ULuNElM%3A%2CW3Vj-LKh19Bc_M%2C_&usg=__2FUyiKHROQEvofSyKEX6hjcgxdM%3D&sa=X&ved=0ahUKEwixqui725vaAhWBBsAKHdPXBAcQ9QEIQzAG#imgrc=oYHSYLtJw6YKDM:

hydrowiring2.jpg

However if i disconnect the usb cable from the uno the 5v10amp relay turns on and stays on!

Your wiring shows the 5V pin is connected to the 5V supply, but check your circuit some Solderless bread boards have a break it the two side power bus bars. This is normally indicated by a break in the red and black lines. If this is the case you need to bridge them with a wire.

Thank you Grumpy_Mike

I have check and i do have power both sides of the breadboard it is bridged opposite the relays.

So when you disconnect the USB cable the Arduino should still be being powered from the mains adaptor and there should be no change. If you are getting a change it means that things are not wired like your diagram says they are.

No change at all it does not reset the display does not blink it carries on as normal other that that relay clicking on. I just don't get it I've moved wires to make sure they are not picking up noise from he mains etc I'm stumped I've now rebuilt this 3 times same outcome every time i have even moved the pin from 9 to 8 to make sure it's not the channel. I do however seem to be getting 4.6v at the relay signal terminal but I'm unsure how sensitive the transistor is on the relay board and whether that is to low of a voltage triggering the relay.

No it is all down to wiring it up like you were supposed to do. Ditch the Solderless bread board, they are generally crap. Solder everything up on strip board and connect to the Arduino with 0.1 inch pitch headers. Solder USA compleat strip not just individual ones.

Jonathanw82:
I do however seem to be getting 4.6v at the relay signal terminal...

Because you have powered the Uno with 5volt on the V-in pin (wrong).
Leo..

Leo - I thought that at first but looking at that diagram he has also connected power to the 5V pin, so the 5V connection to the Vin pin will just stop the regulator being reversed biased.

It's probably not an issue..... but just check to see what lines there are ....marked with the red arrows.

The resolution of the original image isn't enough for us to see what components you have, and their pin labels.

Hi,
Can you post some pictures of your project so we can see your component layout please?

Thanks.. Tom... :slight_smile:

Hello All

After a lot of playing I have solved the problem, its an unusual one that I can only say relates to this one type of relay.

No it is all down to wiring it up like you were supposed to do.

Then wiring diagram was for illustration purpose only it was the first time I had used fritzing.
The circuit was already made on strip soldered board. (it was not wiring).

Ok as I mentioned I used a 20k pull up resistor across the relay 5v and signal pin with is across it held the relay off (HIGH) until the relay signal pin was connected to the Arduino then it activated on (LOW) even though the channel was HIGH.

After a lot of metering etc, I found the PSU was outputting 5.2v and the Arduino even though was being powered with 5.2v only had a channel high output of 4.6v maybe due to losses in an aftermarket uno.

Giving 0.6v loss the same as the relays onboard transistor triggering voltage.

So to fix the problem, I kept the 20k pull up resistor between the 5v pin and signal pin but then put a diode inline with the signal, blocking the 4.6v from the Arduino (holding the relay High) when the Arduino goes low the relay activates as expected.

After buying some relays of different makes I found this problem was only relative to this certain type of relay and not to others, strange.

Thank you to everyone for there input and support ideas very much appreciated! :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile:

Jonathanw82:
Hello All

Then wiring diagram was for illustration purpose only it was the first time I had used fritzing.
The circuit was already made on strip soldered board. (it was not wiring).

Can I suggest that is the last time you use Fritzy.
It is not good a representing signal or power flow, there is the occurence of LOW RES images which yours unfortunately is an example. (Not your fault.)
Can you post a proper schematic using pen/pencil, ruler and paper please.
The advantage is your hand has ALL the schematic symbols programmed in, all you have to do is draw them.
There are some good free CAD programs available, but don't go worrying about them at this stage, just get a schematic on paper.
Some CAD progs are KiCad, ExpressPCB, and others, that will be suggested.
Thanks.. Tom.. :slight_smile:

4.6V as a HIGH on a digital output is about right, it can be from 5V to 4.2V output.
The diode is a good fix.

Then wiring diagram was for illustration purpose only it was the first time I had used fritzing.

This is like asking your girlfriend to marry you, and then when she has told everyone and booked the wedding and sent out the invitations you say “that wasn’t a proposal that was just what I would say if I were to propose “

In electronics “illustration purpose“, without saying it is such, is just like the above example, it is lying.

After buying some relays of different makes I found this problem was only relative to this certain type of relay and not to others, strange.

Not strange at all, it is just that you don’t understand what you are doing and do not know what is right and what is wrong.