Clocks
Interval timer
Work, rest, repeat. Turn the knob to set the work minutes and press it to start; it beeps when each part ends.
- Medium
- about 55 min
- ESP32-S3
- 8 parts
- 11 wires
- 96 lines of code
ESP32 packs are built on real parts: the simulator runs Arduino Uno circuits only.
Idea
A knob is the fastest way to set a number. The encoder inside sends two signals slightly out of step, and which one changes first tells you the direction.
What you learn
Reading a rotary encoder, and a program that moves between states: setting, working, resting.
How it works
input
Rotary knob
decides
ESP32-S3
output
OLED 0.96 in
Buzzer
Powered with 5 V through the board's USB-C port.
A reference design. The sketch is compiled with the real ESP32 toolchain, and its pins are checked against the wiring table automatically. It has not yet been built and tested on real parts by us, so wire it on the bench and run the checks before you build it into a case. Real modules vary: follow the labels printed on yours, not their position in the picture.
Parts
0 of 8 ready
No prices here: they change by shop and by week. Buy each part by its name and the note beside it.
You also need
A computer with Arduino IDE 2 installed (free)
3D wiring
One jumper per step, with the USB cable unplugged. Several wires may share 3V3, 5V or GND: the expansion board or breadboard gives each its own pin.
Step 0 of 11
Lay out the parts
Seat the board on its expansion board or breadboard and lay the modules out round it. Nothing is connected yet.
All connections
interval-timer.ino
// Interval timer
// Work, rest, repeat. Turn the knob to set the work minutes, press to start
// or stop. It beeps when each part ends.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const int PIN_KNOB_CLK = 5;
const int PIN_KNOB_DT = 6;
const int PIN_KNOB_SW = 7;
const int PIN_OLED_SDA = 8;
const int PIN_OLED_SCL = 9;
const int PIN_BUZZER_POS = 17;
const int REST_MINUTES = 5;
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
int workMinutes = 25;
bool running = false;
bool resting = false;
unsigned long phaseEnd = 0;
int lastClk = HIGH;
void beep(int times) {
for (int i = 0; i < times; i++) {
tone(PIN_BUZZER_POS, 1500, 150);
delay(250);
}
}
void startPhase(bool rest) {
resting = rest;
phaseEnd = millis() + (unsigned long)(rest ? REST_MINUTES : workMinutes) * 60000UL;
}
void draw() {
oled.clearDisplay();
oled.setTextSize(1);
oled.setCursor(0, 0);
oled.print(!running ? "SET WORK MINUTES" : resting ? "REST" : "WORK");
oled.setTextSize(4);
oled.setCursor(4, 22);
if (running) {
unsigned long left = phaseEnd > millis() ? (phaseEnd - millis()) / 1000 : 0;
oled.printf("%02lu:%02lu", left / 60, left % 60);
} else {
oled.printf("%02d:00", workMinutes);
}
oled.display();
}
void setup() {
pinMode(PIN_KNOB_CLK, INPUT_PULLUP);
pinMode(PIN_KNOB_DT, INPUT_PULLUP);
pinMode(PIN_KNOB_SW, INPUT_PULLUP);
pinMode(PIN_BUZZER_POS, OUTPUT);
Wire.begin(PIN_OLED_SDA, PIN_OLED_SCL);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.setTextColor(SSD1306_WHITE);
lastClk = digitalRead(PIN_KNOB_CLK);
draw();
}
void loop() {
// One click of the knob: CLK falls, and DT tells the direction.
int clk = digitalRead(PIN_KNOB_CLK);
if (clk != lastClk && clk == LOW && !running) {
workMinutes += digitalRead(PIN_KNOB_DT) == HIGH ? 1 : -1;
workMinutes = constrain(workMinutes, 1, 90);
draw();
}
lastClk = clk;
if (digitalRead(PIN_KNOB_SW) == LOW) {
delay(30);
while (digitalRead(PIN_KNOB_SW) == LOW) delay(5);
running = !running;
if (running) startPhase(false);
tone(PIN_BUZZER_POS, 1000, 60);
draw();
}
static unsigned long lastDraw = 0;
if (running) {
if (millis() >= phaseEnd) {
beep(resting ? 1 : 3);
startPhase(!resting);
}
if (millis() - lastDraw > 250) {
lastDraw = millis();
draw();
}
}
delay(1);
}
Libraries
- Adafruit GFX Library: install it from Sketch > Include Library > Manage Libraries.
- Adafruit SSD1306: install it from Sketch > Include Library > Manage Libraries.
Uploading
First time with an ESP32: in Arduino IDE open Boards Manager, search “esp32” and install “esp32 by Espressif Systems”.
- Open the sketch in Arduino IDE.
- Plug the board in with the USB-C cable.
- Tools > Board > ESP32S3 Dev Module, then Tools > Port and choose the board's port.
- Press Upload (the arrow). Wait for “Done uploading”.