Introduction:
"This project demonstrates a simple yet powerful home automation system using an ESP32 microcontroller, a 5V relay module, and an LED bulb. Control your home appliances effortlessly via Wi-Fi, showcasing the potential of IoT (Internet of Things) in everyday life."
Overview:
"This project demonstrates a smart home automation system using ESP32 and the Blynk app to control an LED bulb via a 5V relay module. The system enables wireless control of home appliances, combining IoT technology with simple hardware and software integration."
Features:
Wireless control of devices using the Blynk app.
Integration of ESP32 for Wi-Fi connectivity.
Real-time status updates.
Easy to scale for more devices.
Hardware:
ESP32 Microcontroller
5V Relay Module
LED Bulb
Power Supply
Connecting Wires
Software:
Blynk App
Arduino IDE (for programming ESP32)
4. Circuit Diagram
1. Hardware Setup:
Connect the positive terminal of the LED to a GPIO pin on the ESP32 (e.g., GPIO 2).
Connect the negative terminal of the LED to a 330-ohm resistor, then to the ground (GND) of the ESP32.
connect relay VCC pin to ESP32 VIN pin.
connect relay GND pin to ESP32 GND pin.
connect relay INPUT pin to ESP32 D2 pin (e.g GPIO 2).
Default Behavior:
GPIO 2 is often preconfigured for output purposes on many ESP32 boards (e.g., built-in LED).
It's connected to the onboard LED on many development boards, making it convenient to use without external wiring.
Bootstrapping:
GPIO 2 is part of the ESP32 boot process and must be in a specific state during startup:
Pulled LOW (0): Normal boot.
Pulled HIGH (1): Bootloader (download mode).
Since it can tolerate changes in state without affecting the boot process in most configurations, it is a safe and reliable pin to use.
Hardware Design:
It is exposed and accessible on nearly all ESP32 boards.
It is not internally connected to peripherals (like ADC or touch sensors), so it is straightforward to use as a general-purpose output pin.
Yes, you can use almost any GPIO pin for your LED. However, some pins have special functions or restrictions:
Good Choices for LED Output
GPIO 4, 5, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 32, 33
Pins to Avoid or Use with Caution
GPIO 0, GPIO 2, GPIO 12, GPIO 15: These are bootstrapping pins. Incorrect states during startup can interfere with the boot process.
GPIO 6 to GPIO 11: These are connected to the onboard SPI flash and cannot be used as general GPIOs.
GPIO 34 to GPIO 39: These are input-only pins and cannot drive an LED.
Conflicts with Other Peripherals:
If GPIO 2 is already being used by a peripheral or the onboard LED, using a different GPIO prevents conflicts.
Application-Specific Needs:
For example, you might want to reserve GPIO 2 for debugging purposes or use it for an interrupt input.
2. Programming the ESP32:
To program an ESP32 in the Arduino IDE, you need to install some tools, libraries, and board support. Follow these step-by-step instructions:
Go to the Arduino IDE download page.
Download the latest version for your operating system (Windows, macOS, or Linux).
Install it by following the on-screen instructions.
Open Arduino IDE.
Go to Preferences:
In the menu, navigate to File → Preferences.
Add the ESP32 Board URL:
In the "Additional Board Manager URLs" field, add this URL:
bash
Copy code
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
If there are already URLs, separate them with a comma or new line.
Click OK.
Open Board Manager:
Navigate to Tools → Board → Boards Manager.
Search for ESP32:
In the search bar, type ESP32.
Install the ESP32 Package:
Click "Install" next to "esp32 by Espressif Systems."
Wait for the installation to complete.
For Blynk and relay control, you'll need specific libraries:
Install Blynk Library
Go to Library Manager:
Navigate to Tools → Manage Libraries...
Search for Blynk:
Type Blynk in the search bar.
Install:
Select the "Blynk" library and click "Install."
Ensure you install the latest version.
Install WiFi Library
The ESP32 package already includes the necessary WiFi libraries, so additional installation is usually not required.
Optional Libraries
For advanced features or additional components, install libraries like:
Adafruit_Sensor (for sensors).
DHT (for temperature and humidity sensors).
Go to Tools → Board.
Scroll down to find ESP32 Arduino and select your specific ESP32 board (e.g., ESP32 Dev Module).
Connect your ESP32 to your computer via USB.
Go to Tools → Port and select the port corresponding to your ESP32.
Open a simple example to ensure everything is working.
Go to File → Examples → WiFi → WiFiScan.
Upload the sketch to your ESP32:
Click the Upload button (arrow icon).
Open the Serial Monitor (Tools → Serial Monitor) to see the output.
If the ESP32 doesn’t upload correctly:
Press and hold the BOOT button on the ESP32 while uploading.
Ensure the correct USB drivers are installed (e.g., CP210x or CH340 for your ESP32 board).
code:
// Define the Blynk template ID (unique identifier for the project template)
#define BLYNK_TEMPLATE_ID "TMPL3LvPlMTOR"
// Define the Blynk template name (name of your project in the Blynk app)
#define BLYNK_TEMPLATE_NAME "LED on and off"
// Define the Blynk authentication token (unique key to link your ESP32 with the Blynk app)
#define BLYNK_AUTH_TOKEN "Kbb0EXLJRKIbE0fZGk5i1_sFjtb3Xs7-"
// Enable printing of debug information to the serial monitor
#define BLYNK_PRINT Serial
// Include the library for WiFi functionality on the ESP32
#include <WiFi.h>
// Include the library for WiFi client functionality
#include <WiFiClient.h>
// Include the Blynk library for ESP32 communication
#include <BlynkSimpleEsp32.h>
// Declare a character array to store the Blynk authentication token
char auth[] = "Kbb0EXLJRKIbE0fZGk5i1_sFjtb3Xs7-";
// Declare a character array to store the WiFi SSID (network name)
char ssid[] = "V2027";
// Declare a character array to store the WiFi password
char pass[] = "safwan123";
// Setup function runs once when the ESP32 starts
void setup() {
// Start the serial communication at 9600 baud rate for debugging
Serial.begin(9600);
// Initialize the Blynk library with the auth token, WiFi credentials,
// Blynk server address ("blynk.cloud"), and port number (80)
Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
}
// Loop function runs continuously after the setup function
void loop() {
// Run the Blynk library, which handles communication with the Blynk server
Blynk.run();
}
Now, copy and paste the Blynk Auth Token. It’s in the Blynk web dashboard.
And then, enter your WIDI SSID and password.
Now, select the board and port. After that, click the upload button. When the program is uploading, press and hold the boot button for a second.
OK, now you can control the LED bulb using the Blynk web dashboard and mobile dashboard.
3. Blynk App Configuration:
OK, let’s create the Blynk web dashboard for this project.
First, go to the Blynk website and create a new account using your Gmail address. If you already have an account, log in to your account using Gmail and your password.
Now let’s create a new template for this project. For that click on the “New Template” button and name it as you like. Then, select your board type as ESP32.
Next, go to the “Detastreams” tab and create a digital data stream. Select Digital PIN 2 for that. Name it as you like.
Now, click the web dashboard tab and drag and drop a button widget to the dashboard. And then, click the setting icon on the button widget and select the datastream you created earlier. After, save it.
And then, click the search icon and create a new Device. For that, select the template you created earlier.
OK, the Blynk web dashboard is ready for you.
Next, click the + icon and add a button widget to the dashboard. And then, click on the button and select the datastream you created in the web dashboard. You can change other settings as you like.
OK, the Blynk mobile dashboard is ready for you.
Now, connect the ESP32 board to the computer. And then, let’s create the program for that.
5.Applications
Home automation systems for lights, fans, and appliances.
IoT-based energy management.
Scalable for larger smart home systems.
Discuss any challenges faced and how you overcame them.
Share key learnings from the project.
Encourage visitors to explore more or connect with you:
"Want to learn more or build a similar project? Feel free to contact me or explore the resources on ElectroMentor!"
Have questions or need more information?
📞 Contact Mohammed shafwan at 6360322698
🌐 Visit us at https://sites.google.com/view/electromentor/project?authuser=0 for exciting updates and resources!