Automatic dusk-to-dawn light
Lights switch on by themselves at dusk and off again in the morning.
Shop by category
Shop all componentsRead a button as an input and light an LED only while the button is held down. An input pin left unconnected picks up electrical noise and reads randomly, so it needs pulling to a known voltage. INPUT_PULLUP switches on a resistor inside the Nano that holds the pin at 5 V, which is why an unpressed button reads HIGH. Pressing the button connects the pin to ground, so it reads LOW. That inversion catches everyone at first: pressed means LOW, not HIGH.
Every part for this build comes in the Geekobot Explorer Kit box, a Geekobot exclusive, together with the parts for all 61 projects, from a first blinking LED to a smart fan and a reaction game. The wiring below is the kit's own wiring.
Tick anything you already have and the total updates.
The brain of every Explorer Kit project
Male-to-female and female-to-female leads for the modules
LEDs from the kit's red, green, yellow and blue packs
220 ohm resistors: one in series with every LED segment
Nano I/O shield from the kit: every pin has its own S, V, G header, so modules plug in with jumpers
Follow the colours: red is power, dark grey is ground, blue is a signal.
explorer_kit_04_push_button_led.inoVerify & upload in Electrolarkint button = 2;
int led = 8;
int presses = 0;
bool wasPressed = false;
void setup()
{
pinMode(button, INPUT_PULLUP); // Internal resistor holds the pin HIGH
pinMode(led, OUTPUT);
Serial.begin(9600);
Serial.println("Press the button");
}
void loop()
{
bool pressed = (digitalRead(button) == LOW); // LOW means pressed
if (pressed)
{
digitalWrite(led, HIGH);
}
else
{
digitalWrite(led, LOW);
}
// Count only the moment of pressing, not the whole hold
if (pressed && !wasPressed)
{
presses = presses + 1;
Serial.print("Press number ");
Serial.println(presses);
}
wasPressed = pressed;
delay(20);
}
Use the kit's mini-USB cable, install the CH340 driver on Windows if no port appears, pick Tools > Board > Arduino Nano and try Tools > Processor > ATmega328P (Old Bootloader) if the upload times out.
Turn it round: the long leg (anode) goes to the pin side through the 220 ohm resistor, the short leg to GND.
The sketch uses INPUT_PULLUP, so one leg goes to the pin and the other to GND. Use two legs on opposite sides of the button.
An input pin left unconnected picks up electrical noise and reads randomly, so it needs pulling to a known voltage. INPUT_PULLUP switches on a resistor inside the Nano that holds the pin at 5 V, which is why an unpressed button reads HIGH. Pressing the button connects the pin to ground, so it reads LOW. That inversion catches everyone at first: pressed means LOW, not HIGH.
The LED is dark while the button is untouched. Hold the button and the LED lights. Release it and the LED goes out immediately.
Yes. It is project 4 of 61 in the Geekobot Explorer Kit, in level 1 (Foundations). Every part on the list comes in the kit box, and you can also buy the parts one by one from the list above.
No. Every Explorer Kit project is wired on the Nano I/O shield and the breadboard with jumper wires.
7 parts: Generic Nano CH340 Development Board (Soldered), USB Cable, Dupont Jumper Wires (40 pcs), Through-Hole LED Pack (Pack of 10), Carbon Film Resistor 1/4W 5% (Pack of 10), Tactile Push Button Switch and Arduino Nano IO Expansion Shield (328P Compatible). On this page you can tick the parts you already have and add only the rest to your cart.
It is a beginner project and takes about 20 minutes. The build on this page has 5 steps.
No. This page has the complete, tested code (explorer_kit_04_push_button_led.ino).
About ₹442 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Reading a digital input; Why INPUT_PULLUP is needed; Pressed reads LOW, not HIGH; Detecting the moment of a change rather than a held state.
Lights switch on by themselves at dusk and off again in the morning.
A precise thermostat that keeps tropical fish at a steady 25–26 °C.
Starts the pump when the tank runs low and stops it when it is full.
Tap a hidden pad to switch a lamp on or off, even through a wooden desk.
Learning project. Check every circuit and the datasheet of each part before you power it up.