Google+ Followers

Thursday, September 10, 2015

68. The RasPiO Duino HAT

44,000 page views!!
Here's the previously-mentioned HAT (Hardware Attached on Top) that has been developed by Alex Eames of RasPi.TV and RasP.iO.  This comes as a kit which is easy to solder together.  The aim with this was to make it easy to carry out Arduino programming on the Raspberry Pi.  So the scripts are in the Arduino language and are run on the built-in Atmel ATMEGA328P-PU (ie an Arduino) chip.

Having uploaded the Arduino sketch on the Arduino IDE from the Raspberry Pi, from that point on, the Pi itself does nothing more than supply power to the Arduino, although having said that, you could conceivably have Pi software that interacts with the Arduino, or even does something completely different.

Depending on the software that you write, the hat could be removed and all it would need then would be power to continue running the sketch.  For £14 (delivered) within the UK, it represents great value.

There is an extremely clearly written e-book (Learning Arduino Programming with RasPiO Duino) available HERE.  The book brings you from the very simple Blink sketch right up to using the analog inputs for fading LEDs using pulse-width modulation (PWM) and taking advantage of the eye-brain system's persistence of vision |(POV), previously explained in my post 15 HERE.

The sketch I am demonstrating is almost identical to my ATTiny85 code which I used in that post, which came originally from HERE.

Here's a picture of the RasPiO Duino hat mounted on my Raspberry Pi B+:
You can see that I have made use of the RasPiO Duino's prototyping area (with lots of holes) to make connections to the RGB LED common cathode to GND and the red, green and blue anodes to the Arduino pins 11, 10 and 9, through 330Ω resistors, on the blue mini-breadboard.  Instead of using the mini-breadboard, the RasPiO Duino's prototyping area could have been used instead.

There's also a light dependent resistor (LDR) shown at the bottom centre, connected in series with a 10kΩ resistor to GND at one end, and Arduino pin 3 at the other end.  In the above picture, the RGB LED is hidden under a light diffuser (A draft Guinness can widget). I also keep a 2 inch length of heat shrink over the LDR so that I can see the mood lamp working during the daylight hours.

The idea is of course, to program, through the Raspberry Pi, the Arduino chip, as a night-time mood lamp, cycling through all the colours in turn, provided there is minimal light reaching the LDR.

Here's a video where all is revealed:

and here's the code:

Must say, I do love mood lamps.  Thanks, and thanks Alex.

Wednesday, September 9, 2015

67. The Sense HAT (Hardware Attached on Top)

HATs (Hardware Attached on Top) are the latest thing for the Raspberry Pi.  They are units of circuitry that neatly attach on to the Pi's 40 GPIO pins, which not only give very good mechanical stability, but also allows the relevant pins to be connected.  The last HAT I got was the RasPiO Duino by Alex Eames (see It's a great piece of equipment which came as a kit, but is very easy to solder together.  There will be more about that later!

In the meantime, I'm on the learning curve for the Sense HAT, by the Raspberry Pi Foundation itself. There's a whole story about this, available HERE, so I'll not say much more than that it's the heart of the Astro Pi unit, two of which Tim Peake, UK astronaut, will be taking into space on the International Space Station at the end of this year.  So I just had to get one! (only £24.50 including delivery).

The Sense HAT has a number of goodies as follows:

  • a 3DoF (degrees of freedom) Accelerometer, a 3 DoF Gyroscope and a 3 DoF Magnetometer
  • a Temperature and Barometric Pressure sensor
  • a Relative Humidity and Temperature sensor
  • a 5-button miniature joystick
  • a slot which allows the Raspberry Pi Camera Board to be connected simultaneously
  • an 8 x 8 RGB LED array (nearly 200 LEDs!)
  • an Atmel ATTINY88 micro controller unit (not re-programmable)

With the Python API and lots of documentation and code on GitHub, there's a wealth of stuff to be getting on with.  The mind just boggles with the possibilities of this machine!
Here's mine mounted on my Pi 2B, with my Raspberry Pi Camera Board also connected:
The Sense Hat mounted on a Pi 2 connected to monitor, WiFi, wireless keyboard and mouse, with the RasPiCam on top

The LEDs are actually displaying a multi-coloured display from  It's difficult to get the exposure right with such bright LEDs - they all come out almost white.  There are a number of Python test scripts which can put the various sensors and input/output devices through their paces. Here's a video of my version of the script running:


The video shows a white square outline surrounded by black, which I made on MS Excel and converted into a jpg.  The PiCam "sees" the pattern on my PC monitor, and the software converts it into an 8 x 8 array of pixels, displayed on the 64 RGB LED array, but also on the Pi's monitor, so that I can see what the PiCam sees.

There are a couple of things to note here - firstly the LED array image is not square, but squashed vertically into a rectangle.  This must be because of the viewing angle.  The other thing to note is that it's the top left of the image that is displayed.  This can no doubt be changed in the software. Additionally, the output picture on the Pi's monitor is pulsating.  This seems to happen in camera.start_preview() mode, the zoom appearing to vary with each pulse.  This could be an explanation of the squashed rectangle LED image, if that is truly what the PiCam sees.

Here's the Python script:

To illustrate the ability of this system to reproduce colour, here is an example where the Raspberry Pi's startup image is reproduced:

As I said before, it's difficult to photograph the coloured LEDs so that the bright colours do not white-out. As viewed by the eye, the colours are much more convincing.

Other scripts I have tried so far include: - this cycles the colours for all the LED pixels at the same time (ie all the same) - a simple demonstration of the Conway Game of Life - this script displays scrolling text showing the environmental readings for example, "Temperature = 36.2.   Pressure = 1025.6. Humidity =  32.5" - this is a simple animation of a pair of eyes - gives a contiunous print oyt on the terminal of pitch, roll and yaw of the Sense Hat - outputs on the LED array, any letter or character entered on the keyboard - indicates on the LED array, which of the positions the joystick has been moved - shows a beautiful animation of the colours of the rainbow moving across the array - makes each pixel independently and randomly change colour - makes a letter on the array and rotates it through 90 degree steps - makes an alphanumeric character rotate - any movement of the Sense Hat causes an exclamation mark to be displayed on the array - makes inverted text scroll on the array

Most of these have been provided by Ben Nuttall of the Raspberry Pi Foundation - thanks Ben!