Potentiometer as Analog Input

First off I'm new here so if you have any suggestions for my forum etiquette please tell me!

I just started work on a door unlocking mechanism, and I was going to use a potentiometer as an input device. Basically, I would have a dial with a knob to turn, and depending on the position of the knob I would be able to enter different numbers.

I just got my hands on a 5 kOhm potentiometer, and hooked it up to my board, from the 5V output to the A1 analog input. I then set up the board to transmit the input data to my computer through the USB port. This is my code:

int val=0;//value to store the input data void setup(){ Serial.begin(9600);//set up serial communication } void loop(){ val= analogRead(1);//read value from A1 Serial.println(val);//display value }

Unfortunately, when I hooked up everything like I described, the board kept sending me "1023," which I believe means a full 5V across the input. Regardless of what position I turned he knob to, it always read the same thing (plus or minus 1 or 2). Can anyone shed some light on this situation?

Also, when I applied Ohm's law, I assumed that with an output of 50mA, that the voltage (at 5 kOhms resistance) should be about...250 Volts. That obviously can't be right (It's been a long time since I've taken any physics courses dealing with electricity :-[)

You most likely have the pot wired wrong. A pot has three terminals, two fixed end points and one movable wiper terminal (usually the center pin). You should wire the end points, one to +5vdc, one to ground pin. The wiper terminal then wires to the analog input pin. If the values don't increase with normal clockwise rotation of the pot's shaft just reverse the two end terminals. The pot is acting like an adjustable voltage divider, when the wiper is at the ground end point you will read 0 count, when at the +5vdc end point it will read 1023 counts, and any thing in between those values as you move the shaft. That make sense?

250 Volts. That obviously can't be right

That of course is a true statement. ;)

Lefty

5v --- | | | R1 | | ----- Wiper | | | R2 | | 0v---

The above shows a pot... really!

R1 + R2 will always equal the full resistance of the pot.

As you turn the wiper, R1 rises and R2 falls, or vice-versa.

The current flowing through the wiper to the Arduino analog input may be taken as "zero"... the impedance of the pin is high, as long as it is configured as an input, which it will be if you haven't done anything to upset that.

If R1 is 1/5th of R2, the voltage at the wiper will be 4v If R1 equals R2 the voltage at the wiper will be 2.5v If R2 is 1/5th of R1, the voltage at the wiper will be 1v

The pot creates a voltage divider.

Ohms law comes into this only so that you can see how much current is flowing through the pot. If the pot is too small, the current will be high enough to melt the pot or to overwhelm the current generating capabilities of your power supply.... the "5v" will drop to something lower.

(If the pot is too large, the current "straight" through the pot will be tiny, and then the very tiny current flowing into the Arduino input will cease to be "unimportant".)

(If the pot is too large, the current "straight" through the pot will be tiny, and then the very tiny current flowing into the Arduino input will cease to be "unimportant".)

With a statement like that one has to wonder what is a good safe and useful value range for a pot in this application?

If my memory and math is correct the output on that pin is capable of 40mA so at 5VDC it would be 125 ohms for the smallest value.

one has to wonder what is a good safe and useful value range for a pot in this application?

Yeah... sorry to have dodged that one. The 5k pot the OP had seemed quite reasonable... especially if he/ she is running the Arduino from a wall wart, not batteries. I hope some other (more brave) people will chime in with specific recommendations for new users.

=== While writing: A delay inside the main loop of the program would be a good idea, so that the very rapid reads occuring at the moment don't overwhelm the system's (relatively) slow ability to report those readings over the serial link....

void loop(){
  val= analogRead(1);//read value from A1
  Serial.println(val);//display value
  delay(300);
}

.. would give a little more than three readings a second... more than enough? And that wouldn't(?) overwhelm the serial channel.

No worries. Just thinking out loud.

That 5K pot only draws about a single milliamp max at 5V through that pin.

I was more wondering what the maximum useful value pot would be.

I was more wondering what the maximum useful value pot would be.

Well the answer, as almost always, is in the Atmel 328 datasheet:

The ADC is optimized for analog signals with an output impedance of approximately 10 k[ch937] or less. If such a source is used, the sampling time will be negligible. If a source with higher impedance is used, the sampling time will depend on how long time the source needs to charge the S/H capacitor, with can vary widely. The user is recommended to only use low impedance sources with slowly varying signals, since this minimizes the required charge transfer to the S/H capacitor.

I suspect a pots impedance, as seen by an analog input, is equal to at most one half the pots total resistance, it should be lower as the wiper leaves the midpoint of the pot in either direction.

So I would guess any pot of 20K ohms or lower would work well with the capabilities of the D/A input circuitry.

Lefty

Woah, alright first of all I really appreciate all of the responses!

@retrolefty: Good stuff. I rewired my pot the way you described, and it worked like a charm. I also added in the .3 s delay just so my eyes could keep up with the output. Everything works like I expected it to!

@tkbyd Also good stuff (with pictures!)

Ohms law comes into this only so that you can see how much current is flowing through the pot. If the pot is too small, the current will be high enough to melt the pot or to overwhelm the current generating capabilities of your power supply.... the "5v" will drop to something lower.

(If the pot is too large, the current "straight" through the pot will be tiny, and then the very tiny current flowing into the Arduino input will cease to be "unimportant".)

So basically what you're saying here is that in this respect, the voltage and resistance are fixed values (provided you're not turning the knob on the pot, of course). And that will give me a value for the current moving through the pot. Obviously, if the current becomes too high, either my pot fries, or my board?

@Mr. Swarf I too have been wondering what values of resistors I should use. I guess the only limitation would be a lower bound on the resistance (again, as to protect the board from frying?). I guess there's an upper bound depending on the resolution of the analog port (about .005 V, since 5V/1023=.005) as well.

Anyways, now that I've hooked up my pot properly, I noted that the analog values I get are definitely non-linear. I was thinking of putting a mathematical equation into my code (log, 1/x, etc.) that would essentially linearize the values I'm getting from the analog input. Is there anything blatantly wrong with this approach?

P.S. I probably should have majored in electrical engineering instead of bioengineering. Ah well ::)

I noted that the analog values I get are definitely non-linear.

Any chance you have an "audio" taper pot vs a linear taper?

Not that it's a big deal - you're going to move the knob to different positions, and use the reading there like they were numbers on a dial? So 1:00 would be one value (+/- some small range), 2:00 another, 3:00 a third, .. up to 11:00, which is about the limit of a typical pots movement, unless you have a multiturn pot.

Talking about multiturn pots, I've got a couple of these kinds of goodies out in the garage somewhere. http://cgi.ebay.com/Helipot-10-Turn-Pot-10K-Dial-/320635852782?pt=LH_DefaultDomain_0&hash=item4aa762d7ee

Disclaimer: There is a universal law that says even if you know you have something, if you can't locate it you really don't have it. Know what I mean? ;)

Lefty

Oh, for sure - someplace I have a drawer of little toggle switches which I can’t locate - had to resort to buying a couple today to move my project along.