# Issue with Unicorn HAT Mini

**URL:** <https://forums.pimoroni.com/t/issue-with-unicorn-hat-mini/14382>\
**Category:** Support\
**Created:** [June 28, 2020, 5:48pm UTC](https://forums.pimoroni.com/t/issue-with-unicorn-hat-mini/14382 "2020-06-28T17:48:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lh123](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@lh123](https://forums.pimoroni.com/u/lh123)\
**Post date:** [June 28, 2020, 5:48pm UTC](https://forums.pimoroni.com/t/issue-with-unicorn-hat-mini/14382/1 "2020-06-28T17:48:48Z")

</div>

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: [https://learn.pimoroni.com/tutorial/unicorn-hat/getting-started-with-unicorn-hat](https://learn.pimoroni.com/tutorial/unicorn-hat/getting-started-with-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.

---

<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:** [June 28, 2020, 6:21pm UTC](https://forums.pimoroni.com/t/issue-with-unicorn-hat-mini/14382/2 "2020-06-28T18:21:16Z")

</div>

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.

> **[pimoroni/unicornhatmini-python](https://github.com/pimoroni/unicornhatmini-python/tree/master/examples)**
>
> Python library for the Pimoroni Unicorn HAT Mini RGB LED Display - pimoroni/unicornhatmini-python

---

<div class="post-metadata">

**Author:** ![lh123](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@lh123](https://forums.pimoroni.com/u/lh123)\
**Post date:** [June 28, 2020, 6:50pm UTC](https://forums.pimoroni.com/t/issue-with-unicorn-hat-mini/14382/3 "2020-06-28T18:50:32Z")

</div>

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: rainbow.py
Displays a concentric rainbow that moves around the Unicorn HAT Mini display.
Press Ctrl+C to exit!
""")

unicornhatmini = UnicornHATMini()
unicornhatmini.set_brightness(0.1)
unicornhatmini.set_rotation(0)
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)

    unicornhatmini.show()
    time.sleep(1.0 / 60)

```

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

---

<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:** [June 28, 2020, 7:07pm UTC](https://forums.pimoroni.com/t/issue-with-unicorn-hat-mini/14382/4 "2020-06-28T19:07:19Z")

</div>

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

---

<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:** [June 28, 2020, 9:29pm UTC](https://forums.pimoroni.com/t/issue-with-unicorn-hat-mini/14382/5 "2020-06-28T21:29:38Z")

</div>

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.

```auto
#!/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.setmode(GPIO.BCM)  
GPIO.setwarnings(False)
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

    unicornhatmini.set_brightness(B)

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

    if B >= 1.0:
        B = 1.0

    unicornhatmini.set_brightness(B)  

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:
    try:
        rotation = int(sys.argv[1])
    except ValueError:
        print("Usage: {} <rotation>".format(sys.argv[0]))
        sys.exit(1)

unicornhatmini.set_rotation(rotation)
display_width, display_height = unicornhatmini.get_shape()

unicornhatmini.set_brightness(B)

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 = Image.new('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)
                else:
                    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)
                else:
                    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)
        unicornhatmini.show()
        os.system("sudo shutdown now -P")
        time.sleep(30)

    unicornhatmini.show()
    time.sleep(0.05)

```

---

<div class="post-metadata">

**Author:** ![lh123](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@lh123](https://forums.pimoroni.com/u/lh123)\
**Post date:** [June 29, 2020, 10:08am UTC](https://forums.pimoroni.com/t/issue-with-unicorn-hat-mini/14382/6 "2020-06-29T10:08:11Z")

</div>

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

---

<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:** [June 29, 2020, 11:57am UTC](https://forums.pimoroni.com/t/issue-with-unicorn-hat-mini/14382/7 "2020-06-29T11:57:46Z")

</div>

A lot of that code is from the examples. The color shift stuff is from the text.py 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.

> [@Adjust Python examples](https://forums.pimoroni.com/t/adjust-python-examples/13954/96):
>
> The “and the Temperature is” displays as text. The %s displays what’s after the other % sign out side the brackets. % (T) displays the variable T which is the rounded off temperature. Taken from the BME280 T = bme280.get\_temperature() T = round(T) And the c (more text) to denote degrees Celsius. Have a look at this. This will work with a BME680 etc, with some code changes. #!/usr/bin/env python3 import sys import os import time, datetime import RPi.GPIO as GPIO from PIL import Image, Imag…

---

<div class="post-metadata">

**Author:** ![lh123](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@lh123](https://forums.pimoroni.com/u/lh123)\
**Post date:** [June 29, 2020, 12:18pm UTC](https://forums.pimoroni.com/t/issue-with-unicorn-hat-mini/14382/8 "2020-06-29T12:18:27Z")

</div>

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 :-)

---

<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:** [June 29, 2020, 12:32pm UTC](https://forums.pimoroni.com/t/issue-with-unicorn-hat-mini/14382/9 "2020-06-29T12:32:27Z")

</div>

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.
