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'
Ghanima
  • 15,958
  • 17
  • 65
  • 125
ram
  • 69
  • 1
  • 9

1 Answers1

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()
joan
  • 71,852
  • 5
  • 76
  • 108