ESP32 Part 1 - Foundations

E04. Pinout, Strapping Pins, ADC, DAC and Touch

Learn which ESP32 pins are safe to use, then read a potentiometer with the 0-4095 ADC and turn the number into LOW, MID or HIGH.

Estimated time 3 hours

Learning outcomes

  • Pick safe pins and avoid the pins the ESP32 uses at start-up
  • Read a potentiometer on GPIO 34 as a number from 0 to 4095
  • Turn a reading into LOW, MID or HIGH with if / else if / else
  • Know that some ESP32 pins can also sense touch

Parts and preparation

ESP32 DevKit, 10 k ohm potentiometer, breadboard, jumper wires.

Before power: inspect wiring, confirm supply voltage and ensure all connected circuits share GND.

What you will do

Find out which pins are safe to use, then read a potentiometer (pot).

Sketch 1 prints the pot reading, from 0 to 4095. Sketch 2 turns that number into a word: LOW, MID or HIGH.

ESP32 DevKit highlighting strapping pins
Leave the start-up pins alone when you wire buttons and sensors.

Use the GPIO numbers, not Uno numbers

ESP32 pins are named by GPIO number, such as GPIO 2 or GPIO 34. Use the numbers printed on your board, not Uno names like D7 or A0.

On a classic ESP32 two groups need care. GPIO 34 to 39 can only be inputs - fine for a pot, no good for an LED. GPIO 6 to 11 are used by the board's own memory chip - never connect anything to them.

Start-up pins (strapping pins)

When the ESP32 starts, it checks a few pins to decide how to start. These are called strapping pins. Wiring a button or sensor to them can stop the board starting or uploading.

PinCourse advice
GPIO 0Used for uploading - keep buttons off it
GPIO 2Fine for an LED (often the on-board LED)
GPIO 5Fine as an output after start-up (the NeoPixel lessons use it)
GPIO 12Avoid - can stop the board starting
GPIO 15Avoid for inputs
GPIO 6-11Never use

Reading a pot: 0 to 4095

The Uno's analogRead gives 0 to 1023. The ESP32 gives 0 to 4095, so it can tell apart smaller changes. analogReadResolution(12) makes sure you get the 0-4095 range.

Power the pot from 3V3, not 5 V. Readings near the very ends of the turn are less accurate - that is normal.

Use GPIO 32 to 39 for anything you read while Wi-Fi is on. Many other pins stop reading when Wi-Fi is running.

Uno 10-bit versus ESP32 12-bit ADC scales
Uno: 0-1023. ESP32: 0-4095.
Uno R3ESP32 course
Reading range0-10230-4095
Pot pinA0GPIO 34
Pot power5V3V3

Touch pins and true analog output

Some ESP32 pins can feel a finger: touchRead(4) gives a smaller number when you touch a wire on GPIO 4 - try it in the challenge. A classic ESP32 can also output a real voltage between 0 and 3.3 V on GPIO 25 and 26 (the DAC). The course does not need the DAC.

Wiring and safe build sequence

  1. Potentiometer outer legs -> 3V3 and GND; middle leg (wiper) -> GPIO 34
  2. USB to DevKit; Serial Monitor 115200
Power rule: switch off before moving wires. Arduino I/O pins are control signals; high-current loads require a driver and suitable external supply.

Worked sketch 1: Read the pot

Download .ino

What this sketch is for: Turn the pot slowly from one end to the other and watch the numbers.

const int potPin = 34;                 // the pot's middle leg is on GPIO 34

void setup() {                         // runs once
  Serial.begin(115200);                // start Serial at the ESP32 speed
  analogReadResolution(12);            // readings will be 0 to 4095
}

void loop() {                          // runs over and over
  int reading = analogRead(potPin);    // read the pot
  Serial.println(reading);             // print the number
  delay(200);                          // slow the printing down
}

How the code works

  1. analogReadResolution(12) gives readings from 0 to 4095.
  2. GPIO 34 can only be an input - perfect for a pot.
  3. Power the pot from 3V3, not 5 V.

Worked sketch 2: LOW, MID or HIGH

Download .ino

What this sketch is for: Split the turn of the pot into three bands and print a word for each. Match the breadboard layout below before upload.

Breadboard layout for ESP32 potentiometer on 3V3, GND and GPIO 34
Breadboard for sketch 2: pot outer legs to 3V3 and GND; middle leg to GPIO 34. Power the pot from 3V3, not 5 V. Click to enlarge.
const int potPin = 34;                 // the pot's middle leg is on GPIO 34

void setup() {                         // runs once
  Serial.begin(115200);                // start Serial at the ESP32 speed
  analogReadResolution(12);            // readings will be 0 to 4095
}

void loop() {                          // runs over and over
  int reading = analogRead(potPin);    // read the pot
  Serial.print(reading);               // print the number first

  if (reading < 1365) {                // bottom third of the turn
    Serial.println("  LOW");           // print LOW after the number
  } else if (reading < 2730) {         // middle third
    Serial.println("  MID");           // print MID
  } else {                             // top third
    Serial.println("  HIGH");          // print HIGH
  }

  delay(250);                          // slow the printing down
}

How the code works

  1. 1365 and 2730 are about one third and two thirds of 4095.
  2. if / else if / else checks the tests in order and runs only the first one that is true.
  3. Build from the breadboard photo: 3V3 and GND to the pot ends; middle leg to GPIO 34.

Test and record evidence

Expected result: Sketch 1: the number rises from about 0 to 4095 as you turn the pot. Sketch 2: each line shows the number then LOW, MID or HIGH, changing as you turn.

Practical evidence checklist

Common faults and checks
  • Number never changes: check the middle leg is on GPIO 34 and the outer legs are on 3V3 and GND.
  • Never goes above 1023: add analogReadResolution(12) in setup().
  • Number jumps around a little: normal - the ESP32 reading is a bit noisy.
Challenge (extension - optional): Touch test: in a new sketch, print touchRead(4) every 200 ms with a jumper wire in GPIO 4. Touch the end of the wire and see how the number changes.

Check your understanding

Q1. What range does analogRead give on the ESP32 in this lesson?

Show answer

0 to 4095.

Q2. Why is the pot on GPIO 34?

Show answer

It is an input pin that still reads while Wi-Fi is on.

Q3. Can GPIO 34 light an LED?

Show answer

No - GPIO 34 to 39 can only be inputs.

Q4. Which pins must you never use on a classic ESP32?

Show answer

GPIO 6 to 11.

Q5. What is a strapping pin?

Show answer

A pin the ESP32 checks at start-up to decide how to start.

Q6. Should you power the pot from 5 V?

Show answer

No - use 3V3.

Q7. In sketch 2, what is printed for a reading of 3000?

Show answer

HIGH (it is 2730 or more).

Code Along 21: ESP32: LOW, MID or HIGH

Practise in your browser

Write and test this lesson's code on a virtual board. Work through the 1 exercises in order. Press Test and each one checks itself.

Exercises passed 0 / 1About 10 minOpen as its own page

ESP32 code on the virtual board

The virtual board is an Uno, not an ESP32, so there is no Wi-Fi here. You practise the code around it. A pot reading is a pretend number in a variable, like int reading = 2048;. The LED on GPIO 2 works the same; the test list calls it pin D2.

Exercise 21.1

LOW, MID or HIGH

Not started

The loop gives you three pretend readings, one at a time. For each one print:

  • LOW if it is below 1365
  • MID if it is below 2730
  • HIGH otherwise
Sample output
LOW
MID
HIGH
int readings[] = {500, 2000, 3500};  // three pretend pot readings

void setup() {
  Serial.begin(115200);
  for (int i = 0; i < 3; i++) {
    int reading = readings[i];
    // Print LOW, MID or HIGH

  }
}

void loop() {
}

Turn on JavaScript to edit, run and test this code in the browser.