Setting the threshold
The knob has no scale, and which way raises the threshold is not printed on it. So measure it: this sketch watches DIG, and at the moment it changes, reads ANA. That reading is the threshold, in millivolts. Or skip the numbers and set it by the LED, with the sensor where you want it to switch.
The knob is a share of VCC
The trimmer divides VCC, so the threshold is a share of it: a quarter of the way round from the GND end is about 0.82 V from 3V3 and about 1.25 V from 5V. It turns through a little less than one full turn, end stop to end stop, and there is no scale on it.
Nor is it printed which end is which. The trimmer's own sheet does not say which way round its legs are numbered, so this book does not say whether turning clockwise raises the threshold. Turn it a little and watch what happens; that takes ten seconds and is never wrong.
Measure it with the sketch
The comparator changes DIG at exactly one voltage: the knob's. So the moment DIG changes, the input is at the threshold, and reading ANA right then measures it. That is the whole sketch: remember DIG, and when it changes, print ANA in millivolts.
Sweep the TK08 slowly up and down through the change:
Sweep the input block slowly through the knob.
HIGH at 1204 mV
LOW at 1199 mV
HIGH at 1206 mVThe threshold is about 1.2 V. The few millivolts between lines are noise and the ADC's error, not the knob moving. Now turn the knob a little, sweep again, and you know which way is up.
Or set it by the LED
Most of the time you do not need the number. You need it to switch here. So put the sensor in the condition you want it to switch at: the light level of dusk, the loudness of a clap, the position of the dial. Then turn the knob slowly until the red LED just changes, and stop.
No board is needed for this, only power on VCC and GND. The LED shows LOW, so with the switch at H it goes out as the knob passes below the input.
The code
Remembers DIG, and when it changes, reads ANA in millivolts and prints both. On an ESP32 analogReadMilliVolts uses the chip's own calibration; on an Uno or a Pico the count is scaled by the ADC's full scale, which you set.
/*
Analog to Digital Signal - find the threshold TK29 / /p/tk29
A TK08 Rotary Potentiometer, or any analog block, pushed into IN,
parts facing the same way. Either switch position.
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;
// Uno and Pico only: the ADC's full scale, in mV.
// Uno: 5000. Pico: 3300.
const float FULL_SCALE_MV = 5000.0;
int lastDig;
float readMilliVolts() {
#if defined(ARDUINO_ARCH_ESP32)
return analogReadMilliVolts(ANA_PIN); // calibrated in the chip
#else
return analogRead(ANA_PIN) * FULL_SCALE_MV / 1023.0;
#endif
}
void setup() {
Serial.begin(115200);
pinMode(DIG_PIN, INPUT); // the block has its own pull-up
lastDig = digitalRead(DIG_PIN);
Serial.println("Sweep the input block slowly through the knob.");
}
void loop() {
int dig = digitalRead(DIG_PIN);
if (dig != lastDig) { // DIG has just changed...
float mv = readMilliVolts(); // ...so ANA is at the threshold
lastDig = dig;
Serial.print(dig == HIGH ? "HIGH at " : "LOW at ");
Serial.print(mv, 0);
Serial.println(" mV");
}
}Sweep the input block slowly through the threshold in both directions. Each line is one crossing. The number is the input's voltage at that moment, which is where the knob is set, within a few millivolts.
The same in MicroPython. On an ESP32 or ESP32-S3, read_uv uses the chip's calibration; on a Pico, the count is scaled to its 3.3 V full scale.
"""
Analog to Digital Signal - find the threshold TK29 / /p/tk29
A TK08 Rotary Potentiometer, or any analog block, pushed into IN,
parts facing the same way. Either switch position.
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 and sys are built in.
"""
import sys
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
def read_mv():
if sys.platform == "esp32":
return adc.read_uv() / 1000 # calibrated in the chip
return adc.read_u16() * 3300 / 65535
print("Sweep the input block slowly through the knob.")
last = dig.value()
while True:
now = dig.value()
if now != last: # DIG has just changed...
mv = read_mv() # ...so ANA is at the threshold
last = now
print("HIGH at" if now else "LOW at", round(mv), "mV")Sweep the input block slowly. MicroPython's loop is slower than compiled code, so a fast sweep reads ANA a little past the threshold; slowly, the difference is a few millivolts. Stop it with Ctrl-C.
When it does not work
DIG has not changed since the sketch started. Sweep the input block slowly from one end to the other. If still nothing, the knob is beyond what the block reaches, or above the comparator's range: about 1.8 V from 3V3. Turn it towards the other end.
A few millivolts apart is noise and the ADC's own error. Tens of millivolts apart usually means the block was moving fast: ANA is read a moment after DIG changes. Sweep more slowly, or average several crossings.
The input is sitting right on the threshold and DIG is chattering. Every line in the burst is a real change of DIG. Move the input block on, and the burst stops; the numbers in it are all the threshold.
It is a single-turn trimmer, and it has an end stop at each side of its travel. Turn gently: forcing it past the stop can break it. It is rated for about 200 adjustments, so set it and leave it rather than using it as a control.
A TK20 in the socket, a lamp on your board, and a threshold at the very bottom of the knob.
A light switch →Edit this page — content/books/analog-to-digital-signal/setting-the-threshold.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.