6

What's the difference between soft pwm and pwm(maybe hardware pwm) in raspberry 3 b+. From the pinout (https://pinout.xyz), it seam that there are only 2 pwm pin, pin 32 for pwm0 and 33 for pwm1. But the api for GPIO seam that, we can generate pwm in any pins. What's the difference, does anyone give me an answer. Thanks so much.


import RPi.GPIO as GPIO
from time import sleep

ledpin = 12             # PWM pin connected to LED
GPIO.setwarnings(False)         #disable warnings
GPIO.setmode(GPIO.BOARD)        #set pin numbering system
GPIO.setup(ledpin,GPIO.OUT)
pi_pwm = GPIO.PWM(ledpin,1000)      #create PWM instance with frequency
pi_pwm.start(0)             #start PWM of required Duty Cycle
while True:
    for duty in range(0,101,1):
        pi_pwm.ChangeDutyCycle(duty) #provide duty cycle in the range 0-100
        sleep(0.01)
    sleep(0.5)for duty in range(100,-1,-1):
        pi_pwm.ChangeDutyCycle(duty)
        sleep(0.01)
    sleep(0.5)

1 Answer 1

15

Note that I am the author of pigpio so will be biased in my assessment of the different types of PWM. Actually there are three different types of PWM supported on the Pi.

Fully hardware PWM

  • This type of PWM is generated by the Pi's PWM peripheral.
  • The timing of the pulses is controlled by the PWM peripheral.
  • It is the most accurate and arguably the most flexible.
  • It can be generated on GPIO 12/13/18/19.

However there are only two channels, so only two different PWM streams can be generated at a time. GPIO 12/18 are on one channel, GPIO 13/19 on the other.

Suitable for:

  • jitter free servos,
  • glitch free LED brightness control,
  • motor speed control.

DMA timed PWM

  • This type of PWM is generated by the Pi's DMA peripheral.
  • The timing of the pulses is controlled by DMA.
  • This type of PWM may be generated on any GPIO on the expansion header.
  • All GPIO may have different settings.

It is not as timing accurate as fully hardware PWM but appreciably more accurate than software timed PWM. Depending on the implementation it is not as flexible as fully hardware PWM, e.g. the number of frequencies is much more limited and the number of steps between on and off is much more limited.

Suitable for:

  • jitter free servos,
  • glitch free LED brightness control,
  • motor speed control.

Software Timed PWM

  • This type of PWM is generated by software.
  • The timing of the pulses is controlled by the (Linux) scheduler.
  • This type of PWM may be generated on any GPIO on the expansion header. * * All GPIO may have different settings.
  • The timing accuracy will vary according to the number of GPIO being used for PWM.

It is appreciably less timing accurate than fully hardware PWM or DMA timed PWM. It is much more flexible than DMA timed PWM and just as flexible as fully hardware PWM, e.g. the number of frequencies is unlimited and the number of steps between on and off is unlimited.

  • Not really suitable for servos.
  • Will control LED brightness but will suffer from glitches
  • Suitable for motor speed control.
4
  • Is it correct that the hardware PWM is only accessible from C library and the python library uses software PWM?
    – Sasan
    Mar 4, 2021 at 14:03
  • Depends on the library. pigpio Python uses hardware PWM.
    – joan
    Mar 4, 2021 at 14:38
  • I did not know about DMA timed PWM. Can you please show some code / docs / library to use DMA timed PWM? Sep 14, 2022 at 3:13
  • 1
    @programmerRaj abyz.me.uk/rpi/pigpio
    – joan
    Sep 14, 2022 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.