Digi International Inc XB3C1 Manual Do Utilizador

Página de 146
Get started with MicroPython
Example: code a request help button
Digi XBee3 Cellular LTE Cat 1 Smart Modem User Guide
51
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 (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
and then run it:
# 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
. For SMS failures, see