Check the pull-up mode of a pin

You can use the pull() method to check the pull-up mode of a pin. The mode options are:

The following example demonstrates how to check the pull direction of one of the pins on the XBee device and the resultant values on the pin.

  1. Access the MicroPython environment.
  2. Copy the sample code shown below.
# Import the pin module
 
from machine import Pin
 
print("\nChecking the default pull-direction of the AD0/DIO0 pin...")
pinpull = Pin.board.D0.pull()
# This call should return "1", meaning it is set to "PULL_UP".
print("AD0/DIO0 is set to: %d\n" % pinpull)
 
print("The two different values for pull direction can be viewed:")
print("Pin.PULL_UP: %d" % Pin.PULL_UP)  # This should return "1".
print("Pin.PULL_DOWN: %d\n" % Pin.PULL_DOWN)  # This should return "2".
 
# Now, make a pin object for pin AD0/DIO0, set as an input, and pulled
# down to ground (0).
print("Creating a pin object for AD0/DIO0, pulled DOWN...")
d0 = Pin("D0", Pin.IN, Pin.PULL_DOWN)
print("Checking the pull direction of this pin...")
pinpull = d0.pull()
print("Pull direction of AD0/DIO0: %d\n" % pinpull)
# This should return "2", since it was just set to "PULL_DOWN".
print("Checking the value present on the pin...")
pinval = d0.value()
print("Value on AD0/DIO0: %d" % pinval)
print("This should return 0, since the pin is pulled down to ground.\n")
 
print("Changing the pin mode to be PULL_UP.")
d0.pull(Pin.PULL_UP)
print("Checking the pull direction of this pin...")
pinpull = d0.pull()
print("Pull direction of AD0/DIO0: %d" % pinpull)
print("This should return 1, since it was just set to PULL_UP.\n")
print("Checking the value on the pin again...")
pinval = d0.value()
print("Value on AD0/DIO0: %d" % pinval)
print("This should now return 1 now, instead of 0.  This means the pin was")
print("successfully \"pulled up\" to Vdd, or a logic 1.\n")
 
# Now that DIO0 is pulled up, we can examine how a pulled-up input works.
# Holding down the button "SW2"/"DIO0", check the value on the pin again.
print("Now we can examine how a pulled-up pin acts when connected to ground.")
_ = input("Press and hold SW2 on the XBIB board, then press Enter.")
pinval = d0.value()
print("\nValue on AD0/DIO0: %d" % pinval)
print("The value should now be 0.  This is because SW2 connected the pin to")
print("ground, causing current to flow through the pull-up resistor, which")
print("dropped the voltage to 0.")
  1. At the MicroPython >>> prompt, type Ctrl+E to enter paste mode. The terminal displays paste mode; Ctrl-C to cancel, Ctrl-D to finish.

  2. At the MicroPython >>> prompt, right-click and select the Paste option.
  3. Once pasted, the code should execute immediately. You should see output showing the different values generated by the pull and value commands.