jakerthegamer3
Posts: 12
Joined: Sun Mar 04, 2018 6:50 pm

i want to make a servo move with potentiometer

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

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: i want to make a servo move with potentiometer

Wed Mar 07, 2018 8:15 am

Here 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

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.

PiGraham
Posts: 3971
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: i want to make a servo move with potentiometer

Wed Mar 07, 2018 9:31 am

Is that using a MCP3008 ADC?

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: i want to make a servo move with potentiometer

Wed Mar 07, 2018 10:32 am

PiGraham wrote:
Wed Mar 07, 2018 9:31 am
Is that using a MCP3008 ADC?
I think so.

jakerthegamer3
Posts: 12
Joined: Sun Mar 04, 2018 6:50 pm

Re: i want to make a servo move with potentiometer

Wed Mar 07, 2018 3:18 pm

joan wrote:
Wed Mar 07, 2018 8:15 am
Here 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.
and what pin do i connect the potentiometer

PiGraham
Posts: 3971
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: i want to make a servo move with potentiometer

Wed Mar 07, 2018 3:43 pm

Some useful info here:
https://learn.adafruit.com/raspberry-pi ... rs/mcp3008

The older guide linked to there shows a potentiometer on a breadboard wired to an MCP3008

Return to “Python”