Introduction: Light-Dependent Servo: Automate Any Bedside Lamp!

This is a night light that automatically turns on when it's dark and off when it's bright, that you can make using any lamp, a servo motor a photoresistor, and the brains of the operation, Arduino!.

My goal for this project is to design for versatility and have any bedside lamp light up, whenever I turn my my main lights off, so I don't have to blindly navigate my way to my bed or turn on the flashlight on my phone to get there. I also have a tendency to forget to turn off the bedside light in the morning wasting electricity, so I also want it to turn off if my room is bright (from opening the curtains).

Step 1: Tools and Materials

  • Arduino Uno
  • LDR
  • 10KΩ Resistor
  • Servo motor
  • Jumper Cables

Step 2: Connecting the LDR and Servo to the Arduino

Wiring the Servo motor to the Arduino

  1. Connect the servo motor's ground identified as either the brown or black pin to the Arduino's GND pin.
  2. Connect the servo motor's VCC identified as either a red or orange pin in the middle to the Arduino's 5V pin.
  3. Connect the signal pin from the servo, i.e. the remaining pin, to the digital pin 9 on the Arduino.

Wiring the LDR as an input signal using a voltage divider.

  1. Connect a 10K Ω resistor on one of the pins of the LDR, and this same pin is connected to A0 on the Arduino. This is the green wire.
  2. Then, connect the remming pin to the the power rail on the breadboard. This is the white wire.
  3. Lastly, complete the circuit by connecting the unconnected end of the resistor to the common ground rail. This is the yellow wire.

Step 3: Securing the Servo to the Light Switch

Add hot glue on the bottom surface of the servo motor and glue it to the base of the lamp such that the control horns touch the light switch. Play with moving the control horns and make sure it actually moves the switches, then glue it in place. Further secure it by taping the servo to the lamp.

Step 4: Coding

#include 
//cosntants for the pins where sensors are plugged into.
const int sensorPin = 2;
Servo lightServo;   // create a servo object
//Set up some global variables for the light level an initial value.
int lightInit;  // initial value
int lightVal;   // light reading
void setup()
{
  lightServo.attach (9);
  
  lightInit = analogRead(sensorPin);
  //we will take a single reading from the light sensor and store it in the lightCal  
}
void loop()
{
  lightVal = analogRead(sensorPin); // read the current light levels
  //if lightVal is less than our initial reading withing a threshold then it is dark.
  if(lightVal - lightInit <  50)
  {
    lightServo.Wtite (150);  // turn on light by physically moving switch
  } else {
  //otherwise, it is bright  
   lightServo.Wtite (180);        // turn off light by physically moving switch
  }
}

Step 5: Demo

The LDR being so close to the light ended up setting up a false feedback to the Arduino creating an unstable infinite feedback loop thus turning the light on and off. This is due to the positioning of the LDR causing it to detect that the light has turned on, and given 'bright' conditions, it turns the lights off, but given dark conditions, it turns the light on, and so forth. This can easily be resolved by simply moving the LDR away from the light source by about a meter.