Motion Controlled Christmas Lights, by Charlie, William, & Anthony
by PHS_Engineering in Circuits > Arduino
218 Views, 1 Favorites, 0 Comments
Motion Controlled Christmas Lights, by Charlie, William, & Anthony
This inscrutable tells you how to make a motion controlled Christmas lights, just as the title suggests. We used an ultrasonic sensor to detect close objects and an relay complete the circuit and to turn the lights on.
Supplies
What you'll need:
- 8 wires
- 4 Dupont wires
- 1 relay
- 1 Arduino Uno
- Christmas Lights
- 2 Alligator wires
- 1 Ultrasonic sensor
Build the Relay Circuit
Build the circuit as shown in the image. For further assistance look at the drawing of the circuit.
Cut the Lights
Next you will want to cut the lights. Cut the wire that does not connect to any of the lights near the first light bulb (as shown as in the picture).
Attach the Christmas Lights
Next you will use the alligator clips to connect the Christmas lights to the relay circuit. Now you have a closed circuit.
Make the Ultrasonic Sensor Circuit
Make a basic ultrasonic sensor circuit as shown in the image. Again look at the drawing of the circuit for additional help.
Step 5: Write the Code
The code below will allow the user to control the Christmas lights, using an ultrasonic sensor:
#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04
// defines variables
int duration; // variable for the duration of sound wave travel
int distance;//variable for the distance measurement
int Relaypin=13;
void setup() {
// put your setup code here, to run once:
pinMode(13,OUTPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
Serial.println("with Arduino UNO R3");
}
void loop() {
// put your main code here, to run repeatedly:
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); //Creates a ten microsecond delay between the Christmas lights being on and off. digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm"); //Prints the distance of your hand from the ultrasonic sensor in centimeters in the serial monitor.
if (distance<=10 and distance>=0){ //This if statement says that if your hand is 10 centimeters or closer to the ultrasonic sensor, it will turn the Christmas lights on.
digitalWrite (Relaypin,HIGH);
}
else{
digitalWrite(Relaypin,LOW); //If it reads that your hand is more than 10 centimeters away, it will turn the Christmas lights off.
}
}
Plug in the Lights, Upload the Code, Enjoy!
Congratulations! You now have motion activated Christmas lights! Enjoy this project and be sure to check out other projects our peers created under PHS_Engineering.