Project 3.01 Read the Potentiometer
Potentiometers are useful for measuring the position of a dial or for use in other position sensors. The potentiometer on the Arno produces a voltage signal that is proportional to the position of the thumbwheel. This simple sketch reads the output voltage of the thumbwheel potentiometer using the chip’s built-in analog-digital converter.
Concepts: ADC, analogRead, voltage divider
Circuits:
Concepts: ADC, analogRead, voltage divider
Circuits:
This is the first project where we assign an analog pin number to a variable:
int POT1 = A0;
The value of the potentiometer is read using an analogRead statement:
potRead = analogRead(POT1);
The potentiometer has three connectors: one is connected to the 5V power supply, one to ground (0 V), and the third is the signal that’s connected to pin A0 and ranges between 0 and 5V. The 10-bit ADC (analog-digital converter) in the 32U4 chip returns an int value between 0 and 1024 that is proportional to the output voltage of the potentiometer. We convert the value to volts by multiplying it by 5 (the power voltage of the Arno board and the reference voltage of the ADC) and dividing by 1024 (the range of the ADC). We want the result to be a float variable so that the decimal part can be kept. Since we start with an int variable, we must cast the it into a float:
volts = float(potRead*5) / 1024;
The Serial.print statements should mostly look familiar. We add a second argument to the statement where we print the float value to tell the computer how many decimal places to print:
Serial.print(volts,3);
int POT1 = A0;
The value of the potentiometer is read using an analogRead statement:
potRead = analogRead(POT1);
The potentiometer has three connectors: one is connected to the 5V power supply, one to ground (0 V), and the third is the signal that’s connected to pin A0 and ranges between 0 and 5V. The 10-bit ADC (analog-digital converter) in the 32U4 chip returns an int value between 0 and 1024 that is proportional to the output voltage of the potentiometer. We convert the value to volts by multiplying it by 5 (the power voltage of the Arno board and the reference voltage of the ADC) and dividing by 1024 (the range of the ADC). We want the result to be a float variable so that the decimal part can be kept. Since we start with an int variable, we must cast the it into a float:
volts = float(potRead*5) / 1024;
The Serial.print statements should mostly look familiar. We add a second argument to the statement where we print the float value to tell the computer how many decimal places to print:
Serial.print(volts,3);