Help me to fix this. Working a relay module with Arduino

Hello dear,
As a beginner, I tried to make a circuit with the arduino board but found some bugs in it. Please help me to fix it.

The project is to work a motor when an ultrasonic sensor detects it.

The Problem is,

Initially, my motor runs without any reason where the 5V Dc supply gives. And while after resetting my Arduino it runs properly according to my program.
(On the program side, Initially, I make the output pin low and whenever it needs high it becomes high. as like that).

And the next problem is,

There's a pin in my relay module named as 'IN' which I connected to Arduino for getting output pulse to my relay in order to the written program.
While connecting this IN pin to the Arduino there is a status led in the relay module becomes glow with dim light and whenever the Arduino gives pulse to the relay module it glows bright and motor begin to run.

Please help me to solve these issues.


Noooooooooooo!

Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.
Posting images:
https://forum.arduino.cc/index.php?topic=519037.0


In Setup():

digitalWrite(waterPump, LOW);
pinMode(waterPump, OUTPUT);

#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04
// defines variables
long duration; // variable for the duration of sound wave travel
float distance; // variable for the distance measurement
const int watrPump = 13;

void setup()
{
  Serial.begin(9600);
  pinMode(watrPump, OUTPUT);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
  Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
  Serial.println("with Arduino UNO R3");
}


void loop()
{
  // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  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; // Speed of sound wave divided by 2 (go and back)
  // Displays the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  if (distance <= 5) //You can adjust the distance in cm
  {
    delay(300);//initial delay
    digitalWrite(watrPump, HIGH); //sets the water pump high (ON)

    //delay(3000); // For setting the pump's delay for 3 sec
  }
  else
    digitalWrite(watrPump, LOW); // To turn off the Water pump

}

ultrasonic sensor:

relay module:

https://osoyoo.com/2017/08/28/arduino-lesson-1-channel-relay-module/

It appears your relay energizes when a LOW is applied to the ‘sig’ terminal.


Add this line to setup()


digitalWrite(watrPump, LOW); // To turn off the Water pump  <————<<<<<
pinMode(watrPump, OUTPUT);

larryd:
It appears your relay energises when a LOW is applied to the ‘sig’ terminal.

Indeed it does!

larryd:
Add this line to setup()

digitalWrite(watrPump, LOW); // To turn off the Water pump  <————<<<<<

pinMode(watrPump, OUTPUT);

No, you meant to say:

digitalWrite(watrPump, HIGH); // To turn off the Water pump  <————<<<<<
pinMode(watrPump, OUTPUT);

Wouldn't it be more normal to set the pinMode first?

No! Absolutely wrong!

Doing that will pulse the relay on briefly.

While you may think that should not happen as the LOW pulse would only be very short (note how slow digitalWrite and pinMode actually are when compiled!), it has been frequently reported in the past that the relay flicks on and briefly activates the controlled device, sometimes with a nasty result. This may relate to saturation of the controlling transistor,

So, the digitalWrite turns on the pullup first.
Interesting

TheMemberFormerlyKnownAsAWOL:
So, the digitalWrite turns on the pullup first.

Yes, but that is perfectly fine as you are going to make it an OUTPUT anyway. :sunglasses:

Hi,
Can I suggest you write some code that JUST controls the relays so you can see how they work with your code.

Tom... :slight_smile:

No, you meant to say:

digitalWrite(watrPump, HIGH); // To turn off the Water pump  <————<<<<<

pinMode(watrPump, OUTPUT);

No.

If the pump and Arduino are on the same supply, the OP could be using the N.C. contact, but she/he needs to tell use exactly how things are connected.

Alas, we may never know. :cry: