1

I am working on a self driving rc car project and I need a servo to steer my car. As I was getting a bit jitter with RPi.GPIO I tried pigpio library. I am using a Raspbery Pi Model B. But it won't move my servo. I am using pin 10 which is gpio 16. My this code with rpi.GPIO is working -

from time import sleep
import RPi.GPIO as gpio
gpio.setmode(gpio.BOARD)
gpio.setup(10,gpio.OUT)
pwm = gpio.PWM(10,80)
pwm.start(0)

def SetAngle(angle):
    duty = angle / 18 + 2
    gpio.output(10, True)
    pwm.ChangeDutyCycle(duty)
    sleep(1)
    gpio.output(10, False)
    # pwm.ChangeDutyCycle(0)
try:
    while True:
        angle = float(input('ENTER ANGLE PLEASE: '))
        SetAngle(angle)

except:
    SetAngle(0)
    pwm.stop()
    gpio.cleanup()

But this code using pigpio is not -

import pigpio
import math

GPIO_SERVO_PIN  = 10

MIN_ANG=-180.0 #degrees
MAX_ANG=180.0  #degrees

MIN_PW=1000 # microseconds
MAX_PW=2000 # microseconds

ANG_RANGE=MAX_ANG-MIN_ANG
PW_RANGE=MAX_PW-MIN_PW
PWAR=float(PW_RANGE)/ANG_RANGE

RAD2DEG=180.0/math.pi

def angleToPulseWidth(angle):
   """
   angle is mapped to valid pulse widths for servo
   which are determined by experiment.
   """
   assert MIN_ANG <= angle <= MAX_ANG
   return MIN_PW + ((angle - MIN_ANG) * PWAR)

pi = pigpio.pi()
if not pi.connected:
    exit()
while True:
    angle = float(input('Please enter a angle: '))
    pw = angleToPulseWidth(angle)
    pi.set_servo_pulsewidth(GPIO_SERVO_PIN, pw)

I have run the following command also which let's us use the pigpio library -

sudo pigpiod

  • 4
    pigpio ONLY supports BCM pin numbers – Milliways Jun 17 at 7:38
  • Thank you @Milliways, it worked. I just came to know about the library today but I didn't find this information by google search. – Raj Jun 17 at 9:24
  • 3
    If it works now please make an answer. This finished the question and will show others that your question has a solution and they don't try to help you for nothing. – Ingo Jun 17 at 10:42
  • Ok good suggestion @Ingo – Raj Jun 18 at 5:38
1

PiGPIO supports only BCM pins. Looks like I have used board pins due to which my servo is not moving. I changed the pin numbering to BCM and passed the pin GPIO number 18. Do not write the pin number write the BCM number. For example - If I inserted it in pin 12 which is BCM 18 then to set servo position I'll do -

pi.set_servo_pulsewidth(18, pw)

not

pi.set_servo_pulsewidth(12, pw)

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.