Uno R4 knob
A potentiometer on an Uno R4’s A0 fades an LED on pin 9, and the monitor prints the same pin in ten bits and in the converter’s own fourteen. The whole build, an Arduino Uno R4 and 3 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.
The parts retail for about $26. On Mokxi they're free.
What you'll learn
- Reading a knob with an analog input
- Fading an LED with PWM
- Scaling one range to another
- The R4's 14-bit converter and
analogReadResolution
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R4 Minima
- Parts: 5
How it works
A potentiometer between 5V and GND has its wiper on A0. The sketch reads it and turns that into a brightness for a yellow LED on pin 9, behind a 220 ohm resistor. Turn the knob and the LED fades.
analogRead on the Uno R4 returns ten bits, 0 to 1023, the same as an older Uno. The sketch scales that to 0 to 255 for analogWrite.
Twice a second it prints the reading and the duty. It also switches to 14 bits, reads the same pin again, prints that value out of 16383, and switches back to 10 bits. The converter always had the extra bits.
Parts list
5 parts, plus the jumper wires. They retail for about $26; on Mokxi they're free.
- 1 × Arduino Uno R4 Minima
- 1 × Full-size breadboard
- 1 × PotentiometerIn Halloween Prop Kit, Starter Projects Kit.
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, yellowIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
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 pin | Connects to |
|---|---|
| Potentiometer pin 1 | Arduino Uno R4 Minima pin GND |
| LED, yellow pin C | Arduino Uno R4 Minima pin GND |
| Resistor, 220 Ω pin 1 | Arduino Uno R4 Minima pin 9 |
| Potentiometer pin 3 | Arduino Uno R4 Minima pin 5V |
| Potentiometer pin 2 | Arduino Uno R4 Minima pin A0 |
| LED, yellow pin A | Resistor, 220 Ω pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Analog: a knob on A0 sets the brightness of an LED on D9.
//
// Wire a 10 k potentiometer between 5V and GND with its wiper on A0, and an
// LED and a 220 ohm resistor from D9 to GND. With nothing wired, A0 floats and
// the reading means nothing, which is the honest answer rather than an
// invented one.
//
// Two things about this board are worth knowing while you turn the knob:
//
// * The converter is **fourteen bits**, but analogRead returns **ten**
// (0 to 1023) because that is what the Arduino core does and what every
// sketch written for an Uno expects. analogReadResolution(14) below asks
// for the rest, and the report prints both.
// * Full scale is 5 V, not 3.3. An Uno R4 runs its analogue supply at 5 V,
// which is why a sensor written for an Uno moves across without a divider.
//
// analogWrite on D9 is a GPT channel at 490 Hz, the same rate an Uno's
// phase-correct pins run at. Which GPT channel D9 is, is this model's belief
// and is marked unverified in docs/uno-r4.md section 7.
const int KNOB = A0;
const int LED = 9;
unsigned long lastReport = 0;
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
Serial.println("Mokxi Arduino Uno R4 Minima: analog example");
Serial.println("A0 in counts, D9 in duty. Turn the knob.");
}
void loop() {
int counts = analogRead(KNOB); // ten bits by default: 0..1023
int duty = (counts * 255) / 1023; // analogWrite takes 0..255
analogWrite(LED, duty);
unsigned long now = millis();
if (now - lastReport >= 500) {
lastReport = now;
Serial.print("A0 = ");
Serial.print(counts);
Serial.print(" of 1023, duty ");
Serial.print(duty);
// The same pin at the converter's own width, to show what the ten-bit
// default is hiding.
analogReadResolution(14);
Serial.print(", raw ");
Serial.print(analogRead(KNOB));
Serial.println(" of 16383");
analogReadResolution(10);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Starts serial at 115200 baud and sets the LED pin as an output. It prints a startup line and a hint to turn the knob. The knob pin needs no setup.
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
Serial.println("Mokxi Arduino Uno R4 Minima: analog example");
Serial.println("A0 in counts, D9 in duty. Turn the knob.");
}loop sketch.ino
Reads KNOB and scales the count to a duty from 0 to 255 with (counts * 255) / 1023. It writes that to the LED with analogWrite. Every 500 ms it prints the count and duty, then briefly sets analogReadResolution(14) to print the raw 14-bit value, and returns to 10 bits.
void loop() {
int counts = analogRead(KNOB); // ten bits by default: 0..1023
int duty = (counts * 255) / 1023; // analogWrite takes 0..255
analogWrite(LED, duty);
unsigned long now = millis();
if (now - lastReport >= 500) {
lastReport = now;
Serial.print("A0 = ");
Serial.print(counts);
Serial.print(" of 1023, duty ");
Serial.print(duty);
// The same pin at the converter's own width, to show what the ten-bit
// default is hiding.
analogReadResolution(14);
Serial.print(", raw ");
Serial.print(analogRead(KNOB));
Serial.println(" of 16383");
analogReadResolution(10);
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED flickers or the reading jumps This project
Check that the pot has 5V on one end, GND on the other and the wiper on A0. A floating A0 gives meaningless readings. - The LED does not light This project
The LED's anode goes through the 220 ohm resistor to pin 9 and its cathode to GND. Check the direction. - 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 LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin. - A blue or white LED stays dark on 3.3 V. LED
Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.