# Pico Display Buttons w/ CircuitPython?

**URL:** <https://forums.pimoroni.com/t/pico-display-buttons-w-circuitpython/21164>\
**Category:** Support\
**Created:** [January 8, 2023, 7:07pm UTC](https://forums.pimoroni.com/t/pico-display-buttons-w-circuitpython/21164 "2023-01-08T19:07:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![KenL](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@KenL](https://forums.pimoroni.com/u/KenL)\
**Post date:** [January 8, 2023, 7:07pm UTC](https://forums.pimoroni.com/t/pico-display-buttons-w-circuitpython/21164/1 "2023-01-08T19:07:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alphanumeric](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/alphanumeric/32/2729_2.png) [@alphanumeric](https://forums.pimoroni.com/u/alphanumeric)\
**Post date:** [January 8, 2023, 7:28pm UTC](https://forums.pimoroni.com/t/pico-display-buttons-w-circuitpython/21164/2 "2023-01-08T19:28:24Z")

</div>

Maybe poke around here, if you haven’t already.  
[CircuitPython](https://circuitpython.org/)

Edit: That kind of leads you here.  
[Welcome To CircuitPython | Welcome to CircuitPython! | Adafruit Learning System](https://learn.adafruit.com/welcome-to-circuitpython)

Striking out here also. =(

---

<div class="post-metadata">

**Author:** ![alphanumeric](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/alphanumeric/32/2729_2.png) [@alphanumeric](https://forums.pimoroni.com/u/alphanumeric)\
**Post date:** [January 8, 2023, 7:39pm UTC](https://forums.pimoroni.com/t/pico-display-buttons-w-circuitpython/21164/3 "2023-01-08T19:39:31Z")

</div>

Most of what I’m finding points to stuff like this.  
[digitalio – Basic digital pin support — Adafruit CircuitPython 8.0.0-beta.6 documentation](https://docs.circuitpython.org/en/latest/shared-bindings/digitalio/)

The `import board` isn’t going to work in your case though? That throws another wrinkle into it.

---

<div class="post-metadata">

**Author:** ![KenL](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@KenL](https://forums.pimoroni.com/u/KenL)\
**Post date:** [January 8, 2023, 9:43pm UTC](https://forums.pimoroni.com/t/pico-display-buttons-w-circuitpython/21164/4 "2023-01-08T21:43:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alphanumeric](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/alphanumeric/32/2729_2.png) [@alphanumeric](https://forums.pimoroni.com/u/alphanumeric)\
**Post date:** [January 9, 2023, 10:00am UTC](https://forums.pimoroni.com/t/pico-display-buttons-w-circuitpython/21164/5 "2023-01-09T10:00:12Z")

</div>

You would need to edit it to blink the Display Packs RGB LED.

Mine is setup as a Power Source Status indicator. Micro Python though, not Circuit Python.

---

<div class="post-metadata">

**Author:** ![jrb1](https://avatars.discourse-cdn.com/v4/letter/j/ba8739/32.png) [@jrb1](https://forums.pimoroni.com/u/jrb1)\
**Post date:** [May 18, 2025, 5:38am UTC](https://forums.pimoroni.com/t/pico-display-buttons-w-circuitpython/21164/6 "2025-05-18T05:38:45Z")

</div>

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](https://www.pishop.us/product/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](https://www.raspberrypi.com/documentation/microcontrollers/pico-series.html#pico-1-family) 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:

```auto
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.

Hope that helps,

- James

---

<div class="post-metadata">

**Author:** ![bablokb](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@bablokb](https://forums.pimoroni.com/u/bablokb)\
**Post date:** [May 18, 2025, 1:49pm UTC](https://forums.pimoroni.com/t/pico-display-buttons-w-circuitpython/21164/7 "2025-05-18T13:49:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jrb1](https://avatars.discourse-cdn.com/v4/letter/j/ba8739/32.png) [@jrb1](https://forums.pimoroni.com/u/jrb1)\
**Post date:** [May 20, 2025, 7:53am UTC](https://forums.pimoroni.com/t/pico-display-buttons-w-circuitpython/21164/8 "2025-05-20T07:53:00Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![bablokb](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@bablokb](https://forums.pimoroni.com/u/bablokb)\
**Post date:** [May 20, 2025, 6:27pm UTC](https://forums.pimoroni.com/t/pico-display-buttons-w-circuitpython/21164/9 "2025-05-20T18:27:48Z")

</div>

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:

```auto
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

```
