Menu
CoddyTech

Read a Knob

Part of the Inputs section of Coddy's Arduino journey. Lesson 8 of 22.

A digital pin sees two values, HIGH or LOW. The pins marked A0 to A5 can do more: they are analog inputs and measure a voltage anywhere between 0 and 5V.

analogRead(A0) measures pin A0 and returns a whole number from 0 (0 volts) to 1023 (5 volts). That is 1024 steps, each about 5 thousandths of a volt. An analog read needs no pinMode().

A potentiometer is a knob with three legs. The outer two go to 5V and GND, and the middle one gives a voltage that follows the knob: 0V at one end, 5V at the other, 2.5V in the middle.

int reading = analogRead(A0);
Serial.println(reading);
challenge icon

Challenge

Easy

The knob's middle leg is on pin A0. Print what it reads:

  • read A0 with analogRead() into an int
  • print it, then wait 100 ms

Press Run, turn the knob on the board and watch the numbers, then Check.

Try it yourself

void setup() {
  Serial.begin(9600);
}

void loop() {
  // read A0, print the reading, wait 100 ms
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Inputs

Practice on your own: Arduino playground