Unleashing Creativity with ESP32: A Step-by-Step Guide to Building an Interactive Birthday Melody Project
Administration
/ 4 Dec, 2022
In the world of microcontrollers and embedded systems, the ESP32 has emerged as a versatile and powerful tool for creating innovative projects. With its rich set of features and extensive community support, the ESP32 opens up a realm of possibilities for developers and hobbyists alike. In this blog post, we will explore the process of building an interactive birthday melody project using an ESP32 microcontroller, along with a PIR motion sensor, buzzer, LEDs, and an LCD display. We will walk through each step, from setting up the Wokwi simulator to implementing the code and customizing the project to suit your preferences.
Inspiration Behind the Project:
Before diving into the technical details, let me share the inspiration behind this project. A few days ago, I came across an audio message from my 3-year-old niece on our family's group chat. Amidst the flurry of Happy Birthday GIFs and messages, this little boy stood out with his heartfelt and touching gesture. He sung "Happy Birthday" to his older sister, starting with a slow tempo and gradually increasing the speed. I was amazed and touched by the sincerity and creativity of his greeting. It made me realize that sometimes the most beautiful gifts come from the heart, and this inspired me to create a birthday melody project using an ESP32.
Step 1 Use a Wokwi simulator platform
To begin our project, we will utilize the Wokwi simulator, an online platform that allows us to simulate and test Arduino and ESP32 projects without the need for physical hardware. Wokwi provides a virtual environment where we can connect components, write code, and observe the project's behavior in real-time. This simulator is a game-changer for developers, as it enables rapid prototyping, experimentation, and iteration without the hassle of wiring up physical components. to Start the Wokwi simulation visit and create your account in www.wokwi.com
Fresh wokwi esp 32 arduino framework environment
Step 2: Create circuit diagram design
The goal of our birthday melody project is to create a system that detects motion using a PIR sensor and triggers a delightful birthday melody. When motion is detected, the buzzer will play the "Happy Birthday" tune, accompanied by a sequence of flashing LEDs. Additionally, an LCD display will show a personalized birthday message. We will implement this project using an ESP32 microcontroller and simulate it within the Wokwi environment, leveraging the power of C++ programming.
To bring our project to life, let's take a closer look at the circuit diagram. At the heart of the system is the ESP32 microcontroller, which will orchestrate the various components. The PIR motion sensor, connected to GPIO 13, will act as the trigger, detecting any movement in its vicinity. The buzzer, attached to GPIO 12, will be responsible for playing the melodic tunes. We will also incorporate three LEDs, connected to GPIOs 14, 27, and 26, to add a visual element to the birthday celebration. To protect the microcontroller from potential short circuits or damage, we will include resistors on the anode wire of each LED. Lastly, the LCD display will be interfaced with the ESP32 using the I2C protocol, with the SDA and SCL pins connected to GPIOs 21 and 22, respectively.
Step 3: Coding the Birthday Melody
Now, let's dive into the code that will breathe life into our project. We will be using C++ and the Arduino framework, which is fully compatible with the ESP32 in the Wokwi simulator. To start, we will include the necessary libraries, such as Wire.h for I2C communication and LiquidCrystal_I2C.h for controlling the 16x2 LCD display. We will define constants for the pin numbers of the PIR sensor, buzzer, and LEDs, making the code more readable and maintainable.
Next, we will create an instance of the LiquidCrystal_I2C object, specifying the I2C address and pin configurations for the LCD display. To store the "Happy Birthday" melody, we will define two arrays: melody and durations. The melody array will hold the frequency values for each note, while the durations array will store the corresponding duration of each note.
To play individual notes, we will implement the playNote function, which utilizes the ledcWriteTone function to generate the desired frequency and duration. The playBirthdayTune function will orchestrate the entire melody by iterating over the notes and durations arrays, calling the playNote function for each note. This function will also control the LEDs, creating a synchronized light show during the melody playback.
In the setup function, we will initialize the PIR sensor, buzzer, LEDs, and LCD display. We will also perform a quick test to ensure that the PIR sensor is functioning correctly by displaying a message on the LCD.
The heart of the project lies in the loop function, where the magic happens. It continuously checks for motion using the PIR sensor. When motion is detected (indicated by a LOW signal in the Wokwi simulator), it triggers a series of actions. First, the LCD displays a "Motion Detected!" message, followed by the birthday greeting. Then, the buzzer starts playing the "Happy Birthday" melody, accompanied by the LEDs flashing in sequence. To add an extra touch of excitement, the melody is played again at a faster tempo after the initial playback.
---------------------------- code sample -------------------------------------------
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int pirPin = 13;
const int buzzerPin = 12;
const int ledPins[] = {14, 27, 26};
const int numLeds = sizeof(ledPins) / sizeof(ledPins[0]);
LiquidCrystal_I2C lcd(0x27, 21, 22);
int melody[] = {262, 262, 294, 262, 349, 330, 262, 262, 294, 262, 392, 349, 262, 262, 523, 440, 349, 330, 294, 466, 466, 440, 349, 392, 349};
int durations[] = {500, 500, 1000, 1000, 1000, 2000, 500, 500, 1000, 1000, 1000, 2000, 500, 500, 1000, 1000, 1000, 1000, 1000, 500, 500, 1000, 1000, 1000, 2000};
void playNote(int frequency, int duration) {
ledcWriteTone(0, frequency);
delay(duration);
ledcWriteTone(0, 0);
delay(50);
}
void playBirthdayTune(int tempo = 120) {
int numNotes = sizeof(melody) / sizeof(melody[0]);
for (int i = 0; i < numNotes; i++) {
int noteDuration = durations[i] * (60.0 / tempo);
playNote(melody[i], noteDuration);
for (int j = 0; j < numLeds; j++) {
digitalWrite(ledPins[j], i % numLeds == j ? HIGH : LOW);
}
delay(noteDuration * 0.1);
}
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], HIGH);
}
}
void setup() {
pinMode(pirPin, INPUT);
pinMode(buzzerPin, OUTPUT);
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
lcd.init();
lcd.backlight();
ledcSetup(0, 2000, 8);
ledcAttachPin(buzzerPin, 0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PIR Sensor Test");
delay(2000);
lcd.clear();
}
void loop() {
if (digitalRead(pirPin) == HIGH) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motion Detected!");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Happy Birthday");
lcd.setCursor(0, 1);
lcd.print("to You!");
playBirthdayTune();
delay(1000);
playBirthdayTune(180);
lcd.clear();
delay(5000);
}
}
---------------------------------------------------------------- code
Step 4: Simulating and Testing the Project
With our code in place, it's time to put our project to the test using the Wokwi simulator. By clicking on the PIR sensor in the simulator, we can simulate motion detection. Upon triggering the sensor, the LCD will display the "Motion Detected!" message, followed by the birthday greeting. The buzzer will spring to life, filling the virtual environment with the joyous notes of the "Happy Birthday" melody. As the tune plays, the LEDs will illuminate in a synchronized pattern, adding a vibrant visual element to the celebration. The faster tempo playback after the initial melody adds an extra burst of energy and excitement to the experience.
Step 5: Customization and Modification
One of the great advantages of using the Wokwi simulator is the ease with which you can modify and customize your project. Don't be afraid to experiment and let your creativity run wild! You can try different melodies, adjust the tempo to your liking, or even add more LEDs to create a grander light show. The possibilities are endless, and the simulator provides a safe and convenient platform to iterate on your design without the need for physical hardware.
Step 6: Bonus - Bringing the Project to Life with Physical Components
While the Wokwi simulator is an excellent tool for prototyping and testing, there's something truly magical about seeing your project come to life with physical components. Once you're satisfied with your simulated design, you can take the next step and bring your birthday melody project into the real world using an ESP32 development board and the necessary components. The process of wiring up the circuit and uploading the code to the physical ESP32 is a rewarding experience that will deepen your understanding of embedded systems and microcontroller programming.
Here is my physical output using ESP 32
In this blog post, we have embarked on a journey to create an interactive birthday melody project using an ESP32 microcontroller, PIR motion sensor, buzzer, LEDs, and an LCD display. By leveraging the power of the Wokwi simulator, we were able to design, code, and test our project in a virtual environment, making the development process more efficient and accessible.
Through this project, we have seen how the ESP32 can be used to create engaging and interactive experiences. By combining motion detection, audio playback, visual effects, and personalized messages, we have crafted a birthday celebration that is sure to bring joy and surprise to the recipient.
But the beauty of this project lies not only in the final result but also in the journey of creation. By following the step-by-step guide, you have gained valuable knowledge and skills in embedded systems, C++ programming, and circuit design. You have also experienced the power of simulation and the flexibility it offers in terms of experimentation and customization.
I encourage you to take this project further and explore the endless possibilities that the ESP32 and other microcontrollers offer. Whether you want to add more sensors, integrate wireless connectivity, or create your own unique variations, the sky's the limit. The knowledge and experience you have gained from this project will serve as a strong foundation for your future endeavors in the world of embedded systems and IoT.
So go ahead, unleash your creativity, and continue to build amazing projects that bring joy and innovation to the world. Happy tinkering, and happy birthday to all the celebrants out there!
0 comments