The first read
A TK08 potentiometer in IN, four wires from OUT, and a sketch that prints DIG and ANA side by side four times a second. Turn the TK08 and ANA climbs steadily; DIG sits at 0, then jumps to 1 at one point and stays there. That point is the knob.
A block in, four wires out
Push a TK08 Rotary Potentiometer into the IN socket, its parts facing the same way as the TK29's. It is the easiest block to learn on: turning it moves the input anywhere from 0 V to VCC, slowly, under your hand.
Then four wires from OUT. GND to GND. VCC to 5V on an Uno, 3V3 on the three 3.3 V boards. DIG to a digital pin and ANA to an analog pin; the pins are the same in every sketch in this book. Put the slide switch at H.
The sketch
Two calls do the work. digitalRead(DIG_PIN) returns the comparator's
answer. analogRead(ANA_PIN) returns the TK08's voltage, the thing the
comparator is deciding about. There is no INPUT_PULLUP: the TK29 has its
own 10 kΩ pull-up on DIG, so plain INPUT is right.
What you should see
With the TK29's knob somewhere in the lower half and the TK08 turned down, the serial monitor at 115200 prints DIG 0 and a small ANA. Turn the TK08 slowly up:
DIG 0 ANA 112
DIG 0 ANA 245
DIG 0 ANA 371
DIG 1 ANA 402
DIG 1 ANA 560
DIG 1 ANA 791ANA climbs with every step. DIG does nothing, then changes once, at one reading, and stays. Where it changed is the threshold: here between 371 and 402 on an Uno, somewhere near 1.9 V. Your numbers depend on where the knob is. Turn back down and DIG returns to 0 at about the same point, and the TK29's red LED comes on as it does.
That pair is the whole block: the number your ADC makes, and the decision the comparator makes from the same voltage, with no code in between.
The code
No library. digitalRead returns DIG, the comparator's answer, and analogRead returns ANA, the TK08's voltage as a count: 0 to 1023 on an Uno and a Pico, 0 to 4095 on an ESP32 or ESP32-S3. Change the two pins to the ones you wired.
/*
Analog to Digital Signal - first read TK29 / /p/tk29
A TK08 Rotary Potentiometer, or any analog block, pushed into IN,
parts facing the same way. Slide switch at H.
Wiring, OUT to your board. Count from the square pad, parts up,
OUT at the bottom:
GND -> GND
VCC -> 5V on an Uno; 3V3 on an ESP32, ESP32-S3 or Pico
(DIG is pulled up to VCC)
DIG -> D2 on an Uno, GPIO 25 on an ESP32, GPIO 7 on an
ESP32-S3, GP15 on a Raspberry Pi Pico
ANA -> 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. Arduino Uno
Tools > Port the one that appears when you plug in
Tools > USB CDC On Boot Enabled (ESP32-S3 only)
No library needed.
*/
// DIG_PIN, then ANA_PIN.
// Uno: 2 and A0. ESP32: 25 and 34. ESP32-S3: 7 and 4. Pico: 15 and 26.
const int DIG_PIN = 2;
const int ANA_PIN = A0;
void setup() {
Serial.begin(115200);
pinMode(DIG_PIN, INPUT); // the block has its own pull-up
}
void loop() {
int dig = digitalRead(DIG_PIN); // the decision: 1 or 0
int ana = analogRead(ANA_PIN); // the number it was made from
Serial.print("DIG ");
Serial.print(dig);
Serial.print(" ANA ");
Serial.println(ana);
delay(250); // four times a second
}The slide switch is at H, so DIG is 1 while ANA is above the knob. Slide it to L and run again: DIG turns over and ANA does not change. The TK29's LED lights whenever DIG prints 0.
The same read in MicroPython, for an ESP32, an ESP32-S3 or a Pico. value() returns DIG as 1 or 0, and read_u16 returns ANA as a count from 0 to 65535 on all three.
"""
Analog to Digital Signal - first read, MicroPython TK29 / /p/tk29
A TK08 Rotary Potentiometer, or any analog block, pushed into IN,
parts facing the same way. Slide switch at H.
Wiring, OUT to your board. Count from the square pad, parts up,
OUT at the bottom:
GND -> GND
VCC -> 3V3 (never 5V: DIG is pulled up to VCC)
DIG -> GPIO 25 on an ESP32, GPIO 7 on an ESP32-S3,
GP15 on a Raspberry Pi Pico
ANA -> 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.
"""
import sys
import time
from machine import ADC, Pin
# DIG_PIN, then ANA_PIN.
# ESP32: 25 and 34. ESP32-S3: 7 and 4. Pico: 15 and 26.
DIG_PIN = 7
ANA_PIN = 4
dig = Pin(DIG_PIN, Pin.IN) # no pull: the block has its own
adc = ADC(Pin(ANA_PIN))
if sys.platform == "esp32": # ESP32 and ESP32-S3
adc.atten(ADC.ATTN_11DB) # the full range, to about 3.1 V
while True:
print("DIG", dig.value(), " ANA", adc.read_u16())
time.sleep_ms(250)There is no Uno here: an Uno cannot run MicroPython. On an ESP32 the atten line widens the ADC's range to about 3.1 V; without it the range stops near 1 V. The Pico's ADC has no such setting. Stop it with Ctrl-C.
When it does not work
ANA is not reaching the analog pin, or the pin in the sketch is not the one it is wired to. Count OUT from the square pad: GND, VCC, DIG, ANA, so ANA is the far end. On an ESP32, ANA must go to an ADC1 pin such as GPIO 34.
The knob is set beyond where the TK08 reaches, or above the comparator's input range. Turn the TK08 to about a third of its travel and turn the TK29's knob slowly until DIG changes. If DIG changes and the sketch does not, check the DIG wire.
That is the input sitting right at the threshold, and the comparator answering every bit of noise. It is normal for this board, which has no hysteresis. Turn the TK08 a little further and it settles.
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.
Finding out where the knob has put it, in volts, and putting it where you want.
Setting the threshold →Edit this page — content/books/analog-to-digital-signal/the-first-read.mdx
Questions about this product
See what other owners have asked, and read their solutions.
Analog to Digital Signal
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.