Night Light Smart Motion Sensor Using Arduino

by 1005172 in Circuits > Arduino

8 Views, 0 Favorites, 0 Comments

Night Light Smart Motion Sensor Using Arduino

Capture.JPG

My circuit is a Night Light motion sensor, which is used to detect if there is any motion during the dark. The LDR detects if there is light or not, and the PIR sensor detects if there is any motion in the dark. So if you cover your hand over the LDR, and then wave your other hand or just keep it slightly above the PIR sensor, the RGB LED should turn from OFF to blue.

Supplies

  1. Breadboard
  2. Normal LED
  3. RGB LED
  4. PIR sensor
  5. LDR sensor
  6. Bunch of jumper wires
  7. Arduino
  8. Computer (to connect Arduino)
  9. Pushbutton
  10. Four 330 ohm resistors
  11. Two 10k ohm resistors

POWERING THE BREADBOARD

Arduino 5V+ rail

Arduino GND– rail

PLACING THE LDR ON THE BREADBOARD

Placement

  1. LDR legs → b15 and b17

Connections

  1. + rail → a15
  2. c17 → Arduino A0
  3. 10kΩ resistor:
  4. d17 → d20
  5. e20 → – rail

PLACING THE PIR SENSOR

Placement

  1. VCC → b25
  2. OUT → b26
  3. GND → b27

Connections

  1. + rail → a25
  2. – rail → a27
  3. c26 → Arduino D2

PLACING RGB LED

Placement

  1. Red → b35
  2. Long (+) → b36
  3. Green → b37
  4. Blue → b38

Positive

  1. b36 → + rail


RESISTORS TO RGB CONNECTION

RED Leg

  1. 330Ω resistor:
  2. d35 → d40
  3. e40 → Arduino D10

GREEN Leg

  1. 330Ω resistor:
  2. d37 → d42
  3. e42 → Arduino D11


Blue Leg

  1. 330Ω resistor:
  2. d38 → d44
  3. e44 → Arduino D12


PUSHBUTTON

Placement (across island)

  1. e45 / f45
  2. e47 / f47

Connections

  1. + rail → a45
  2. a47 → Arduino D4
  3. 10kΩ resistor:
  4. d47 → – rail


CODE

// ===== PIN DEFINITIONS =====

int ldrPin = A0;

int pirPin = 2;

int buttonPin = 4;

int ledPin = 9;


int redPin = 10;

int greenPin = 11;

int bluePin = 12;

bool manualMode = false;


// ===== SETUP =====

void setup() {

pinMode(pirPin, INPUT);

pinMode(buttonPin, INPUT);


pinMode(ledPin, OUTPUT);

pinMode(redPin, OUTPUT);

pinMode(greenPin, OUTPUT);

pinMode(bluePin, OUTPUT);


Serial.begin(9600);

}


void setRed() {

digitalWrite(redPin, LOW);

digitalWrite(greenPin, HIGH);

digitalWrite(bluePin, HIGH);

}


void setBlue() {

digitalWrite(redPin, HIGH);

digitalWrite(greenPin, HIGH);

digitalWrite(bluePin, LOW);

}


// ===== MAIN LOOP =====

void loop() {

int lightLevel = analogRead(ldrPin);

int motion = digitalRead(pirPin);


// Button press toggles mode

static bool lastButtonState = LOW;

bool currentButtonState = digitalRead(buttonPin);


if (currentButtonState == HIGH && lastButtonState == LOW) {

manualMode = !manualMode; // toggle mode

delay(200); // debounce

}


lastButtonState = currentButtonState;


if (manualMode) {

turnEverythingOff();

setRed(); // RED = Manual Mode

}

else if (motion == HIGH && lightLevel < 400) {

digitalWrite(ledPin, HIGH);

setBlue(); // BLUE = Auto Mode active

}

else {

turnEverythingOff();

}

}


void turnEverythingOff() {

digitalWrite(ledPin, LOW);


// Common anode RGB LED

digitalWrite(redPin, HIGH);

digitalWrite(greenPin, HIGH);

digitalWrite(bluePin, HIGH);

}


SCHEMATIC/CIRCUIT DIAGRAM

Capture.JPG

THIS IS THE DIAGRAM FOR THE CIRCUIT.

MISTAKES

My Mistakes:


  1. Trying to add too get too fancy with my circuit.
  2. LDR not sensing properly, so I had to take a new one.
  3. You have to wait 30-60 seconds for the PIR sensor to warm up.
  4. Taking too much time to figure out how to build my circuit, but then I broke it down all over again just to restart. DON'T DO THIS.

VIDEO

Downloads