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
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
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.
Pinout
The PCB-mount buttons usually have four pins.
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.
The panel-mount buttons usually have two pins.
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.
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
This image is created using Fritzing. Click to enlarge image
- How to connect Arduino to a panel-mounted 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.
- Read the Arduino pin state.
※ 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
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.
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
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.
※ 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
Learn More
※ 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!