Using Unicorn HAT in IDLE

IDLE is the go-to editor for Python on the Raspberry Pi, but it has a couple of gotchas that you must remember when trying to use most Python libraries.

Before we begin

Before we start, you should have Unicorn Hat set up, tested and ready to go. If you need to know how to do this, read our getting started with Unicorn Hat guide.

Starting IDLE

Remember to SUDO

sudo ( pronounced 'Sue do' ) is the Simon Says of the Linux world. It instructs your Raspberry Pi to run a particular command with special permissions that grant it access to things that you might not normally be able to access.

Unicorn HAT does some funky things in order to work, so it needs you to run IDLE as a 'super user' ( also known as a root user ). It's not as difficult as it sounds...

LXTerminal

IDLE is a graphical editor, so you need to be running your Pi in desktop mode to use it. Once you're in desktop mode you have to get back to a terminal ( or command prompt ) in order to run IDLE as root. To do this we use LXTerminal, it's a nice little windowed terminal that will give you a portal into the powerful underpinnings of Linux, right on your shiny graphical desktop.

If an LXTerminal icon is visible on your desktop, just double click it to start. Otherwise, click the "Menu" button, mouse over "Accessories" and finally click the "Terminal" option.

Once you've started LXTerminal type the following to start IDLE as root:

sudo idle

This will let you use all manner of libraries for add-ons like Unicorn HAT, Explorer HAT, PiGlow and more.

Lighting some Pixels

First you need to import the Unicorn HAT library, do this by adding the line:

import unicornhat

Then you can set one or more pixels to colours of your choice:

unicornhat.set_pixel(0,0,255,0,0)

The parameters in set_pixel are X position, Y position, Red Amount, Blue Amount and Green Amount. The X and Y positions are numbers between 0 and 7 which indicate one of the 8 rows/columns of Unicorn HAT.

The colour amounts are values between 0 and 255 which indicate how much of that colour should be displayed on that pixel. You can mix colours, for example 255 red and 255 blue = purple.

This should set the top left pixel to Red, but it wont show up yet. To make Unicorn HAT dislay your changes, you must always call:

unicornhat.show()

Shopping basket

Need something for this project? You can use the links below to add products to your Pimoroni Shop basket for easy checkout.

Unicorn HAT
£24.00
Want to checkout or change something? Click here to view your cart.

Tutorial
Beginner
Unicorn HAT, Raspberry Pi, Python, LEDs

Phil Howard

phil@pimoroni.com
@gadgetoid
Phil is Pimoroni's software guru, instantly recognisable by his somewhat pirate-themed moustache growing attempts. Usually found buried neck deep in Python libraries, he's also been known to escape on occasion and turn out crazy new products. If you need a helping hand, he's a prolific Twitter user and rampages around the forums like a T-Rex. Ask him if you need help with Pimoroni's software libraries, or Propeller HAT.