Sunrise-Sunset Light Switch - Make: Basic Arduino Projects - 26 Experiments with Microcontrollers and Electronics (2014)

Make: Basic Arduino Projects - 26 Experiments with Microcontrollers and Electronics (2014)

Chapter 2. Sunrise-Sunset Light Switch

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

§ Arduino microcontroller

§ SW1: mini pushbutton

§ LED1: red LED

§ LED2: green LED

§ C1: 100 uF electrolytic capacitor

§ R1: 10K ohm resistor (brown, black, orange stripes)

§ R2: 330 ohm resistor (orange, orange, brown stripes)

§ R3: photocell

§ Full-size clear breadboard

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)

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 Chapter 1. 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 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);

}

}

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)

TECH NOTE

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)

TECH NOTE

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.

TROUBLESHOOTING TIP

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

Sunrise-Sunset Detector with Serial Monitor

This Ultimate Microcontroller project demonstrates the power of electronic sensors to detect physical stimuli such as light, sound, and pressure. With a slight modification to the sketch, messages can scroll across a Serial Monitor. The Arduino IDE has a Serial Monitor for displaying the messages produced by the Arduino. You can access the Serial Monitor by following these two steps:

1. Move your mouse to the main toolbar of the Arduino IDE and click Tools.

2. Move the cursor, highlight “Serial Monitor,” and click it.

The Serial Monitor will be displayed on your computer’s screen. It’s just that easy! The modifications to your sketch to display the messages “Sunrise” and “Sunset” on the Serial Monitor are shown in Example 2-2.

Example 2-2. Sunrise Sunset Detector with Serial Monitor sketch

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);

// initialize serial communications at 9600 bps:

Serial.begin(9600); // Add code instruction here!

}

void loop(){

// read the state of the light sensor 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);

// display message

Serial.println("Sunset\n"); // Add code instruction here!

}

else {

// turn red LED off:

digitalWrite(redledPin, LOW);

// turn on onboard LED and green LED;

digitalWrite(greenledPin13,HIGH);

// display message

Serial.println("Sunrise\n"); // Add code instruction here!

}

}

With the modifications made to the original sketch, upload it to the Arduino and open the Serial Monitor. As you wave your hand over the photocell, you see the messages “Sunrise” (no hand over the sensor) and “Sunset” (hand over the sensor) displayed on the Serial Monitor. Figure 2-5shows the two messages displayed on the Serial Monitor.

Experiment with the location of the Sunrise-Sunset detector to obtain the best circuit response. Enjoy!

The block diagram in Figure 2-6 shows the electronic component blocks and the electrical signal flow for the Sunrise-Sunset Light Switch. A Fritzing electronic circuit schematic diagram of the switch is shown in Figure 2-7. Electronic circuit schematic diagrams are used by electrical/electronic engineers to design and build cool electronic products for society.

Serial Monitor displaying “Sunset” and “Sunrise” messages

Figure 2-5. Serial Monitor displaying “Sunset” and “Sunrise” messages

Sunrise-Sunset Light Switch block diagram

Figure 2-6. Sunrise-Sunset Light Switch block diagram

TECH NOTE

Always document your experiments and design changes in a lab notebook in case you develop that million dollar idea!

Something to Think About

How can the Serial Monitor display actual light sensor data?

Sunrise-Sunset Light Switch circuit schematic diagram

Figure 2-7. Sunrise-Sunset Light Switch circuit schematic diagram

Chapter 1. The Trick Switch

Resistor-Capacitor Timing Basics

In electronics, sometimes we want to keep a device on for a certain amount of time even when an electrical switch is turned off. Ordinary pushbuttons used to turn electronic devices on and off can easily be operated by a timed delay switch. How awesome would it be to create such a device to delay turning off a simple LED? Such a gadget could be used to trick your friends, family, or even the local Makerspace when they see the LED staying on after the pushbutton has been released. With a few electronic components from the Ultimate Microcontroller Pack, you can make an LED (light-emitting diode) stay on for a few extra seconds when a pushbutton switch is turned off. Figure 1-1 shows an assembled Trick Switch. The electronic components required to build the Trick Switch are shown in the Parts List.

Parts List

§ Arduino microcontroller

§ SW1: mini pushbutton

§ LED1: red LED

§ C1: 100 uF electrolytic capacitor

§ R1: 10K ohm resistor (brown, black, orange stripes)

§ R2: 330 ohm resistor (orange, orange, brown stripes)

§ Full-size clear breadboard

Trick Switch circuit built on a full-size clear breadboard (both the 100 uF electrolytic capacitor and red LED negative pins are wired to ground)

Figure 1-1. Trick Switch circuit built on a full-size clear breadboard (both the 100 uF electrolytic capacitor and red LED negative pins are wired to ground)

TECH NOTE

You can create your own electrical circuits and test them using diagrams with an online simulator called Circuit Lab.

Let’s Build a Trick Switch

When you press the pushbutton switch on this device, the LED turns on. The capacitor will begin storing electrical energy from the +5VDC power supply circuit of the Arduino. Releasing the pushbutton switch cuts off the flow of electricity from the source, but the energy stored in the capacitor keeps the Arduino running for a few extra seconds. The Arduino keeps the LED lit until the capacitor’s stored energy is empty. You can build the Trick Switch using the electronic components from the Parts List and the Fritzing wiring diagram shown in Figure 1-2. 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 wiring diagram of Figure 1-2 or the actual Trick Switch device shown in Figure 1-1.

3. Type the Pushbutton sketch shown in Example 1-1 into the Arduino text editor.

4. Upload the Pushbutton sketch to the Arduino.

5. Press the mini pushbutton for a moment. The red LED turns on. After one to two minutes, the red LED will turn off.

Trick Switch Fritzing diagram

Figure 1-2. Trick Switch Fritzing diagram

TROUBLESHOOTING TIP

If the Trick Switch device doesn’t work, check for incorrect resistor values, incorrect wiring, sketch typos, and improper orientation of polarized electronic components (the LED and capacitor).

Example 1-1. Pushbutton sketch

/*

Pushbutton Sketch

Reads the capacitor voltage at digital pin 2 and turns on and off a light-

emitting diode (LED) connected to digital pin 12.

17 Nov 2012

by Don Wilcher

*/

// constants won't change; they're used here to

// set pin numbers:

const int buttonPin = 2; // the number of the pushbutton pin

const int ledPin = 12; // the number of the LED pin

// variables will change:

int buttonStatus = 0; // variable for reading the pushbutton status

void setup() {

// initialize the LED pin as an output:

pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

}

void loop(){

// read the status of the pushbutton value:

buttonStatus = digitalRead(buttonPin);

// check if the pushbutton is pressed

// if it is, the buttonEvent is HIGH:

if (buttonStatus == HIGH) {

// turn LED on:

digitalWrite(ledPin, HIGH);

}

else {

// turn LED off:

digitalWrite(ledPin, LOW);

}

}

TECH NOTE

The ledPin value can be changed to 13 to operate the onboard LED.

Trick Switch with On/Off Indicators

In developing new products, electronics designers are always improving designs by adding features and functions that excite the customer. The Trick Switch device you built can be improved by adding an LED indicator. This LED indicates when the Trick Switch timing cycle is done.Figure 1-3 shows you where to add a green LED to the Trick Switch on the full-size clear breadboard.

Adding a green LED indicator to the Trick Switch circuit built on a full-size clear breadboard

Figure 1-3. Adding a green LED indicator to the Trick Switch circuit built on a full-size clear breadboard

To complete the new product design, you need to make a few changes to the Pushbutton sketch. Modify the sketch using the code changes shown in Example 1-2.

Example 1-2. Pushbutton sketch modified to include LED indicators

// constants won't change; they're used here to

// set pin numbers:

const int buttonPin = 2; // the number of the pushbutton pin

const int ledPin = 12; // the number of the LED pin

const int ledPin13 = 13; // onboard LED

void setup() {

// initialize the LED pins as outputs:

pinMode(ledPin, OUTPUT);

pinMode(ledPin13, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

}

void loop(){

// read the state of the pushbutton value:

int buttonStatus;

buttonStatus = digitalRead(buttonPin);

// check if the pushbutton is pressed

// if it is, the buttonStatus is HIGH:

if (buttonStatus == HIGH) {

// turn LED on:

digitalWrite(ledPin, HIGH);

// turn off onboard LED:

digitalWrite(ledPin13,LOW);

}

else {

// turn LED off:

digitalWrite(ledPin, LOW);

// turn on onboard LED:

digitalWrite(ledPin13, HIGH);

}

}

After you’ve saved the sketch changes and uploaded them to the Arduino, the green LED will turn on. When you press the mini pushbutton, the green LED will turn off, and the red LED will turn on. Pretty awesome stuff. Enjoy!

The block diagram in Figure 1-4 shows the electronic component blocks and the electrical signal flow for the Trick Switch. A Fritzing electronic circuit schematic diagram of the switch is shown in Figure 1-5. Electronic circuit schematic diagrams are used by electrical/electronic engineers to design and build cool electronic products for society.

Trick Switch block diagram

Figure 1-4. Trick Switch block diagram

Something to Think About

Try different resistor and capacitor values and see what happens. Can you detect any patterns? How can a small piezo buzzer be used with the Trick Switch?

Trick Switch circuit schematic diagram

Figure 1-5. Trick Switch circuit schematic diagram