Read a pot, print to Serial Monitor

The USB connection is used for serial communication between Arduino and computer.

The potentiometer pin 2 is connected to Arduino pin A0. analogRead is then used to measure the voltage at the pin. (The potentiometer rating doesn’t matter, but avoid lower resistances than 5k, so you don’t get an uneconomically high current flow.) You will get a value between 0 and 1023 (where 0 equals ground and 1023 equals +5 volts).

Use a small delay to let the circuitry rest before doing the subsequent read, or the value can be erroneous.

Code

void setup() {
  Serial.begin(9600); // Set up serial communication with the computer
}
 
void loop() {
  int analogValue = analogRead(A0);  // Read the input on analog pin 0
  Serial.println(analogValue);       // Print value to serial monitor
  delay(1);                          // Don't do analogRead too often
}