In this project we will create an alarm system that will light a light and buzz a buzzer whenever anything approaches too closely to our ESP32 board! It can even send an alert to your phone!
The parts of the system are:
LEDS that the ESP32 can turn on, blink or flash
A buzzer that the ESP 32 can buzz, or even play notes on
The ultrasonic distance sensor that can detect objects and tell how far away they are
We will start off by wiring up the LED and buzzer.
How Will This Work?
We're going to first code up parts of this system in Tinkercad, which can simulate an Arduino and all its parts. After the simulation works, we're going to copy the sketch, or program, over to the Arduino desktop IDE and download it to the ESP32. (ESP32s and Arduinos are very similar and mostly compatible. But the ESP32 can do Wifi and Bluetooth connections)
Part 1 Objectives:
1. Create circuit with LED and Arduino.
2. Write a simple Arduino sketch
3. Upload and run the sketch
Activity: Light an LED
1. Log onto Tinkercad using the following link:
https://www.tinkercad.com/joinclass/6Z498FY1WAF9.
Your nickname is your first name. Make sure you click on 'circuits' on the left side of the page and choose 'Create new Circuit'
Don't worry if you blow up some Tinkercad parts--they are easily repaired and replaced.
2. Start with an Arduino connected to a breadboard.
3. Add an LED and connect one end to pin 4 and the other end to ground. Note that the ground on the Arduino is already connected to the breadboard for you.
2. Click on the CODE button (in the upper right) to open the code editor. A program to make a small built-in LED (circled in yellow in picture) blink is automatically loaded on your Tinkercad Arduino. Start the simulation to see what the code does.
3. Now modify the program as shown to make the LED on the breadboard blink,
Setting Up The Arduino IDE
Try it Live!
The microprocessors that we are using are called ESP32. First we will make sure the settings in the Arduino IDE program are correct. Then we will upload the sketch and make a real circuit work.
Copy your sketch from Tinkercad to the Arduino IDE desktop interface. If you are using Blocks in Tinkercad, you can get the text code by changing "Blocks" to "Blocks + Text".
Make sure that everything is properly connected to the breadboard and that the LED is oriented correctly. Connect the USB cable to the computer, check to make sure the port is correct, and upload your sketch. If the LED does not start blinking and your connections look correct, your LED could be backwards.
Part 2 Objectives:
1. Create circuit with Ultrasonic Distance Sensor
2. Write a sketch to output distance value to serial monitor
3. Modify sketch to light LED if object is too close
Activity: Light LED based on distance sensor
1. The ultrasonic sensor allows you to measure distances by sending out a signal and measuring how long it takes to return. We are using the one with four pins. Two of them are for power and ground. One to send out the signal (trigger) and the last to receive it (echo). To find the equivalent sensor in Tinkercad, look at All components and choose the blue sensor (the turquoise sensor has only 3 pins).
Here is an example circuit and code for the ultrasonic distance sensor.. Connect the Trigger to pin 6 and Echo to pin 5. (Redo figure to connect wires to correct pins)
You can use a codeblock in Tinkercad to read from this sensor. The blue output block is not active unless it is inside a control block.
Open the serial monitor at the bottom of the Code section to see the distance values. Click on the sensor while the simulation is running to use the green dot to change the output distance.
After the simulated distance sensor works, can you modify your code so that the LED will light up when an object gets too close? Hint: Start by creating a new variable and assign it to the value from the distance sensor.
Then check the value of the variable to decide whether or not to turn on the LED.
As before, download and test. This time, to see the results, you will need to open the serial monitor and set the baud rate to be the same as what the ESP 32 is transmitting. Then as you hold your hand over the ultrasonic sensor, you should see the distance change!
Part 3 Objectives:
1. Add a buzzer to circuit
2. Create a sound on the piezo buzzer using tone() command or block3. Activate buzzer if object is too close
4. Bonus: Use map to create familiar tune with buzzer
Activity: Sound a Buzzer
1. Create a new circuit in Tinkercad and wire a piezo buzzer to pin 7 and create the sketch. ,
Now modify the sketch so that the buzzer sounds if the object is too close to the ultrasonic sensor.
Bonus:
We can create more pleasant sounds from the buzzer. Let's start by restricting the notes to two octaves--the one below middle C and the one above middle C. Fortunately, the Arduino has a function that will convert the range of potentiometer values to the range of notes we would like to play. Replace the block that computes tonevalue with this block:
in text coding, use this commamd:
tonevalue=map(inputvar,0,1023,131,523);
to get the same notes. In the challenge, you will see what notes the numbers correspond to.
If you have trouble writing the code or want more information about the buzzer, click here. (This needs to be fixed)
Try it Live!
Try the simulations as real circuits.
Warning: with a real circuit, as long as the circuit is connected and the ESP32 is plugged in, the tune will repeat over and over. This is especially annoying, when the buzzer keeps playing while you are trying to upload a new sketch.
Part 4 Bonus:
- Make the ESP32 a Wifi access point
- Have it communicate with your phone
- Display the distance in the phone browser
Activity: Send the distance to your phone using Wifi
1. Send the distance data results to your phone
Copy the WiFi test program from this page. This program will turn your ESP32 into a web access point, and you need to give your WiFi connection a unique name.
- To test it, copy the program to the Arduino IDE, change the name of the WiFi connection, upload it to your ESP32.
- After it is finished uploading, check the IP address in the serial monitor, which should be 192.168.4.1. Now connect your phone to your ESP32 WiFi.
- Open a web browser on your phone and type the IP address in the serial monitor into your browser's address bar.
2. A program to test communication between the ESP32 and your phone is on TinkerCAD. Everyone needs a unique name for their ESP32 WiFi, so first change the name in the sketch.
3. Then upload the Wifi test code from TinkerCAD to your ESP32.
4. Connect your phone's WiFi to your ESP32. Open the browser on your phone and type 192.168.4.1 into the browser's address bar. Look for the test message in your browser.
5. Can you send the output from the distance sensor to your browser on your phone? An example sketch is available in TinkerCAD.
Part 5 Bonus:
Activity: Play music with your piezo buzzer.



No comments:
Post a Comment