Push Button Using Raspberry Pi Pico

 

Control LED with Push Button Using Raspberry Pi Pico (MicroPython)

Objective

To control an LED using a push button on a Raspberry Pi Pico, written in MicroPython. This project is perfect for beginners learning input and output (I/O) control on microcontrollers.


 Components Required

S.N.ComponentQuantity
1Raspberry Pi Pico1
2LED (any color)1
3220Ω Resistor1(optional)
4Push Button1
5Breadboard + WiresAs needed
6Micro USB Cable1

 Circuit Description

  • LED's anode (long leg) is connected to GPIO 0 through a 220Ω resistor, and cathode to GND.

  • Push button is connected between GPIO 1 and 3.3V.

  • An internal pull-down resistor is enabled in code to keep the button input low when not pressed.

  • When the button is pressed, the input pin reads HIGH, and the LED turns ON.

       
ckt diagram







 Code: MicroPython for Button Controlled LED

python

from machine import Pin from time import sleep led = Pin(0, Pin.OUT) button = Pin(1, Pin.IN, Pin.PULL_DOWN) print("Press button to turn ON the LED") while True: if button.value() == 1: led.on() elif button.value() == 0: led.off() sleep(0.001)

 Working Principle

  • Pin(0, Pin.OUT) sets GPIO 0 as an output for the LED.

  • Pin(1, Pin.IN, Pin.PULL_DOWN) sets GPIO 1 as an input and activates the internal pull-down resistor.(you may used external resistor for pull up or down )

  • In the loop:

    • If the button is pressed (button.value() == 1), the LED turns ON.

    • If the button is not pressed (button.value() == 0), the LED turns OFF.

  • sleep(0.001) adds a tiny delay to avoid unnecessary CPU load.


Applications

  • Basic digital input handling.

  • Foundation for building user interfaces (like switches or sensors).

  • Can be expanded into memory games, door alarms, or counters.


 Advantages

  • Simple and very easy to understand.

  • Helps grasp core GPIO concepts (input/output).

  • Can be modified to include more buttons, LEDs, or logic.


 Limitations

  • No software debounce (LED may flicker if button bounces).

  • Only controls one LED with one button.

  • Doesn’t store LED state after button release.


 Suggestions

  • Add software debounce logic using delay or state tracking.

  • Use toggle logic to remember the state after button release.

  • Expand with multiple LEDs and buttons for more complex projects.


References

  • Raspberry Pi Pico GPIO documentation.

  • Youtube.

  • Concept explained and guided by ChatGPT.


 Conclusion

This simple LED control project using a push button and Raspberry Pi Pico is an excellent way to start learning real-world input/output programming. It lays the foundation for more advanced interaction-based microcontroller projects.

Comments

Popular posts from this blog

Astable circuit using 555 timer ic

Monostable circuit using 555 timer ic

LDR base solar light tracking system using Transistor DIY project