empowering creative people

Videos / Raspberry Pi Workshop - Chapter 2 - Using HATs and the Sense HAT Emulator

The full Raspberry Pi Workshop in step-by-step format can be found here http://coreelec.io/piworkshop All the examples for this section came straight out of the Sense Hat Emulator.

Search Videos

Related Content



In this section I’ll be talking about HATS  - HAT which means Hardware Attached On Top - describes a hardware module that you can plug on top of your Raspberry Pi, connecting via the GPIO. This makes it quick and easy to connect your Pi to different pieces of  hardware to experiment with.

For this section I’ll be demonstrating the use of a Sense HAT and having a bit of a play around with that. If you don’t have a Sense HAT I’ll also demonstrate using a pre-installed emulator that was packaged with the latest release of Rasbian so you can have a play with the functionality of the Sense Hat but in a virtual environment.

Coming over onto the bench, I have a Sense HAT plugged into my Raspberry Pi and as you can see it’s running this rather dazzling rainbow effect across the LED matrix. This Sense HAT is plugged into the top and it’s just been supported with some mounting hardware through the M3 screw holes. Maybe if I put some paper towel over this you’ll be able to see the colours a little better with the white balance of the camera. So, this Sense HAT has built on to it, of course this huge LED matrix - red green yellow blue, a IMU which is for measuring movements. So it has an accelerometer, a gyroscope and a magnetometer for measuring the magnetic field of the earth for instance. This is all bundled into a package that is called an IMU or Inertia Measurement Unit. It also has humidity temperature and pressure sensing on board so you could, for instance measure altitude or use it as a small weather station and you could even display data on the LED matrix and perhaps color code it - that would be pretty neat.  But let’s jump over on to the desk top and see if we can have a bit of a play with this demo script.

This is the script that is currently running - this is for that rainbow effect and of course we’ve got our imports at the top, we’re importing something called Sense HAT, we’ve got the table of  hues - I guess our colors, that’s the initial colors that the display starts off at. We’re initialising the hat, we’ve got some function called scale, it looks like that’s going to control our brightness because thats using the number 255 which is as high as an 8 BIT number can count. So that looks like its going to be able to change our brightness and I might change that to 100 just now and have a look at what happens. Ah yeah so back on the bench the display has dimmed significantly and I’m not sure how well that’s coming up on camera and maybe it’s better with tissue paper - maybe without but perhaps you can see the colors scrolling a lot better now without the camera being quite so dazzled.

Let’s have another look - so this is the main loop thats running and the very first line is to rotate the hue, so that is whats going to create our scrolling effect, presumably. We’re doing some increment by

small number and then taking the module of one. So lets perhaps speed it up - lets speed it up  a lot, let’s go .05 and have a look. Ah here we go, this is definitely scrolling significantly faster - and again with the tissue paper - just in case - that’s a lot more vivd now.  Ok so that is probably enough for the LED matrix, how about we look at something else.

I also have a demonstration script called plumb line - so if we give this a run and see I have this blue LED effect here and it’s always moving towards which ever side of the Raspberry Pi is towards the ground.  So as I tilt it you can see the line is stretching out towards where the Pi is being tilted. That blue may be a little hard to see as well - maybe it’s good, but lets have a bit of a play with the script and see if we can change a few things.  Again we’ve got some imports including import the sense HAT, we’re setting an origin, I guess that’s where the LED is going to be zero level.  We can see that reading from the accelerometer is quite easy - when we import Sense HAT libraries we’ve been given these methods that make it quite easy to use the peripheries that they provide. So we have this ‘get accelerometer raw’  method attached to the hat object and we’re just putting that in ‘a’ and then we’re calculating hat and we’re drawing a line and here is where the line, I guess, is being calculated - the coordinates of where that line is going to be drawn on to.  You can see that from ‘a’ we’re  extracting the ‘x’ accelerations so we have x and y acceleration for this accelerator. Of course there is a ‘z’ but z acceleration being up and down with the Raspberry Pi isn’t going to be very useful for a plumb line. So we’re just extracting x and y, multiplying them by sum number, that may have something to do with  this comment here about overdrawing and then scaling down.

Ok, so maybe we can change the colour, so in this section where we have the line being drawn - it’s being drawn from the origin to the destination that was calculated. That’s fair enough. But the fill colour for the line is what I’m interested in - I’m going to make this white which means I need 255 for the red value which is the first and 255 for the green value which is second and of course 255 for the blue which is third.  If I rerun this script and go back over to the bench. We can see that first up it’s a lot brighter - there’s a lot more light coming out of it and it is white so we’re using all three colours of LED that are inside that LED package and perhaps that shows up a little better on the camera.

So that’s demonstrated the use of the accelerometer and the LED matrix and also perhaps how we can manipulate some data. Maybe if you don’t have a sense hat we can have a look at some options there so I’m going to quit that script and close these.

Now in the latest bundle of Debian - the Raspian plus pixel release - we have in our programming menu this sense hat emulator which you may have already discovered if you’ve been exploring - well this is pretty neat and what it allows us to do is run code for the sense hat even if we don’t have one. We can still see what effects it would produce and we can still play with the inputs. We can virtually  tilt the sense hat for instance by changing this orientation values or we can change temperature pressure and humidity and of course we can use a small joy stick button on the board as well. That would be useful for say, navigating menus so that’s what this directional pad is for. So if I open up this bar graph  script that’ll give us access to the temperature and humidity which is what is going to be graphed. Now in this line where we have sense hat - that’s what allows us to run the code of course  on the sense hat - but if we change that import from sense hat to sense_emu (not for emu but for emulator!) and save the script. If we run this now rather than the code running and being displayed on the sense hat on the physical hardware, it’s now running within this emulated environment so we can kind of prototype or play around with code here and then once we’re happy with it, or if we’re waiting for our sense hat to arrive in the mail we can prototype our code in the emulated environment and once our sense hat is plugged in we can remove the ‘emu’, put in hat and the code will run on the hardware instead.

But for now I’m going to show you how the emulator works so I’m going to leave that as emulator. So this is three bar graphs, red, green and blue on our LED matrix and it looks like that corresponds to temperature, Pressure and Humidity. If I hover over this temperature slider and just roll my mouse wheel we can see that the red line has now shifted to indicate  a change in temperature. Presumably the same happens with pressure although it seems to happen across a much smaller scale as well so maybe there’s a neat scaling function, in fact there it is there - this is almost identical to the scaling function that we wrote in the last video. This just goes to show that this kind of function is useful for a lot of different reasons. Coming back over we can scroll the humidity slider and we see that humidity bar drop off. Just to push that point again - if I go up to my import line and go from emulator to hat, save and run - then back on the bench we have that very same script running on the set up. Now of course I don’t have as powerful control over my environment here, so I can’t get these sliders to move up and down quite so easily. Maybe if I put my finger over the temperature pressure sensor I guess I’m affecting the humidity maybe with some perspiration on my finger.

That completes our chapter on the basics of Python and the GPIO and we now have at our disposal some useful tools for writing code so that our Raspberry Pi can interact with the world around it. We can drive LED’s - perhaps to indicate status,we can read from buttons for user input and we’ve just seen a glimpse of some more complicated hardware like LED Matrixes and Inertial Measurement Units. Hopefully you’re already getting some ideas about what you could build with such hardware.

I’ll see you in chapter 3!