Reading it · 07 of 11

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

Four wires out, one block in
Your board
DIG to
D2
ANA to
A0
VCC to
5V
On an Uno, VCC goes to 5V: the block in IN runs from it, and DIG rises to it. D2 reads the decision with digitalRead, A0 the number with analogRead. Count the OUT pins from the square pad: GND, VCC, DIG, ANA.

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 791

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

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

When it does not work

DIG changes but ANA stays at 0.

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.

ANA changes but DIG never does.

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.

DIG flickers between 0 and 1 at one spot.

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.

The serial monitor stays empty on my ESP32-S3.

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.

Where this goes next

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

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