Skip to content
All exercises
Exercise 1DeviceBeginner

The Smart Machine Monitor

Build the “brain” of a machine on a production line: an ESP32 reads the motor temperature and the vibration level, switches a cooling fan and lights an alarm lamp when a limit is exceeded.

Duration
90 minutes
IoT layer
Device layer: sensors and actuators
Libraries
DHT sensor library for ESPx

What you will learn

  • Tell sensors (inputs) apart from actuators (outputs) in an IoT device
  • Wire a digital sensor, an analogue input, an LED and a relay to an ESP32
  • Read sensor values in an Arduino program and scale them to useful units
  • Write threshold logic that switches actuators automatically
  • Find and fix logic and wiring errors by testing in a simulator

Open in Wokwi

No installation, no hardware. Open a new ESP32 project in the free Wokwi simulator, then:

  1. Paste sketch.ino into the sketch.ino tab and replace the whole content of the diagram.json tab with our diagram.json.
  2. Add the libraries: open the Library Manager tab and add each library from libraries.txt (or create a libraries.txt file and paste the list).
  3. Press the green Start button and watch the Serial Monitor.

Wiring

The same circuit is used in all three exercises. The diagram.json file below builds it in Wokwi for you.

Component (Wokwi part)Component pinESP32 pinRole in the scenario
DHT22 temperature/humidity sensorVCC / SDA / GND3V3 / GPIO 15 / GNDMotor temperature
PotentiometerVCC / SIG / GND3V3 / GPIO 34 / GNDMachine vibration level (0–100 %)
Red LED + 220 Ω resistorAnode (via resistor) / CathodeGPIO 2 / GNDAlarm lamp
Relay moduleVCC / IN / GND5V / GPIO 5 / GNDCooling fan switch

Step by Step

A machine on a production line must be protected from overheating and from excessive vibration — both are early signs of a coming failure. In this exercise you build the brain of a smart machine monitor:

  • a DHT22 sensor measures the motor temperature,
  • a potentiometer stands in for the vibration sensor (0–100 %),
  • a relay switches the cooling fan,
  • a red LED is the alarm lamp.

The ESP32 reads the sensors every two seconds, compares the values with limits and switches the outputs. This is the device layer of an IoT system — in Exercises 2 and 3 you will connect the same machine to the network and to a dashboard.

Set up the circuit in Wokwi

Open a new ESP32 project at wokwi.com/projects/new/esp32. You will see three things: the code editor with the sketch.ino tab, the diagram.json tab and the simulation area with an ESP32 board.

  1. Copy diagram.json from the code section at the bottom of this page. In Wokwi, open the diagram.json tab, select everything and paste. The DHT22, the potentiometer, the LED with its resistor and the relay module appear, already wired.
  2. Copy sketch.ino into the sketch.ino tab.
  3. Open the Library Manager tab, click the + button and add DHT sensor library for ESPx.

Compare the wires with the wiring table above: each part has a power wire, a ground wire and one signal wire to the ESP32.

Understand the code

Read the program before you run it. These are the key blocks:

Pin constants and limits — giving every pin and every limit a name makes the program easy to read and easy to rewire:

const int DHT_PIN   = 15;   // DHT22 data pin
const int POT_PIN   = 34;   // potentiometer – vibration level
const float TEMP_LIMIT = 35.0;  // °C – above this the fan starts
const int   VIB_LIMIT  = 70;    // % – above this the alarm lamp lights

Scaling an analogue value — the ESP32 reads the potentiometer as a number from 0 to 4095. map() turns it into a percentage:

int vibration = map(analogRead(POT_PIN), 0, 4095, 0, 100);

Decisions in boolean variables — each condition is stored as true or false before it controls an output. This makes the logic easy to check:

bool overheated    = data.temperature > TEMP_LIMIT;
bool highVibration = vibration > VIB_LIMIT;
bool alarm = overheated || highVibration;   // || means "or"

Switching the actuators — condition ? HIGH : LOW means “if the condition is true, HIGH, otherwise LOW”:

digitalWrite(RELAY_FAN, overheated ? HIGH : LOW);
digitalWrite(LED_ALARM, alarm ? HIGH : LOW);

The Serial Monitor is your window into the device. Serial.printf() prints every value and every decision. Finally, delay(2000) waits two seconds — the DHT22 must not be read more often than that.

Run and test four situations

Press the green Start button. The Serial Monitor opens below the circuit and prints a line every two seconds.

  • Change the temperature: click the DHT22 while the simulation is running. A small window with sliders opens — drag the temperature.
  • Change the vibration: turn the potentiometer knob with the mouse.

Test these four situations and note the Serial Monitor output, the fan (relay) and the alarm lamp each time:

Situation Temperature Vibration Expected fan Expected alarm
Normal 24 °C 20 % OFF NO
Hot motor 40 °C 20 % ON YES
High vibration 24 °C 85 % OFF YES
Hot and vibrating 40 °C 85 % ON YES

Does your machine behave as the table predicts? If not, compare your wiring with the wiring table before you change the code.

Challenge tasks

Change the program yourself. Test each change before you start the next one.

  1. Early warning: add a yellow LED on GPIO 4 (with its own 220 Ω resistor) that lights when the temperature is above 30 °C.
  2. New limit: change the vibration limit to 60 % and test the four situations again.
  3. Humidity check: if the humidity is above 80 %, print Check enclosure – condensation risk.
  4. Advanced: keep the fan running for 10 seconds after the temperature drops below the limit. Use millis() to remember when the motor was last too hot.

Find the bug

Your teacher gives you two faulty versions of the program. Each one contains one wrong line. Run each version, describe what goes wrong, explain why and correct it.

Fault 1

bool overheated = data.temperature < TEMP_LIMIT;

Fault 2

const int POT_PIN = 35;
Show the explanation (try first!)
  • Fault 1: the comparison is reversed (< instead of >). The fan runs when the motor is cold and stops when it is hot — exactly the wrong way round. On a real line this would let the motor overheat.
  • Fault 2: nothing is wired to GPIO 35, so the vibration always reads about 0 % and the vibration alarm can never trigger. The circuit looks fine — only testing reveals the error.

Reflect

Discuss with your partner:

  • Which situations would be dangerous on a real production line if the logic were wrong?
  • Why do engineers test control logic in a simulator before they connect real machines?
  • Which other sensors could protect a machine — current, proximity, sound, light?

Code

/*
  SmartVET – Module 2 – Exercise 1
  Smart Machine Monitor: reading sensors and controlling actuators
  Board: ESP32 DevKit (Wokwi simulator or real board)
  Library: "DHT sensor library for ESPx" (DHTesp)
  License: CC BY-NC-SA 4.0 – SmartVET Erasmus+ project 2025-1-PL01-KA210-VET-000351305
*/
#include "DHTesp.h"

const int DHT_PIN   = 15;   // DHT22 data pin – motor temperature and humidity
const int POT_PIN   = 34;   // potentiometer – simulates machine vibration level
const int LED_ALARM = 2;    // red LED (through a 220 ohm resistor) – alarm lamp
const int RELAY_FAN = 5;    // relay module input – cooling fan

const float TEMP_LIMIT = 35.0;  // °C – above this the fan starts
const int   VIB_LIMIT  = 70;    // % – above this the alarm lamp lights

DHTesp dht;

void setup() {
  Serial.begin(115200);
  dht.setup(DHT_PIN, DHTesp::DHT22);
  pinMode(LED_ALARM, OUTPUT);
  pinMode(RELAY_FAN, OUTPUT);
  Serial.println("SmartVET machine monitor started");
}

void loop() {
  TempAndHumidity data = dht.getTempAndHumidity();
  int vibration = map(analogRead(POT_PIN), 0, 4095, 0, 100);

  bool overheated   = data.temperature > TEMP_LIMIT;
  bool highVibration = vibration > VIB_LIMIT;
  bool alarm = overheated || highVibration;

  digitalWrite(RELAY_FAN, overheated ? HIGH : LOW);
  digitalWrite(LED_ALARM, alarm ? HIGH : LOW);

  Serial.printf("Temp: %.1f C | Humidity: %.1f %% | Vibration: %d %% | Fan: %s | Alarm: %s\n",
                data.temperature, data.humidity, vibration,
                overheated ? "ON" : "OFF", alarm ? "YES" : "NO");

  delay(2000);  // the DHT22 needs at least 2 seconds between readings
}

This code has not yet been verified in Wokwi or on real hardware. Please report any problems.

Troubleshooting

The Serial Monitor shows “Temp: nan C”.

The program could not read the DHT22. Check that the SDA pin goes to GPIO 15 and that the library in libraries.txt is exactly “DHT sensor library for ESPx” — other DHT libraries are not reliable on the ESP32. On real hardware, also check VCC on 3V3, GND, and the 10 kΩ pull-up if you use a bare 4-pin sensor. While the value is nan, every comparison is false, so the fan never starts.

Compilation fails with “DHTesp.h: No such file or directory”.

The library is missing. Open the Library Manager tab in Wokwi and add “DHT sensor library for ESPx”, or make sure libraries.txt contains that exact line. In the Arduino IDE, install the same library from the Library Manager.

Vibration is always 0 % (or always 100 %).

The potentiometer’s middle pin (SIG) must go to GPIO 34. If the value never changes, SIG is on the wrong pin. If it only moves the “wrong way”, VCC and GND are swapped — that is harmless, just swap them back. On real hardware, never connect the potentiometer to 5V.

The alarm lamp never lights, even when the Serial Monitor says “Alarm: YES”.

Check the LED’s direction: the anode (A, the long leg) goes to the resistor, the cathode (C) goes to GND. Also check that the resistor sits between GPIO 2 and the anode and that its value is 220 Ω, not 220 kΩ.

On a real board, the relay does not click.

Most 5V relay modules switch with the ESP32’s 3.3V signal, but some need more. Check VCC on 5V and IN on GPIO 5. If it still does not switch, use a 3.3V-compatible relay module or drive it through a transistor. Never switch mains voltage in class.

The Serial Monitor shows strange characters.

The baud rate does not match. Wokwi sets it automatically; in the Arduino IDE choose 115200 baud, the value used in Serial.begin().

Optional real hardware

You do not need any of this — the whole exercise runs in Wokwi. If your school has ESP32 boards, these are the parts for the same circuit.

PartQtyNote
ESP32 DevKit (ESP32-DevKitC V4 or similar)
1Its GPIO pins use 3.3V logic and are not 5V tolerant. Power the sensors from the 3V3 pin.
USB cable (data capable; micro-USB or USB-C depending on the board)
1Charge-only cables carry no data — if the board does not show up on the computer, try another cable first.
Breadboard (830 tie points)
1The ESP32 DevKit is wide; use a full-size breadboard so that free rows remain on both sides.
Jumper wires male-male (at least 15)
1
DHT22 (AM2302) temperature & humidity sensor
1Measures the motor temperature in our scenario. Powered from 3V3, its data line is 3.3V too — safe. Wait at least 2 seconds between readings.
10 kΩ resistor(optional)
1Only for a bare 4-pin DHT22: pull-up between the data line and 3V3. A 3-pin DHT22 module has one on board; not needed in Wokwi.
10 kΩ potentiometer
1Stands in for the machine’s vibration level. Outer legs to 3V3 and GND — NOT 5V — middle leg to GPIO 34.
5 mm red LED
1The alarm lamp. Long leg (anode) to the resistor, short leg (cathode) to GND.
220 Ω resistor
1For the red LED. Add a second one if you do the yellow-LED task.
5V 1-channel relay moduleCaution
1Switches the cooling fan. Connect VCC to the 5V pin. Most 5V relay modules work with the ESP32’s 3.3V signal, but some do not trigger — then use a 3.3V-compatible module or a transistor driver. NEVER switch mains voltage (230V) in class.
5 mm yellow LED(optional)
1Only for the early-warning task in Exercise 1 (GPIO 4, with its own 220 Ω resistor).