3

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

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. 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.

This type of PWM may be generated on any GPIO on the expansion header. All GPIO may have different settings.

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. 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.

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.

Not really suitable for servos, will control LED brightness but will suffer from glitches, suitable for motor speed control.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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