pin 0DS18B20ESP32one core shownOLED1void loop() {2 checkButton();3 readTemp(); // 750 ms4 drawScreen(); // 23 ms5}3button2screen1tempz zCPUtapflag
Back: the ESP32 board with a button, a temperature sensor and a screenFront: the superloop card and three FreeRTOS tasks (taller = higher priority)Tokens: the CPU (who runs now), a button tap and an ISR's flagAn ESP32 with a button, a DS18B20 temperature sensor and an OLED screen. On the left, a superloop runs three jobs in turn; on the right, the same jobs as FreeRTOS tasks with priorities 3, 2 and 1. In the superloop, readTemp() holds the CPU for 750 ms, so one pass is 773 ms and a press can wait that long. As tasks, the temp task sleeps while it waits, and an ISR wakes the button task, which runs at once.

The superloop

  1. Our ESP32 runs a superloop: loop() calls three jobs in order, then starts again, forever. The CPU, the part that runs code, can only do one job at a time. Check the button, read the temperature, draw the screen, repeat.

Where it breaks

Tasks and priorities

What happens

;; step 1/10 · superloop
superloop  loop() runs its jobs in order, forever
  line 2  checkButton()
  line 3  readTemp()
  line 4  drawScreen()
one core: while one job runs, the others wait

// ESP32 (Arduino core): button on pin 0, DS18B20, SSD1306 OLED
void readTemp() {
  startConversion();      // Convert T on the 1-Wire bus
  delay(750);            // DS18B20: up to 750 ms at 12 bits
  temp = readScratchpad();
}

void loop() {
  checkButton();
  readTemp();   // 750 ms
  drawScreen(); // 23 ms
}

The card shows loop(); the panel shows the whole sketch. startConversion() and readScratchpad() stand for the usual OneWire calls (Convert T, then Read Scratchpad). checkButton() takes microseconds, so we count it as 0 ms; 773 ms is the sum of the other two jobs. 23 ms is a lower bound: 1,024 bytes x 9 clocks x 2.5 µs, the SSD1306's fastest I2C clock (400 kHz). The address and control bytes, and a slower 100 kHz bus, make a real redraw take longer. The ESP32 has two cores, and ESP-IDF's FreeRTOS can run two tasks at the same moment. To keep the picture simple we follow one core; the rule "the highest-priority Ready task runs" holds on each.