How to Make a Desk Lamp with Arduino?
1.Introduction
Do you know that Arduino can light up an LED when you press a button? In this project, we will use the Plus Mainboard, a key switch and an LED to make a small desk lamp.
2.Components Required
|
|
|
|
|
---|---|---|---|---|
Keyestudio Plus Mainboard*1 |
Button*1 |
Red LED*1 |
10KΩ Resistor*1 |
Button Cap*1 |
|
|
|
|
|
Breadboard*1 |
220Ω Resistor*1 |
USB Cable*1 |
Jumper Wires |
|
3.Component Knowledge
Button: The button can control the circuit on and off. The circuit is disconnected when the button is not pressed. But it breaks when you release it. Why does it only work when you press it? It starts from the internal structure of the button, which is shown in the figure:.
Before the button is pressed, 1 and 2 are on, 3 and 4 are also on, but 1, 3 or 1, 4 or 2, 3 or 2, 4 are off (not working). Only when the button is pressed, 1, 3 or 1, 4 or 2, 3 or 2, 4 are on.
The key switch is one of the most commonly used components in circuit design.
Schematic diagram of the button:
What is button jitter?
We think of the switch circuit as “press the button and turn it on immediately”, “press it again and turn it off immediately”. In fact, this is not the case.
The button usually uses a mechanical elastic switch, and the mechanical elastic switch will produce a series of jitter due to the elastic action at the moment when the mechanical contact is opened and closed (usually about 10ms). As a result, the button switch will not immediately and stably turn on the circuit when it is closed, and it will not be completely and instantaneously disconnected when it is turned off.
How to eliminate the jitter?
There are two common methods, namely fix jitter in the software and hardware. We only discuss the jitter removal in the software.
We already know that the jitter time generated by elasticity is about 10ms, and the delay command can be used to delay the execution time of the command to achieve the effect of jitter removal.
Therefore, we delay 0.05s in the code to achieve the key anti-shake function.
4. Circuit Diagram and Wiring Diagram
Note: How to connect the LED
How to identify the 220Ω 5-band resistor and 10KΩ 5-band resistor
5. Code
/*
Small_Desk_Lamp
http//www.keyestudio.com
*/
int buttonPin = 5; //the button is connected to 5
int ledPin = 12; // LED is interfaced with 12
int ledState = LOW; // ledState records the state of the LED
int buttonState; // buttonState records the state of the button
int lastButtonState = LOW; // lastbuttonState the state that the button is pressed before
long lastDebounceTime = 0;
long debounceDelay = 50;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
}
void loop() {
//reading is used to save the data of the buttonPin
int reading = digitalRead(buttonPin);
//record the current timee once the data changes
if (reading != lastButtonState) {
lastDebounceTime= millis();
}
// wait for 50ms and determine again to make sure whether the state is as same as the state of the button
// if not, change the state of the button
// at same time, if the state of the button is high(pressed)then change the state of the led
if ((millis() - lastDebounceTime) >debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState= !ledState;
}
}
}
digitalWrite(ledPin, ledState);
// chnage the previous state value of the button
lastButtonState = reading;
}
6. Result
Burn the project code, connect the wires and power on first. Then press the button, the LED will turn on. Press the button again, the LED will turn off.