Power Saving Ultrasonic Switch

by Jandre395 in Circuits > Microcontrollers

1445 Views, 9 Favorites, 0 Comments

Power Saving Ultrasonic Switch

20150106_182724.jpg

This is a prototype switch that automatically switches off any appliance that you have connected to your relay being a 2 way up to a 10 way relay for this specific one i am using a 2 way relay and will only be showing how to build the switch NOT CONNECT IT as it is very straight forward, this prototype also save enormous amount of electricity for obvious reasons.

First Gather Parts

20150106_182739.jpg
20150106_182749.jpg
20150106_182729.jpg
20150106_182734.jpg

1. arduino uno R3

2. (2) way relay, any will work

3. jumper cables

4. breadboard (not necessary but easier to work with)

5. HC-SR04 ultrasonic ping sensor

Tools

20150106_182812.jpg

All you need are

1. A to B arduino USB

2. Arduino programming software

3. Your hands

Assembly

20150106_182831.jpg
20150106_182842.jpg
20150106_182846.jpg
20150106_182826.jpg

1. take 1 ground and 1 5v jumper cable from your arduino to your breadboard

2. connect the grounds and the vcc from the relay and the sensor to the grd and vcc rail on your breadboard

3. connect all of your relay channels to the breadboard and the to digital pin 10 on your arduino

4. connect the echo pin on the ultrasonic sensor to digital pin12

5. connect the trig pin on the sensor to digital pin13

6. done !!!

Coding

Connect your arduino to your pc

make sure you have selected the right board

then upload this sketch


#define trigPin 13

#define echoPin 12

#define led 11

#define led2 10

void setup() {

Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(led, OUTPUT);

pinMode(led2, 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 < 4) { // This is where the Relay On/Off happens

digitalWrite(led,HIGH); // Relay on

digitalWrite(led2,LOW);

}

else {

digitalWrite(led,LOW);

digitalWrite(led2,HIGH);

}

if (distance >= 200 || distance <= 200){

Serial.println("Out of range");

}

else {

Serial.print(distance);

Serial.println(" cm");

}

delay(5000);

}


Testing

place this setup within a meter of your door and walk through after connecting the relays if you have any problems please feel free to contact me.