Toggle on/off relay using hc-SR04 sensor

Hi everyone, I am building a project where i want the HC-SR04 sensor to turn relay on and off alternatively when it senses a motion using arduino uno. The implementation is in the room where when someone enters a room, light should turn on and when they leave, the light should turn off.

If someone could help me with the code, that would be really helpful.
Regards.

By help with the code, what do you mean? What have you got so far? What does not work? or do you need someone to write the whole code?

Yes, a whole code indeed, I do not know anything about coding.

Then my fee is £200

Well, I am not willing to pay anything. If i did i would not come on this forum. I am learning one thing at a time about this arduino coding...

I am learning one thing at a time about this arduino coding...

What have you learnt so far ?

Have you looked at any example code for the sensor ?

Got this PM

All I'm saying is If you send me the working code once, I can change the parameters and adjust the code to my requirement like the distance, delay and other basic stuff.

Why do these people feel entitled to get work done for free? As the experienced amongst us know, the code is trivial for this application. Asking google actually give me the code for this project. It has taken me longer to type this, than it took me to copy and paste the code I found on the internet.

yaser786:
Well, I am not willing to pay anything. If i did i would not come on this forum.

Well, you have just learned a lesson about people's sense of humour. :grinning:

Of course people will help you if you are actually doing something with the parts in question. So what parts do you have and what have you done with them so far?

Now as to your proposal. If the sensor is in the doorway, it is clearly not useful simply to have the light turn on and off alternately as people pass. It will be wrong if two people enter the room or if someone stops in the doorway and turns round. :astonished:

missdrew:
It has taken me longer to type this, than it took me to copy and paste the code I found on the internet.

So did you? Copy and paste it? Did you actually reply to a PM? :roll_eyes:

I copied the code to notepad, had a quick look and closed it (I could do a lot better than that code).

I have also replied to 5 PMs - advising the OP on the 3rd PM that further PMs sent to me and read would cost him £500 each - he now owes me £1000 but refuses to pay.

Welcome aboard the good ship Arduino

i want the HC-SR04 sensor to turn relay on and off alternatively when it senses a motion using arduino uno

What should happen if 2 people enter the room one after the other ? Presumably the light should stay on

UKHeliBob:
What should happen if 2 people enter the room one after the other ? Presumably the light should stay on

See #7.

I want to implement this project in a bathroom. So the contradiction of two people standing/going in/out should not happen

"Should not" eh? :roll_eyes:

You have much to learn, it seems!

You want to put this an 'a' bathroom, not 'my' bathroom. Are you sure this is for your hose & not your college?

P.S. If you do need this for college, please just tell us when it is due so we can make this a priority for you.

It is for my house and not for college.

OK - no problem. Post the code you have tried so far, and we will see what we can help with.

I copied this code from a youtube video link, The code enables the sensor to turn on light when detecting anything and then you have to set a timer for the light to turn off.

/*
HC-SR05 pin Arduino pin Relay Board
vcc 5V X
Trig pin 9 X
Echo pin 8 X
Gnd Gnd Gnd
X pin 12 signal pin
X Vin +V
*/
int s;
int state=0;
void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(8, INPUT);
pinMode(9, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
}

void loop() {
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

digitalWrite(9, HIGH);
delayMicroseconds(5);
digitalWrite(9, LOW);
delayMicroseconds(5);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.

duration = pulseIn(8, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

if(inches<12){ // change number increse or decrese distanse eg 12 for 12 inches
digitalWrite(12, HIGH);
delay(2000);
}else{
digitalWrite(12, LOW);
}

delay(100);
}

long microsecondsToInches(long microseconds) {
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

Now explain EXACTLY how you want the code to work. e.g

Sensor detects something <24 inches away and turms light on, if it then detects the something is further away than 24 inches then turn the light off
or
does the sensor just detect something is close and toggles the light

We can't read your mind on how you want this to work.

So i have placed the HC-SR04 sensor on the outer frame of the bathroom door. I want the sensor to turn on light when someone enters the bathroom and turn it off when the person exits it. With the above posted code, the light just turns on but to turn it off i have to put a timer in the code. I set it as 720000UL for 12 minutes. So once light is on, it will only turn off after 12 minutes even if someone is inside the bathroom. I want the sensor to turn the light on and off alternatively upon object detection.

OK - so you know how to read the sensor - just toggle a value (like this var=!var;)
And please remember to pay for the PM support I gave you.