You can build a Sunrise-Sunset Light Switch by modifying the Trick Switch device. 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:
- Place the required parts on your workbench or lab tabletop.
- 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.
- Type Example 2-1 into the Arduino IDE.
- Upload the Sunrise-Sunset sketch to the Arduino. The green LED will be on.
- 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.
Figure 2-2. Sunrise-Sunset Light Switch Fritzing diagram
Example 2-1. 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);
}
}
Leave a Reply