Ultrasonic sensor to 3 x 5V Relays

Hi, I’m having problems with 5V relays being inconsistent in terms of turning on and off. It seems after a while they eventually break and the appliance connected to them remains powered. The relays are all wired through the NO connection. The code and schematic is as below:

#include <millisDelay.h>

millisDelay relayDelay;

/*
* Ultrasonic Sensor HC-SR04 and Arduino Tutorial
*
* by Dejan Nedelkovski,
* www.HowToMechatronics.com
*
*/


// defines pins numbers
const int trigPin = 10;
const int echoPin = 13;
int RELAY1 = 3;

// defines variables
long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(RELAY1, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);

    //change distances here
    if (distance > 1 && distance <= 8) { //distance in centimeter, fx between 1 - 8 cm
      for(int i = 0; i < 50; i++){
      digitalWrite(RELAY1, HIGH);
      delay(50);
      }
      Serial.println("relay 1 on");
      //distance = get_ultrasonic_distance();
      delay(3000); //delay in milli seconds - controls how long the relay is on for
    }

    else {
      digitalWrite(RELAY1, LOW);
      Serial.println("relay 1 off");
      //distance = get_ultrasonic_distance();
    }
}


long  get_ultrasonic_distance()
{
    long distance_ = 0;
    distance_ = duration*0.034/2;
    if ((distance_ != 0))
    {
    return distance_;
    }
    else return 0;
}

Any help would be greatly appreciated, I’m wondering if its likely to be the quality of the relays I’m using? They are these: KY-019 1 channel 5V relays

It’s unclear just what this means.  What’s the power source for the relay coils?

The power supplying does not look okey.
You can’t feed 5 volt into Vin, the barrel jack, on an UNO. 7.5 volt is minimum. Feed 5 volt into the 5 volt pin.
You should not feed the relays from the 5 volt UNO pin. That pin is not designed to deliver current for relays or motors.

I’ve tried powering them through the barrel jack. That didn’t work consistently over a long period of time. Now I’m powering them and the ultrasonic sensor through the USB on the Arduino, which seems to work better.

Should I be powering the relays and ultrasonic sensor through the breadboard from a separate 5v power supply?

Thank you for your response. I’m not powering them through the barrel jack anymore, but still having issues. I’m powering the relays and sensor through the USB.

What is the relay switching? AC, DC, voltage, current?

Supply the controller with 5 volt at the 5 volt controller pin.
Connect all 5 volt using devices from the power supply. Do not use breadboards for power distribution. They only handles low current, not current for relays, motors, servos etc.

How much current does it take to switch a 5V relay?

The relays are switching a variety of appliances, all 240V. None of the appliances are using that much power and are relatively low wattage. Any ideas?

If You have a DMM, Digital multimeter You can measure the resistance of the relay coil and calculate the current, or measure the current directly. Knowing my small relays I will say from 50 mA and up. Also, breadboards are unsafe, easy to intermittently loose contact and face unexpected errors.

Never ever let 240 volt into a breadboard!

Look up the specifications on your specific relays. A typical value is about 75 mA. Too much to drive with a pin (40 mA ABSOLUTE MAX) but low enough that 3 or 4 will run off USB power (500 mA) without trouble.

Of course I wouldn’t let 240v near a breadboard! It’s all safe and cleared by an electrician as an artwork for public exhibition. Just need to get to the bottom of this mysterious problem with the relays behaving strangely…

That’s what I thought, and about what it says on the data sheets. Thank you.

Really nice to hear!

The handling of the coils needs to be investigated.
To debug code I, since ages, use “test printouts” to tell what’s going on in the code. In this environment is spells “Serial monitor” and Serial.print. Could You apply that idea to Your work?

Read my questions regarding the supplying of power to things.

Of course. Wouldn’t want you to worry unnecessary. Is it really true that I can’t switch relays with 70mA of current through a breadboard??

I take your point that breadboards are prone to loose connections… Maybe I’ll buy some 4 channel relay boards and go straight to that from the 5V pin on the Arduino, with the Arduino powered through the USB. Does that sound a better solution?

Correct me if im wrong but it is not a good idea to switch 220V with these relays. They will die very quickly even at low loads.

I’ve been using the serial monitor… No cigar unfortunately as the problem is isolated from the messages sent from the microcontroller and there’s no feedback…

Thank you… Is there anything else you would suggest that is relatively cheap and suitable please?