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 "Extra" on the XRP Controller board.
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!
The Button on the Button LED module is connected to the IO22 pin of the Pico. This is in parallel with the User Button on the XRP controller board.
Therefore, the button on the module acts the same as the user button. The code in the Button exercise will work with the botton on the module.
Use the same program from the Button exercise to test if the button on the module is working.
The LED on the Button LED module is connected to the IO28 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 IO28 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(28, 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
IO28 pin is connected to the XRP controller board battery voltage sensor. Connecting the Button module to this pin will interfere with reading the battery voltage when the button is pressed. This is not of importance for this exercise.
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(28, 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!")