Unicorn Hat - Different IO then GPIO18


Hello there. I am interested in the Unicorn Hat, but I need to use it while playing analog music. The notes on the Unicorn page say “As the HAT uses the PWM hardware and GPIO18, it will interfere with analogue audio playback.”

I am not familiar with the Pi at all but hope you can answer a quick question. Is there some hardware requirement within the Pi to use GPIO18 (for example, if it is the only pin capable of sending control signals to the Unicorn)? Could I possibly drive it from a different Pi I/O pin by making some change in the Pi software? (I understand this will mean I have to use a jumper wire to drive it which is fine in this case).

As a secondary question, do you know if it is possible to put the analog audio signal on an I/O pin? I would like to connect to it directly without needing to insert a plug into the stereo jack.

Thanks so much!


It’s possible to change the output pin that the hardware PWM signal is routed to, switching it to GPIO12- but this doesn’t change the fact that it interferes with audio. It’s only really useful if you want to use GPIO18 for I2S audio- the creator of Pi DAC+ has UnicornHAT/Pi DAC+ running side-by-side with such a setup.

I’m not aware of any method to get analog audio out to the Raspberry Pi’s GPIO Pins, but there are several test points on the underside of the Pi that you can ( and I’ve done this to hook my Pi up to an RF Modulator ) solder wires directly to. I used a Model B+ and soldered wires directly to PP25 and PP26, these might differ on Pi 2 but since they’re exactly the same layout on the B+ and Pi 2 I’m willing to bet they’re the ones you’d need.

Whether or not soldering to test pads constitutes an alternative to plugging in a stereo jack is up to you- but it works for me… Note: this still wont workaround the problem with UnicornHAT and audio- you’ll need to either use digital audio, or use an Arduino or similar to drive the Unicorn HAT.


I have a Pi-DAC+ and a unicorn pHAT. I’ve managed to get the pHAT running usings pin 12 (pwm0) and pin 13 (pwm1) using unicorn.c example. However, as soon as I do this the audio on the DAC cuts out and will not come back without a reboot. @gadgetoid You mentioned that this was possible - do you have a link? Is anything special required?


Assuming you have WiringPi installed and access to /usr/local/bin/gpio what happens if, after the audio cuts out, you run:

gpio mode 1 alt0

I’m wondering if it’s still affecting the pin in some way that it shouldn’t be. Although I haven’t tried this setup myself, and I don’t believe there’s a guide anywhere for it either- at least not one I’ve been able to find!


A few more data points:

If I run the default unicorn.c example it kills the audio. If I run gpio mode 1 alt0 afterwards the audio is restored.

If I modify unicorn.c to use the second PWM channel this isn’t required. It doesn’t break the audio.

In both cases if the program is run while audio is playing this causes ALSA to crash. e.g. if I’m playing music from mpc the music stops, and any attempt to talk to mpc just hangs. I’ve found it’s possible to recover from this crash using:

service mpd stop
alsactl kill quit
alsactl restore 0
service mpd restart

I can understand why this crash happens when using the first PWM channel (pin 18), but not with the second (pin 13)?


I’ve fixed it! Although I don’t totally understand why what I’ve changed works.

In unicorn.c I had changed the channel from 0 to 1, but also needed to change the DMA number. I changed DMA from 5 to 8 and it works perfectly - no crashes, and the Pi-DAC+ and Unicorn pHAT can happily co-exist.

It should be possible to make the same change in unicornhat.py, but when I do (just in Python-space) the examples fail to start with the following traceback:

Traceback (most recent call last):
  File "rainbow.py", line 7, in <module>
    import unicornhat as unicorn
  File "/home/snorf/unicorn-hat/library/UnicornHat/unicornhat.py", line 16, in <module>
  File "build/bdist.linux-armv7l/egg/neopixel.py", line 114, in begin
RuntimeError: ws2811_init failed with code -1
Exiting cleanly

At this point I think it probably makes sense to move discussion to an issue on GitHub.


Hi, you might like to know that your exploits are being followed. I’m making a music box with the HifiBerry AMP2 and a Unicorn HAT connected on GPIO12. I’ve got the same issue with losing sound when I turn on the HAT. I haven’t yet tried changing the DMA.

Before I mess with unicorn.c (does this require recompiling, btw?), does anyone have any updates on this topic?


Are you specifically looking to write code in C? Since in Python you should just be able to change the pin here: https://github.com/pimoroni/unicorn-hat/blob/81fe536ac0092071b504244f5c87edc7505c66c2/library/UnicornHat/unicornhat.py#L9

To another supported pin (PCM, SPI etc) and it will work.

See the documentation under GPIO Usage in the README of the underlying library here: https://github.com/jgarff/rpi_ws281x


Thanks very much for the input.

No, I really don’t want to write C. Python is definitely preferred. I only mentioned the C code because the previous post said that changing the python code alone did not work, and I seem to be at the same place.

I have made the change to the output pin in the python code (to GPIO12) and this works, but currently activating the LEDs kills sound (using the DAC-based AMP2, which uses GPIO18) and I don’t yet know why.

Thanks for this, I had not seen that documentation. I think I need to digest this information and then find the equivalents for how the the AMP2 works with ALSA.

But the first thing I’ll do is disable the onboard (analogue) sound as I’m still loading the module for that (just lazy). That might be what’s crashing when the sound goes off. Then I’ll check the python DMA/PWM settings (keeping in mind the warnings in the README).


The hardware used to generate signals on BCM 12 and BCM 18 is, in both cases, the same hardware. It’s “PWM0” that’s routed to both of these pins.

As far as I understand it, most DACs will use PWM which is a separate, albeit somewhat similar hardware peripheral. Do you have a link to details on the DAC product you’re using?

The analogue audio could very easily be crashing when rpi_ws281x grabs the PWM hardware, but I’m not sure why that would take PCM down with it.

I know at least IQaudIO have had a DAC and Unicorn HAT running from the same Pi: https://twitter.com/IQ_audio/status/577154256073048065


This is all I’ve seen so far about how the HifiBerry boards work:
Mine is the ‘AMP2’.


This post is much more informative. I’m pretty sure that my AMP2 uses the same technology as the DAC+ (I recall reading that is a DAC+ with an amp on top).


[SOLVED] for me too!

So all I needed (apart from following the setup instructions for the AMP2, here), was to alter two lines in unicornhat.py:

LED_PIN        = 12      # was 18
LED_DMA        = 8       # was 5

Then connect GPIO12 to where the Unicorn HAT expects its input (GPIO18).
I am happily playing music and watching flashy lights from the same Pi. Woot!


Ahhh! It was clobbering the DMA channel that i2s was using? I know they shuffled around recently and caused some real trouble with SD card IO.

Glad you’re up and running!