Arduino MicroPython Button
This guide shows you how to use a button with an Arduino and MicroPython. You will learn:
How a button works
How to connect the button to Arduino
How to write MicroPython code for Arduino to read the button's state
How to write MicroPython code for Arduino to detect the button's press/release events
Or you can buy the following sensor kits:
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 .
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:
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.
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.
image source: diyables.io
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.
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.
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.
This image is created using Fritzing. Click to enlarge image
This image is created using Fritzing. Click to enlarge image
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP)
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.
from machine import Pin
import time
BUTTON_PIN = 'D2'
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP)
while True:
button_state = button.value()
print(button_state)
time.sleep(0.5)
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.
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.
>>> %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.
You can see the explanation in the comments part of the Arduino MicroPython code that was given before.
Let's change the code so it can recognize when buttons are pressed and released
from machine import Pin
import time
BUTTON_PIN = 'D2'
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP)
prev_button_state = 1
while True:
button_state = button.value()
if prev_button_state == 0 and button_state == 1:
print("The button is released")
if prev_button_state == 1 and button_state == 0:
print("The button is pressed")
prev_button_state = button_state
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.
>>> %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.