Introduction: Light Switch With Motion Detector

This project takes a RGB LED and controls it using 4 buttons and a motion sensor.


Supplies

  1. Jumper cables
  2. 1 Arduino
  3. 2 breadboard
  4. 4 buttons
  5. 1 Ultrason sensor (USS)
  6. 4 10 k resistors
  7. 3 220 resistors
  8. Paper Pyramid

Step 1: Build Circuit

Connect 5v and GND to breadboard

Place 4 push buttons across the central groove of the breadboard

  1. GND the push buttons to the breadboard
  2. Connect the push buttons to the Arduino through 10 k resistors
  3. buttonOne -> 13
  4. buttonTwo -> 12
  5. buttonThree -> 11
  6. buttonOff -> 10

Connect RGB LED to breadboard

  1. GRD RGB to breadboard
  2. Connect RGB to the Arduino through 220 resistors
  3. redPin -> 3
  4. greenPin -> 5
  5. bluePin -> 6

Connect Ultrasonic Sensor to breadboard and Arduino

  1. GRD and 5v USS to breadboard
  2. echoPin -> 8
  3. trigPin -> 9

Place Paper Pyramid on RGB LED

Step 2: Understand the Code

const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;

const int buttonOne = 13;
const int buttonTwo = 12;
const int buttonThree = 11;
const int buttonOff = 10;

const int echoPin = 8;
const int trigPin = 9;
long duration;
int distance;
int buttonState;

void setup() {
Serial.begin(9600);

pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

pinMode(buttonOne, INPUT);
pinMode(buttonTwo, INPUT);
pinMode(buttonThree, INPUT);
pinMode(buttonOff, INPUT);

pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);

}

void loop() {
buttonState = digitalRead(buttonOne);
Serial.println(buttonState);
if (buttonState == LOW) {
setColor(255, 255, 255);
}

buttonState = digitalRead(buttonTwo);
Serial.println(buttonState);
if (buttonState == LOW) {
setColor(155, 155, 155);
}

buttonState = digitalRead(buttonThree);
Serial.println(buttonState);
if (buttonState == LOW) {
setColor(55, 55, 55);
}

buttonState = digitalRead(buttonOff);
Serial.println(buttonState);
if (buttonState == LOW) {
setColor(0, 0, 0);
}

pinMode(echoPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");

if (distance > 300) {
delay(5000);
setColor(0,0,0);
delay(500);
}

}

void setColor(int r, int g, int b) {
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
}

Step 3: Variables

const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;

const int buttonOne = 13;
const int buttonTwo = 12;
const int buttonThree = 11;
const int buttonOff = 10;

const int echoPin = 8;
const int trigPin = 9;
long duration;
int distance;
int buttonState;

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

Step 4: Setup

void setup() {
Serial.begin(9600);

pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

pinMode(buttonOne, INPUT);
pinMode(buttonTwo, INPUT);
pinMode(buttonThree, INPUT);
pinMode(buttonOff, INPUT);

pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);

}

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

Step 5: Loop

void loop() {
buttonState = digitalRead(buttonOne);
Serial.println(buttonState);
if (buttonState == LOW) {
setColor(255, 255, 255);
}

buttonState = digitalRead(buttonTwo);
Serial.println(buttonState);
if (buttonState == LOW) {
setColor(155, 155, 155);
}

buttonState = digitalRead(buttonThree);
Serial.println(buttonState);
if (buttonState == LOW) {
setColor(55, 55, 55);
}

buttonState = digitalRead(buttonOff);
Serial.println(buttonState);
if (buttonState == LOW) {
setColor(0, 0, 0);
}

pinMode(echoPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");

if (distance > 300) {
delay(5000);
setColor(0,0,0);
delay(500);
}

}

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

Step 6: Function

void setColor(int r, int g, int b) {
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
}

This function is for the RGB

When it is called the 3 int will set the color levels for the RGB

Step 7: Reflection

  1. I got the ideafor this project because I was recently in room where a system similar to this was implemented.
  2. 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.