Button toggle logic to control LED

Hardware Required
- PICUNO Microcontroller board
- 1 × LED
- 1 × 220Ω resistor
- 1 × Push Button (4-pin or 2-pin)
- 1 × 10kΩ resistor (for pull-down)
- Breadboard
- Jumper wires
- USB cable
Description
Pressing the button once will turn ON the LED, and pressing it again will turn it OFF. This is useful when a button needs to act like a switch. We use a pull-down resistor to keep the GPIO pin LOW when the button is not pressed.
Circuit Diagram
[Fritzing image to be added here]
Circuit
- Connect the PICUNO board to the computer using a USB cable.
- Connect the anode (longer leg) of the LED to GPIO 6 through a 220Ω resistor.
- Connect the cathode (short leg) of the LED to GND.
- Connect one leg of the push button to 3.3V.
- Connect the other leg of the button to GPIO 7.
- Connect one end of the 10kΩ resistor to GPIO 7 and other end to GND.
Schematic
Button one side → 3.3V
Other side → GPIO 7
GPIO 7 → 10kΩ → GND
GPIO 6 → 220Ω → LED anode
LED cathode → GND
Code - C
#define LED_PIN 6
#define BUTTON_PIN 7
bool ledState = false;
bool prevButtonState = false;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
}
void loop() {
bool currentButtonState = digitalRead(BUTTON_PIN);
if (currentButtonState == HIGH && prevButtonState == LOW) {
ledState = !ledState;
digitalWrite(LED_PIN, ledState);
delay(200); // Debounce delay
}
prevButtonState = currentButtonState;
delay(10);
}
#define BUTTON_PIN 7
bool ledState = false;
bool prevButtonState = false;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
}
void loop() {
bool currentButtonState = digitalRead(BUTTON_PIN);
if (currentButtonState == HIGH && prevButtonState == LOW) {
ledState = !ledState;
digitalWrite(LED_PIN, ledState);
delay(200); // Debounce delay
}
prevButtonState = currentButtonState;
delay(10);
}
ledState - keeps track of the LED status.
if (current == HIGH && previous == LOW) - Detects rising edge.
ledState = !ledState - Toggles LED state.
digitalWrite(LED_PIN, ledState) - Updates the LED.
delay(200) - Simple debounce logic.
if (current == HIGH && previous == LOW) - Detects rising edge.
ledState = !ledState - Toggles LED state.
digitalWrite(LED_PIN, ledState) - Updates the LED.
delay(200) - Simple debounce logic.
Code - Micropython
from machine import Pin
import time
led = Pin(6, Pin.OUT)
button = Pin(7, Pin.IN)
led_state = False
prev_button_state = 0
while True:
current_button_state = button.value()
if current_button_state == 1 and prev_button_state == 0:
led_state = not led_state
led.value(led_state)
time.sleep(0.2) # Debounce delay
prev_button_state = current_button_state
time.sleep(0.01)
import time
led = Pin(6, Pin.OUT)
button = Pin(7, Pin.IN)
led_state = False
prev_button_state = 0
while True:
current_button_state = button.value()
if current_button_state == 1 and prev_button_state == 0:
led_state = not led_state
led.value(led_state)
time.sleep(0.2) # Debounce delay
prev_button_state = current_button_state
time.sleep(0.01)
led_state = True - Variable to track current LED status (OFF initially).
if current_button_state == 1 and prev_button_state == 0 - Detects button press transition.
led_state = not led_state - Toggles the LED status.
led.value(led_state) - Updates the actual LED output.
time.sleep(0.2) - Adds delay to avoid false triggers due to bouncing.
if current_button_state == 1 and prev_button_state == 0 - Detects button press transition.
led_state = not led_state - Toggles the LED status.
led.value(led_state) - Updates the actual LED output.
time.sleep(0.2) - Adds delay to avoid false triggers due to bouncing.