empowering creative people

Videos / Set up your Pimoroni Unicorn Hat on Raspberry Pi

The Pimoroni Unicorn HAT lets you add 64 dazzling RGB LEDs to your Raspberry Pi, and makes for a sickeningly colourful display. I'll show you how to get yours set up, and we'll have a go at modifying an example script.

Search Videos

Related Content

Transcript

+

G'day, Welcome back to the lab, today we're going to set up a Pimoroni Unicorn HAT, so you can see this is a pretty cool LED display that you can just plug straight into the top of your Raspberry Pi and we'll have a look at setting that up, running an example, and even modifying the example code a little bit. You can see this is already running this LED sequence that kind of evolves over time this might reset in a second yeah, so you can see that that's a pretty cool sequence that evolves over time and it can either explode or die out and reset and we'll have a look at how that's done let's get started.

So I've already powered down my Raspberry Pi, plugged in the Unicorn HAT, and now restarted the Raspberry Pi, so here we are on the desktop and I've already opened up Pimoroni's own 'Getting Started' tutorial, where we're going to find the installation commands. If we scroll down we have the, under setting up the unicorn hat, we have this let's call it a one liner command; curl fetches data from a server. This is the server that we're fetching data from, and then this pipe character which is the key above the enter key, it shares the backslash key, that passes data from the left to the right. So we're passing whatever we fetch from the server we're passing it to bash because what we're actually fetching from the server is a script, so it's going to automate the installation process for it. So I'm just going to highlight that whole command and copy it and then I already have a terminal opened up, so I'll just paste that. Note that I'm running this from the home directory if you're not in the home directory you can just enter the CD and that will make sure that you change to the home directory. So I'll just paste that command and hit Enter, and this might take a couple of minutes it does a bit of updating, this is just saying "Yeah do you want to run it?" you're writing that's from a script from the internet, I trust that source so I'm just going to say 'Y' for yes and away we go this will take a couple of minutes.

That was running for about two minutes and now we've just been here with the prompt, "Do we want to perform a full install?" this is going to install, I think, the examples for us. So that's going to be something great to have so I am definitely going to say yes to that and away it goes, now it has to download the examples and documentation and we've just been hit with a prompt without audio configuration, that looks fine. I'm just going to say yes to that script as well so this is now fetching another script and it's going to trigger a reboot so I'm going to hit yes to this reboot and I'll see you when my Pi has finished rebooting.

All right that only took about half a minute so we're back on the clean desktop and if I go to my file explorer, just in the home directory, I'll make that slightly bigger, we can see that a directory called Pimoroni has been created because this is a Pimoroni Unicorn HAT, so just opening that we can see there's a unicorn hat directory if you followed my tutorial with the blinkt, then you'll have another blinkt directory here so this is where all the Pimoroni scripts get stored if you have multiple Pimoroni devices. And inside Unicorn hat we have some documentation so that will be the function reference and examples and this projects which are Unicorn paint, I might have to have a look at that later. For now, we're just going to run an example and if you were to open up one of these scripts, just from this interface, let's grab this game of life, if you open up this script in Python, and just try to run it it's not going to work. So let's just have a quick look at that I'll hit f5 to run, the shell pops up, it apparently runs the script and then stops and nothing happened on the bench. So with these example scripts you actually need to run them from the terminal, so let's have a look at doing that, I'll close that shell I don't need this file explorer and I need the terminal. So I need to change into Pimoroni examples what was in that Unicorn Hat, examples and let's have a look at what we've got in here. I'd like to run this game of life script, that's a bit of a classic I think so what I need to run that uses the command sudo - let's move that mouse,

Sudo Python game_of_life.py

and then the name of the scripts and you can use tab completion to quickly punch that out. If we hit enter so that the script actually ran and exited, so I'll rerun it and we'll take a quick look at what happens on the board. Ready set go okay so we get this nice kind of organic, it's called a cellular automata, so this is an old I think it's from the 1970s invented by John Conway as it kind of a life simulation and this is simulating cells I guess perhaps living in a petri dish or even much like larger organisms but that aside you can see that when we run it we get this really cool visual effect and that can reach this kind of point of stability or it can kind of explode and die off it's a really cool simulation and I think this will be a good case study to modify because you can see the the simulation has kind of reached this, this steady state, and I mean that's cool, it's cool that it can even do that, but maybe we can kind of include something so that if a steady state is reached we can restart the script and you know we have this continuously evolving system. So yeah as always let's have a bit of a poke around in the example, I'm just going to hit ctrl C to stop that script and I'll minimize my terminal. So this is the Python script that we were working with I'm just going to make that font a little bit bigger to make it a bit friendlier for those following along at home. Okay I think that's okay, so there's I mean there's heaps going on in the scripts or just will just implement that simple change that I was talking about before. What can we do, we have, ah okay this is all this is all just set up so it looks like down the bottom we're going to, yeah so all of this top part of the script is just essentially function definitions, and down here is where the script is driven. So you can see the meat of the script is this while not life all dead, the script iterates to the next generation it shows that generation and then it does a short delay. So that's fine for what's happened already but if we want to restart the script when steady state is reached, then we're going to have to do something with this. So I think what we'll do is grab we'll grab that as well so we'll grab this whole block, and we're going to indent the whole region so you can do that with the format menu and indent region, or you can use this control and right square bracket. That's what I'm going to do I'm just going to have that highlighted and here control right square bracket, that indents the whole region and now we'll create an infinite loop which is while true, that's created a while true and I guess what we, what we can do to detect steady state is kind of used a frame counter so we can count the number of generations that have evolved, so I can say while true the next line can be just frame and I'll set that equal to zero so what we do now is we set frame equal to zero and we create a new instance of this game of life and then the game of life plays out and this plays out while not life or dead, so it kind of it played that while ever the there are cells on the board that are alive. So I suppose what we want from that is to increment the frame counter and check if the frame counter is too big, how can we do that? Well what we can do is put all of this inside this while not all life dead, we can just say frame equals frame plus one or you could do the plus equals and we can throw in a condition if frame is greater than I dunno, lets just pick a condition, we'll keep it short pick one hundred and fifty, if the frame is greater than 150 and all they're going to do is break from that while loop, and what that will do, that break is going to break from this loop and it's going to return to this top-level infinite loop and all that does is reset the frame count and initializes another game of life.

So lets I mean I think I think that's about ready to run, let's give it a go. I'll pull up the shell again because remember we have to run it from the shell, I'll just clear that to make it a bit easier to look at and let's run this script. Okay so this is some pretty interesting behaviour, you can see there are a few blips of life and then we hit this steady state and I wonder if it's going to restart, a there it goes! So when all the life dies out the simulation immediately restarts and when steady state is reached it kind of lives on for a little while until it hits that frame limit and then it restarts. So that kind of gives us a bit of headroom for these long really cool-looking simulations that can go on for a long time, or even, you might find some that kind of explode out and then come back in and do this repeatedly and it gives you a bit of time to kind of observe that behavior you can see this has this piece of life is just travelling in a straight line and the field wraps around. So as it hits an edge it wraps around and comes out the other edge, so that's really cool.

Now some of you following at home may have noticed that only half of your unicorn HAT is lining up, the other half is just completely blank and this is something that's really easy to fix if you scroll up to the top of your script and in fact when you run the script you get this prompt on the terminal but if you miss that it's a really easy fix to get your whole Unicorn HAT working. All you need to do is at the top of the scripts, you have this set layout command, and all you have to do is is replace this unicorn.auto with unicorn.hat and we should be able to rerun the script I mean mine was working on the auto anyway but I did encounter a case before where it didn't where it didn't run properly and I did have to replace that line. So if we rerun the script and yeah I mean that's working just as well using unicorn.hat rather than unicorn.auto.

So that wraps things up for this brief tutorial, we managed to download and install the Pimoroni unicorn hat examples we opened up an example, did a bit of troubleshooting with maybe if you only had half of the display working and we also kind of turned the dials on one of the examples and got it to behave maybe a little more how we wanted it to. I'll see you next time you

comments