Relay problems G5V-1 using ultrasonic Distance Sensor

So I have the base of a BC547 connected to pin 8 and I have a relay with a diode running parallel with the coil using the G5V-1 5V relay. I'm powering one side with 5V and the other side is connected to the collector of the BC547 and the emitter is connected to ground. I'm using a ultrasonic sensor that I want to tell if something gets within 3 inches of it to turn on the relay. However the relay chatters. SO I put a pause and tried again however this time the relay won't engage at all. I'm posting the code as well as the schematic and was hoping someone could help me understand why the relay won't latch. I've used dozen relays and none latch. I can short it with a 5V signal and it will latch fine however using the Arduino Nano to turn this relay on has proven to be troublesome. Any help would be greatly appreciated. Thanks.

Code:
[

#define trigPin 12
#define echoPin 13
int Relay = 8; // Connect Relay pin to 8
int ledPin= 6; //Connect LEd pin to 6
int duration, distance; //to measure the distance and time taken

void setup() {
Serial.begin (9600);
//Define the output and input objects(devices)
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(Relay, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW); //Blink once
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW); // Blink Twice
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW); // Blink thrice
delay(500);

}
void loop() {

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
//when distance is greater than or equal to 200 OR less than or equal to 0,the buzzer and LED are off
if (distance >= 10 || distance <= 0)
{
Serial.println("no object detected");
digitalWrite(Relay,LOW);
digitalWrite(ledPin,LOW);
}
else {
Serial.println("object detected \n");
Serial.print("distance= ");
Serial.print(distance);
digitalWrite(ledPin,HIGH);
digitalWrite(Relay, HIGH);
}
}
]

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Ops diagram.

Thanks Tom.... :slight_smile:

Hi,
What model Arduino are you using?

First you need capacitors around the 7805, consult the data sheet.

You cannot drive a UJT like BC547 directy to the base, you need a current limiting resistor in series with the base.

Please include on your circuit, where MOTSENSE and DISTSENSE are connected to.

Also some 0.1uF bypass capacitors around the circuit would help too.

You need about 1K, resistors in series with each of the bases of the BC547 transistors.

Tom... :slight_smile:

Your schematic is connecting the base of the transistor to pin 5 (not 8) but that aside are you powering all this from the 5v on the Arduino board? Try removing the relay and just using an LED - does that 'chatter' or stay lit / unlit as expected?

If it works then I suspect one of the following:

  • that the relay coil is demanding too much current, so that the Arduino is struggling or resetting
  • the relay is noisy (despite your diode protection) and causes the Arduino to reset

I would certainly put a 1k resistor in line with the base of the transistor, you are overloading it (it won't solve your issue but you could end up with a defunct transistor).

If you then supply that resistor (connected to the base of the transistor) with 5 volts does the relay latch as expected?

Try powering the relay from a separate supply (not connected in any way to the Arduino except for the ground line).

What do you intend to drive from the relay? As long as it's low level voltage stuff then fine. Otherwise I would highly recommend an optically isolated relay like the one I show in my video #18, much safer for you and your Arduino!

Thank you for your replies.. I'll make all the necessary changes,