Aydin Gokce
Published © GPL3+

Power Saving Automatic Light Control

Arduino-based project which automatically turns on your lights when you enter, and automatically turn off after 30 minutes.

BeginnerFull instructions provided1 hour2,970
Power Saving Automatic Light Control

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Relay (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
9V battery (generic)
9V battery (generic)
×1

Story

Read more

Schematics

Automatic power-saving light control "schematic"

Look at the text and do what it says. Email me if you are unsure of any instructions

Code

Automatic power-saving light control (code)

Arduino
paste the code into your IDE and upload it to the arduino
/*------------------------------------
Code by Aydin Gokce
Feel free to use.
ig: @aydinguy
6/17/2018
------------------------------------*/
//Assigns names to digital pins on the arduino
const int trigPin = 9; 
const int echoPin = 10;
const int lights = 11;

//Establish variables duration and distance to be used later in code
long duration; 
int distance;


void setup() {
//Establishes which functions are inputs and which ones are outputs
pinMode(trigPin, OUTPUT); 
pinMode(echoPin, INPUT); 
pinMode(lights, OUTPUT);
//Starts serial communication with baud rate 9600
Serial.begin(9600);  
}

void loop() { 
//starts off the values as LOW
digitalWrite(lights, LOW);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

/*Gives a 5v current for 10 microseconds which allows the HC-SRO4 to emit an ultrasound of 40,000 hz*/
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

//sees the value that the echo pin returned
duration = pulseIn(echoPin, HIGH);

/*You need to multiply the duration by 0.034 because the speed of sound is 0.034 cm/microsecond and divide it by 2 because of the time it takes to go there and back*/
distance= duration*0.034/2;

/*If there is anything within 30 cm (door, you, or whatever) it gives the relay a current which closes the circuit allowing for the 120v to go to the lights*/
if(distance < 30){
  digitalWrite(lights, HIGH);
  delay(1800000);
}
else{
  digitalWrite(lights, LOW);  
}

}

Credits

Aydin Gokce
2 projects • 4 followers
suffering from severe mesothelial pain

Comments