2

I've been working on a project using the Pi ... I need to give PWM output but every one says that only one GPIO pin is compartible with PWM but i need 4 is there any way to give PWM output using 4 GPIO pins...

Plese help

1

I for myself have made bad experiences using the pi and python for pwm. The software pwm the RPi.GPIO library provides is very prone to 'hick-ups' when ever any additional workload appears. In my case for example opening a ssh connection immediately caused an irregular flicker. It will work with any gpio the way Deepesh descibed. But you must not be sensitive to occasional failures in your motor speed/light intensity/what ever. Python on the raspberry pi will not do its purpose in case of pwm if you want to achieve good results! When using the Raspberry for pwm I would highly recommend using C for coding (if you are able to do so, use assembler. It's not so hard to implement pwm with assembler ;). Then using 4 or 5 pins for seperate pwm funktions should not be a problem, I guess. But - believe me - no python

1

An alternative way is to add hardware that can do the PWM, example PCA9685.

0

Never mind, I found a way
Have a look at this:

import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.OUT)

while True:
    GPIO.output(4,True)
    time.sleep(0.00001)#1
    GPIO.output(4,False)
    time.sleep(0.02)#2
#This is how PWM works

Just increase the number of zeres after '.' in 1 to make the LED or whatever glow dimmer

  • 1
    Could we see the code you use for four LEDs with different brightness? – joan Aug 18 '16 at 10:10
  • 1
    This will work to do a relatively crude form of PWM that's effective to control the brightness of an LED -- but anything with a granularity finer than 10 ms will probably be visibly inconsistent, and rack up processor time (i.e. waste system resources). It will never work to do the more precise sort of PWM required for servos, etc. If you have a 26 pin model and you want to do PWM on more than pin 18 you should probably look into the pigpio python bindings. The 40-pin models do have 4 PWM pins. – goldilocks Aug 18 '16 at 12:44
  • @goldilocks I can't find any info that the 40-pin model has 4 PWM pins... – stevieb Aug 18 '16 at 14:26
  • See here; the pins crossed out are only on 40-pin models. The only one that isn't crossed out which has a "PWM" function (they're light blue) is pin 18, but there are three more that are crossed out: 12, 13, and 19. I'm not a python user so I can't say how to use RPi.GPIO for this, but it involves setting the mode of the pin to something other than input or output, then configuring a PWM clock. There are potentially other ways to create a PWM pulse, but they require some more complex low level hacking which is why I recommended you try pigpio. – goldilocks Aug 18 '16 at 14:34
  • 1
    @goldilocks Re PWM pins. GPIO 12/13/18/19 (and others on the compute module) may be set to PWM mode. However there are only two PWM channels. 12 and 18 share a channel so have exactly the same PWM settings, similarly 13 and 19 share a channel. – joan Aug 18 '16 at 16:53

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.