import pigpio
import math
import RPi.GPIO as GPIO
from gpiozero import Button
import time

limit = 17
switch = Button(limit)

GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

MIN_ANG=-180.0 #degrees
MAX_ANG=180.0  #degrees

MIN_PW=1000 # microseconds
MAX_PW=2000 # microseconds



def estop():
    pi.set_servo_pulsewidth(GPIO_SERVO_PIN, 1500.0)
    pi.set_servo_pulsewidth(GPIO_SERVO_PIN, 1250.0)
    pi.set_servo_pulsewidth(GPIO_SERVO_PIN, 1500.0)

def angleToPulseWidth(angle): # stay between 90(right), 0 stop, -90(left)
   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:
while True:
    switch.when_pressed = estop
    angle = float(input('Please enter a angle: '))
    pw = angleToPulseWidth(angle)
    pi.set_servo_pulsewidth(GPIO_SERVO_PIN, pw)

I am trying make this drink tray move in and out using a MG996R servo. The servo was modded to be continuous by removing the physical gear metal block and pushout the electronic feedback loop. I am pigpio as the library for PWM to control my motor. I have a limiter switch to stop it if it moves back to far. The problem I having right now is that if I use my hands to press the switch it stop it. But when i let the tray press the switch it end not stopping the motor and just keep on running. Images are display below. My 3D printed rail system: "CAD file"

enter image description here

enter image description here

enter image description here

  • What pulsewidth are you sending to stop the servo? Does that pulsewidth stop the servo when sent from the command line with pigs? E.g. pigs s 27 0
    – joan
    Mar 21, 2020 at 9:53
  • The plusewidth I use is 1500, It stop my servo completely. The Code is : pi.set_servo_pulsewidth(GPIO_SERVO_PIN, 1500.0) I have not tried with the command line yet, everything is control by the python script above. @joan
    – Kedd22
    Mar 22, 2020 at 21:22


Your Answer

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

Browse other questions tagged or ask your own question.