# Display Hat Mini - LED logic inverted?

**URL:** <https://forums.pimoroni.com/t/display-hat-mini-led-logic-inverted/20523>\
**Category:** Support\
**Created:** [October 15, 2022, 5:51pm UTC](https://forums.pimoroni.com/t/display-hat-mini-led-logic-inverted/20523 "2022-10-15T17:51:55Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![DarrenHill](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/darrenhill/32/771_2.png) [@DarrenHill](https://forums.pimoroni.com/u/DarrenHill)\
**Post date:** [October 15, 2022, 5:51pm UTC](https://forums.pimoroni.com/t/display-hat-mini-led-logic-inverted/20523/1 "2022-10-15T17:51:55Z")

</div>

I’m just having a play with the buttons and LED on my display hat mini, and I’ve noticed something weird - under gpiozero the LED logic is inverted?!?!

This is the simple code I’m using to test it:

```auto
from gpiozero import LED, Button
from signal import pause

button_a = Button(5)
button_b = Button(6)
button_x = Button(16)
button_y = Button(24)

led_r = LED(17)
led_g = LED(27)
led_b = LED(22)

led_r.on()
led_g.on()
led_b.on()

button_a.when_pressed = led_r.on
button_b.when_released = led_r.off

button_x.when_pressed = led_g.on
button_y.when_released = led_g.off

pause()

```

The three led\_x.on() functions turn the LEDs off, and the button presses also work backwards (button A actually turns the red LED segment off, and B turns it on - same for X/Y and the green LED segment).

Can someone who has one of these confirm if this is general, or if I’ve just got a weird HAT?

Note that all the pin assignments etc come from pinout.xyz, as none of the examples in either Python library actually do anything with the LED…

---

<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:** [October 15, 2022, 7:04pm UTC](https://forums.pimoroni.com/t/display-hat-mini-led-logic-inverted/20523/2 "2022-10-15T19:04:18Z")

</div>

A low / ground turns an led on, keep that in mind.

> **[display\_mini\_hat\_schematic.pdf](https://cdn.shopify.com/s/files/1/0174/1800/files/display_mini_hat_schematic.pdf?v=1635342066)**
>
> 33.29 KB

---

<div class="post-metadata">

**Author:** ![DarrenHill](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/darrenhill/32/771_2.png) [@DarrenHill](https://forums.pimoroni.com/u/DarrenHill)\
**Post date:** [October 15, 2022, 7:30pm UTC](https://forums.pimoroni.com/t/display-hat-mini-led-logic-inverted/20523/3 "2022-10-15T19:30:42Z")

</div>

Which would explain it, just seems rather weird.

But I guess it’s not really designed to play with gpiozero.  
Just found [some other code on the net](https://github.com/ClemensAtElektor/rpi_weather_display) with more details in it on how to use the LED and buttons within the displayhatmini module rather than the ST7789 one.

---

<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:** [October 15, 2022, 11:30pm UTC](https://forums.pimoroni.com/t/display-hat-mini-led-logic-inverted/20523/4 "2022-10-15T23:30:31Z")

</div>

Some of those RGB LEDs are common cathode and some are common anode. That affects how they work. The Pimoroni code takes that into account.  
Can `led_r.on()` be changed to led\_r.on(lo) or led\_r.on(0)?

---

<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:** [October 15, 2022, 11:36pm UTC](https://forums.pimoroni.com/t/display-hat-mini-led-logic-inverted/20523/5 "2022-10-15T23:36:38Z")

</div>

Did you see these examples?  
[pimoroni-pico/micropython/examples/pico\_display at main · pimoroni/pimoroni-pico (github.com)](https://github.com/pimoroni/pimoroni-pico/tree/main/micropython/examples/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:** [October 15, 2022, 11:51pm UTC](https://forums.pimoroni.com/t/display-hat-mini-led-logic-inverted/20523/6 "2022-10-15T23:51:06Z")

</div>

With the Pimoroni uf2 etc you can do stuff like this.

```auto
from pimoroni import Button
button_a = Button(7, invert=False)
button_b = Button(8, invert=False)
button_c = Button(9, invert=False)
button_up = Button(22, invert=False)
button_down = Button(6, invert=False)

```

and

```auto
from pimoroni import RGBLED
led = RGBLED(6, 7, 8)
led.set_rgb(0, 0, 0) 

```

It’s what I’m doing on Tufty and Display Pack.

---

<div class="post-metadata">

**Author:** ![DarrenHill](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/darrenhill/32/771_2.png) [@DarrenHill](https://forums.pimoroni.com/u/DarrenHill)\
**Post date:** [October 16, 2022, 7:03am UTC](https://forums.pimoroni.com/t/display-hat-mini-led-logic-inverted/20523/7 "2022-10-16T07:03:04Z")

</div>

No I didn’t. I’d have been a bit wary of them anyway as they’re on Pico rather than Pi, plus I’d not sure there’s suitable stuff in the display hat module to cover that.

This is fundamentally the problem, the documentation for the display hat is surprisingly sparse compared to other boards available like the Unicorn hats.

To save a second post, just to be clear, this is on a Pi (zero in this case) rather than a pico, so there is no uf2 involved. I’ll have a play to see what is actually in the display hat module, but I can’t find much documentation for it anywhere to detail what commands are available.

The issue I think is what you mentioned in the first posts - that they turn on when grounded/low, which is presumably the opposite to what gpiozero is expecting, hence the inversion.

It’s not really a major issue or anything that’s not simple to work around, but it’s just a bit weird.

---

<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:** [October 16, 2022, 11:24am UTC](https://forums.pimoroni.com/t/display-hat-mini-led-logic-inverted/20523/8 "2022-10-16T11:24:48Z")

</div>

Ops sorry, confusion on my end, I mixed up what display you were using.  
Not sure why I was thinking Pico instead of Pi.  
Let me take a minute to rethink things and get back to you with some better info etc.

---

<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:** [October 16, 2022, 11:33am UTC](https://forums.pimoroni.com/t/display-hat-mini-led-logic-inverted/20523/9 "2022-10-16T11:33:06Z")

</div>

I’m using RPi.GPIO for button press etc. As an example, my shutdown code is as follows.

```auto
#!/usr/bin/python3

import os
import time, datetime

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)  
GPIO.setwarnings(False)
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)

def Shutdown(channel):  
    os.system("sudo shutdown now -P")
    time.sleep(30)

GPIO.add_event_detect(5, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)

```

---

<div class="post-metadata">

**Author:** ![DarrenHill](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/darrenhill/32/771_2.png) [@DarrenHill](https://forums.pimoroni.com/u/DarrenHill)\
**Post date:** [October 16, 2022, 4:53pm UTC](https://forums.pimoroni.com/t/display-hat-mini-led-logic-inverted/20523/10 "2022-10-16T16:53:06Z")

</div>

No worries, indeed thanks for the support (as ever).

Looking more deeply into the [actual module](https://github.com/pimoroni/displayhatmini-python/tree/main/library/displayhatmini), in there I’ve found the built-in ones for the buttons and the LED.

There is:

- _set\_led(self, r=0, g=0, b=0):_ with ranges from 0.0-1.0 for each element
- _on\_button\_pressed(self, callback):_
- _read\_button(self, pin):_
- _set\_backlight(self, value):_
- _display(self):_

That last one I need to look into more, as currently I’m using the ST7789 module for that and the displayhatmini one for the LED and buttons, but I think I can do everything with just the displayhatmini one.

I did some quick testing with the first two (_set\_led_ and _on\_button\_press_ and so far it seems to be working ok.

Just a little disappointing to have to go digging into the code to get the available commands rather than them being documented somewhere. Not up to the usual high standards we’ve come to expect from the team.

---

<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:** [October 16, 2022, 7:12pm UTC](https://forums.pimoroni.com/t/display-hat-mini-led-logic-inverted/20523/11 "2022-10-16T19:12:34Z")

</div>

I’ve had to go that route once or twice myself. Digging into the files for info.  
It’s nice when there is a Function Reference. I get the feeling they (Pimoroni) are a bit understaffed?  
That being said, I have always got a good support when I did a “contact us” to the support team.  
[Contact Us for Raspberry Pi Technical Support - Pimoroni](https://shop.pimoroni.com/pages/contact-us)

I better own up and say I don’t own one of those displays. I do have several of the Pico based ones on the go though. And many many breakout garden SPI displays I use with Pi’s and Pico. I am in a way partially responsible for the move to Pico Graphics. I got greedy and wanted to run two Display Packs on a Pico. That nudged Phil Howard into doing a redo, that he was already contemplating.

---

<div class="post-metadata">

**Author:** ![DarrenHill](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/darrenhill/32/771_2.png) [@DarrenHill](https://forums.pimoroni.com/u/DarrenHill)\
**Post date:** [October 16, 2022, 8:34pm UTC](https://forums.pimoroni.com/t/display-hat-mini-led-logic-inverted/20523/12 "2022-10-16T20:34:14Z")

</div>

I’ve had mine sat in a drawer for a while, until I found both time and some idea of what to do with it.

Got a spare Pi Zero though recently (amazing what happens when you visit the store in Cambridge!) so brought it into play. Thought I’d have some fun on a project to make a clock/radio controller/news headline/weather display doodah.

Clock is already up and running, and I’ve already got the infrastructure in place for the the radio bit (already do it via Zigbee and MQTT with Home Assistant, so easy to tap into that) and the other bits are OK. Just need to choose suitable APIs and tap into them…

Not really played with Pico, aside from making a clock from one with a unicorn pack to fill a need in my lounge when a digi-box got replaced and we lost a clock display there.

---

<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:** [October 16, 2022, 11:24pm UTC](https://forums.pimoroni.com/t/display-hat-mini-led-logic-inverted/20523/13 "2022-10-16T23:24:18Z")

</div>

Lots of weather doodahs here, Pi and Pico based. All show real time values from attached sensors. I will be delving into MQTT at some point in the near future.

---

<div class="post-metadata">

**Author:** ![DarrenHill](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/darrenhill/32/771_2.png) [@DarrenHill](https://forums.pimoroni.com/u/DarrenHill)\
**Post date:** [October 22, 2022, 8:27am UTC](https://forums.pimoroni.com/t/display-hat-mini-led-logic-inverted/20523/14 "2022-10-22T08:27:29Z")

</div>

MQTT is very easy and very powerful. To be honest I’m a little surprised that it’s passed you by so far, given your obvious skills and enthusiasm for these things.

Currently I don’t have local sensors (it’s on the to-do list), so far it’s just playing with cloud services and APIs.

---

<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:** [October 22, 2022, 10:39am UTC](https://forums.pimoroni.com/t/display-hat-mini-led-logic-inverted/20523/15 "2022-10-22T10:39:50Z")

</div>

The first thing I do is to have a look at the schematic. That tells you how the wiring is and how the software has to be configured.

In general, I tend to keep away from special (breakout-level) libraries - they often work around specific hardware features (like these described above) and that results in non-portable code. You start using one breakout and then switch to another one with a slightly different setup, and now you start over again. If you stick to basic hardware-level libs (like gpiozero, Rpi.gpio, st7789) all the special setups for a board are transparent in your own code and therefore much easier to port.
