Analog Read Mapping?

Quick question which might be a stupid one.

So I’m trying to read the value of a potentiometer using analogRead(). When reading up on it I saw that it takes 0-5v. But the potentiometer I’m trying to read has a range of 1.4-3.5v. So I think thats why analogRead() isn’t picking up on the movement of the pot for a few seconds and capping out to 1023 prematurely.

So is there a way to map analogRead() so that when my potentiometer reads 1.4 analogRead returns 0 and when it reads 3.5 it returns 1023.

Thanks.

#include <stdio.h>

int motor1Pot = A1;
int motor2Pot = A4;

int m1PotVal;
int m2PotVal;

int m1PotValMap;
int m2PotValMap;

int sped=0;
void setup() {
  Serial.begin(9600);
 
  pinMode(motor1Pot, INPUT);
  pinMode(motor2Pot, INPUT);
}

void loop(){
  readPots();
}

void readPots(){
  m1PotVal = 0;
  m2PotVal = 0;
  
  m1PotVal = analogRead(motor1Pot);
  m2PotVal = analogRead(motor2Pot);

  Serial.print(m1PotVal);
  Serial.print("      ");
  Serial.print(m2PotVal);
  Serial.println(); 
}

A potentiometer is a variable resistor. It doesn’t produce one voltage or another. That depends on what you have it connected to. You haven’t shown us that. You also haven’t shown us the code you’re running. So there is very very little chance that anyone can really help you much at this point.

analogRead returns a value from 0 to 1023 that is relative to the voltage on the pin as a proportion of the voltage on the analog reference. Normally that’s 5V but it doesn’t always have to be.

The voltage level has nothing to do with the speed of the read. I don’t know what you mean at the end of that first paragraph.

If you want to map one set of values onto another then look at the appropriately named map function in the reference area.

I did upload the code that I am running. Atleast it shows it for me. Ill post again here just incase. But let me break it down a little bit easier.

I have a linear actuator which has a pot feed back. I am powering that pot with 5v. When I read the value of the pot full retracted with a multimeter it reads 1.44V. When I read it at full extension with a multimeter it reads 3.56V.

When I connect the pot to the Arduino and try to use analogRead(), the output is all messed up. It reads 0 after moving the actuator a few inches out. Then picks up at 4-6-8-16-32-64-96 then jumps to 500. Then from 500 to about 880 it works fine as it should. Then after that it jumps to 1023 but the actuator isn’t fully extended yet.

So I had no idea why it was doing this. I thought that maybe it was because analogRead() takes 0-5V but the only voltage that is important to reading is 1.44-3.56.

I hope that makes more sense.

#include <stdio.h>

int motor1Pot = A1;
int motor2Pot = A4;

int m1PotVal;
int m2PotVal;

int m1PotValMap;
int m2PotValMap;

int sped=0;
void setup() {
  Serial.begin(9600);
 
  pinMode(motor1Pot, INPUT);
  pinMode(motor2Pot, INPUT);
}

void loop(){
  readPots();
}

void readPots(){
  m1PotVal = 0;
  m2PotVal = 0;
  
  m1PotVal = analogRead(motor1Pot);
  m2PotVal = analogRead(motor2Pot);

  Serial.print(m1PotVal);
  Serial.print("      ");
  Serial.print(m2PotVal);
  Serial.println(); 
}

I see the code. Don’t know why I didn’t earlier. Maybe my phone.

So I had no idea why it was doing this. I thought that maybe it was because analogRead() takes 0-5V but the only voltage that is important to reading is 1.44-3.56.

Well 1.44 and 3.56 are all well within the 0-5 volt window so it should read that no problem.

Show us how it is wired. Do you know the specs on the pot? How many ohms? Sometimes when input impedance is really high it takes analogRead a couple of times to settle down but I wouldn’t expect a pot on an actuator to be that high.

“I only show ONE actuator. I left off the direction switch and the voltage divider. The 12volt battery’s negative is currently NOT connected to the 6volt battery’s negative.”

That was the write up from my partner who takes care of the electrical aspect of the project. I hope that makes more sense to you then it does to me. We’ve been trying to figure this out for a while and I just know were missing something. If you are confused by that I can ask him to elaborate more.

Thanks a bunch!

Linear_Actuator_with_Potentiometer_PA-14P.png|0x0

Please ask him for an accruate drawing. I don't do the whole "ignore this piece and that piece isn't really connected this way" BS. Surely an accurate drawing can be created. I'm not even looking at these if they aren't what you actually have.

That is the wiring that we are currently using. He was more talking to me because we stripped a lot of our other implementations of the build until we get the pot reading correctly.

So that drawing is all the implementations of the build that we are currently using and its still having issues.

I can’t get the second one to open.

I tried making them different formats.

Derekbird02:
Quick question which might be a stupid one.

So I'm trying to read the value of a potentiometer using analogRead(). When reading up on it I saw that it takes 0-5v. But the potentiometer I'm trying to read has a range of 1.4-3.5v. So I think thats why analogRead() isn't picking up on the movement of the pot for a few seconds and capping out to 1023 prematurely.

So is there a way to map analogRead() so that when my potentiometer reads 1.4 analogRead returns 0 and when it reads 3.5 it returns 1023.

Thanks.

did you try reading one pot at a time ie something like this in code:

int motor1Pot = A1;
int motor2Pot = A4;

int m1PotVal;
int m2PotVal;

int m1PotValMap;
int m2PotValMap;

void setup() {
  Serial.begin(9600);
}

void loop() {
  m1PotVal = analogRead(motor1Pot);
  Serial.println(m1PotVal);
}

does it read any better?

the ADC on arduino is multiplexed and normally when reading more that one channel, one practice is to double read each channel and only take the second reading to account for the multiplex switching.

ie something like this:

void loop(){  
  analogRead(motor1Pot); //discard first reading
  m1PotVal = analogRead(motor1Pot);
  
  analogRead(motor2Pot); //discard first reading
  m2PotVal = analogRead(motor2Pot);

  Serial.print(m1PotVal);
  Serial.print("      ");
  Serial.print(m2PotVal);
  Serial.println();
}

hope that helps...

Hi;
OPs images.



Tom... :slight_smile:

Hi,
What do you read from the analogRead data, for the two voltage limits you are restricted too?
What are the max and min analogRead values you get?

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,
What do you read from the analogRead data, for the two voltage limits you are restricted too?
What are the max and min analogRead values you get?

Thanks.. Tom... :slight_smile:

When my actuator is fully retracted (multimeter reads 1.4v) analogRead() tells me 0. I start to move the actuator, the multimeter increases voltage, but analogRead() still says 0. Then after like 3 inches of moving the actuator analogRead() starts to increase in increments of 4 all the way to about 90. Then it jumps from 90 all the way to 500. Then from 500-900 it runs as it should. Then it jumps from 900 all the way to 1023 but the actuator is fully extended yet. Then when the actuator is fully extended it multimeter read 3.56v

I hope that makes sense. Thanks.

Hi,
Are you using the code in post #1?
If so, edit out ALL reference to the second analog input.

What DMM value do you get at the analog input pin of the Arduino?

Tom... :slight_smile:

TomGeorge:
Hi,
Are you using the code in post #1?
If so, edit out ALL reference to the second analog input.

What DMM value do you get at the analog input pin of the Arduino?

Tom... :slight_smile:

basically what I suggested in post #9 :wink:

Derekbird02:
When my actuator is fully retracted (multimeter reads 1.4v) analogRead() tells me 0. I start to move the actuator, the multimeter increases voltage, but analogRead() still says 0. Then after like 3 inches of moving the actuator analogRead() starts to increase in increments of 4 all the way to about 90. Then it jumps from 90 all the way to 500. Then from 500-900 it runs as it should. Then it jumps from 900 all the way to 1023 but the actuator is fully extended yet. Then when the actuator is fully extended it multimeter read 3.56v

I hope that makes sense. Thanks.

If you measured 1.4V at the analog input pin but the serial monitor is showing "0" then something is seriously wrong. Maybe your multimeter is not connected to the same ground.

Likewise, if serial monitor is showing "1023" then the voltage at the analog input pin, referenced to the Arduino ground pin, is certainly 5V (or 3.3V for a 3.3V Arduino.)

The jump from 90 to 800 says there is something mechanically wrong with the pot. Do you have a second one there to try?

When the analog reference voltage is 5.0V an input of 1.4V should read as 286 and in input voltage of 3.5V should read as 716. You can map those to the full 1-1023 range with:

int position = map(analogRead(A0), 286, 716, 0, 1023);

Im trying to read the values of analogRead() of my linear actuators potentiometer and the output is very weird

When my actuator is fully retracted (multimeter reads 1.4v) analogRead() tells me 0. I start to move the actuator, the multimeter increases voltage, but analogRead() still says 0. Then after like 3 inches of moving the actuator analogRead() starts to increase in increments of 4 all the way to about 90. Then it jumps from 90 all the way to 500. Then from 500-900 it runs as it should. Then it jumps from 900 all the way to 1023 but the actuator is fully extended yet. Then when the actuator is fully extended it multimeter read 3.56v

I have the pot in A14, connected to ground and 5V in the Arduino.

If anyone has any clue, please help.

#include <stdio.h>

int motor1Pot = A1;

int m1PotVal;

int m1PotValMap;

void setup() {
  Serial.begin(9600);
  pinMode(motor1Pot, INPUT);
}

void loop(){
  readPots();
}

void readPots(){
  m1PotVal = 0;
  
  m1PotVal = analogRead(motor1Pot);

  Serial.println(m1PotVal);
}

(deleted)

Why did you start a whole new thread? Now you gotta answer all the questions from the old one again: