Introduction: (Intuitive) How a Potentiometer Works and How to Use One With Arduino
As a high school student with nothing to do in the summer, I recently started playing with arduino's. For my first project, the cheapest and most effective way to provide input for my arduino was a potentiometer. I found that they came in many forms from knobs to sliders to even joysticks. I read many guides and watched a couple videos, and all of them told me how to wire one up and gave generic explanations such as "it is a simple voltage divider" or "variable resistor, leaving to wonder why there was a third pin and how my arduino read the potentiometer. In a couple days, after playing with a voltmeter, battery, resistors, and a couple potentiometers, I figured it out, and thought what I learned could help out others new to these things.
So here is another instructable on potentiometers.
I neither am responsible for nor can guarantee any effects from this instructable- good or bad.
Step 1: Tools and Materials
Since this instructables is more about the theory these are all optional, but they can/will help you understand.
3) A couple resistors- to be safe- above 10 ohms
4) Power source; a AA battery works fine
5) Arduino, usb cable, and computer to code it
Step 2: What Its Made of and How It Works
If you look at the picture- I wish I had seen this earlier- you can see that the first and third pins are joined by a resistive material, and the middle pin is connected to a wiper, which can slide around, changing the amount of resistance the middle pin and the outer pins.
Note that as you turn the wiper, resistance between one of the outer pins and the middle pin decreases, while the resistance between the middle pin and the other outer pin increases. That is why if you wire either of the outer pins and the middle pin, you will have a variable resistor.
Also you can switch the outer pins and, as a result, the direction of rotation will be flipped. For example, if you had to rotate right for a certain outcome, after reversing the pins, you would have to rotate left to get the same outcome.
Step 3: How an Arduino Reads One
This is the part I had most trouble with. I would think, why can't you wire two of the pins, one to +5V and the other to the analog input pin, and vary its resistance? Eventually I realized that to get analog values, the arduino works like a voltmeter, with one one (imaginary) probe at +5V and the other (also imaginary) probe at the input pin.
If you try and read the voltage across a single resistor, You will get the same voltage, regardless of the the value of the resistor (unless its a short). To explain this, I will explain how a voltmeter works: voltmeters have a large internal resistance and are wired in parallel to the component you are trying to find the potential across.
SInce the voltmeter is in parallel, the voltage across the component is equal to that across the voltmeter. Because of this, a small amperage flows across the meter, and the meter knows this value- yes, a voltmeter is basically a type of ammeter.
Then using the formula V=IR, the meter multiplies this value by it own resistance to get a reading, and since it is parallel and the voltage across it is the same as the component, the voltmeter doesn't care about the resistance of the component (unless its near 0); its reading only depends on the power source.
To see for yourself:
1) Take a 100 ohm, or any reasonably high value resistor and wire it to a battery.
2) Read the voltage across it
3) Add another resistor in series (between the first resistor and the battery terminal) and repeat step 2 with the probes accross both resistors
4) Try it with no resistor, don't connect the two terminals of the battery; leave a gap. This is the same thing as putting the voltmeter accross the two ends of a battery
5) You will get the same reading every time
So how do we change the value?
The answer is add a resistor in series, but keep the voltmeter probes across the first one
Now, you will see the potential drop.
This is why we add resistors, when a led ( or any other component) is rated lower than the battery, the addition of the resistor lowers the potential across the led
Wait a second, if the middle wiper is connected the one of the arduino's voltmeter probes, then it is basically in the middle of two resistors in series (the resistive materials on either side of the wiper. It'll make more sense if you look at my drawing over the diagram of the potentiometer. So as you turn the knob, one resistor gets bigger, and the other gets smaller, and voila, the arduino gets a varying input.
Step 4: How to Wire One to an Arduino and How to Read the Input
To wire one, you wire the first pin to ground, the middle pin to the analog input pin of you choice, and the third pin to +5v. To switch the direction of rotation, just switch the first and third pin,
To get simple analog input, you need two very useful methods; analogRead() and map()
analogRead() tests the potential accross the analog input pin and returns a value between 0 and 1023
analogRead() has only one parameter: the value of the sensor pin.
Now map is technically not needed, but it is very convenient. What it does is scale the value to whatever rang you want, It has four parameters: lower bound of input, higher bound of input, lower bound of output, higher bound of output
so if you write map(0, 1023, 0, 10), instead of having 1023 when the knob is turned all the way you get 10, and if it turned halfway, you get around 5.
If you want, you can scale it yourself; map(), like many other methods, exists for the sake of convenience.
Step 5: Done, Any Questions?
That's it. I hope you know understand how a potentiometer works,
Still struggling a bit?
Well, google it.
Just kidding, don't hesitate to leave a question in the comments. I will try my best to answer them (if I can) as fast as I can.
Also remember, I am new this as well. If I have stated anything inaccurate, please comment, as I love learning more about these things.