analog to digital signal/Reading it/08. Setting the threshold
Reading it · 08 of 11

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

Setting the threshold
VCC
Knob, % of the way from the GND end25 %
Threshold
0.82 V
Usable travel
55 %
Per 10 % of travel
0.33 V
25 % of the way round from the end tied to GND gives 0.82 V. The knob divides VCC and nothing else, so the same position gives a different threshold at 3V3 and at 5V. You cannot read it off the knob; the sketch below finds it by watching DIG change.

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 mV

The 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.

adc_find_threshold.ino
/*
  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.

When it does not work

It never prints anything.

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.

It prints two different numbers for HIGH and LOW.

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.

It prints a burst of lines at one spot.

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.

The knob feels stiff, or has stopped at an end.

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.

Where this goes next

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

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

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.

Browse Modules and blocks on the forum