Arduino MicroPython Button

This guide shows you how to use a button with an Arduino and MicroPython. You will learn:

Arduino MicroPython button

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×Breadboard-mount Button with Cap
1×Breadboard-mount Button Kit
1×Panel-mount Button
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Uno/Mega/Giga
1×(Recommended) Enclosure For Arduino Giga

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Button

Buttons can be challenging when working with Arduino, especially for beginners in electronics. Let's go over the basics and avoid some common mistakes.

Two Main Issues to Consider:

  • Floating Inputs: When a button is connected to an Arduino without a resistor, the Arduino might not correctly detect whether the button is pressed. This happens because the input pin can be in an undefined state. To solve this, you'll need to use a pull-down or pull-up resistor. We'll explain how to set this up later.
  • Chattering: Buttons can "bounce," which means they quickly switch between pressed and unpressed states, even if only pressed once. This can lead to the Arduino registering multiple presses. While this might not always be a problem, it can be an issue in applications where accurate button press counting is critical. We'll discuss debouncing techniques to handle this.

By understanding these issues and applying the right solutions, you'll be prepared to use buttons effectively in your Arduino projects.

Push buttons are switches that close when pressed and open when released. They're also known as tactile buttons or momentary switches. There are two main types of push buttons:

  • PCB-mount push buttons: Designed for mounting on breadboards or printed circuit boards.
  • Panel-mount push buttons: Intended for mounting on panels or enclosures.
Arduino MicroPython Push button

Pinout

The PCB-mount buttons usually have four pins.

Button Pinout

Even though a button has four pins, only two of them are actually used. This is because the pins are internally connected in pairs. While there are four possible ways to connect a button to a PCB, two of these configurations are symmetrical.

How To Use Button

Why are only two pins used on a button that has four?

⇒ To provide stable mounting on the PCB (printed circuit board) and to endure the force applied when the button is pressed.

The panel-mount buttons usually have two pins.

two-pin push button Pinout
image source: diyables.io

How It Works

  • When the button is not pressed, pin A and pin B are not connected.
  • When the button is pressed, pin A and pin B are connected.
How Button Works

Arduino - Button

One button pin attaches to VCC or GND, while the other pin connects to a pin on the Arduino. We can determine if the button is pressed by checking the input pin status on the Arduino.

Button State and Pressing State

The behavior of an Arduino pin when a button is pressed or released depends on the button's connection and the pin's configuration.

Two Common Connection Methods:

  • Pull-Down Resistor:
    • Connect one side of the button to VCC (power) and the other side to an Arduino pin.
    • Use either an internal or external pull-down resistor on the Arduino pin.
    • The Arduino pin reads HIGH when the button is pressed and LOW when the button is not pressed.
  • Pull-Up Resistor:
    • Connect one side of the button to GND (ground) and the other side to an Arduino pin.
    • Use either a built-in pull-up resistor or an external pull-up resistor on the Arduino pin.
    • The Arduino pin reads LOW when the button is pressed and HIGH when the button is not pressed.

    Why Use a Pull-Up or Pull-Down Resistor?

    Without a resistor, the Arduino pin may be in an unstable state when the button is not pressed, causing inaccurate readings. Using a pull-up or pull-down resistor guarantees a stable state.

    Recommended Configuration:

    For beginners, we recommend using a built-in pull-up resistor on the Arduino pin. This simplifies the wiring and eliminates the need for an external resistor. You can configure this in your MicroPython code using button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP).

    Remember: Using a pull-up or pull-down resistor is essential for reliable button readings on an Arduino.

Wiring Diagram

  • How to connect Arduino to breadboard-mount button
The wiring diagram between Arduino MicroPython Button

This image is created using Fritzing. Click to enlarge image

  • How to connect Arduino to a panel-mounted button
The wiring diagram between Arduino MicroPython two-pin push button

This image is created using Fritzing. Click to enlarge image

How To Program For Button

  • Sets up an Arduino pin to work as an input with a built-in pull-up resistor, using the pinMode() function.
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP)
  • Read the Arduino pin state.
button_state = button.value()

※ NOTE THAT:

There are two common scenarios:

  • First: If the input is HIGH, perform an action. If the input is LOW, perform the opposite action.
  • Second: If the input changes from LOW to HIGH (or from HIGH to LOW), perform an action.

Depending on what we want to achieve, we choose one of these methods. For example, when using a button to control an LED:

  • If we want the LED to turn ON when the button is pressed and turn OFF when released, we should use the first case.
  • If we want the LED to toggle ON and OFF each time the button is pressed, we should choose the second case.

Arduino MicroPython Code - Reading the state of button

from machine import Pin import time BUTTON_PIN = 'D2' # The Arduino Giga WiFi pin D2 connected to the button # Set up the button pin. The Pin.PULL_UP enables the internal pull-up resistor. button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP) while True: # Read the state of the button button_state = button.value() # Print the button's state print(button_state) # Wait for half a second before reading the button again time.sleep(0.5)

Detailed Instructions

Here’s instructions on how to run the above MicroPython code on Arduino with Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
  • Follow the provided diagram to connect the button to the Arduino.
  • Use a USB cable to connect the Arduino board to your computer.
  • Open Thonny IDE and go to Tools Options.
  • Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
  • Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
  • Save the MicroPython code to your Arduino by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5) to execute the code.
  • Press the green Run button or F5 to execute your script.
  • Check out the message in the Shell at the bottom of Thonny IDE.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot 1 1 1 0 0 0 0 0 1 1 1 1
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

1 indicates unpressed, 0 indicates pressed.

Code Explanation

You can see the explanation in the comments part of the Arduino MicroPython code that was given before.

Arduino MicroPython Code - Detecting the button's press and release event

Let's change the code so it can recognize when buttons are pressed and released

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-button """ from machine import Pin import time BUTTON_PIN = 'D2' # The Arduino Giga WiFi pin D2 connected to the button # Setup the button pin. The Pin.PULL_UP enables the internal pull-up resistor. button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP) # Initialize the previous state of the button prev_button_state = 1 # Assuming the button is unpressed initially while True: # Read the current state of the button button_state = button.value() # Check if button was released if prev_button_state == 0 and button_state == 1: print("The button is released") # Check if button was pressed if prev_button_state == 1 and button_state == 0: print("The button is pressed") # Save the current state as the previous state for the next loop iteration prev_button_state = button_state

Detailed Instructions

  • Copy the code above and insert it into Thonny IDE.
  • Click the green Run button or press F5 to start the script.
  • Click the button and then release it.
  • Look at the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The button is pressed The button is released
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

※ NOTE THAT:

Occasionally, if you press a button once, it might display multiple presses in Thonny's Shell. This frequent problem is known as chattering phenomenon, or bouncing. You can learn more and find a solution in the Arduino MicroPython Button Debounce tutorial.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!