Sensing the analog world

3. Reading the potentiometer

In the Electronics crash course we talked about alternating current vs. direct current and also introduced the potentiometer, a variable resistor. Remember that according to Ohm's law, resistance is proportionate to voltage.


Analog Inputs

To sense a gradually changing electrical signal, we'll use Arduino's analog inputs, located on the left side of the board. These special pins are connected to the Arduino's analog to digital converter (ADC), equipped to convert an analog signal between 0V and 5V into a range of numbers from 0-1023 (zero counts as a value). 

Now place the potentiometer pins in three different rows of the breadboard. Connect the middle pin to the analog port A0 and the other two to the ground (-) and power (+) rails of the breadboard respectively.


Now upload the following code to the Arduino:

int potPin = 0;
 
void setup() 
{
  Serial.begin(9600);
}
 
void loop() 
{
  int reading  = analogRead(potPin);
  Serial.println(reading);
  delay(500);
}

Turn the knob on the variable resistor and you will see the number change between 0 and 1023.

The Serial Monitor is displaying the analog reading value from A0 using the line:

  int reading  = analogRead(potPin);

The voltage at A0 is being transformed into a number between 0 and 1023.