Reading pots

Hi all,

I am new here and pretty new to the arduino so I apologise if I am posting in the wrong section or my question is too simple.

I am trying to use the Diecimila board to read 0-5v analogue inputs from various engine sensors, and then to output a PWM signal to control one servo which mechanically controls pressure ratio from a turbocharger. I am first simulating the system on the bench using rotary potentiometers instead of engine sensors for values. The problem I am having is that when I try to use a multiplier or divider of my pot values, the output value skips from negative to positive integers. I thought the problem may be the speed of the analogue read vs the time taken for the arithmetic, so I introduced a delay. It was still the same. I used a part of the "smoothing" code from the playground and it is still the same.

Can anyone help me get sensible numbers form the pots? What should I check?

Thanks
Greg

In addition,

I have just checked the pot outputs on a meter and it seems that the problem is with the pots themselves. This is strange because I used a simple program to check them previously and print the decimal value and they had a sensible range from 0-1023 which changed correctly with angle.

Now they seem to be putting out strange nonsensical values, non-linear and sometimes negative. Have I broken the pots? How could this happen?

One other thing to mention is that the board switched off (COM port unavailable and no power LED) a little while ago (a few hours after the pot value problem surfaced). The board smelt a bit hot so I unplugged it from USB and the component marked "ZL05" was hot (power resistor??). After a couple of minutes it was fine and worked again.

The pots are getting a good 5v supply, are connected correctly and are earthed correctly. They check out fine in a resistance check when not connected to the board.

Any ideas?

Thanks
Greg

Hi Greg, the negative numbers are probably due to a coding error. Try a simple sketch that reads a single pot and displays that value and the result of your multiplication to the serial port. If you read the pot value into a variable, make sure its an integer. If your multiplier is greater than 32 then store the result in a long variable (16 bit integers greater than 1024 times 32 will be negative).

If you still get negative numbers than post the results along with the sketch here.

Hi mem,

The sketch I am using to check the problem is this one here:

#include <Servo.h>

int potPin = 0; // select the input pin for the potentiometer
int val = 0; // variable to store the value coming from the sensor

Servo servo1;

void setup()
{
servo1.attach(14);
servo1.setMaximumPulse(2500);
Serial.begin(19200);
}

void loop()
{

{

val = analogRead(potPin); // read the value from the sensor
servo1.write(val/4); // send value /4 to servo
Serial.print(val, DEC); // print pot value
Serial.println(); // print line return

}
Servo::refresh();
}

I am only using it to check the value from the pot. As there is only one pot in this simple code I can also display the correct value of val/4 in serial monitor. I do not have the servo or any other pots connected at the moment. Everything is connected correctly and the pot seems ok on resistance test.

The problem with this code even, is that the first say 5 degrees of pot angle gives the full range of values (1023 down to about 20) then the values drop right off to less than 10 and stay there until the end of the pot wiper travel. :-?
Strangely, using this other piece of code the pot numbers are correct for the whole range?

int potPin1 = 0; // select the input pin for the potentiometer
int potPin2 = 1;
int ledPin = 13; // select the pin for the LED
float val1 = 0; // variable to store the value coming from the sensor
float val2 = 0;
float val3 = 0;

void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
Serial.begin(19200);

}

void loop() {
val2 = analogRead(potPin2); // read the value from the sensor
val1 = analogRead(potPin1);
val3 = val1/val2;
Serial.print(val1, DEC);
Serial.print(" ");
Serial.print(val2, DEC);
Serial.print(" ");
Serial.print(val3, DEC);
Serial.println();

digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(val1); // stop the program for some time
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(val1); // stop the program for some time

}

Is it the delay function that makes the difference?

Also, forgive my ignorance, but how do I get the serial monitor to display the fraction (or decimal place data)? The serial monitor displays the nearest whole number, even though I have used floats instead of ints in this code?

You are attaching the servo to pin 14 which is the same as analog pin 0, the same pin you have the pot connected to. Try attaching the servo to a digital pin between 2 and 12.

There is a routine to print floats at the end of this thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1207226548

Great!

Thanks very much. I will move the servo to pin 9.

Cheers! :slight_smile: