Ultrasonic/Relay...Please help

Hi everyone, I'm new to Arduino and new to this forum. I'm trying to set something up and need some help. I have an ultrasonic sensor set up with a red and green LED, the red LED is usually on but if something blocks the ultrasonic sensor with 10cm the red LED turns off and green one comes on, so far so good. Next I have added a relay board onto this with the idea that the relay would trigger when something is within 10cm along with the lighting of the green LED, there is a light on the board of the relay which comes on as expected, I eventually want to connect a wireless doorbell to the relay so that the doorbell sounds when something crosses within 10cm but for now I just have a third LED connected to the relay with a 9v battery, The + connector of the battery clip goes to COM output on relay then I have a wire going from NO output via a resistor to the anode leg of the LED, the - connector from the battery clip goes to the cathode leg of the LED, the LED stays off even when the light on the relay board is lit. if I connect the NC output of the relay instead of the NO the LED stays permanently lit instead of turning off when the relay activates. I don't know what is wrong, please help.

thanks in advance for any help

I don't know what is wrong

No, me neither.

Post your code.
Draw a schematic.

Ok, here is the code and schematic.

/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
Red POS to Arduino pin 11
Green POS to Arduino pin 10
560 ohm resistor to both LED NEG and GRD power rail
More info at: http://goo.gl/kJ8Gl
Original code improvements to the Ping sketch sourced from Trollmaker.com
Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar
*/

#define trigPin 12
#define echoPin 13
#define led 11
#define led2 10
const int relayPin =6; //the “s” of relay module attach to

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(relayPin, OUTPUT);
}

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 10) { // This is where the LED On/Off happens
digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
digitalWrite(led2,LOW);
digitalWrite(relayPin, HIGH);
delay(2000);
digitalWrite(relayPin, LOW);
}
else {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
if (distance >= 200 || distance <= 0){
Serial.println(“Out of range”);
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}

schematic:

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Hi everyone,
I’m new to Arduino and new to this forum.
I’m trying to set something up and need some help.
I have an ultrasonic sensor set up with a red and green LED, the red LED is usually on but if something blocks the ultrasonic sensor with 10cm the red LED turns off and green one comes on, so far so good.
Next I have added a relay board onto this with the idea that the relay would trigger when something is within 10cm along with the lighting of the green LED, there is a light on the board of the relay which comes on as expected.
I eventually want to connect a wireless doorbell to the relay so that the doorbell sounds when something crosses within 10cm but for now I just have a third LED connected to the relay with a 9v battery,
The + connector of the battery clip goes to COM output on relay then I have a wire going from NO output via a resistor to the anode leg of the LED, the - connector from the battery clip goes to the cathode leg of the LED.

The LED stays off even when the light on the relay board is lit.
If I connect the NC output of the relay instead of the NO the LED stays permanently lit instead of turning off when the relay activates.
I don’t know what is wrong, please help.

Tom… :slight_smile:
Can you post a picture of your project, fritzy diagrams do not show how you have connected everything up.

Try this code.
I tweaked your’s slightly.
Untested but does compile.

#define trigPin 12
 #define echoPin 13
 #define led 11
 #define led2 10
 const int relayPin =6; //the "s" of relay module attach to
 long duration, distance;

 void setup() {
   Serial.begin (9600);
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);
   pinMode(led, OUTPUT); // Green LED
   pinMode(led2, OUTPUT); // Red LED
   pinMode(relayPin, OUTPUT);
   digitalWrite(trigPin, LOW);  // Added this line
   delayMicroseconds(100); // Added this line
 }

 void loop() {

digitalWrite(led,LOW); // Turn off Green  
digitalWrite(led2,HIGH); // Turn on Red

   digitalWrite(trigPin, HIGH);
   delayMicroseconds(10); // Added this line
   digitalWrite(trigPin, LOW);

   duration = pulseIn(echoPin, HIGH);
   distance = (duration/2) / 29.1;

   if (distance >= 200 || distance <= 0){
     Serial.println("Out of range");
   }
   else {
     Serial.print(distance);
     Serial.println(" cm");

     if (distance < 10) {  // This is where the LED On/Off happens
       digitalWrite(led,HIGH); // TUrn on Green
       digitalWrite(led2,LOW); // Turn off Red
       digitalWrite(relayPin, HIGH); // Power Relay
       delay(2000);
       digitalWrite(relayPin, LOW); // Remove Power to Relay
     }
     
  }

   delay(500);
 }

Your Fritzing diagram has a few issues- I hope u are using resistors with the LEDs.
A photo of the actual setup will be better.

Let us know what the above code does.

Oh, and don’t forget to connect the grounds!

Your Fritzing diagram has a few issues- I hope u are using resistors with the LEDs.
A photo of the actual setup will be better.

Let us know what the above code does.

Yes I am using resistors on all LEDs. I have tried the code you tweaked but still not working, thanks for trying though. Here is a photo of the complete setup, I’m thinking that maybe I have a faulty relay module because the light on the board lights up but I cant hear any clicks from the relay and I think I should be able to.

Hi,
I think you have the output of your relay connected to the wrong terminals.

Attached is a pic of the relay with connections, you need to be between COMMON and NORMALLY OPEN.
Also you have no series resistor with the LED you are switching with the relay contacts.

Tom… :slight_smile:

Thanks...I thought the common on the relay terminals was the centre one....ill try moving them around. I do have a resistor on the LED you mention but its soldered on and covered with heat shrink so cant see it in the picture. ill let you know what happens when I change the connection from the relay. Thanks for your help.

ok, progress....I connected the relay to the common as shown in your pic and the NO and I get a definite clicking sound to show the relay is working but the LED still doesn't light, however if I connect the LED to the COM & NC connections on the relay the LED comes on and then goes off when something crosses within 10cm of the ultrasonic and back on again after

Now that you have the relay connected correctly, try it again with the code I gave you.
If it still operates opposite to what you expect, then try reversing the LOW/HIGH output to pin 6.

Yes I am using your code and ive tried reversing HIGH/LOW output but still the same, only seems to work if I connect to NC, this probably wont be a problem, the LED comes on if I disconnect the power from the Arduino but I could probably rectify this by putting an on/off switch on the power line from the relay. Thanks again for all your help, been puzzling over this all week and feel a bit stupid now for having the wrong COM connection but we live and learn eh?....lol

I have a project with a similar circuit, however, I switch the VCC to the relay coil via a BJT.

I have never used one of those relay boards, so not sure what the signal line does.
However, it seems the relay board may not be operating as you expect.

In any case you should determine how the relay board functions.

You could try the following.

  1. Start with nothing connected.
  2. Connect the Arduino 5V and GND pins to the power rails on the breadboard.
  3. Connect the relay board "+" & "-" pins to the breadboard power rails and leave the signal pin unconnected.
  4. With a voltmeter measure the resistance between C & NO; C & NC - write it down.
  5. Power the Arduino by plugging it into the USB on your computer.
  6. Note if the LED on the relay board comes on or not.
  7. With a voltmeter measure the resistance across the relay outputs i.e. C & NO; C & NC - write it down.
  8. Unplug the Arduino.
  9. Connect the relay board signal pin to the 5V power rail of the breadboard.
  10. Repeat steps 5 thru 8.

Let me know what u find.

A HIGH signal to the relay is no effect, it engages the common to the NO connection when the signal is driven LOW. There is a OPTO isolator inside that is tied to VCC, by forcing the output of the pin LOW the pin provides the path for the relay to be energized. You have to think backwards on these things. The jumper on the side of the relay can be removed and you can supply the relay side with external power so the UNO is not having to supply the power to drive the relay coil. Handy if you use a bunch of relays.

What are you using to power the UNO? If you are using a 9 volt battery there, it may not be enough power to run the board and the relay. Yes you need a resistor on the LED, maybe a 2.2K to limit the current to the LED. Makes me think that you maybe starved for power since you have not blown the LED already.