Get started with MicroPython Example: code a request help button
Digi XBee® 3 Cellular LTE Cat 1 AT&T Smart Modem User Guide
40
On the board you see DIO0 written below SW2, to the left of the button. This represents the pin that
the button is connected to.
In MicroPython, you will create a pin object for the pin that is connected to the SW2 button. When you
create the pin object, the DIO0 pin is called D0 for short.
The loop continuously checks the value on that pin and once it goes to 0 (meaning the button has been
pressed) a print() call prints the message Button pressed! to the screen.
At the MicroPython >>> prompt, copy the following code and enter it into MicroPython using paste
mode (Ctrl+E), right-click in the Terminal, select Paste to paste the copied code, and press Ctrl+D to
run the code.
# Import the Pin module from machine, for simpler syntax.
from machine import Pin
# Create a pin object for the pin that the button "SW2" is connected to.
dio0 = Pin("D0", Pin.IN, Pin.PULL_UP)
# Give feedback to inform user a button press is needed.
print("Waiting for SW2 press...")
# Create a WHILE loop that checks for a button press.
while (True):
if (dio0.value() == 0): # Once pressed.
print("Button pressed!") # Print message once pressed.
break # Exit the WHILE loop.
# When you press SW2, you should see "Button pressed!" printed to the
screen.
# You have successfully performed an action in response to a button press!
Note You can easily copy and paste code from the online version of this guide. Use caution with the
PDF version, as it may not maintain essential indentations.
Note If you have problems pasting the code, see Syntax error at line 1. For SMS failures, see Error
Failed to send SMS.