Final test: Arduino and ESP32
The test covers the whole course: digital and analog inputs and outputs, PWM, sensors, the servo motor, the ESP32 and a web server. It has two parts: a knowledge test with 15 questions and practical tasks where you write programs.
What you need to know
- the difference between
setup()andloop(), the commandspinMode,digitalWrite,digitalReadanddelay - connecting an LED (resistor, anode and cathode) and a button with
INPUT_PULLUP analogRead(0–1023 on the Arduino, 0–4095 on the ESP32), the functionsmapandconstrain- PWM:
analogWriteon the Arduino andledcWriteon the ESP32;toneandnoTone - the DHT11, LDR and HC-SR04 sensors (how distance is calculated) and the servo motor
- differences between the Arduino Uno and the ESP32: 3.3 V, Wi-Fi, input-only pins
millis()instead ofdelay()and a simple web server on the ESP32
What the test looks like
- Duration: two school lessons – the first for the knowledge test and task 1, the second for tasks 2 and 3 (or at home, as practice).
- Knowledge test: 15 multiple-choice questions, 2 points each.
- Practical tasks: 3 tasks, 10 points each. For full points the program must work on the board (or in Tinkercad) and be clear, with comments.
| Points (out of 60) | 0–23 | 24–32 | 33–42 | 43–51 | 52–60 |
|---|---|---|---|---|---|
| Grade | 1 | 2 | 3 | 4 | 5 |
The scale is a suggestion for self-assessment; a teacher can adjust it.
Knowledge test (15 questions)
Choose one answer for each question and click "Check". Each correct answer is worth 2 points.
Open the test
Knowledge test: Arduino and ESP32 (15 questions)
1. How many times does setup() run?
2. What does pinMode(7, OUTPUT) do?
3. What is needed so an LED does not burn out?
4. What value does analogRead() return on the Arduino Uno when the pin is at 5 V?
5. What does map(512, 0, 1023, 0, 100) return?
6. What range does analogWrite() accept on the Arduino Uno?
7. What is the highest voltage you may connect to an ESP32 pin?
8. What does the DHT11 sensor measure?
9. The echo of an ultrasonic sensor returned after 580 µs. How far is the obstacle? (sound: 0.034 cm/µs)
10. How is the angle of a servo motor set?
11. Which ESP32 pins can only be inputs?
12. What is the advantage of millis() over delay()?
13. What does the ESP32 have that the Arduino Uno does not?
14. A common cathode RGB LED gets the values (0, 0, 255). Which color do we see?
15. Which connection do the OLED display and the MPU6050 use?
Practical tasks
For each task write the program, build the circuit and check that it works. Scoring: correct wiring 3 points, working program 5 points, clear code with comments 2 points.
- Three LEDs and a button (Arduino Uno). The LEDs are on pins 4, 7 and 12 and the button on pin 2. Each button press lights one more LED; after the third press all are on, and the fourth press turns them off and everything starts again.
- Parking sensor (Arduino Uno). An HC-SR04 ultrasonic sensor (Trig on pin 9, Echo on pin 10) and a buzzer on pin 8. When an obstacle is closer than 100 cm, the buzzer beeps – the closer the obstacle, the faster the beeps. Under 10 cm the tone is continuous.
- LED from a web page (ESP32). The ESP32 makes a web page with two links, "On" and "Off", that turn the LED on GPIO2 on and off. The page shows whether the LED is on.
Solution 1: three LEDs and a button
// Test – task 1: a button on pin 2 turns on three LEDs in turn (4, 7, 12),
// each press lights the next one; after the third, all turn off.
const int LED[3] = { 4, 7, 12 };
const int BUTTON = 2;
int howMany = 0; // how many LEDs are on (0–3)
int previous = HIGH;
void show() {
for (int i = 0; i < 3; i++) {
digitalWrite(LED[i], i < howMany ? HIGH : LOW);
}
}
void setup() {
for (int i = 0; i < 3; i++) pinMode(LED[i], OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
}
void loop() {
int now = digitalRead(BUTTON);
if (previous == HIGH && now == LOW) {
howMany++;
if (howMany > 3) howMany = 0;
show();
delay(30);
}
previous = now;
}
Solution 2: parking sensor
// Test – task 2: parking sensor
// Arduino Uno, HC-SR04: Trig 9, Echo 10, buzzer on pin 8
// the closer the obstacle, the faster the beeps; under 10 cm the tone is continuous.
const int TRIG = 9;
const int ECHO = 10;
const int BUZZER = 8;
long distanceCm() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long t = pulseIn(ECHO, HIGH, 30000); // 0 if the echo does not return
return t * 0.034 / 2; // sound goes to the obstacle and back
}
void setup() {
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
Serial.begin(9600);
}
void loop() {
long d = distanceCm();
Serial.println(d);
if (d == 0 || d > 100) {
noTone(BUZZER); // nothing nearby
delay(100);
} else if (d < 10) {
tone(BUZZER, 1000); // continuous tone
delay(100);
} else {
tone(BUZZER, 1000, 80); // short beep
delay(d * 10); // the pause grows with the distance
}
}
Solution 3: LED from a web page
// Test – task 3: ESP32 – a web page with two buttons turns the LED on GPIO2 on and off
#include <WiFi.h>
#include <WebServer.h>
const char* SSID = "NETWORK_NAME";
const char* PASSWORD = "PASSWORD";
const int LED_PIN = 2;
WebServer server(80);
bool isOn = false;
void page() {
String s = "<!DOCTYPE html><html><head><meta charset='utf-8'></head><body>";
s += "<h1>The LED is " + String(isOn ? "on" : "off") + "</h1>";
s += "<p><a href='/on'>On</a> | <a href='/off'>Off</a></p></body></html>";
server.send(200, "text/html", s);
}
void turnOn() {
isOn = true;
digitalWrite(LED_PIN, HIGH);
page();
}
void turnOff() {
isOn = false;
digitalWrite(LED_PIN, LOW);
page();
}
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.println(WiFi.localIP());
server.on("/", page);
server.on("/on", turnOn);
server.on("/off", turnOff);
server.begin();
}
void loop() {
server.handleClient();
}
After the test
If you lost points somewhere, revise that lesson:
- LED, button and PWM – LED, button and traffic light, LED, button and PWM (ESP32)
- sensors – LDR sensor, temperature sensor, ultrasonic sensor
- sound and colors – buzzer and RGB LED
- ESP32 and a web server – DHT11 and a weather station, smart classroom
