On this page you will find a quick experiment I did to test to see if it is possible to turn a regular potentiometer into a rotation sensor by using the analog pins on an Arduino.
I managed to get its accuracy to be within one degree, but you might be able to get it even better with higher quality potentiometers and some experimentation.
You can, of course, just buy a rotation sensor, but I didn’t have one on hand when I needed it, so I had to get creative.
This technique could also be applied to make a DIY tilt sensor of sorts or perhaps for making a device that is able to measure angles.
Wiring the Potentiometer to the Arduino
You can check out the link to the Arduino site below for information on how to wire the potentiometer to the Arduino. I’m using the same setup.
http://www.arduino.cc/en/Tutorial/Potentiometer
One thing you need to keep in mind is that you should use a potentiometer which increases its resistance linearly. Some potentiometers increase their resistance logarithmically. These types won’t work.
Arduino Sketch
Here is the Arduino “Angle Finder” code.
float maxreading=170; float minreading=30; int anglePin = 2; void setup() { Serial.begin(9600); } void loop() { float multValue = ((maxreading-minreading)/180); float input = analogRead(anglePin); //Uncomment the line below when finding the maximum and minimum readings by turning your potentiometer 180 degrees. //Serial.println(analogRead(anglePin)); float angleReading = (input - minreading) / multValue; //Uncomment the line below to get the angle reading. //Serial.println(angleReading); delay(500); }
Calibrating the Potentiometer
In the code above there are two variables that need to be changed to match your potentiometer. They are named maxreading and minreading.
To find these values, the first thing you need to do is uncomment the line with the text “Serial.println(analogRead(anglePin));”
Upload the Sketch and open the Serial Monitor. You should see a reading from your potentiometer. The number should either decrease or increase when you rotate your potentiometer.
Mark a 0 degree mark on your potentiometer and a 180 degree mark located 180 degrees away from the 0 degree mark. The zero degree mark should be on the side of the potentiometer which gives the lower numbers in the Serial Monitor.

Align your potentiometer to the 0 degree mark and use the value displayed in the Serial Monitor for the minreading variable.
Next, align your potentiometer to the 180 degree mark and use the new value displayed in the Serial Monitor for the maxreading variable.
Once this is done, you can go ahead and comment out the “Serial.println(analogRead(anglePin));” line again.
To get the angle reading in the Serial Monitor after you have everything set up, uncomment the “Serial.println(angleReading);” line and re-upload the sketch. After this has been done you should see your potentiometer’s angle displayed in the Serial Monitor.
Accuracy
You might find that your angle value jumps around by a certain amount, possibly because the Arduino analog pins are so sensitive. I know mine did, but it stayed within a degree. This is accurate enough for many applications, but not all.
Some potentiometers seem to work better than others. The potentiometers I had on hand when I tried this probably weren’t the best. They were the grab bag variety. Higher resistance values seem to work better than lower resistance values.
Try experimenting to see how good you can get it.
Have Fun!

