ESP32 Alcohol Detection and Notification System

Overview
This project demonstrates an Alcohol Detection and Notification System using an ESP32 microcontroller, and MQ3 gas sensor, an OLED display, and a buzzer. The system detects alcohol level in the environment and performs the following actions:
- Displays a welcome animation on the OLED screen followed by a standby message
- Detects alcohol levels using the MQ3 sensor.
- Upon detection, displays "Alcohol Detected" on the OLED and sends an email
- Confirms email transmission on the OLED display and triggers the buzzer as an alert
Why We Built This Project: Addressing the Drink-and-Drive Menace
Every year, thousands of lives are lost due to accidents caused by drunk driving. The tragic stories of families shattered and lives cut short have inspired us to take a proactive step towards creating a safer world. Alcohol consumptions impairs judgement and reaction time, making it one of the leading causes of fatal road accidents globally. In our quest to address this issue, we developed the Alcohol Detector with Email Alerts. This project aims to detect alcohol presence in real time and immediately alert stakeholder, such as family members, fleet managers, or safety authorities. By leveraging cutting edge technology like ESP32 microcontroller, the MQ3 alcohol sensor, and an OLED display, we not only monitor but also communicate alerts effectively using automated emails. This solution is envisioned as a small yet significant step toward reducing accidents and promoting responsible behavior. With such devices integrated into vehicles, workplaces, or public safety setups, we can contribute to a safer and more responsible society
Features
- Accurate Alcohol Detection: Utilizes the MQ3 gas sensor for real-time alcohol detection
- Email Notification: Sends email alerts via the ESP32's Wi-Fi capabilities.
- OLED Display Integration: Provides visual feedback and status updates
- Buzzer Alert: Auditory alert when alcohol is detected and email is sent
- Custom PCB: Designed by me and manufactured by JLCPCB, simplifying the wiring and assembly
Things used in this project:
1. ESP32 (38 PIN) Wi-Fi + Bluetooth NodeMCU-32 Development Board:

- a powerful microcontroller with built-in Wi-Fi and Bluetooth capabilities.
- Serves as the project's brain handling sensor input, email notification and display updates.
2. Buzzer:

- Provides an auditory alert when alcohol is detected and an email is sent
3. 0.96 Inch Blue OLED Display Module I2C-4pin:

- 128x64 resolution with I2C communication
- Displays system status, detection results, and notifications
4. MQ-3 Alcohol Sensor:

- Detects alcohol in the air
- Outputs analog and digital signals proportional to the alcohol concentration.
5. Custom PCB

- Designed to house the component and reduce wiring complexity Fabricated by JLCPCB, ensuring high quality manufacturing and fast delivery.
Why JLCPCB: The World's Best PCB Manufacturer

when it comes to PCB manufacturing, JLCPCB is an undisputed leader in the industry. With unmatched precision, exceptional quality, and cutting-edge technology, JLCPCB ensures that every PCB meets the most demanding requirements of professionals and hobbyists alike. Whether your project is for industrial applications, aerospace, medical equipment, or consumer electronics, JLCPCB delivers excellence in every order.
Here's what makes JLCPCB the best PCB manufacturer in the world:
Unmatched Material Versatility
JLCPCB provides a wide range of base materials to suit any project requirements:
- FR-4: The Standard for reliability and durability.
- Flex PCB: Ideal for flexible applications
- Aluminum & Copper Core: Perfect for high-performance thermal experiment
- High-Frequency Materials: Options like Rogers PTFE Teflon for RF applications
Layer Options for Every Need
From simple 1 layer PCBs to advanced 32 layer PCBs, JLCPCB can handle projects of any complexity. Their high-precision multiplayer boards cater to industries where performance and reliability are paramount
Flexible PCB Quantities
- Order as little as 5 PCBs or scale up to 30 pieces per batch.
- Whether you're prototyping or mass-producing, JLCPCB provides the perfect balance of cost and quantity.
Thickness & Colors: Tailored to Your Design
- PCB Thickness options: 0.4mm, 0.6mm, 0.8mm, 1.0mm, 1.2mm, 1.6mm, and 2.0mm, catering to a variety of design specifications
- PCB Colors: choose from standard green to more vibrant colors options like white, purple, red, yellow, and blue to give your board a professional and aesthetic finish.
Surface Finish Options for Superior Quality
- HASL (with lead) or Lead-Free HASL for affordability.
- ENIG (Electroless Nickel Immersion Gold) for corrosion resistance and better conductivity.
High-Spec Options for Demanding Applications
JLCPCB ensures high-quality standards with these advanced specification:
- Outer Copper Weight: choose between 1oz and 2oz for better current carrying capacity
- Via treatments include: Tented Untented or Plugged Vias, Epoxy Filled & Capped options foe enhanced reliability and strength.
Perfect for Any Product Type
JLCPCB's Capabilities cater to a wider variety of applications, including:
- Industrial Electronics
- Consumer Electronics
- Aerospace and Defense
- Medical Devices
Whether you're building prototypes or manufacturing mission critical systems, JLCPCB is your trusted partner
How This Project Works
- Step1: Initialization: When powered on, the OLED displays an animated rectangle that grows into a welcome screen with "Alcohol Detector" text.
- Step2: Standby Mode: The OLED shows "Alcohol Not Detected Yet" while the MQ3 sensor monitors the environment for alcohol.
- Step3: Alcohol Detector: If alcohol is detected:
The OLED displays "Alcohol Detected" and "Sending Mail...........". An email alert is sent using the ESP32's Wi-Fi connection. Once the email is sent, the buzzer beeps, and the OLED shows "Mail Sent". - Step4: Reset: After the alert sequence, the system resets to standby mode, ready to detect again.
Setting Up a Third-Party App Password for the project
For this project to send email alerts, you need to configure your email account to allow ESP32 to sends emails on your behalf Due to enhanced security measures, most email providers (like Gmail) do not allow direct login from devices or applications. Instead, they require you to generate a third party app password for Gmail:
Why a Third-Party App Password is Required
- Using your email password directly in the code is insecure and can comprise your account
- A third-party app password ensures secure access to your email without exposing your primary password.
- It is a one-time password specific to the app or device and can be revoked anytime.
Steps to Create a Third Party App Password
1. Enable 2-Factor Authentication (2FA):
- Log in to your Gmail Account
- Navigate to Google Account Settings by clicking on your profile picture and selecting "Manage your Google Account".
- Go to the security table and scroll down to "signing in to google"
- Enable 2-Step Verification and follow the steps to set it up
2. Generate the App Password:
- Sign in to your Google Account
- Click Security
- Click App Passwords under Signing in to Google
- Select the app and device you're using
- Click Generate
- Copy the password and enter it in the app
- Make sure that 2 step verification is on
3. Use the App Password in the Your Code:
#define AUTHOR_PASSWORD "your_app_password"
Code Explanation
The code initializes the ESP32, connects it to WI-FI, and sets up the OLED display. The MQ3 sensor monitors the environment for alcohol. When alcohol is detected an Email is sent using the ESP_Mail_Client library.
The OLED provides real-time feedback, and the buzzer signals the user
The following highlights the code's key features:
- Wi-Fi Setup: Establishes a connection using WiFi.begin() and monitors the status.
- MQ-3 Sensor Input: Reads digital signals from the MQ3 sensor to determine alcohol presence.
- Email Notification: Sends and email alert via SMTP configuration.
- OLED Animation: Displays a growing rectangle animation followed by system status updates
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <ESP_Mail_Client.h>
// Wi-Fi credentials
#define WIFI_SSID "Your_SSID"
#define WIFI_PASSWORD "Your_PASSWORD"
// Email credentials
#define SMTP_HOST "smtp.gmail.com"
#define SMTP_PORT 465
#define AUTHOR_EMAIL "Your_Email@gmail.com"
#define AUTHOR_PASSWORD "Your_Email_App_Password"
#define RECIPIENT_EMAIL "Recipient_Email@gmail.com"
// GPIO pins
#define MQ3_SENSOR_PIN 35
#define BUZZER_PIN 25
// OLED display settings
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// SMTPSession for email
SMTPSession smtp;
// Global variables
bool emailSent = false;
// Function to handle email sending status
void smtpCallback(SMTP_Status status) {
if (status.success()) {
Serial.println("Email sent successfully!");
emailSent = true;
} else {
Serial.printf("Failed to send email: %s\n", status.errorReason().c_str());
}
}
// Function to initialize OLED display with an animation
void initializeDisplay() {
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED initialization failed!");
while (true);
}
// Welcome animation
int rectWidth = 10, rectHeight = 10;
for (int i = 0; i < 20; i++) {
display.clearDisplay();
display.fillRect((SCREEN_WIDTH - rectWidth) / 2, (SCREEN_HEIGHT - rectHeight) / 2, rectWidth, rectHeight, SSD1306_WHITE);
display.display();
rectWidth += 5;
rectHeight += 3;
delay(50);
}
// Display project title
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(15, 20);
display.println("Alcohol Detector");
display.display();
delay(2000);
}
// Function to send an email alert
void sendEmail() {
// Configure SMTP session
Session_Config config;
config.server.host_name = SMTP_HOST;
config.server.port = SMTP_PORT;
config.login.email = AUTHOR_EMAIL;
config.login.password = AUTHOR_PASSWORD;
config.secure.startTLS = true;
// Compose the email message
SMTP_Message message;
message.sender.name = "ESP32 Alcohol Detector";
message.sender.email = AUTHOR_EMAIL;
message.subject = "Alert: Alcohol Detected!";
message.addRecipient("User", RECIPIENT_EMAIL);
message.text.content = "Alcohol has been detected by the ESP32 system.";
message.text.charSet = "utf-8";
message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
// Connect to SMTP server and send email
if (!smtp.connect(&config)) {
Serial.printf("SMTP connection failed: %s\n", smtp.errorReason().c_str());
return;
}
if (!MailClient.sendMail(&smtp, &message)) {
Serial.printf("Email sending failed: %s\n", smtp.errorReason().c_str());
}
}
// Function to handle buzzer alert
void beepBuzzer() {
digitalWrite(BUZZER_PIN, HIGH);
delay(200);
digitalWrite(BUZZER_PIN, LOW);
}
// Setup function
void setup() {
Serial.begin(115200);
// Initialize OLED display
initializeDisplay();
// Setup GPIO pins
pinMode(MQ3_SENSOR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
// Connect to Wi-Fi
Serial.print("Connecting to Wi-Fi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(300);
}
Serial.println("\nConnected to Wi-Fi!");
// Setup email callback
smtp.callback(smtpCallback);
}
// Main loop
void loop() {
int alcoholStatus = digitalRead(MQ3_SENSOR_PIN);
if (alcoholStatus == LOW && !emailSent) {
// Alcohol detected
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
display.println("Alcohol Detected!");
display.println("Sending Email...");
display.display();
// Send email alert
sendEmail();
// Wait for email to be sent
while (!emailSent) {
delay(100);
}
// Trigger buzzer and update display
beepBuzzer();
display.clearDisplay();
display.setCursor(0, 0);
display.println("Mail Sent!");
display.display();
// Prevent rapid re-triggering
delay(5000);
emailSent = false;
} else if (alcoholStatus != LOW) {
// No alcohol detected
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
display.println("Alcohol Not Detected");
display.display();
}
delay(1000);
}
Circuit Diagram
The PCB was designed for this project, streamlining connections between components. The following connections were used ESP32 Pins:
- MQ3 Sensor: Pin 35 for digital input
- Buzzer: Pin 25 for the output
- OLED Display: I2C pins SDA and SCL
Video Tutorial:
prefer this video for this project:
Conclusion
This Alcohol Detection System demonstrates how technology can play pivot role in addressing read world problems like drunk driving. By integrating the ESP32 microcontroller, MQ3 sensor, OLED display, and Buzzer, this project provides a robust and efficient solution to detect alcohol and alert stakeholders through automated email notifications.
The System's design ensures accuracy, reliability and responsiveness, making it suitable for applications in vehicles , workplaces and public safety environments. The inclusion of custom PCBs from JLCPCB add a layer of professionalism and durability to the project, ensuring long term use and adaptability This project is a small step toward building a safer world