The first read
Three wires, no library and a sketch that prints the reading five times a second. Turn the wheel from stop to stop and the number runs from 0 to full scale: 1023 on an Uno or a Pico, 4095 on an ESP32 or an ESP32-S3.
Three wires
GND to GND. VCC to your board's logic supply: 5V on an Uno, 3V3 on the three 3.3 V boards. SIGNAL to an analog input. NC stays unconnected.
The analog pin is the same in every sketch in this book: A0 on an Uno, GPIO 34 on an ESP32, GPIO 4 on an ESP32-S3, GP26 on a Pico. On the two ESP32s both pins are on ADC1, the converter that keeps working when Wi-Fi is on. GPIO 34 on a classic ESP32 is input-only, which costs nothing here: an analog input is all this pin has to be.
If your block came with a TinkerBlock cable, it carries all four wires, and the NC wire simply ends at a pin that goes nowhere.
The sketch
One call does the work. analogRead(POT_PIN) measures the voltage on the pin
and returns a whole number, from 0 at 0 V up to ADC_MAX at the top of the
ADC's range. There is no pinMode: analogRead sets the pin up itself.
ADC_MAX is not used for anything yet except the printout, but every later
sketch divides by it, and it is the one number that changes from board to
board. Set it now and the later sketches are right the first time.
What you should see
The serial monitor at 115200 prints a line five times a second. Turn the wheel slowly from one stop to the other:
0 of 4095
0 of 4095
812 of 4095
2034 of 4095
3377 of 4095
4095 of 4095Which stop gives 0 depends on the part, and a divider you can turn explains why nobody can tell you in advance. With the wheel still, the last digit or two will wander. That is normal, and smoothing a jittery reading settles it.
The code
No library and no pinMode: analogRead sets the pin up itself. Change POT_PIN to the pin you wired SIGNAL to, and ADC_MAX to your board's full scale.
/*
Disc Potentiometer - first read TK07 / /p/tk07
Wiring. Count from the square pad on the TinkerBlock board, wheel
up, header at the bottom:
GND -> GND
VCC -> 5V on an Uno; 3V3 on an ESP32, ESP32-S3 or Pico
(at one stop, SIGNAL gives your pin whatever VCC is)
NC -> nothing (unconnected on the board)
SIGNAL -> A0 on an Uno, GPIO 34 on an ESP32, GPIO 4 on an
ESP32-S3, GP26 on a Raspberry Pi Pico
Arduino IDE
Tools > Board your board, e.g. ESP32S3 Dev Module
Tools > Port the one that appears when you plug in
Tools > USB CDC On Boot Enabled (ESP32-S3 only)
No library needed.
*/
// The pin SIGNAL is wired to.
// Uno: A0. ESP32: 34. ESP32-S3: 4. Pico: 26.
const int POT_PIN = 4;
// What analogRead returns at full scale on your board.
// Uno: 1023. ESP32, ESP32-S3: 4095. Pico: 1023.
const int ADC_MAX = 4095;
void setup() {
Serial.begin(115200);
}
void loop() {
int reading = analogRead(POT_PIN); // 0 at one stop, ADC_MAX at the other
Serial.print(reading);
Serial.print(" of ");
Serial.println(ADC_MAX);
delay(200); // five lines a second
}POT_PIN is A0 on an Uno and the GPIO number on the others: 34 on an ESP32, 4 on an ESP32-S3, 26 on a Pico. ADC_MAX is 1023 on an Uno and on a Pico (arduino-pico's analogRead defaults to 10 bits), 4095 on the two ESP32s.
The same read in MicroPython, for an ESP32, an ESP32-S3 or a Pico. ADC reads the pin, and read_u16() returns 0 to 65535 on every one of them.
"""
Disc Potentiometer - first read, MicroPython TK07 / /p/tk07
Wiring. Count from the square pad on the TinkerBlock board, wheel
up, header at the bottom:
GND -> GND
VCC -> 3V3 (never 5V: at one stop, SIGNAL gives your pin VCC)
NC -> nothing (unconnected on the board)
SIGNAL -> GPIO 34 on an ESP32, GPIO 4 on an ESP32-S3,
GP26 on a Raspberry Pi Pico
Thonny
Run > Configure interpreter MicroPython (ESP32) or
MicroPython (Raspberry Pi Pico)
Save it to the board as main.py to run it on every power-up.
Nothing to install: machine, sys and time are built in.
"""
from machine import ADC, Pin
import sys
import time
# The GPIO number SIGNAL is wired to. ESP32: 34. ESP32-S3: 4. Pico: 26.
POT_PIN = 4
pot = ADC(Pin(POT_PIN))
if sys.platform == "esp32": # both ESP32s report "esp32"
pot.atten(ADC.ATTN_11DB) # measure up to about 3.1 V
while True:
reading = pot.read_u16() # 0 at one stop, 65535 at the other
print(reading)
time.sleep_ms(200) # five lines a secondThere is no Uno here: an Uno cannot run MicroPython. On the ESP32 ports the atten() line matters: without it the ADC measures only up to about 1 V, and the reading hits 65535 under a third of the way round. The Pico has no atten() and does not need one.
When it does not work
Check that VCC is connected: with nothing on it the track has no top, and SIGNAL sits at 0 V. Then check POT_PIN is the pin SIGNAL is really on. On an Uno that is A0, not 0: analogRead(0) happens to mean A0 there, but on the other boards the number is a GPIO number.
ADC_MAX is set for a different board. On an ESP32 the reading goes to 4095, so a sketch that assumes 1023 thinks it has hit the top a quarter of the way round. Set ADC_MAX to the value in the comment for your board.
Set Tools > USB CDC On Boot to Enabled and upload again. Without it the S3's USB port does not bring up a serial port at boot, so the sketch runs with nowhere to print.
That is read_u16(), which scales every board's ADC to 16 bits so the same code runs on an ESP32 and a Pico. The underlying ADC is still 12-bit: the number moves in steps of 16 rather than 1.
Why it is 1023 on one board and 4095 on another, and what it is a fraction of.
What the number means →Edit this page — content/books/disc-potentiometer/the-first-read.mdx
Questions about this product
See what other owners have asked, and read their solutions.
Disc Potentiometer
Loading discussions…
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.