Light Switch With Motion Detector
by chapmanjaden07 in Circuits > Arduino
718 Views, 3 Favorites, 0 Comments
Light Switch With Motion Detector

This project takes a RGB LED and controls it using 4 buttons and a motion sensor.
Supplies
- Jumper cables
- 1 Arduino
- 2 breadboard
- 4 buttons
- 1 Ultrason sensor (USS)
- 4 10 k resistors
- 3 220 resistors
- Paper Pyramid
Build Circuit

Connect 5v and GND to breadboard
Place 4 push buttons across the central groove of the breadboard
- GND the push buttons to the breadboard
- Connect the push buttons to the Arduino through 10 k resistors
- buttonOne -> 13
- buttonTwo -> 12
- buttonThree -> 11
- buttonOff -> 10
Connect RGB LED to breadboard
- GRD RGB to breadboard
- Connect RGB to the Arduino through 220 resistors
- redPin -> 3
- greenPin -> 5
- bluePin -> 6
Connect Ultrasonic Sensor to breadboard and Arduino
- GRD and 5v USS to breadboard
- echoPin -> 8
- trigPin -> 9
Place Paper Pyramid on RGB LED
Understand the Code
Variables
Setting variables for push buttons, RGB, and USS
I do this to make the code more manageable and easier for someone to read with out actually knowing all of the code
Setup
Serial.begin to initialize serial communication between the Arduino board and USS
pinMode to set push buttons, RGB, and USS as inputs or outputs
The RGBs are outputting light
The push buttons are inputing info to the Arduino
Loop
digitalRead to determine whether the pushButton has been pressed
if statements to tell the Arduino what to do when each button is pressed
setColor is a function I used to make the code more organized
if statement for the USS that says if there is nothing within 300 cm of the USS then the Arduino will call the setColor function
Function
This function is for the RGB
When it is called the 3 int will set the color levels for the RGB
Reflection
- I got the idea for this project because I was recently in room where a system similar to this was implemented.
- While working on this project a problem I ran into was figuring out how to make it so that the light wouldn’t turn off immediately after somebody left the room. I then realized that I just needed to zoom out a little and then I found my answer.