Β LED WITH SWITCH INTERFACE

 

πŸ’‘ LED with Switch – Interface with RP2040 (Raspberry Pi Pico)

🧾 Overview:

A switch (push-button) can be used as a digital input to control an LED on the RP2040. When the button is pressed, the LED turns ON; when released, it turns OFF. This demonstrates basic input-output (I/O) interfacing.

βš™οΈ Required Components:

  • RP2040 board (e.g., Raspberry Pi Pico)

  • 1 Γ— LED (any color)

  • 1 Γ— 220Ξ© resistor (current limiting for LED)

  • 1 Γ— Push-button switch

  • 1 Γ— 10kΞ© resistor (pull-down for switch)

  • Breadboard and jumper wires

πŸ”Œ Circuit Diagram:

ComponentGPIO Pin
Button1GPIO5
Button2GPIO4
LED1GPIO2
LED2GPIO3

πŸ“ Notes:

  • Pin(2, Pin.OUT) sets GPIO2 as output (for LED).

  • Pin(3, Pin.OUT) sets GPIO3Β as output (for LED).
  • Pin(4, Pin.IN, Pin.PULL_DOWN) sets GPIO5 as input with internal pull-down.
  • Pin(5, Pin.IN, Pin.PULL_DOWN) sets GPIO4Β as input with internal pull-down.
  • The LED turns ON only when button is pressed (input is HIGH).

  • You can change GPIO pins if needed.

βœ… Wokwi Setup Steps:

  1. Go to https://wokwi.com

  2. Create a new project β†’ Choose Raspberry Pi Pico (Python)

  3. Add LED and push-button components

  4. Connect:

    • GPIO2 β†’ LED1 β†’ GND (with 220Ξ© resistor)

    • GPIO5 β†’ One side of button1

    • GPIO3 β†’ LED2 β†’ GND (with 220Ξ© resistor)

    • GPIO4 β†’ One side of button2

    • Other side of button β†’ GND

πŸ§ͺ Sample Code:

from machine import Pin
from utime import sleep_ms

# Configure buttons with internal pull-up resistors
button1 = Pin(5, Pin.IN, Pin.PULL_UP)
button2 = Pin(4, Pin.IN, Pin.PULL_UP)

# Configure LEDs as output
led1 = Pin(2, Pin.OUT)
led2 = Pin(3, Pin.OUT)

# Main loop
while True:
if button1.value() == 0: # Button1 pressed
led1.value(1)
else:
led1.value(0)

if button2.value() == 0: # Button2 pressed
led2.value(1)
else:
led2.value(0)

sleep_ms(50) # debounce delay

🧠 Key Points:

  • Always use a resistor with the LED to limit current.

  • Use a pull-down resistor (internal or external) with the button to avoid floating input.

  • Polarity matters: Connect the LED correctly (Anode to GPIO, Cathode to GND).

  • Buttons can bounce β€” use small delays or software debouncing if needed.

 

πŸ“ž For More Details & Project Support:

Power Integrated Solutions
Networks | Electronics | Home Automation | Water Automation | IoT | PLC | Embedded | DBMS

πŸ“ Location:
10A/3, Radhakrishnan Colony,
Sasthri Road, Tennur,
Tiruchirappalli, Tamil Nadu – 620017


πŸ“ View on Google Maps

πŸ“§ Email:

πŸ“± Phone / WhatsApp:
+91 76393 85448
+91 82488 85959

🌐 Let’s Build the Future with Innovation in Education & Technology!