Hi, I recently tried to use analogread to get a reading from a linear actuator potentiometer. The potentiometer outputs a voltage between ground and 5v,linearly moving from one to the other based on the position of the shaft.
The problem I get is that when I do analogread, i dont get a smooth reading from one value (near 0) to another value (near 1024). I get something like 900ish OR 10ish. Nothing in between. Its like it wants to read it as a digital value.
I put the potentiometer line to an DMM, and can see the voltage going from 5v to 0v over 20seconds linearly. I tried also to tie the line to a pulldown and pullup resistor. Neither seemed to work, other than scaling the 900 down.
With seeing your wiring setup and sketch code it's hard to guess what the problem might be. There should be no reason the analog read shouldn't read as expected, assuming your code can sample the analog input pin is a timely manner.
Where are you getting the 5V from? If it is a separate supply have you commend up the ground connections? This is the sort of result you see if you haven't got a proper signal ground.
@Lefty: I'll upload my code shortly and take a picture of the board and wiring later today. I think its sampling the pin correctly, but then again Im not getting what I expect so that could be my problem.
@Mike: I getting both my ground and 5V voltage from the powersupply unit located under my Function Generator and Oscilloscope. So the board and the linear actuator share the same 5V and ground.
@Royk: I have it plugged into the analog pin 0. Will take a picture and post it later today. I hope that I didnt make an error in how access it.
//Smart Prostetic Project
//Mechanical Engineering Senior Design
//August 17, 2009
//ME students:
//Nathan Supplee
//John Salvia
//EE students:
//Kirit Patel
//Ziad Yousif
int switchPin = 2; // switch input
int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)
int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
int enablePin = 9; // H-bridge enable pin
int ledPin = 13; // LED
int sensorPin = 0; // pin that the sensor is attached to
int sensorValue = 0; // the sensor value
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
int min_dist = 10; //minimum distance the linear actuator can contract
int max_dist = 11; //maximum distance the linear actuator can expand
//function: Setup
//purpose: To setup the proper input/output pins and start calibration
// function
void setup() {
// set the switch as an input:
pinMode(switchPin, INPUT);
// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(ledPin, OUTPUT);
// set enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
// blink the LED 3 times. This should happen only once.
// if you see the LED blink three times, it means that the module
// reset itself,. probably because the motor caused a brownout
// or a short.
blink(ledPin, 3, 500);
//calibrate the linear actuator
calibrate();
}
//function: Calibrate
//purpose: to calibrate the linear actuator, and determine maximum
// and minimum distances
void calibrate()
{
unsigned long w;
//signale begining of calibration period.
digitalWrite(ledPin, HIGH);
w=millis();
//for 10 seconds minimize the linear actuator distance
while (millis() < (w + 10000)) {
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
}
//now take a reading of the potentiometer and store value as Minimum distance
sensorMin = analogRead(sensorPin);
w=millis();
//for ten seconds maximize the linear actuator distance
while (millis() < w + 10000) {
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
}
//now take a reading of the potentiometer and store value as Maximum distance
sensorMax = analogRead(sensorPin);
// signal the end of the calibration period
digitalWrite(ledPin, LOW);
}
//function: Loop
//purpose: Once setup and calibration are complete, we have enter an infinite loop.
// This loop will run as long as there is power to the microcontroller. We
// will control the linear actuator and take readings from it. Based on these
// reading we will move the linear actuator to its proper position.
void loop() {
delay(10); //delay so that we dont overwhelm the sensor input
sensorValue = analogRead(sensorPin); //take a reading from the potentiometer
delay(10);
// if the switch is high, motor will turn on one direction.
// As long as we havent passed our minimum distance
if ((digitalRead(switchPin) == HIGH)&&(sensorValue>min_dist)) {
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
}
// if the switch is low, motor will turn in the other direction:
// As long as we havent passed our maximum distance
else if((digitalRead(switchPin) == LOW)&&(sensorValue<max_dist)){
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
}
// tell the motor to stop if above conditions are not met
else
{
digitalWrite(motor1Pin,LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin,LOW); // set leg 2 of the H-bridge low
}
}
/*
blinks an LED
*/
void blink(int whatPin, int howManyTimes, int milliSecs) {
int i = 0;
for ( i = 0; i < howManyTimes; i++) {
digitalWrite(whatPin, HIGH);
delay(milliSecs/2);
digitalWrite(whatPin, LOW);
delay(milliSecs/2);
}
}
Little point in including all the code - what you should be doing is cutting the code down to the minimum required to demonstrate the problem, using serial prints or whatever you have to hand.