Add the buzzer and build an alert that beeps until you press the button - and stops by itself after 15 seconds.
| Part | How many |
|---|---|
| Wiring from Lessons 1-3 | |
| Active buzzer module | 1 |
| Female-to-male jumper wires | 3 |
The module has a small transistor that switches the buzzer. It is sold as a 5 V module, but here its VCC goes to 3V3: then the ESP32's 3.3 V signal can switch it fully on and off. The beep is quieter than at 5 V.
Powering it from 5V could leave it beeping all the time, because a 3.3 V signal may not be enough to switch a 5 V module off. That is why this kit uses 3V3.
used % 500 says where we are in each half-second: the first 150 ms are on, the other 350 ms off. The pattern repeats without any delay().
An alert that cannot be stopped is a problem too: the button acknowledges it, and after 15 seconds it stops by itself.
Every item below is in the kit. Not in the box: The buzzer module (optional add-on).
All 6 parts in one box, with this project and 4 more to build from them.
One of each part. Packs (resistor assortment, jumper wires) already hold more than a single build needs. Backorder parts ship once they arrive. Delivery is AED 20, free on orders AED 100+.
Unplug USB before you change any wire. Pin names are the labels printed on the board.
| Part / pin | Connect to | Note |
|---|---|---|
| Buzzer I/O (signal) | GPIO27 | May be labelled S or IN |
| Buzzer VCC | + rail (3V3) | Not 5V - see "How it works" |
| Buzzer GND | − rail (GND) | |
| OLED, GY-521, button | As in Lessons 1-3 | GPIO21, GPIO22, GPIO26 |
One thing at a time. Tick nothing off until it matches the diagram.
Written for the board in the kit. Copy it into the Arduino IDE; change the pin numbers if you wire it differently.
/*
DIY KIT - ESP32 Fall-Detection Learning Prototype
Lesson 4 of 5: Buzzer Alert
WIRING (unplug the USB cable before you wire)
Keep everything from Lessons 1-3.
Active buzzer module, with 3 female-to-male jumper wires:
I/O (signal, may be labelled S or IN) -> GPIO27
VCC -> 3V3 (NOT 5V - see the lesson)
GND -> GND
WHAT SHOULD HAPPEN
Three short test beeps. Then press the button to start an ALERT: the
buzzer beeps 150 ms on, 350 ms off and the OLED says ALERT. Press again
to acknowledge (stop) it. If nobody presses, it stops by itself after
15 seconds. Serial Monitor at 115200 baud.
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const int SDA_PIN = 21;
const int SCL_PIN = 22;
const int BUTTON_PIN = 26;
const int BUZZER_PIN = 27;
const unsigned long BEEP_ON_MS = 150;
const unsigned long BEEP_OFF_MS = 350;
const unsigned long MAX_ALERT_MS = 15000; // an alarm must not run for ever
const unsigned long DEBOUNCE_MS = 35;
// Most modules beep when the pin is HIGH. If yours beeps in the gaps or
// all the time, change false to true and upload again.
const bool BUZZER_ACTIVE_LOW = false;
Adafruit_SSD1306 display(128, 64, &Wire, -1);
bool displayOk = false;
bool alerting = false;
unsigned long alertStart = 0;
bool rawButton = HIGH, stableButton = HIGH;
unsigned long rawSince = 0;
void buzzer(bool on) {
if (BUZZER_ACTIVE_LOW) digitalWrite(BUZZER_PIN, on ? LOW : HIGH);
else digitalWrite(BUZZER_PIN, on ? HIGH : LOW);
}
bool pressedNow() {
bool reading = digitalRead(BUTTON_PIN);
if (reading != rawButton) {
rawButton = reading;
rawSince = millis();
}
if (millis() - rawSince > DEBOUNCE_MS && reading != stableButton) {
stableButton = reading;
return stableButton == LOW;
}
return false;
}
void show(const char *line1, const char *line2) {
if (!displayOk) return;
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println("BUZZER ALERT LAB");
display.setTextSize(2);
display.setCursor(0, 20);
display.println(line1);
display.setTextSize(1);
display.setCursor(0, 56);
display.println(line2);
display.display();
}
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
buzzer(false);
Wire.begin(SDA_PIN, SCL_PIN);
displayOk = display.begin(SSD1306_SWITCHCAPVCC, 0x3C) || display.begin(SSD1306_SWITCHCAPVCC, 0x3D);
Serial.println("Lesson 4: three test beeps");
for (int i = 0; i < 3; i++) {
buzzer(true); delay(100);
buzzer(false); delay(200);
}
show("READY", "press to start alert");
Serial.println("Press the button to start an alert, press again to stop it");
}
void loop() {
bool pressed = pressedNow();
unsigned long now = millis();
if (!alerting) {
if (pressed) {
alerting = true;
alertStart = now;
Serial.println("ALERT started");
show("ALERT", "press to acknowledge");
}
} else {
unsigned long used = now - alertStart;
if (pressed || used >= MAX_ALERT_MS) {
alerting = false;
buzzer(false);
Serial.println(pressed ? "ACKNOWLEDGED" : "TIMED OUT after 15 s");
show(pressed ? "STOPPED" : "TIMED OUT", "press to start alert");
} else {
// Where are we in one 500 ms beep cycle? The first 150 ms are "on".
unsigned long phase = used % (BEEP_ON_MS + BEEP_OFF_MS);
buzzer(phase < BEEP_ON_MS);
}
}
delay(2);
}
| In the code | What it does |
|---|---|
| const int BUZZER_PIN = 27; | The buzzer's signal pin. |
| const bool BUZZER_ACTIVE_LOW = false; | Most modules beep when the pin is HIGH. If yours beeps in the gaps or all the time, change it to true. |
| used % (BEEP_ON_MS + BEEP_OFF_MS) | Where we are in each 500 ms cycle; under 150 means "beep". |
| MAX_ALERT_MS = 15000 | The time-out: the alert stops by itself after 15 seconds. |
| Problem | What to check |
|---|---|
| No sound at all | Unplug USB. Check I/O to GPIO27, VCC to 3V3 and GND, by the labels. Some 5 V modules are very quiet or silent at 3.3 V - tell us if yours is. |
| It beeps in the gaps, or all the time | Your module beeps when the pin is LOW. Set BUZZER_ACTIVE_LOW to true and upload again. |
| Only a click, no beep | It may be a passive buzzer. Check the label says active, or ask us. |
| "OLED not found" or a black screen | Unplug USB. Check OLED VCC to 3V3 (not 5V), GND, SDA to GPIO21 and SCL to GPIO22 - by the labels printed on the OLED. |
| The upload fails or stops at "Connecting..." | See Start here, step 6. |
Time how long the alert runs before TIMED OUT with a stopwatch. Then change MAX_ALERT_MS and check again.
So the ESP32's 3.3 V signal can switch it fully off; at 5V it might never stop.
300.
An alarm that can never stop is a problem too - and the battery or the people nearby would suffer.
Most problems are one wire in the wrong hole. Check these first:
Open the troubleshooting table for this step →
Hi DIY KIT, I'm stuck on the ESP32 Fall-Detection Learning Prototype. Lesson: __ Step number: __ What should happen: __ What happens instead: __ Error message (copied exactly): __ I'll attach a photo of my wiring.
A button starts a 20-second countdown on the OLED and a second press cancels it - the user-interface half of the fall detector, with no motion yet. Do Start here first.
The GY-521 learns which way is "upright" in 3 seconds, then the OLED shows how far you have tilted it from there - and flags 55°, the limit the main build uses.
Watch total g-force on a live graph and count every impact (above 2.5 g) and free fall (below 0.45 g). Choose the main build's limits from your own data.
We use cookies that the shop needs to work — signing in, your cart, form security. We also measure how the shop is used so we can stock the right parts and fix what is broken. Choose “Only essential” and we will not load Google Analytics or the Meta pixel, and will not record your visit. Read more.