I’m just a beginner, so I apologize if this has been answered (I have been searching) or is really obvious.
I’ve got the 1.14" Pico Display Pack and would like to use to use as part of a macro pad for my Mac. I thought I would use CircuitPython since I already know how to integrate with keys and rotary switches.
But I would also like to use the Pico Display’s built-in buttons and RGB LED. Is this possible with CircuitPython? (example code and steps would be greatly appreciated - again I am a novice.). Many thanks in advance.
Thanks for the suggestions. Unfortunately when I run the blinky example from digitalio-Basic digital pin support- page, it just flashes the pi pico’s LED, not the one on the pico display.
Hi,
I needed to do exactly that and I see you didn’t get an answer.
You’ve probably found the adafruit_st7789.mpy module which can be used to draw onto the Display Pack and you can look at the example code in the CircuitPython library so I won’t go into that.
So to get the 4 switches (a/b/x/y) and the LED to work all you have to do is look at the pinout diagram of the display pack here pico display pack For simplicities sake let’s say you need the Y button. We can see that SW_Y is on pin 20 of the display pack. So we just need to use that pin and get CircuitPython to read it as a digitial input.
To do that we have to first identify which pin that is on the Pi Pico which we can see here for the Pico 1 boards Pico 1 boards is GP15. The Display Pack is mounted to the bottom of the Pico so you have to flip the Display Pack’s pin positions left to right to get the Pico’s pin it will be connected to as both those diagrams show the pin layout from the top of the boards.
Now all you need to do is set that pin up for digital read which you can do like this:
from digitalio import DigitalInOut, Direction, Pull
import time
switch = DigitalInOut(board.GP15)
switch.direction = Direction.INPUT
switch.pull = Pull.UP
old_value = switch.value
while True:
time.sleep(0.1)
if switch.value != old_value:
if switch.value:
print("Released")
else:
print("Pressed")
old_value = switch.value
Needs to be a “pull up” input pin because of the way the switch is physically wired (connected to ground when pressed). This is also why switch.value is True for when the switch is not pressed which reads a bit funny in the code.
For the Display Pack’s LED, you need to set the LED_R, LED_G, and LED_B Display Pack pins as a PWM output from the Pico. Looking at the diagrams linked above again you can see those pins correspond to the Pico pins GP7, GP8, and GP9. There are plenty of CircuitPython PWM output examples for that code.
Just a tip: you can use the keypad module of CircuitPython to process multiple keys at once. The keypad-module will scan the keys asynchronously, so you can do other things (e.g. fancy things on your display) without missing a key-press. Every key-press (and release) will be added to an event-queue and when you come around to process things you just scan the queue for new events.
This is very convenient for displays like this with multiple buttons.
And for reference since it didn’t turn out to be that beginner-friendly, here’s how you get the RGB LED working from CircuitPython.
As with the switches first figure out which of the Pico’s pins correspond the to the Display Pack’s RGB pins which turn out to be GP6, GP7, and GP8 for the red, green and blue pin.
Now we can use the standard CircuitPython PWM object to drive a value into the LED. However there are two things to deal with. First the LED is wired so the full duty cycle (65535) is off and zero is fully on. Second we have to deal with the non-linear response the human eye has to the LED being PWM, which is it sees very small changes in the dark but is not sensitive to small changes up at the high intensities of light. We can deal with both of these with a tiny bit of arithmatic
import board
import pwmio
import math
red = pwmio.PWMOut(board.GP6, frequency=50, duty_cycle=65535)
green = pwmio.PWMOut(board.GP7, frequency=50, duty_cycle=65535)
blue = pwmio.PWMOut(board.GP8, frequency=50, duty_cycle=65535)
def dp_led_value(brightness : float) -> int:
"""
Return pwm duty cycle for a brightness value.
The pimoroni LED on the display pack is fully on
when the pwm output is zero, hence the "1 -" below. The
signal also needs to be "gamma corrected" to give linear
percieved brightness steps for a linear increase in brightness
value input. Gamma value of 3 seemed about right.
param: brightness 0.0 for off to 1.0 for fully on
return: pico pwm duty cycle
"""
duty = (1.0 - math.pow(brightness, 3)) * 65535
return int(duty)
def dp_led(r : float, g : float, b : float) -> None:
red.duty_cycle = dp_led_value(r)
green.duty_cycle = dp_led_value(g)
blue.duty_cycle = dp_led_value(b)
dp_led(1, 0.7, 0.2)
the math in dp_led_value() isn’t perfect, for one thing the green is much brighter than the other two LEDs so that should be handled. Also a gamma value of 3 was just picked as it seemed about right.
The nice thing about CircuitPython is that you have hundreds of libraries. In this case the libraries of choice are adafruit_rgbled and adafruit_fancyled:
def blink_led():
import adafruit_rgbled
import adafruit_fancyled.adafruit_fancyled as fancy
COLORS = [
("WHITE", 0xFFFFFF), ("BLACK", 0x000000),
("RED", 0xFF0000), ("LIME", 0x00FF00),
("BLUE", 0x0000FF), ("YELLOW", 0xFFFF00),
("FUCHSIA", 0xFF00FF), ("AQUA", 0x00FFFF),
("MAROON", 0x800000), ("GREEN", 0x008000),
("NAVY", 0x000080), ("GRAY", 0x808080),
("OLIVE", 0x808000), ("TEAL", 0x008080),
("PURPLE", 0x800080), ("SILVER", 0xC0C0C0)]
colors_adj = fancy.gamma_adjust(
[fancy.unpack(color[1]) for color in COLORS],
gamma_value=2.2, brightness=0.25)
with adafruit_rgbled.RGBLED(
board.LED_RED, board.LED_GREEN, board.LED_BLUE, invert_pwm = True) as led:
for index, color in enumerate(colors_adj):
color = color.pack()
print(f"setting LED color to {COLORS[index][0]} {color:#08x}")
led.color = color
time.sleep(1)
print(f"turning LED off")
led.color = 0x0