Unicorn Hat - Different IO then GPIO18


#1

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!
-Kevin


#2

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.


#3

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?


#4

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!


#5

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)?


#6

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>
    ws2812.begin()
  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.