This project simulates an ESP32 reading a custom HC-SR04 ultrasonic sensor in VS Code. The distance is dynamically controlled using a potentiometer.
- PlatformIO IDE (Compiles the ESP32 C++ code)
- Wokwi Simulator (Runs the simulation)
1. Build the ESP32 Firmware
Click the PlatformIO Build button (the ✓ checkmark icon in the bottom blue status bar) to compile main.cpp. Wait for the SUCCESS message.
2. Install the Wokwi CLI
The custom sensor is written in C and must be compiled into a WebAssembly (.wasm) file. Open a PowerShell terminal in VS Code and run:
iwr https://wokwi.com/ci/install.ps1 -useb | iex(Note: Close and reopen your terminal after this installs so Windows recognizes the command). if above command didn't work, refer the installation guide
3. Compile the Custom Chip Make sure your terminal is in the project's root folder, then run:
wokwi-cli chip compile custom-ultrasonic-sensor.c -o custom-ultrasonic-sensor.chip.wasmdo the above for all the custom chips
4. Start the Simulator
Open the diagram.json file. Click the Wokwi Play button in the top-right corner of the editor (or press Ctrl+Shift+P and select Wokwi: Start Simulator).
The simulation and Serial Monitor will start automatically. Turn the potentiometer knob to change the sensor's distance reading.
The free version of the Wokwi VS Code extension does not support graphical drag-and-drop editing. To change the circuit diagram:
- Go to wokwi.com and build your updated circuit in the web browser.
- Click the
{}(Code) tab in the web editor to view the underlying JSON. - Copy the updated code and paste it into your local
diagram.jsonfile.