Week 4 - Embedded Programming
Published on: February 19, 2025
This week is about embedded programming. I was a little disappointed that we were obliged to simulate the operation of a microcontroller instead of programming a development board as usual. Nevertheless, as I like programming, this week's assignment was fun and a good opportunity to get back into programming microcontrollers, since I finished my last microcontroller project over a year ago.
Browsing through the ESP32 Datasheet
The ESP32 is a small but powerful computer chip that can connect to the internet using Wi-Fi (IEEE 802.11 b/g/n (2.4 GHz)) and Bluetooth (Bluetooth v4.2 BR/EDR and Bluetooth LE). It is often used in smart devices, home automation, and IoT (Internet of Things) projects. The chip has one or two processor cores (Xtensa single-/dual-core 32-bit LX6) that can run at up to 240 MHz, which makes it fast. It also has memory (520kB SRAM and 448kB ROM) to store data and programs.
GPIO Pins and Sensors
The ESP32 has 34 GPIO pins. These pins can be used to connect buttons, sensors, LEDs, motors, and other electronic parts. Some pins can only be used for input (reading signals), while others can send or receive signals. The ESP32 also has special pins for analog sensors, so it can measure voltages and analog values like temperature, light, and humidity.

PWM (Pulse Width Modulation)
The ESP32 can control motors, LEDs, and other devices using PWM (Pulse Width Modulation). This allows it to dim LEDs smoothly or change the speed of motors. It has 16 PWM channels, so it can control many devices at the same time. The chip also has a special motor control unit, which is useful for robotics and automation projects.
Power and Voltage
The ESP32 can work with a voltage between 2.3V and 3.6V, but 3.3V is the most common voltage. It has different power modes to save energy:
- Active Mode: Everything is working (Wi-Fi, Bluetooth, CPU)
- Modem Sleep Mode: The CPU is running, but Wi-Fi and Bluetooth are off
- Light Sleep Mode: The CPU is paused, but it can wake up quickly
- Deep Sleep Mode: Only a small part of the chip is on, using very little power (10 µA consumption)
- Hibernation Mode: Almost everything is off, using almost no power (5 µA consumption)
This helps battery-powered projects last longer.
Wi-Fi and Bluetooth
The ESP32 can connect to Wi-Fi networks to send and receive data. It can also use Bluetooth, so it can talk to smartphones and other Bluetooth devices. It supports Bluetooth Low Energy (BLE), which uses very little power, making it great for wearable devices and wireless sensors.
Why choose the ESP32?
I chose the ESP32 for my project because of its built-in connectivity features and its simplicity to program. In addition it has many pins for connecting different sensors and devices and supports multiple energy-saving power modes.
Simulation of a Microcontroller
I used wokwi.com to simulate the operation of a microcontroller.

Wokwi is an easy-to-use online tool that provides simulations for four different development boards and their simulation on their homepage. As I want to use the ESP32 in my final project, I decided to use that microcontroller this week too.
I selected the "Starter Template" of the basic ESP32 and by pressing the play button, I compiled the "hello world" - script.

Next I wanted to simulate the operation of two ultrasonic sensors.

By clicking the "+" button, you can add components to your simulation. I added two ultrasonic sensors and wired them to the ESP32.

The next step was to write a program. I did a little research and found out that the ultrasonic sensor calculates the distance by using the duration the sound needs to be reflected by the object and returning to the sensor. Thus, the formula to calculate the distance is: (duration × sound speed) / 2.
At the beginning of my program I defined the values for SOUND_SPEED, TIMEOUT and the trigger and echo pins of the sensors.
Then, in the void setup(), I set the baud rate to 115200. The Baud rate represents the number of signal changes or symbols transmitted per second. If you select the wrong Baud Rate, your computer will not be able to understand the transmission as it doesn't know where a symbol ends. Therefore it is important for the serial communication with your computer.
Finally, I set the pinMode() of the used pins to OUTPUT for the trigger pins and INPUT for the echo pins. That's it for the setup.
In the loop, I measured the duration of the left and right sensors and calculated the distance. Finally, I printed the results to the simulated computer via Serial.print().

By clicking on the sensors, you can change the measured distance.

At the bottom of the picture, you can see the measured distances.
Development Board
The next step was to bring the simulation to a real development board. I only had an old ultrasonic sensor at home without pins. So soldering was required.

I wrote a simple sketch to measure the distance with the ultrasonic sensor using Arduino IDE.

The distance was measured and displayed in the serial monitor.
Reflection on the Group Assignment
This week, I explored embedded programming, focusing on different microcontrollers, toolchains, and workflows. I specifically worked with the RP2040 microcontroller and experimented with both the Arduino RP2040 Connect and the Raspberry Pi Pico.


You can find our weekly group assignment on the designated group assignment page.
The RP2040 is a dual-core ARM Cortex-M0+ microcontroller with 30 GPIOs, support for PWM, SPI, I2C, and UART, and an advanced PIO (Programmable I/O) system. Unlike the ESP32, it does not have built-in Wi-Fi or Bluetooth, but it is efficient, fast, and well-suited for real-time applications.
While I had no issues programming C code in the Arduino IDE, I encountered some difficulties when trying to upload new programs to the RP2040 in MicroPython. An old servo control program kept running, preventing new scripts from executing. I resolved this by reflashing the MicroPython firmware, which reset the board. After that, I was able to use Thonny IDE to successfully upload and run a simple "Hello, world!" script.