Obtaining user input using a potentiometer

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
javaben
 
Posts: 6
Joined: Thu Feb 02, 2012 1:39 am

Obtaining user input using a potentiometer

Post by javaben »

Frequently our projects need to obtain user inputted values. Sometimes a GUI display is the best fit, but sometimes, something simple, like a potentiometer, is the best fit - and it can be easier and cheaper too! There are many times where our projects could use a device such as this for obtaining user input.

Take the case of an Arduino controlled thermostat. The setpoint is a good place to obtain the user input using a potentiometer.

In questions I've received, it's been my experience that a lot of people would like to use a potentiometer for user input, but aren't sure how to go about this, with respect to using it in the software. They may be experienced enough to be able to read the value of the potentiometer's wiper from a pin, but not sure where to go from there within their software.

I've added a small page to my blog with additional insight into this, which is available here. http://bbqandbanjos.blogspot.com/2012/0 ... using.html

odometer
 
Posts: 98
Joined: Sun Aug 21, 2011 11:01 pm

Re: Obtaining user input using a potentiometer

Post by odometer »

javaben wrote: They may be experienced enough to be able to read the value of the potentiometer's wiper from a pin, but not sure where to go from there within their software.
Given that it's just simple math, maybe the real problem is unfamiliarity with how Arduino math works.

By the way, your article is not clear on whether the maximum value is 1023 or 1024. If it is a "10-bit analog-to-digital converter", then the maximum value would be 1023. Also, I see a typo in a variable name in your code. (temperatureRange vs tempratureRange)

If the programmer prefers to use only integers, she can do everything using only integers. She need only remember her algebra:

Code: Select all

// This will not work in integer arithmetic...
ratio = counts / 1023;
// because ratio will always be either 0 or 1
setpoint = (ratio * temperatureRange) + baseTemperature;

// So we will rewrite the code:
// since ratio is counts divided by 1023, we can instead write:
setpoint = ((counts / 1023) * temperatureRange) + baseTemperature;

// then, we use algebra to rewrite the equation, as follows:
setpoint = ((counts * temperatureRange) / 1023) + baseTemperature;
Another thing I have noticed, now that you mention it, is that a potentiometer can be used as a simple option selector.
Suppose we label our potentiometer dial, not with numbers, but rather with the letters A, B, and C.

Code: Select all

if (counts < 341) {
  // potentiometer is in lower 1/3 of range
  Serial.println("You chose Option A");
}
else if (counts < 682) {
  // potentiometer is in middle 1/3 of range
  Serial.println("You chose Option B");
}
else {
  // potentiometer is in high 1/3 of range
  Serial.println("You chose Option C");
}

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”