Lesson 8: Potentiometers and Analog Input

Let's learn about about analog inputs. A common use of an analog input is potentiometers (informally called a pot). In the context of robotics, a pot is usually used to determine what angle a mechanism is at. Another really cool use of a pot is in a string potentiometer. Code Red has used these for the past several years, and we highly recommend your team look into them if they haven't already. You can purchase them on AndyMark, or make them yourself for a little bit cheaper. Basically they are 3D printed enclosures with a retractable key chain wound around inside + a spring + a potentiometer. You can use the potentiometer to measure how far the string is pulled out.

We can use the AnalogPotentiometer class to read from a potentiometer. I'm only going to go over the basics of this object, but you can read the full documentation here if you want. Let's get down and dirty... declare and instantiate an AnalogPotentiometer object:

AnalogPotentiometer pot;
pot = new AnalogPotentiometer(0); // this pot is plugged into analog input 0

Just like a DigitalInput, we can use the get() method to get the state of the potentiometer. This time, get() is going to return a double instead of a boolean. This double represents the current reading of the potentiometer. Say that we have a pot attached to an arm, and are using it to get the angle. We could do something like this:

double angle = pot.get();
// do something with that angle, for example:
if (angle > 2) {
    // take action
}

If you need to know where a certain manipulator is, a limit switch is not always practical. In these cases, a potentiometer is usually needed, so I want you to practice with a real situation. I've actually already covered all I need to say for potentiometers. I am going to leave an exersise below, that you should really try, that modifies the above code.

It's worth noting that an AnalogPotentiometer's get() method does not return an angle when it is setup like we have set it up above. Rather, it just returns the value of that Potentiometer's analog input. Usually this value ranges from 0 - 5ish. You actually can setup a potentiometer to return an angle, but you need to use a different constructor. Measure the analog value of your potentiometer when it is at full range. You can then use this constructor which will make the get() method scale its output to the appriate degree value:

int channel = 0;
double fullRange = 4.32; // The analog value when your pot is at full range
double offset = 0; // Use this if you have a desired offset.
AnalogPotentiometer pot = new AnalogPotentiometer(channel, fullRange, offset);

See the official documentation for the AnalogPotentiometer class for more information

I do want to mention the AnalogInput class that the WPI Java Library offers. You should use that if you need to get analog input but are not using a potentiometer. I'm actually going to talk about that more in the next lesson if you need more detail.

Exercises


Using Code Red's Robot Library

Refer to the last lesson's "Using Code Red's Robot Library. The answer here is exactly the same, just use a VirtualizableAnalogInput to replace AnalogInput in your code.