The XRP controller (Raspberry Pi Pico and other electronics), software (Micropython and XRPlib), and mechanical platform are designed to allow additional components to be added.
In this exercise, we'll add a button and LED module.
The parts needed include:
Button_LED_board_connector (3D-print) - [https://www.thingiverse.com/thing:6669721]
(3) M2x12mm bolts and nuts
Place the Button LED module behind the 3-D printed connector, aligning the (3) mounting holes.
See photo -->
Insert the M2x12mm bolts from the front (LED and Button side) and thread on the nuts.
Connect the Grove end of the Qwiic cable to the Button LED module
Clip the assembled module onto the front rail of the XRP, to the left of the Ultrasonic Distance Sensor
Connect the qwiic end of the cable to the connector marked "QWIIC 0" on the XRP Controller board.
It' s on the uppoer right, just abobe the sparkfun logo.
Be careful to insert the qwiic connector in the right orientation. If it isn't going in, take a closer look and try turning it around. Don't force it or something will break!
Note that the QWIIC 0 connector uses IO pins 4 and 5.
The XRPLib may also use these pins for the IIC bus. In these examples, the code overides the II2 function of these pins.
The button on the Button LED module is connected to the IO4 pin of the Pico.
(This pin is "QWIIC 0" and is intended to be used as a 2nd I2C bus, but it isn't connected to other components on the XRP Controller Board.)
This is not the same pin as the user button on the Pico board
XRP Code Editor Blockly code cannot control pin IO4 but we can use MicroPython to read the button status.
In the XRP Code Editor, click File ... New and select "MICRO PYTHON"
Copy this Python program and paste it into the new program window:
from XRPLib.board import Board
import time
from machine import Pin
from machine import Timer
from XRPLib.defaults import imu
board = Board.get_default_board()
ext_led = Pin(5, Pin.OUT)
ext_button_pin = Pin(4, Pin.IN, Pin.PULL_UP) #note that IO4 may be used for I2C by XRPLib
print("")
print("Press external to turn the LED on; Press user button to turn it off.")
while True:
if board.is_button_pressed():
ext_led.off()
print("User Button!")
time.sleep(0.2) # Debounce delay to prevent multiple triggers per press
print("XRP heading: ", imu.get_heading())
if ext_button_pin.value() == 0: # Button is pressed (connected to GND)
print("External Button!")
ext_led.on()
time.sleep(0.2) # Debounce delay to prevent multiple triggers per press
time.sleep(0.05) # Small delay to reduce CPU usage
Click "RUN"
Is the LED on the Button LED module turning on when you press the external button, and off when you press the user button?
If not, check the Grove / qwiic cable connections
Also, when the user button is pressed, the current heading (magnetic orientation) of the XRP is printed on the console.
(The IMU uses the QWIIC 1 bus, so using the QWIIC 0 pin for the external button isn't affecting the use of the IMU.)
See get_heading() XRP API Reference
The LED on the Button LED module is connected to the IO5 pin of the Pico.
This is not the same pin as the internal LED on the Pico board
XRP Code Editor Blockly code cannot control pin IO5 but we can use MicroPython to control the LED.
In the XRP Code Editor, click File ... New and select "MICRO PYTHON"
Copy this Python program and paste it into the new program window:
import time
from machine import Pin
led = Pin(5, Pin.OUT)
while True:
print('external LED on')
led.value(1) # led on
time.sleep(0.5)
led.value(0) # led off
time.sleep(0.5)
print('external LED off')
Click "RUN"
Is the LED on the Button LED module blinking?
If not, check the Grove / qwiic cable connections
Here is a better way to blink an LED in Micropython using the timer and interrupt functionality:
https://github.com/raspberrypi/pico-micropython-examples/blob/master/blink/blink.py
Added while True ... board.wait_for_button to show that the LED continues to blink while waiting.
(There are other Pico MicroPython example in this Gihub for you to explore.)
from XRPLib.board import Board
import time
from machine import Pin
from machine import Timer
board = Board.get_default_board()
led = Pin(5, Pin.OUT)
tim = Timer()
def tick(timer):
global led
led.toggle()
tim.init(freq=2.5, mode=Timer.PERIODIC, callback=tick)
while True:
board.wait_for_button()
print("Button!")