Unicorn HAT & Audio

I’m thinking about ordering a Unicorn HAT and I noticed the warning

“As the HAT uses the PWM hardware and GPIO18, it will interfere with analogue audio playback. HDMI should be just fine though! :D”

So what exactly is the limitation? Can I not even have speakers plugged into the audio jack? Or can I just not have the Unicorn displaying lights at the time as playing audio?

It’s best not to have speakers plugged in at all- the sound that running UnicornHAT will emit through them isn’t pleasant although I don’t believe it will damage speakers.

You can still use audio over HDMI, so playing music while using UnicornHAT isn’t impossible, it just can’t be done with the analog output.

That’s unfortunate, the project I wanted it for is using the analog output to speakers for speaking text…

Now I’m currently using the DOT3k with audio at the same time. I believe, that uses PWM for the backlight LEDs, Is that doing something different or is the Unicorn Hat just using more PWM outputs because of the amount of lights?

Dot3k actually uses a chip called the SN3218, which talks to the Pi over I2C- a protocol which uses both a Clock and Data line- and has 18 PWM driver channels for LEDs- enough to drive the 3 backlight LED and 9 bargraph LEDs.

The LEDs on the Unicorn HAT, however, are completely different beasts. They’re known as WS2812 LEDs, and every single one of them contains its own built-in PWM driver ( that’s the little dark rectangle you can see in each one ).

The Pi needs to communicate with that whole chain of WS2812 LEDs, and it does so using a unique, and very high-speed protocol that spurts out all the data for those 64 pixels through one pin.

To do this with one pin, the WS2812s rely on short and long pulses indicating high and low bits- this means the timing has to be very precise, and very finite. The Pi can’t do this by simply toggling a GPIO pin in Python, so we have to run the PWM engine, and throw it pixel data from reserved memory.

Unfortunately this same PWM engine is the one used to generate the analogue audio signal on the Pi, so the clash happens :(

1 Like

Was interested on the same topic and found this explanation very complete and clear. Thanks!


So is it right to assume this would apply to any neopixel like device… so this would have the same issue:

What about something like this:

Yes, any NeoPixel device you try to drive from the Pi will interfere with your audio…

unless you use some sort of drive like, for example, Propeller HAT ( http://shop.pimoroni.com/products/propeller-hat ), or RasPiO Duino ( http://rasp.io/duino )

Both of these can talk to your Pi via some other means ( i2c/serial ) and easily drive NeoPixels.

The LED Matrix you link should also work fine- it speaks i2c and doesn’t get up to any funny business.