Arduino MicroPython Button Module
Your Arduino Giga R1 WiFi can do a lot on its own, but sometimes a person has to tell it something: "a player buzzed in", "save a reading now", "pause the logger". One press on a button module is the simplest way to send that message. In this MicroPython guide, you will find out:
What is inside the button module, and why it is safer to feed it 3.3V on the Giga.
How to hook up its VCC, OUT and GND pins to the Arduino Giga with three jumper wires.
How to print the raw value of the OUT pin in the Thonny Shell.
How to turn every press into one "pressed" message and one "released" message, using a short sleep_ms() wait.
How to get the same result with the DIYables-MicroPython-Button library, which never pauses your loop.
https://newbiely.com/images/cover/arduino-button-module-micropython.jpg
Or you can buy the following 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 .
In short, the button module is a push button that brings its own resistor. A small tactile key with a colored cap sits on a little PCB, together with a pull-down resistor that holds the output at 0 while nobody touches it. The key is momentary: it is "on" only while your finger stays on it. DIYables sells the modules in a pack of five, one each in red, yellow, green, blue and white, which is handy when each key needs its own job.
image source: diyables.io
A 3-pin 2.54mm header comes out of the board. Read the labels from one end to the other:
VCC pin: the supply pin. The module accepts 3.3V to 5V DC, but on the Arduino Giga it goes to 3.3V.
OUT pin: the signal pin. Wire it to any digital pin of the Arduino. We use D2.
GND pin: wire it to a GND pin of the Arduino.
What MicroPython sees on OUT:
Key not touched: the resistor pulls OUT down, so value() returns 0.
Key held down: OUT is joined to VCC, so value() returns 1.
So the logic is "1 = pressed", the natural way round. You do not need Pin.PULL_UP here. In fact, turning on the internal pull-up would work against the resistor on the module.
※ NOTE THAT:
When the key is down, the OUT pin carries the same voltage you put on VCC. The I/O pins of the Giga R1 WiFi are 3.3V pins. If VCC were tied to 5V, every press would push 5V into pin D2, which can damage the board. Keep VCC on 3.3V.
One more thing about mechanical keys: the metal contacts bounce for a few milliseconds when they close or open. To a fast board, one push can look like several quick pushes. Examples 2 and 3 below show two ways to fix that.
Three jumper wires are all you need, no breadboard. Pin D2 is the same pin the other button tutorials in this series use, so you can reuse their code later.

This image is created using Fritzing. Click to enlarge image
| Button Module | Arduino Giga R1 WiFi |
| VCC | 3.3V (do not use 5V) |
| OUT | D2 |
| GND | GND |
For the first two examples, the machine module that ships with MicroPython is enough.
button_module = Pin('D2', Pin.IN)
level = button_module.value()
button = Button('D2', Pin.PULL_DOWN)
The first script just shows what the board sees. Five times per second, it reads OUT and prints the value with a short label.
from machine import Pin
import time
BUTTON_MODULE_PIN = 'D2'
button_module = Pin(BUTTON_MODULE_PIN, Pin.IN)
while True:
level = button_module.value()
if level == 1:
print("OUT = 1 -> key is down")
else:
print("OUT = 0 -> key is up")
time.sleep(0.2)
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.
Connect VCC to 3.3V, OUT to D2 and GND to GND, as in the wiring diagram.
Plug the Arduino Giga into your computer with the USB Type-C cable.
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.
Hold the key down for about one second, then let it go, and look at the Shell at the bottom of Thonny.
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot
OUT = 0 -> key is up
OUT = 0 -> key is up
OUT = 1 -> key is down
OUT = 1 -> key is down
OUT = 1 -> key is down
OUT = 1 -> key is down
OUT = 1 -> key is down
OUT = 0 -> key is up
OUT = 0 -> key is up
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡
While your finger stays on the key, the Shell keeps showing 1. If you see only 0, check the OUT wire on D2 and the 3.3V wire. If the value jumps around with nothing pressed, the GND wire is probably loose.
Most projects do not care about the level itself. They want to act once at the moment the key goes down, or once when it comes up. This script remembers the last state. When a new reading is different, it waits 50 ms for the bounce to die out, reads again, and prints a message only if the change is still there.
from machine import Pin
import time
BUTTON_MODULE_PIN = 'D2'
SETTLE_MS = 50
button_module = Pin(BUTTON_MODULE_PIN, Pin.IN)
last_state = button_module.value()
while True:
reading = button_module.value()
if reading != last_state:
time.sleep_ms(SETTLE_MS)
reading = button_module.value()
if reading != last_state:
last_state = reading
if reading == 1:
print("The button is pressed")
else:
print("The button is released")
The wiring stays the same.
Paste the new code into Thonny and save it to the Arduino as main.py, over the old file.
Press Run (F5).
Give the key three short taps.
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot
The button is pressed
The button is released
The button is pressed
The button is released
The button is pressed
The button is released
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡
Three taps, three pairs of lines. There are no extra "pressed" lines from bouncing. The only cost is that time.sleep_ms(50) stops the whole loop while it waits, so nothing else can run during those 50 ms.
The third way hands the debounce job to the DIYables-MicroPython-Button library. It keeps track of time on its own, so button.loop() returns at once and the rest of your loop never gets stuck. You call is_pressed() and is_released() for the events, and get_state() for the steady level after debouncing.
from DIYables_MicroPython_Button import Button
from machine import Pin
BUTTON_MODULE_PIN = 'D2'
button = Button(BUTTON_MODULE_PIN, Pin.PULL_DOWN)
button.set_debounce_time(50)
while True:
button.loop()
if button.is_pressed():
print("The button is pressed | steady state:", button.get_state())
if button.is_released():
print("The button is released | steady state:", button.get_state())
In Thonny IDE, open Tools Manage packages.
Type “DIYables-MicroPython-Button” in the search box and find the Button library created by DIYables.
Click DIYables-MicroPython-Button, then click the Install button.
Paste the code above into the editor and save it to the Arduino as main.py.
Click Run (F5).
Press the key, hold it a moment, and let go. Do it twice.
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot
The button is pressed | steady state: 1
The button is released | steady state: 0
The button is pressed | steady state: 1
The button is released | steady state: 0
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡
Each event shows up exactly once, and the steady state agrees with it. If you want the board to react even less to very short touches, raise the number in set_debounce_time(), for example to 100.
※ NOTE THAT:
If you see "pressed" when you let go and "released" when you push, the library is in pull-up mode. Check that you passed Pin.PULL_DOWN as the second argument.
You can also wire a loose 4-leg push button to the Giga, as in the Arduino MicroPython Button tutorial. Here is what changes when you switch to the module:
The bare button has four legs joined in pairs, and it is easy to plug it in the wrong way round. The module has three clearly labeled pins.
The bare button needs a resistor, or the internal pull-up with Pin.PULL_UP. The module already has its pull-down on board.
With the internal pull-up, a press reads as 0, which feels upside down. The module reads 1 for a press.
The bare button usually needs a breadboard. The module connects straight to the Giga with jumper wires.
The debouncing ideas are the same for both. To go further, see the Button Debounce tutorial, or the Button LED tutorial to switch a light with your key.