Posts: 1
Joined: Fri Jan 15, 2016 8:21 pm

Pi, Potentiometer, Stepper Motor

Fri Jan 15, 2016 8:39 pm

I am quite a beginner with this stuff. I have setup a pi to control my garage door through a relay, so that I can open and close it with my smartphone. So I have capabilities of doing things, most times I just need pointed in the right direction. My question now doesn't pertain to the garage door, just wanted to explain that I am not a complete noob.

I am looking for a way to control a stepper motor's rotation through a potentiometer. More specifically I am not looking to control speed but rather the position of rotation. I would like for the potentiometer to be a 1:1 ratio with the stepper motor. (Also am seeking guidance as to whether a stepper or servo would be of better use.)

I understand that an arduino is generally more suited for this use, but I am also seeking a way to eventually have a couple hardware buttons to set points into memory and also to revisit points of rotation through a button press. The ultimate goal is to have a small setup that utilizes a rpi zero to save space (also why I would like to do this without an arduino).

Hopefully this makes sense and I haven't just wasted your time. If anyone has an insight that would be helpful I would be glad to hear it. Thanks in advance!

Posts: 347
Joined: Fri Jan 01, 2016 10:27 pm

Re: Pi, Potentiometer, Stepper Motor

Sat Jan 16, 2016 5:08 am

I'm not exactly sure what you want to accomplish, but you must realize that a pot has only about 270° of rotation, whereas a stepper motor can rotate through any number of degrees.
But, assuming that you want to map the pot position to a specific number of degrees, you need to digitize the pot's wiper position (place the two resistor terminals between Vref an Ground, and measure the voltage on the wiper). Then you calculate how many steps you need to send to the motor based on whatever scale factor you require. If you don't have an encoder on the motor, you will need some time of home switch so that you know where to start from.

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Pi, Potentiometer, Stepper Motor

Sat Jan 16, 2016 7:49 am

Continuos rotation potentiometer.

I know you have already rejected arduino, I'd still recommend it.

You can have hardware buttons + potentiometer + stepper motor driver (or Servo) with arduino, if you are using a Pi you will need to add an ADC as well.

If you want truly continuous rotation then the stepper motor will offer greater accuracy, if you are interested in absolute position and not worried about direction of rotation then the Servo might be cheaper and will probably be easier to setup.

It may be worth adding a 2nd continuous rotation potentiometer to your build to verify that the position of the output matches the setpoint.
Building Management Systems Engineer.

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

Re: Pi, Potentiometer, Stepper Motor

Sat Jan 16, 2016 11:18 am

A Pi could do the job. But it might well be simpler and better suited to an Arduino. Look at something like an Arduino Pro Mini which appears to be about a third of the size of a Pi Zero.

This is a Pi Zero using a pot to set a servo position.


Code: Select all

#!/usr/bin/env python

# servo_pot_cap.py
# 2016-01-16
# Public Domain

import time

import pigpio  # abyz.co.uk/rpi/pigpio/python.html
import pot_cap # abyz.co.uk/rpi/pigpio/examples.html#Python_pot_cap_py




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:

pc = pot_cap.reader(pi, POT_CAP, 1, 1)


   while True:

      s, v, r = pc.read()

      if s: # Valid reading.

         if v < MIN_POT_CAP:
            v = MIN_POT_CAP

         if v > MAX_POT_CAP:
            v = MAX_POT_CAP

         micros = map(v, MIN_POT_CAP, MAX_POT_CAP, MIN_SERVO, MAX_SERVO)

         pi.set_servo_pulsewidth(SERVO, micros)




pc.cancel() # Cancel pot cap reader.
pi.stop() # Disconnect from Pi.

Posts: 347
Joined: Fri Jan 01, 2016 10:27 pm

Re: Pi, Potentiometer, Stepper Motor

Sat Jan 16, 2016 3:31 pm

You also might want to look at a rotary encoder instead of a pot.

Posts: 223
Joined: Fri May 31, 2013 11:29 am
Contact: Website

Re: Pi, Potentiometer, Stepper Motor

Sat Jan 16, 2016 7:33 pm

Here is one possibility for simple DC motors

https://github.com/oh7bf/RasPiHBridge/w ... ion-Sensor

Return to “Automation, sensing and robotics”