Tinkercad classroom link: https://www.tinkercad.com/joinclass/MXSIK2X9I
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 activity are:
- LEDS that the ESP32 can turn on, blink or flash
- The ultrasonic distance sensor that can detect objects and tell how far away they are
- A buzzer that the ESP 32 can buzz, or even play notes on
- Alarm System
- Send distance values to your phone
- Play melodies with the 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. Put together a real circuit using the parts in your kit.
4. Upload and run the sketch on your ESP32 microprocessor.
Activity: Light an LED
1. Make sure you are at a computer with a USB-C port. If not, switch to a computer that has one.
2. Go to Tinkercad classroom.
Join with your student login, which is either (all lowercase) your first name or your first name and the first initial of your last name.
You may find it convenient to arrange your computer screen like this:
To start your first circuit, click on '+Create' on the top right corner of the page and choose 'Circuit'
3. Start with an Arduino connected to a breadboard.
4. 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.
6. 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.
7. Now modify the program as shown to make the LED on the breadboard blink. We've removed the built-in LED code blocks because the ESP32 does not have it. Don't worry if you blow up some Tinkercad parts--they are easily repaired and replaced.
Try it Live!
You should each have a kit with parts to put together a real circuit. Here is a description of the parts in your kit.
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. Here is a diagram of the pin numbers on our microprocessor. Each input/output pin has a power and ground pin. Power and ground on the breadboard can be connected to any power and ground pin on the ESP32.
Copy your sketch from Tinkercad to a new sketch in the Arduino IDE desktop interface. If you are using Blocks in Tinkercad, you can get the text code by stopping your simulation and changing "Blocks" to "Blocks + Text". You can select the text in the right panel, copy it and then paste it into a blank Arduino sketch in the Arduino IDE.
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.
Now you are ready for more interesting projects either in Tinkercad or using the Arduino IDE!
If the LED does not start blinking and your connections look correct, your LED could be backwards.
Feel free to try this at home! You can use the above link to log into Tinkercad from any computer. Set up the Arduino IDE at home for the ESP32 used in this program using these instructions.
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: Measure Distance using an Ultrasonic 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), which is the same as the real one in your kit.
Here is an example circuit for the ultrasonic distance sensor.. Connect the Trigger to pin 6 and Echo to pin 5.
You can use a codeblock in Tinkercad to read from this sensor. Some blocks are not active unless 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.
Try it Live!
As before, connect the actual circuit, download and test. Make sure nothing is blocking the distance sensor.
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!
With hardware everything has to be perfect! Please make sure all the wires are plugged in correctly. If the live circuit does not work, please start by double-checking the connections.
Part 3 Objectives:
- Add a buzzer to circuit
- Create a sound on the piezo buzzer using tone() command or block
Activity: Sound a Buzzer (or How to Annoy Your Parents)
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.
Try it Live!
Try the buzzer in a real circuit.
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.
Now you are ready to create your security system!
You now have all the tools you need to create your alarm system.
Can you modify your code so that the LED will light up and/or the buzzer sounds when an object gets too close? Hint: Start in Tinkercad by creating a new variable and assign it to the value from the distance sensor.
Then using the IF block (in Control) check the value of the variable to decide whether or not to turn on the LED.and/or create a sound.
Once it works in Tinkercad, try it live!
Before you leave the computer room, please prepare for Friday's activity here. Instructions for uploading the sketch for the robot activity at home are also included.
Part 4 Bonus:
- Make the ESP32 a Wifi access point
- Have it communicate with your phone
- Display the distance in the phone browser
- To see the distance on your phone change, you need to refresh the webpage.
- Every time you load a sketch to you ESP32, you need to reconnect to the WiFi access point
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, choose a unique name for your WiFi connection (instead of ESP32-Test), upload it to your ESP32.
- After it is finished uploading, check the IP address in the serial monitor, which is probably 192.168.4.1. Now connect your phone's WiFi to your ESP32 WiFi (look for your unique name)
- Open a web browser on your phone and type the IP address in the serial monitor into your browser's address bar. Don't worry if you get a warning that the "site does not support a securre connection" and continue anyway.
2. A program to test communication between the ESP32 and your phone is on Tinkercad. don't forget to put in the name of your ESP32 WiFi in your 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.
You can build on this to make applications that work through your phone.
Part 5 Bonus:
Activity: Play music with your piezo buzzer.
To Be Continued...
This is the end of this circuit activity but certainly not the end, but it does not have to be the last circuit you ever make. You can continue to create with the parts from this program, but you can also buy inexpensive kits that will give you many more parts so you can continue to create. For instance, here one that we bought for our own children. Most kits have tutorials to get started with your own projects.

