Sunrise-Sunset Light Switch using Arduino microcontroller

Sunrise-Sunset Light Switch using Arduino microcontroller

Resistor-Capacitor Timing Basics

Designing and building new electronic devices is quite easy when you know the secret ingredient to rapid design. The technique is to take an existing electronic device and make a small change to it. For example, the Trick Switch project can easily be changed to a noncontact device by adding a sensor. With the right sensor, a hand wave can turn the LED on. The RC timing circuit wired to the sensor signals the Arduino to turn it off. In this chapter, you will build a light sensor circuit to turn on an LED with the wave of your hand and automatically turn it off. The Ultimate Microcontroller Pack has all the electronic parts you need to the build the project. Figure 2-1 shows the Sunrise-Sunset Light Switch device.

Parts List

  1. Arduino microcontroller
  2. SW1: mini pushbutton
  3. LED1: red LED
  4. LED2: green LED
  5. C1: 100 uF electrolytic capacitor
  6. R1: 10K ohm resistor (brown, black, orange stripes)
  7. R2: 330 ohm resistor (orange, orange, brown stripes)
  8. R3: photocell
  9. Full-size clear breadboard
Sunrise-Sunset Light Switch circuit built on a full-size clear breadboard (the 100 uF electrolytic capacitor and the red
Figure 2-1. Sunrise-Sunset Light Switch circuit built on a full-size clear breadboard (the 100 uF electrolytic capacitor and the red
and green LED negative pins are wired to ground)

Let’s Build a Sunrise-Sunset Light Switch

You can build a Sunrise-Sunset Light Switch by modifying the Trick Switch device from here. The main change you will make is to remove the mini pushbutton and replace it with a photocell. You will also add a green LED to pin D13 of the Arduino. Refer to the Parts List for all the electronic parts required for this project. Here are the steps required to build the electronic device:

  1. From the Ultimate Microcontroller Pack, place the required parts on your workbench or lab tabletop.
  2. Wire the electronic parts using the Fritzing diagram of Figure 2-2 or the actual Sunrise-Sunset Light Switch device shown in Figure 2-1.
  3. Type Example 2-1 into the Arduino IDE.
  4. Upload the Sunrise-Sunset sketch to the Arduino. The green LED will be on.
  5. Wave your hand over the photocell for a moment. The red LED turns on. After a few seconds, the red LED will turn off, and the green LED will turn on.
Sunrise-Sunset Light Switch Fritzing diagram
Figure 2-2. Sunrise-Sunset Light Switch Fritzing diagram

Example: Sunrise-Sunset Light Switch sketch

Sunrise-Sunset Light Switch
Turns on and off a light-emitting diode (LED) connected to digital
pins 12 and 13 after 10 to 20 seconds, by waving a hand over a photocell
attached to pin 2.
23 Nov 2012
by Don Wilcher
// constants won't change; they're used here to
// set pin numbers:
const int lightsensorPin = 2; // the number of the light sensor pin
const int redledPin = 12; // the number of the red LED pin
const int greenledPin13 = 13; // onboard LED and green LED pin
// variables will change:
int sensorState = 0; // variable for reading light sensor status
void setup() {
// initialize the LED pins as outputs:
pinMode(redledPin, OUTPUT);
pinMode(greenledPin13, OUTPUT);
// initialize the light sensor pin as an input:
pinMode(lightsensorPin, INPUT);
void loop(){
// read the state of the pushbutton value:
sensorState = digitalRead(lightsensorPin);
// check if the light sensor is activated
// if it is, the sensorState is HIGH:
if (sensorState == HIGH) {
// turn red LED on:
digitalWrite(redledPin, HIGH);
// turn off onboard LED and green LED:
digitalWrite(greenledPin13, LOW);
else {
// turn red LED off:
digitalWrite(redledPin, LOW);
// turn on onboard LED and green LED;
digitalWrite(greenledPin13, HIGH);

Circuit Theory

The Sunrise-Sunset Light circuit operates like the Smart Switch, except you don’t have to use a mini pushbutton to start the timing function. The mini pushbutton has instead been replaced with a light sensor called a photocell. A photocell is a variable resistor that changes its resistance based on the amount of light touching its surface. Light falling on a photocell will decrease its resistance value. No light will increase its resistance value. Figure 2-3 shows the resistor-capacitor (RC) timing circuit with a photocell variable resistor symbol.

RC timing circuit with a photocell (variable resistor)
Figure 2-3. RC timing circuit with a photocell (variable resistor)

Another type of variable resistor is a 3-pin electronic part known as a potentiometer. By rotating its shaft, the internal resistance value changes. Potentiometers are used in electronic products like radios and TVs to control the volume or sound level.

A photocell is a small electronic component with two leads protruding from a light-sensitive pellet. On top of the pellet is an etched series of squiggly lines. The lines etched on its surface are the resistance portion of the photocell. An actual photocell part can be seen in Figure 2-4.

Photocell (a light-dependent resistor)
Figure 2-4. Photocell (a light-dependent resistor)

LDR (light-dependent resistor) is another word used for photocell.

If you know someone with a DMM (digital multimeter), have him attach your photocell to it. By waving your hand over the photocell, you will see the light-sensitive part change its resistance based on the amount of light touching it. This variable resistance feature will be used to turn on an LED. In the Sunrise-Sunset project (a light-activated switch), the green LED will be on first. Placing your hand over the photocell briefly will turn on the red LED. After the RC timing circuit has completed its charging-discharging cycle, the red LED turns off followed by the green LED turning on.

Due to the sensitivity of the Sunrise-Sunset Light Switch, for best operating results, use ambient (i.e., natural) lighting when testing the device.

Related Project: Sunrise-Sunset Detector with Serial Monitor

Book Reference: Make: Basic Arduino Projects
26 Experiments with Microcontrollers and Electronics
By: Don Wilcher

Write Your Comments


Leave a Reply