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:
- Place the required parts on your workbench or lab tabletop.
- Wire the electronic parts using the Fritzing wiring diagram of Figure 1-2 or the actual Trick Switch device shown in Figure 1-1.
- Type the Pushbutton sketch shown in Example 1-1 into the Arduino text editor.
- Upload the Pushbutton sketch to the Arduino.
- Press the mini pushbutton for a moment. The red LED turns on. After one to two minutes, the red LED will turn off.
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.
Leave a Reply