Built-in project · Arduino Uno R3

Analog rotation sensor on A0, on an Arduino Uno

A rotation sensor module on A0: a pot with its wiper on S, read five times a second as a count from 0 to 1023 and in millivolts. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $29. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Reading a voltage with analogRead
  • How a potentiometer makes a variable voltage
  • Converting a 10-bit reading to millivolts
  • Printing a labeled reading
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 2

How it works

The rotation sensor module is a potentiometer. Its ends are on 5V and GND, and the wiper goes to the S pin, which is wired to A0. Turning the knob moves the voltage on A0 anywhere from 0 V to 5 V.

Five times a second the sketch reads A0, which gives a number from 0 to 1023. It also converts that to millivolts and prints both. The math uses whole numbers: the reading times 5000, divided by 1023.

Parts list

2 parts, plus the jumper wires. They retail for about $29; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Analog rotation sensor pin VCCArduino Uno R3 pin 5V
Analog rotation sensor pin GNDArduino Uno R3 pin GND
Analog rotation sensor pin SArduino Uno R3 pin A0

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Analog: read a voltage on A0 and print it five times a second.
//
// Wire a potentiometer with its ends to 5 V and GND and its wiper to A0, or
// just two resistors making a divider. analogRead returns 0..1023 for 0..5 V
// against the AVCC reference: a 10-bit conversion at 125 kHz, 104 us, which
// analogRead waits out.
//
// The millivolts are worked out in integer arithmetic. There is no soft-float
// runtime here, so a sketch cannot multiply floats, but it can print one,
// and (raw * 5000) / 1023 needs nothing but a 32-bit multiply and divide.

const int SENSOR_PIN = A0;

void setup() {
  Serial.begin(115200);
  Serial.println("Mokxi Arduino Uno: analogRead on A0");
}

void loop() {
  int raw = analogRead(SENSOR_PIN);
  long millivolts = (long)raw * 5000L / 1023L;

  Serial.print("A0 = ");
  Serial.print(raw);
  Serial.print("  (");
  Serial.print(millivolts);
  Serial.println(" mV)");

  delay(200);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Opens serial at 115200 baud and prints a line saying the sketch reads A0. The analog pin needs no other setup.

void setup() {
  Serial.begin(115200);
  Serial.println("Mokxi Arduino Uno: analogRead on A0");
}

loop sketch.ino

Reads SENSOR_PIN with analogRead to get raw. Then raw * 5000 / 1023 gives millivolts, using long so the multiplication does not overflow. It prints both numbers and waits 200 ms.

void loop() {
  int raw = analogRead(SENSOR_PIN);
  long millivolts = (long)raw * 5000L / 1023L;

  Serial.print("A0 = ");
  Serial.print(raw);
  Serial.print("  (");
  Serial.print(millivolts);
  Serial.println(" mV)");

  delay(200);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The reading is stuck at 0 or 1023 This project
    Check S goes to A0 and the module has VCC on 5V and GND on ground.
  • Garbled text in the monitor This project
    Set the monitor to 115200 baud to match Serial.begin.
  • analogRead stays at zero or jumps randomly. Analog rotation sensor
    Connect + to the supply, - to ground and S to an analog pin. S is the wiper. A floating S pin gives random values. Share the ground with the Arduino.
  • Turning it the right way gives backward numbers. Analog rotation sensor
    Swap the + and - wires, or reverse the value in code with 1023 minus the reading. Small jitter is normal, so average a few samples.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.