Home Code A look at the unicorn pHat

A look at the unicorn pHat

by shedboy71

The unicorn pHat is a hat which althoough designed for the Pi Zero also works on other Raspberry Pi’s and has 32 RGB LEDs. There is a python library available for it and also a couple of C examples as well in the github repository

Note: The Unicorn pHAT uses PWM so you can’t use the audio jack

To get the HAT set up and ready to go you can use the one-line product installer, open a terminal window and type the following

curl -sS https://get.pimoroni.com/unicornhat | bash

This should install to pimoroni -> unicornhat

You can look at the examples installed, these are also available in https://github.com/pimoroni/unicorn-hat/tree/master/examples. At a command prompt you would type in sudo python simple.py

Lets face it there is no fun in just running pre-written examples so lets have a look at a couple of easy ones

Code

The first one lights an led in the top left corner red and one in the bottom right cornere blue

import unicornhat as uh

uh.set_layout(uh.PHAT)
uh.brightness(0.5)

while True:
 #top left led
 uh.set_pixel(0, 0, 0, 0, 255)
 #top left led
 uh.set_pixel(7, 3, 255, 0, 0)
 uh.show()
 
 
This one lights all of the leds red
import unicornhat as uh

uh.set_layout(uh.PHAT)
uh.brightness(0.5)

while True:
 for x in range(8):
 for y in range(4):
 uh.set_pixel(x, y, 255, 0, 0)
 uh.show()
 
 
This example flashes all of the leds on and off
import unicornhat as uh
import time

uh.set_layout(uh.PHAT)
uh.brightness(0.2)

while True:
 for x in range(8):
 for y in range(4):
 uh.set_pixel(x, y, 0, 0, 255)
 uh.show()
 time.sleep(0.25)
 uh.clear()
 uh.show()
 time.sleep(0.25)

A nice little hat that will set you back about £10 – buy it from http://shop.pimoroni.com/products/unicorn-phat and support a company making good products for the Raspberry PI

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More