2

I want to produce 12 bit resolution from Pi hardware pwm pin for my project. Please help me to go in right way as I am new to both Python and Pi.

import time
import pigpio as pi
import RPi.GPIO as g
g.setmode(g.BOARD)
g.setwarnings(False)
pi.set_PWM_range(12,4000)
print(pi.get_PWM_range(12))
g.setup(12,g.OUT)
p=pi.PWM(12,4000)
p.start(0)
try:
  while 1:
        for i in range(0,100,5):
           p.ChangeDutyCycle(i)
           time.sleep(0.1)
           print(i)
        for i in range(100,0,-5):
           p.ChangeDutyCycle(i)
           time.sleep(0.1)
           print(i)
except KeyboardInterrupt:
  pass
p.stop()
g.cleanup()

By runing this getting multiple error, i.e:

AttributeError: module 'pigpio' has no attribute 'set_PWM_range'
3
  • You seem to be mixing up RPi.GPIO and pigpio PWM on the same GPIO. RPi.GPIO is only software PWM. pigpio is hardware timed PWM. The pigpio calls you are using do not use the special features of the Pi's hardware PWM peripheral. I give an example of using the hardware PWM peripheral in the script in my answer to this question.
    – joan
    Commented Jan 10, 2020 at 16:24
  • Hi @ram, the following two PWM controllers might help. (1) I2C PCA9685 16-Channel 12-bit PWM Controller Module adafruit.com/product/815 (2) UART Controlled PWM Signal Generator raspberrypi.stackexchange.com/questions/104779/…
    – tlfong01
    Commented Jan 11, 2020 at 14:34
  • You can find cheap PCA9685 PWM controller modules from AliExpress, Amazon etc.:imgur.com/gallery/iNzjvCL
    – tlfong01
    Commented Jan 12, 2020 at 7:39

1 Answer 1

1

Yes. You can do far better.

My pigpio Python module will give a million steps for frequencies less than 250 Hz.

pigpio will give a resolution of 250 million divided by the frequency in hertz that you want to use.

See hardware_PWM

Example Script

#!/usr/bin/env python

import time
import pigpio

PWM=18

FREQ=5000

pi = pigpio.pi()

if not pi.connected:
   exit()

for percent in range(100): # 0-100 percent
   pi.hardware_PWM(PWM, FREQ, percent * 1000000 / 100)
   time.sleep(0.2)

pi.hardware_PWM(PWM, 0, 0) # stop PWM

pi.stop()
1
  • 1
    i go through pigpio module and tried without success.i am posting my code also,not exactly getting the way.
    – ram
    Commented Jan 10, 2020 at 12:02

Your Answer

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

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