i want to make a servo move with potentiometer
Posted: Wed Mar 07, 2018 1:03 am
when potentiometer is at half way the servo is at half way. i need the code for python im using pin 12 of raspberry pi 3 b
A small, affordable computer with free resources to help people learn, make things, and have fun
https://www.raspberrypi.org/forums/
https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=207223
Code: Select all
#!/usr/bin/env python
# servo_pot.py
# 2016-07-25
# Public Domain
import time
import pigpio # http://abyz.me.uk/rpi/pigpio/python.html
SERVO=14
MIN_SERVO=500
MAX_SERVO=2500
MIN_POT_CAP=0
MAX_POT_CAP=1023
def map(val, in_min, in_max, out_min, out_max):
return (val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
pi = pigpio.pi() # Connect to Pi.
if not pi.connected:
exit()
adc = pi.spi_open(1, 40000, 0) # Open SPI channel 1.
while True:
try:
c, d = pi.spi_xfer(adc, [1, 0x80, 0]) # Read channel 0.
v = ((d[1]<<8) | d[2]) & 0x3FF
micros = map(v, MIN_POT_CAP, MAX_POT_CAP, MIN_SERVO, MAX_SERVO)
pi.set_servo_pulsewidth(SERVO, micros)
time.sleep(0.02)
except:
break
print("\nexiting...")
pi.spi_close(adc) # Release SPI handle.
pi.stop() # Disconnect from Pi.
and what pin do i connect the potentiometerjoan wrote: ↑Wed Mar 07, 2018 8:15 amHere is some code. You will need to change the GPIO to suit. The rest is down to you.Code: Select all
#!/usr/bin/env python # servo_pot.py # 2016-07-25 # Public Domain import time import pigpio # http://abyz.me.uk/rpi/pigpio/python.html SERVO=14 [u][b]<------ can i set code to gpio bored and use the pin number i want[/b][/u] MIN_SERVO=500 MAX_SERVO=2500 MIN_POT_CAP=0 MAX_POT_CAP=1023 def map(val, in_min, in_max, out_min, out_max): return (val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min pi = pigpio.pi() # Connect to Pi. if not pi.connected: exit() adc = pi.spi_open(1, 40000, 0) # Open SPI channel 1. while True: try: c, d = pi.spi_xfer(adc, [1, 0x80, 0]) # Read channel 0. v = ((d[1]<<8) | d[2]) & 0x3FF micros = map(v, MIN_POT_CAP, MAX_POT_CAP, MIN_SERVO, MAX_SERVO) pi.set_servo_pulsewidth(SERVO, micros) time.sleep(0.02) except: break print("\nexiting...") pi.spi_close(adc) # Release SPI handle. pi.stop() # Disconnect from Pi.