Ultrasonic Sensor with Arduino [COMPLETE GUIDE]
Ultrasonic Sensor Overview
Introduction to Ultrasonic Sensors
- The video introduces the ultrasonic sensor, explaining its functionality and applications in alarm systems using Arduino and ESP32.
- It highlights that ultrasonic sensors measure distances by emitting sound waves, similar to how bats and dolphins navigate.
How Ultrasonic Sensors Work
- The sensor consists of a transmitter (emits high-frequency sound above 20kHz) and a receiver (detects returning sound waves).
- Distance is calculated using the formula: distance = velocity × time. Since the sound travels to an object and back, the result must be halved for accurate measurement.
Building an Alarm System with Arduino
Components Needed
- For the Arduino project, components include an Arduino board, breadboard, jumper wires, buzzer, and ultrasonic sensor.
Assembly Instructions
- Connect the ultrasonic sensor's VCC and GND pins to Arduino's 5V and ground ports; trigger pin to port 9; echo pin to port 10; buzzer to port 8.
Code Explanation
- The code defines constants/variables needed for operation. It sets up input/output configurations for pins during initialization.
- Serial communication is initiated for monitoring measured distances. The loop function generates sound waves by activating the emitter briefly.
Alarm Activation Logic
Distance Measurement
- Time taken for echo detection is recorded at line 28 of the code; this data is crucial for calculating distance at line 29.
Buzzer Functionality
- An if-statement activates the buzzer when an object is within 50 centimeters of the sensor. If not triggered, it turns off.
Transitioning to ESP32 for Enhanced Features
Limitations of Arduino Setup
- Acknowledges that users can only confirm alarm activation through audible feedback from the buzzer when away from home.
Advantages of ESP32
- Introduces ESP32 as a more advanced microcontroller with Wi-Fi/Bluetooth capabilities suitable for IoT applications compared to standard Arduino boards.
Building an Alarm System with ESP32
Required Components
- For this project: three tiny breadboards, jumper wires, buzzer, ESP32 board, and ultrasonic sensor are necessary.
Assembly Steps
- Connect components similarly as before but use different pin assignments: trigger pin to D25; echo pin to D26; buzzer connected to D19/GND pins.
Programming Environment
- The presenter uses Thonny IDE instead of Arduino IDE due to its compatibility with Python programming on ESP32 devices.
Understanding ESP32 Project Files
Overview of Project Repository
- The project repository on GitHub contains four files within a folder named "code," starting with
boot.py, which connects the ESP32 board to local Wi-Fi.
Boot Script Configuration
- To configure the Wi-Fi connection, users must update the
ssidandpasswordvariables in theboot.pyfile.
Ultrasonic Sensor Class
- The second file,
ultrasonic_sensor.py, defines a class for the ultrasonic sensor, including methods for wave emission control and distance measurement in centimeters.
Email Functionality with Umail
- The third script,
umail.py, enables email sending capabilities since MicroPython lacks built-in email tools. Users need to upload this file and utilize the SMTP object provided by Umail.
Main Application Logic
- The final script,
main.py, initializes the ultrasonic sensor object to measure distances. If measurements fall between 2 and 50 centimeters, it triggers a siren sound and calls thesend_emailfunction with necessary parameters.
Configuring Email Parameters
Required Email Settings
- In the
send_emailfunction, several parameters are essential: provider (Gmail, Outlook, Live/Hotmail), sender's email address (recommended not to use personal emails), sender's name (app password name), and app password itself.
Understanding App Passwords
- An app password is required for ESP32 to access email accounts securely. It simplifies login by generating a unique 16-character password instead of using standard account passwords.
Setting Up Gmail for ESP32
Steps for Gmail Configuration
- Users should create a new Gmail account specifically for this project. They must navigate to their Google Account Security page at myaccount.google.com/security.
Creating an App Password
- On the Security page, locate "How you sign in to Google" section. If "App Password" isn't visible, search for it. Create an app password by naming it (e.g., esp32), then copy the generated password without altering spaces.
Finalizing Code Upload
Completing Setup
- Assign values from your newly created app name and app password into respective variables in
main.py. After completing these steps, upload all four files to your ESP32 device to execute the project successfully.