gr3go
Posts: 13
Joined: Tue Oct 11, 2016 4:59 pm

PWM and audio on RPi3

Tue Nov 15, 2016 12:44 pm

Hi guys,

Is it possible to use the hardware PWM and outputing audio in the same time using a Raspberry Pi 3?
Does the audio driver use the same PWM as the one availble for the user?

This is what I've observed:
- fresh start of the pi (sudo reboot): audio output through 3.5mm jack work
- after configuring the PWM ("gpio mode 1 pwm && gpio pwm-ms && gpio pwmr 5 && gpio pwmc 101 && gpio pwm 1 2") the audio output is not working

User avatar
joan
Posts: 16049
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: PWM and audio on RPi3

Tue Nov 15, 2016 1:14 pm

Hardware PWM on the Pi uses the same peripheral as that used by the audio jack. You can only use one at a time.

Do you need hardware PWM? The Pi can generate a variety of PWM signals without using the hardware PWM peripheral.

gr3go
Posts: 13
Joined: Tue Oct 11, 2016 4:59 pm

Re: PWM and audio on RPi3

Tue Nov 15, 2016 1:41 pm

I'dd need to generate a 38kHz signal with duty cycle ~50%.
Can this be done without using the hardware PWM peripheral?

User avatar
joan
Posts: 16049
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: PWM and audio on RPi3

Tue Nov 15, 2016 2:14 pm

gr3go wrote:I'dd need to generate a 38kHz signal with duty cycle ~50%.
Can this be done without using the hardware PWM peripheral?
If you settle for 50% you could just use a standard clock signal available from GPIO 4, 6, and 20.

Otherwise my pigpio will generate 38kHz carrier at various dutycycles (I use it to generate IR remote signals).

gr3go
Posts: 13
Joined: Tue Oct 11, 2016 4:59 pm

Re: PWM and audio on RPi3

Wed Nov 16, 2016 7:13 am

That is what I need, a 38kHz signal for an IR barrier.
I think, the duty cycle 50% should work in my case.
Otherwise my pigpio will generate 38kHz carrier at various dutycycles (I use it to generate IR remote signals).
What is "my pigpio"? Are you referring to http://abyz.co.uk/rpi/pigpio/?

User avatar
joan
Posts: 16049
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: PWM and audio on RPi3

Wed Nov 16, 2016 8:53 am

gr3go wrote: ...
Are you referring to http://abyz.co.uk/rpi/pigpio/?
Yes.

If you use Raspbian it may be installed (as an older version).

Try sudo pigpiod to see of the daemon is present.

gr3go
Posts: 13
Joined: Tue Oct 11, 2016 4:59 pm

Re: PWM and audio on RPi3

Wed Nov 16, 2016 4:11 pm

I am unable to generate a 38kHz signal with pigpio.
It seems, the output is not very accurate, or maybe I've configured something wrong.

Code: Select all

import pigpio # http://abyz.co.uk/rpi/pigpio/python.html
import time

def carrier(gpio, frequency, micros):
        # http://abyz.co.uk/rpi/pigpio/code/irrp_py.zip
        """
        Generate carrier square wave.
        """
        wf = []
        cycle = 1000.0 / frequency
        cycles = int(round(micros/cycle))
        print("Freq={} Micros={} Cycle={} Cycles={}".format(frequency, micros, cycle, cycles))
        on = int(round(cycle / 2.0))
        sofar = 0
        for c in range(cycles):
            target = int(round((c+1)*cycle))
            sofar += on
            off = target - sofar
            sofar += off
            wf.append(pigpio.pulse(1<<gpio, 0, on))
            wf.append(pigpio.pulse(0, 1<<gpio, off))
            print("Pulse On={} Off={}".format(on, off))
        return wf

# Phy9 - GND
# Phy5 (GPIO 03) - PWM
def pwm_test():
    GPIO = 3
    pi = pigpio.pi() # Connect to Pi.
    pi.set_mode(GPIO, pigpio.OUTPUT) # IR TX connected to this GPIO.
    pi.wave_add_new()
    wf = carrier(GPIO, 38, 1000) # 10ms
    pi.wave_add_generic(wf)
    wave = pi.wave_create()

    for num in range(0,15):
        print("It={}".format(num))
        pi.wave_send_once(wave)
        while pi.wave_tx_busy():
            print("Still busy")
            time.sleep(1)

    input("Press Enter to continue...")
    
    pi.wave_tx_stop()
    pi.wave_delete(wave)

if __name__ == "__main__":
    pwm_test()
Output:

Code: Select all

pi@raspberrypi:~/adam/foos/foos $ python3 ir_pwm_test.py
Freq=38 Micros=1000 Cycle=26.31578947368421 Cycles=38
Pulse On=13 Off=13
Pulse On=13 Off=14
Pulse On=13 Off=13
Pulse On=13 Off=13
Pulse On=13 Off=14
Pulse On=13 Off=13
Pulse On=13 Off=13
Pulse On=13 Off=14
Pulse On=13 Off=13
Pulse On=13 Off=13
Pulse On=13 Off=13
Pulse On=13 Off=14
Pulse On=13 Off=13
Pulse On=13 Off=13
Pulse On=13 Off=14
Pulse On=13 Off=13
Pulse On=13 Off=13
Pulse On=13 Off=14
Pulse On=13 Off=13
Pulse On=13 Off=13
Pulse On=13 Off=14
Pulse On=13 Off=13
Pulse On=13 Off=13
Pulse On=13 Off=14
Pulse On=13 Off=13
Pulse On=13 Off=13
Pulse On=13 Off=14
Pulse On=13 Off=13
Pulse On=13 Off=13
Pulse On=13 Off=13
Pulse On=13 Off=14
Pulse On=13 Off=13
Pulse On=13 Off=13
Pulse On=13 Off=14
Pulse On=13 Off=13
Pulse On=13 Off=13
Pulse On=13 Off=14
Pulse On=13 Off=13
It=0
Still busy
It=1
Still busy
It=2
Still busy
It=3
Still busy
....
With a logic analyzer I was expecting to see those pulses (13 and 14 micros), but unfortunately the waveforms are not as expected:
Image:

Any suggestions/hints?

User avatar
joan
Posts: 16049
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: PWM and audio on RPi3

Wed Nov 16, 2016 4:53 pm

Possibly your Pi3 exhibits this fault viewtopic.php?p=1042078#p1042078 and this issue https://github.com/joan2937/pigpio/issues/80

The end result is that timings seem to be 88% of what they should be which is close to what you are seeing.

I have never got to the bottom of what is going on, mainly because I have never managed to reproduce the error on my Pi3B.

Return to “Beginners”