Button with Pi Pico | Toggle LED

LED toggle pi pico
LED toggle pi pico

In this tutorial, you will learn how to interface push button with Pi Pico and we will discuss how we can interface the switch with Raspberry pi pico. Also, you will learn how to use the button to toggle LED in Pi Pico. Like the previous Pi Pico tutorial, we will program our board using Arduino IDE, MicroPython and CircuitPython. The buttons can be interfaced in multiple ways and I will try to cover each of them.

Button with Pi Pico

Components Required

ComponentAmazon (US)Aliexpress
Pi PicoButton with Pi Pico | Toggle LEDButton with Pi Pico | Toggle LED
Push ButtonButton with Pi Pico | Toggle LEDButton with Pi Pico | Toggle LED
LEDButton with Pi Pico | Toggle LEDButton with Pi Pico | Toggle LED
ResistorButton with Pi Pico | Toggle LEDButton with Pi Pico | Toggle LED
BreadboardButton with Pi Pico | Toggle LEDButton with Pi Pico | Toggle LED
Jumper wiresButton with Pi Pico | Toggle LEDButton with Pi Pico | Toggle LED
Micro USB cableButton with Pi Pico | Toggle LEDButton with Pi Pico | Toggle LED
Button with pi pico Components required

Disclosure: Some of these links are affiliate links. We mayy earn commission on you purchase at no extra cost to you. Support us by buying from here .

Button Pinout and Working

Button pinout and working
Button pinout and working

From the figure, we can see that a push button has 4 terminals. a,b,c and d. Internally, a is connected to b, and c is connected to d. and there is no connection of a,b with c,d as can be seen in the right bottom figure which shows the condition of button when it is not pressed hence making an open circuit. But when the button is pressed, a,b makes connection with c,d, hence making a close circuit as can be seen in left figure at the bottom. To summarize,

  • No matter the state of button pushed or not, a is connected to b and c is connected to d.
  • When button is pressed, a,b are connected to c,d.
  • When button is not pushed, there is no connection between a,b and c,d.

Button interfacing technqiues

Button can be interfaced with Pi Pico in two ways, pull-up configuration and pull down configuration. However since pi pico provides internal pull up configuration we are going to use that. In the pull up configuration, we will be receiving HIGH value at digital pin when button is not pushed and when the button is pushed we get the LOW value.

Button in pull up configuration
Button in pull up configuration

Button with Pi Pico Wiring Diagram

Button with Pi Pico wiring Diagram
Button with Pi Pico wiring Diagram

As can be seen, in the figure that Pi Ground pin is connected to one pin of button and the other pin of button is connected to GP5. We will use the internal pull-up configuration of pi pico.

Adding LED

Button and LED with Pi Pico
Button and LED with Pi Pico

As we have discussed in our previous tutorial “LED blink in pi pico“, that LED needs a resistor so we cna protect it from burning. The anode of LED is connected to GP6 and cathode of LED is connected to GND.

Button with Pi Pico Code

As we have discussed in the introduction, we can program raspberry pi pico in multiple ides and programming languages. In this tutorial, we will be learning how to use Button with pi pico in arduino IDE, microPython and circuitPython.

Button with Pi Pico Arduino IDE

// Define the pin connected to the pushbutton
const int buttonPin = 5;

void setup() {
  // Initialize serial communication
  Serial1.begin(9600);

  // Set the button pin as an input with an internal pull-up resistor
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  // Read the value of the pushbutton
  int buttonValue = digitalRead(buttonPin);

  // Print the value of the pushbutton to the serial monitor
  Serial1.println(buttonValue);

  // Wait a short time before reading the button value again
  delay(50);
}

The code first defines the pin connected to the pushbutton, and then sets it as an input with an internal pull-up resistor in the setup() function. In the loop() function, the code reads the value of the pushbutton using the digitalRead() function, and then prints it to the serial monitor using the Serial1.println() function. It then waits for a short time before reading the button value again.

When the button is pressed, you will see 0, in the serial monitor and in normal conditions it will print 1.

Button with Pi Pico | Toggle LED
Serial monitor Raspberry pi pico

Pi Pico LED Toggle using Button Arduino IDE

// constants won't change
const int buttonPin = 5; // pi pico connected to button's pin
const int LEDPin    = 6; // pi pico pin connected to LED's pin

// variables will change:
int ledState = LOW;     // the current state of LED
int lastButtonState;    // the previous state of button
int currentButtonState; // the current state of button

void setup() {
  Serial1.begin(9600);                // initialize serial
  pinMode(buttonPin, INPUT_PULLUP); // set pi pico pin to input pull-up mode
  pinMode(LEDPin, OUTPUT);          // set pi pico pin to output mode

  currentButtonState = digitalRead(buttonPin);
}

void loop() {
  lastButtonState    = currentButtonState;      // save the last state
  currentButtonState = digitalRead(buttonPin); // read new state

  if(lastButtonState == HIGH && currentButtonState == LOW) {
    Serial1.println("The button is pressed");

    // toggle state of LED
    ledState = !ledState;

    // control LED arccoding to the toggled state
    digitalWrite(LEDPin, ledState); 
  }
  delay(100);
}

In the code, first we define the buttonPin to GP5 and LEDPin to GP6, then we set the state of LED to LOW, since LED will be turned off when we first boot. Then we created a variable to save the current and last state of the button.

In the setup() function, we define the serial monitor by setting up the baud rate and then we set the button as input in pull up configuration and LED pin as output.

In the loop() function, we first save the last state of the button and then read the current state using the digital Read function. Then using the if else conditions we set a condition that if last state of button was HIGH, i.e. button was not pushed and current state is LOW, i.e. button has just pushed, change the state of LED. ! sign means not gate and that state will be written to LED and our LED will be toggle on button pushed or pressed.

Button with Pi Pico MicroPython

from machine import Pin

buttonPin = 5

push_button = Pin(buttonPin, Pin.IN, Pin.PULL_UP)  

while True:
  button_state = push_button.value()
  print(button_state)

First, we imported the Pin function from machine library since we will be using IOs of Pi Pico like we did in last tutorial. Then we define the pin number of button. Then we set push button as input in pull up configuration. Then implemented the reading of push button value and print it in the while loop, which is a continuous or forever loop. You will see the results in the console.

Pi Pico LED Toggle using Button MicroPython

from machine import Pin
from time import sleep

buttonPin = 5
LEDPin = 6
button_state = 0
led_state = 0

led = Pin(LEDPin, Pin.OUT)    
push_button = Pin(buttonPin, Pin.IN, Pin.PULL_UP)  

while True:
  last_State = button_state
  button_state = push_button.value()
  if button_state == 0 and last_State == 1:
    led_state = not(led_state)
    led.value(led_state)
  sleep(0.1)
     

For the toggle of LED using push button, we updated the last code and added LED as output. In the forever loop, we save the last state of button and current state of button in a variable. Then using the if else condition we see if current state is 0 and last state was 1, change the state of LED. If it was on, turn it off. There is a delay of 0.1 seconds in the end for debouncing of button.

Button with Pi Pico CircuitPython

import time
import board
from digitalio

btn = digitalio.DigitalInOut(board.GP5)
btn.direction = digitalio.Direction.INPUT
btn.pull = digitalio.Pull.UP

while True:
    button_state = btn.value
    print(button_state)
    time.sleep(0.1) # sleep for debounce

We first import three libraries, Time for using the sleep function i.e. to create delay, board library to use the GPIO pins in pi pico and digitalio library for controlling the states of LED.

Then we will set the pin number of button to GP5, and then set the direction as INPUT and set it as pull up configuration.

Then using the btn.value , we read the state of push button and print it in the forever while loop.

Pi Pico LED Toggle using Button CircuitPython

import time
import board
import digitalio 

btn = digitalio.DigitalInOut(board.GP5)
btn.direction = digitalio.Direction.INPUT
btn.pull = digitalio.Pull.UP

led = digitalio.DigitalInOut(board.GP6)
led.direction = digitalio.Direction.OUTPUT

button_state = 0
led_state = 0

while True:
    last_state = button_state
    button_state = btn.value
    if not button_state and last_state:
        led_state = not(led_state)
        led.value = led_state
    time.sleep(0.1) # sleep for debounce

The additional things from the previous code is that we set up LED as output on pin GP6. We created some variables, to keep the status of LED and button. In the forever loop, we save the last state of push button and then read the current state of button and if the state has changed i.e. HIGH to LOW, then we change the state of LED.

Conclusion

In this tutorial, you learn how you can use a button with Pi pico using Arduino IDE, Micropython and Circuit Python. Also, we covered how to toggle LED.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.