Motor position with potentiometer

Hi.

I'm trying to work out how to write the code to stop a motor when it reaches the end of the travel of the attached potentiometer.

I need it to stop when it gets to end of travel cw and end of travel ccw.

I am a bit confussed. I can read A0 and store the value but from there I'm stuck

Is the motor driving the rotation of the pot ?

1 Like

Read the potentiometer value and if not at the end of travel, move the motor. Seems pretty simple.

not sure how your motor is attached to a pot

a common radio control servo has a motor that drives a gear train with the output shaft connected to a pot. beacuse of the gear train, the motor has to turn many times to move the output shaft just a fraction of a turn.

circuitry compares the position of the pot to a control signal indicating the target postion.

Read the voltage on A0 in a loop, and when it exceeds a preset limit, stop the motor.

Minimal example:


void loop() {
if (analogRead(A0) > limit) stop_motor();
}
1 Like

No. The motor is controlled by 2 relays.

The pot is attached to the output shaft. The motor is geared down

then you can use a PID algorithm to control the motor voltage to reach a target position, slow down and stop at the target ... without overshoot

I will need to use the code the control 4 different motors. The motors are wing mirrors of a car. So I need it to be more if its moving cw and the reading on A0 hasn't changed stop the motor or if its moving ccw do the same

Are the motors sealed so water cannot get to them?

They are.

And the potentiometers?

They are attached to the motor. They are in a sealed unit

Nice! Do you have access to the three contacts of each potentiometer? And what is the resistance value of the resistors?

The pots share the same 5v supply. The resistance is 3.6k of each pot.

I have access to all the wires

Ok, lets be sure we understand this whole picture. The pots relate to the mirror position and NOT the motor armature position. Correct?

Yes the pot is in relation ro the mirrors position

Ok, back to your first post. With your DVM, measure the voltage on the pot wiper and make a mirror motor go from one limit to the other. Record the voltages you see on the DVM. Those voltages will be your limits. Any voltage in between will relate to the mirror position, as you know.
Then connected to A0 of your Arduino, display the raw values on the serial monitor as you make the same motor movements as your did for the DVM. Record these values as they will be the limits your code will be looking for to stop the motor movement.

I have Recorded the values of the pots for each motor but they are all different.

Is there a simple code that will check if A0 is incrementing or decrementing and stop the motor if it has not changed

I tried this code but didn't work

int sensorPin = A0;   // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue;  // variable to store the value coming from the sensor
int oldSensorValue;

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);

  if (sensorValue++ || sensorValue--) {

    oldSensorValue = sensorValue;

    if (sensorValue != oldSensorValue)
        digitalWrite(ledPin, LOW);
    else
        digitalWrite(ledPin, HIGH);
      

  }
 
 
}

have to compare it to the previous value

Something like this

int sensorPin = A0;
int ledPin = 13;
int sensorValue;
int oldSensorValue;
int hysteresis = 10;
unsigned long currentTime = millis();
unsigned long prevReadTime;
unsigned long readPeriod = 100;

void setup()
{
    Serial.begin(115200);
    pinMode(ledPin, OUTPUT);
}

void loop()
{
    currentTime = millis();
    if (currentTime - prevReadTime >= readPeriod)
    {
        sensorValue = analogRead(sensorPin);
        if (abs(sensorValue - oldSensorValue) >= hysteresis)
        {
            digitalWrite(ledPin, HIGH);
        }
        else
        {
            digitalWrite(ledPin, LOW);
        }
        oldSensorValue = sensorValue;
        prevReadTime = currentTime;
    }
}

The pot is read every 100 milliseconds and if it returns a value greater than the defined hysteresis the LED is turned on