Arduino Projects Experiments Part1
Arduino Projects Experiments Part1
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) {
After youve 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.
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)
10
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.
23 Nov 2012
by Don Wilcher
*/
11
Circuit Theory
The Sunrise-Sunset Light circuit operates like the Smart Switch, except you dont
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.
12
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-5 shows 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.
15