Built-in project · Arduino Uno R3

Mood dial on an Arduino Uno

Turn the potentiometer on A0 and the servo on pin 9 turns to match: map() stretches 0 to 1023 onto 0 to 179 degrees. The whole build, an Arduino Uno and 2 more parts, 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 15 minutes to build and run. Runs in your browser. Free, and no account needed.

Build it as the Starter Projects Kit: the parts in numbered bags and a booklet that checks every step. Retails for about $48, free on Mokxi.

Mood dial · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Reading a potentiometer with analogRead
  • Rescaling numbers with map
  • Controlling a servo with the Servo library
  • Printing only when something changes
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 4

How it works

Turn the potentiometer on A0 and a servo on pin 9 turns to match. The potentiometer is a voltage divider: its middle pin slides from 0 V to 5 V, and analogRead turns that into 0 to 1023.

The map function stretches that range onto 0 to 179 degrees, which pointer.write sends to the servo through the Servo library. The sketch prints the knob value and angle only when the angle changes.

Tape an arrow to the servo horn and put a card with faces behind it, and it becomes a mood dial.

Parts list

4 parts, plus the jumper wires. They retail for about $33; 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
Potentiometer pin 1Arduino Uno R3 pin GND
Hobby servo pin GNDArduino Uno R3 pin GND
Hobby servo pin PWMArduino Uno R3 pin 9
Potentiometer pin 3Arduino Uno R3 pin 5V
Hobby servo pin VCCArduino Uno R3 pin 5V
Potentiometer pin 2Arduino 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
// Mood dial: turn a potentiometer and a servo points to match.
//
//   potentiometer   one end -> 5V, the other end -> GND, middle (wiper) -> A0
//   servo           brown -> GND, red -> 5V, orange -> pin 9
//
// The knob is a voltage divider: the wiper slides from 0 V to 5 V, and A0
// reads that as 0 to 1023. map() stretches 0..1023 onto the servo's 0..179
// degrees, and the horn follows the knob. Tape an arrow to the horn and a
// card of faces behind it, and it tells people how your day is going.
//
// A real servo jolts the 5 V line every time it starts to move. A 100 uF
// capacitor across the supply near the servo, as the kit book suggests, keeps
// that jolt from wobbling the knob's reading.

#include <Servo.h>

Servo pointer;
int lastAngle = -1;

void setup() {
  pointer.attach(9);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: mood dial");
}

void loop() {
  int knob = analogRead(A0);
  int angle = map(knob, 0, 1023, 0, 179);
  pointer.write(angle);
  if (angle != lastAngle) {
    lastAngle = angle;
    Serial.print("knob ");
    Serial.print(knob);
    Serial.print(", angle ");
    Serial.println(angle);
  }
  delay(15);
}

Code walkthrough

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

setup sketch.ino

Attaches the servo to pin 9 and starts the serial port. Attaching tells the library which pin to pulse.

void setup() {
  pointer.attach(9);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: mood dial");
}

loop sketch.ino

Reads A0, maps 0 to 1023 onto 0 to 179, writes that angle to the servo, and prints it if it changed. The 15 ms delay gives the servo time to move between updates.

void loop() {
  int knob = analogRead(A0);
  int angle = map(knob, 0, 1023, 0, 179);
  pointer.write(angle);
  if (angle != lastAngle) {
    lastAngle = angle;
    Serial.print("knob ");
    Serial.print(knob);
    Serial.print(", angle ");
    Serial.println(angle);
  }
  delay(15);
}

Troubleshooting

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

  • The servo jitters This project
    A real servo can pull the 5 V line down. A 100 uF capacitor near it steadies the supply.
  • The servo goes the wrong way This project
    Swap the two outer pins of the potentiometer.
  • The reading jumps around or only covers part of the range. Potentiometer
    Wire the two outer legs to 5 V and ground and the middle leg (the wiper) to an analog pin. Using the wrong two legs gives a dead or partial range.
  • Turning the knob does nothing, or the value is stuck at 0 or 1023. Potentiometer
    Check that the wiper goes to an analog input (A0 to A5 on an Uno), not a digital pin. Make sure both ends are connected, since a floating end gives noise.
  • The servo jitters, twitches or resets the board when it moves. Hobby servo
    Power it from its own 5 V supply, not the Arduino 5 V pin, and join the grounds. A hobby servo can pull several hundred milliamps. Add a 100 uF to 470 uF capacitor across its supply.
  • The servo hums at the end of its travel or will not reach full angle. Hobby servo
    Stay inside the pulse range, about 1000 to 2000 us for most hobby servos. Pushing past the end stop makes it strain. Calibrate the min and max pulse for your servo.
  • 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.