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.
BeginnerRuns in your browser. Free, and no account needed.
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);
}
}
Parts list
5 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R4 Minima
- 1 × Full-size breadboard
- 1 × Potentiometer
- 1 × Resistor, 220 Ω
- 1 × LED, yellow
How it is wired
5 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: Arduino Uno R4 Minima pin GND; Potentiometer pin 1; LED, yellow pin C
- Arduino Uno R4 Minima pin 9; Resistor, 220 Ω pin 1
- Arduino Uno R4 Minima pin 5V; Potentiometer pin 3
- Arduino Uno R4 Minima pin A0; Potentiometer pin 2
- Resistor, 220 Ω pin 2; LED, yellow pin A
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.