Issue with Unicorn HAT Mini

Hi there,
I could do with some help/advice regarding my Unicorn hat mini.
This is my first ever project, so sorry if I am not up to speed with terminology, etc…

I have attached the unicorn hat mini to the raspberry pi and have run through the getting started page for the unicorn hat:

However, when I go to run an example in the terminal nothing happens. And when running the code in the python IDE I get the following error message:
‘Can’t open /dev/mem: Permission denied’

I’m really not sure what I have done wrong here, and any suggestions or help would be much appreciated.

That’s the wrong guide by the way. It’s for the Unicorn Hat not the Mini.
Try running
sudo pip install unicornhatmini
and or
sudo pip3 install unicornhatmini
Then see if one of these examples will work for you.

Tried doing this but get an issue with unicornhatmini = UnicornHATMini()
Output states that no such file or directory

The whole code is:

    import time
import math
from colorsys import hsv_to_rgb
from unicornhatmini import UnicornHATMini

print("""Unicorn HAT Mini:
Displays a concentric rainbow that moves around the Unicorn HAT Mini display.
Press Ctrl+C to exit!

unicornhatmini = UnicornHATMini()
width, height = unicornhatmini.get_shape()

step = 0

while True:
    step += 1

    for x in range(0, width):
        for y in range(0, height):
            dx = (math.sin(step / width + 20) * width) + height
            dy = (math.cos(step / height) * height) + height
            sc = (math.cos(step / height) * height) + width

            hue = math.sqrt(math.pow(x - dx, 2) + math.pow(y - dy, 2)) / sc
            r, g, b = [int(c * 255) for c in hsv_to_rgb(hue, 1, 1)]

            unicornhatmini.set_pixel(x, y, r, g, b)
    time.sleep(1.0 / 60)

And the error is:
Traceback (most recent call last):
File “/home/pi/Desktop/”, line 11, in
unicornhatmini = UnicornHATMini()
File “/usr/local/lib/python3.7/dist-packages/unicornhatmini/”, line 45, in init
self.left_matrix = (spidev.SpiDev(0, 0), 8, 0)
FileNotFoundError: [Errno 2] No such file or directory

Make sure you have SPI enabled under Interfaces in Raspberry Pi Configuration settings.

1 Like

I see my last post is marked as the solution, and liked. =)
That error message IMHO is very confusing, until somebody clues you into what it really means. ;) Then you tend to remember what it really means when you see it again. You’ll get a very similar message if i2c is disabled but should be enabled. ;)
Anyway now you can have some fun.
I have some python code for the mini you might enjoy trying.
It displays the Day, Date, and Time in a scrolling message.
Pressing the B button makes it brighter and pressing A makes it dimmer.
Pressing Y changes the color of the text, and pressing X does a safe shutdown.
I recommend trying all the color options, one of them is pretty cool IMHO.

#!/usr/bin/env python3
import sys
import os
import time, datetime
import RPi.GPIO as GPIO

from colorsys import hsv_to_rgb

from PIL import Image, ImageDraw, ImageFont
from unicornhatmini import UnicornHATMini

unicornhatmini = UnicornHATMini()

GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)  # A
GPIO.setup(6, GPIO.IN, pull_up_down = GPIO.PUD_UP)  # B
GPIO.setup(16, GPIO.IN, pull_up_down = GPIO.PUD_UP) # X
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP) # Y

X = 0
Y = 0
B = 0.3

def Dim(channel):  
    global B
    B = B - 0.1

    if B <= 0.1:
        B = 0.1


def Bright(channel):  
    global B
    B = B + 0.1

    if B >= 1.0:
        B = 1.0


def Shutdown(channel):  
    global X
    X = 1

def color(channel):  
    global Y
    Y = Y + 1
GPIO.add_event_detect(5, GPIO.FALLING, callback = Dim, bouncetime = 2000)
GPIO.add_event_detect(6, GPIO.FALLING, callback = Bright, bouncetime = 2000)
GPIO.add_event_detect(16, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)
GPIO.add_event_detect(24, GPIO.FALLING, callback = color, bouncetime = 2000)

rotation = 0
if len(sys.argv) > 1:
        rotation = int(sys.argv[1])
    except ValueError:
        print("Usage: {} <rotation>".format(sys.argv[0]))

display_width, display_height = unicornhatmini.get_shape()


font = ImageFont.truetype("5x7.ttf", 8)

offset_x = 0

while True:

    if Y > 5:
        Y = 0

    if Y == 0:
        r, g, b = (0, 255, 255)   # Aqua
    elif Y == 1:
        r, g, b = (255, 0, 0)     # Red 
    elif Y == 2:
        r, g, b = (0, 205, 0)     # Green
    elif Y == 3:
        r, g, b = (0, 0, 255)     # Blue  
    elif Y == 4:
        r, g, b = (255, 140, 0)   # Orange

    if offset_x == 0:
        text = time.strftime("%A %B %-d %-I:%M %p")
        text_width, text_height = font.getsize(text)
        image ='P', (text_width + display_width + display_width, display_height), 0)
        draw = ImageDraw.Draw(image)
        draw.text((display_width, -1), text, font=font, fill=255)

    if Y == 5:
        for y in range(display_height):
            for x in range(display_width):
                hue = (time.time() / 10.0) + (x / float(display_width * 2))
                r, g, b = [int(c * 255) for c in hsv_to_rgb(hue, 1.0, 1.0)]
                if image.getpixel((x + offset_x, y)) == 255:
                    unicornhatmini.set_pixel(x, y, r, g, b)
                    unicornhatmini.set_pixel(x, y, 0, 0, 0)
    if Y < 5:           

        for y in range(display_height):
            for x in range(display_width):
                if image.getpixel((x + offset_x, y)) == 255:
                    unicornhatmini.set_pixel(x, y, r, g, b)
                    unicornhatmini.set_pixel(x, y, 0, 0, 0)

    offset_x += 1
    if offset_x + display_width > image.size[0]:
        offset_x = 0

    if X == 1:
        unicornhatmini.set_all(0, 0, 0)
        os.system("sudo shutdown now -P")

1 Like

Oh that looks awesome, thank you so much for your help! :-)

A lot of that code is from the examples. The color shift stuff is from the example.
Getting all those functions working took a little work though. The shutdown via the X button was one of the first things I wanted as I wanted to run mine headless with no keyboard or monitor attached.
You can show the temperature, humidity, and barometric pressure on it too if you add a BME680 or BME280 etc. I have working code for that.

1 Like

I will have to have a mess about with it, I have some python knowledge so I’m hoping to pick some more stuff up through doing :-)

That’s how I learned. All my Python knowledge is from just sitting down and doing / trying.
It can be frustrating but if you keep at it it can be very rewarding.

1 Like