What I did in Week 04

We will learn the theoretical part of how microcontrollers work, we will write code, simulate, and build.

Assignments for the week:
  • Group: demonstrate and compare the toolchains and development workflows for available embedded architectures.
  • Individual: browse through the data sheet for a microcontroller.
  • Individual: write and test a program for an embedded system using a microcontroller to interact and communicate
  • Extra credit: assemble the system.

Group page

link

ESP32 Datasheet

The ESP32 is a very versatile microcontroller that offers up to 34 digital GPIO pins and 18 analog pins, allowing a wide range of connections with sensors, actuators, and other electronic modules.

  • Up to 34 digital GPIO pins
  • Up to 18 analog (ADC) pins
  • 16 PWM channels for motor and LED control
  • UART, SPI, I2C, I2S, and CAN communication protocols
  • Integrated Wi-Fi and Bluetooth connectivity
  • 3.3V and GND power pins
  • TinstallGIT

    Phase 1 · ESP32 Technical specifications.

    The heart of the ESP32 is a high-performance microcontroller designed to handle complex tasks and wireless connectivity, making it ideal for IoT projects and embedded systems. Its capabilities allow for rapid data processing and the simultaneous control of multiple devices.

  • Microcontroller: Tensilica Xtensa LX6, dual-core, 32-bit
  • Operating frequency: up to 240 MHz
  • Flash memory: 4 MB
  • SRAM: 520 KB
  • ADC: 12-bit
  • DAC: 2 channels
  • Connectivity: WiFi 802.11 b/g/n and Bluetooth 4.2 (Classic and BLE).
  • TinstallGIT

    Phase 2 · Main components of the board.

    The heart of the ESP32 is a high-performance microcontroller designed to handle complex tasks and wireless connectivity, making it ideal for IoT projects and embedded systems. Its capabilities allow for rapid data processing and the simultaneous control of multiple devices.

  • Microcontroller: Tensilica Xtensa LX6, dual-core, 32-bit
  • Operating frequency: up to 240 MHz
  • Flash memory: 4 MB
  • SRAM: 520 KB
  • ADC: 12-bit
  • DAC: 2 channels
  • Connectivity: WiFi 802.11 b/g/n and Bluetooth 4.2 (Classic and BLE).
  • Embedded Programming

    In this exercise, an automatic speed control system for a brushless motor was implemented using an ESP32 board, an electronic speed controller (ESC), and an HC-SR04 ultrasonic sensor. The objective was to regulate the motor's speed based on the distance detected by the sensor, simulating a proximity-based automatic control system. The ESP32 was used as the main processing unit due to its ability to generate PWM signals and its versatility in automation and embedded systems applications. The ultrasonic sensor allowed distance measurement using the time-of-flight of ultrasonic waves, while the ESC allowed the brushless motor's speed to be controlled using servo-like signals.

    Command Palette

    Step 1 · Load the code

    The code was developed and executed in the Arduino IDE programming environment version 2.3.3, which allowed the compilation and uploading of the program to the ESP32 board. As a first step, it was necessary to correctly select the board model (ESP32 Dev Module) and the corresponding communication port from the Tools menu, in order to guarantee correct communication between the computer and the microcontroller.

    #include 
    
    Servo esc;
    
    int escPin = 18;
    
    int trigPin = 5;
    int echoPin = 17;
    
    long duration;
    float distance;
    
    void setup() {
    
    Serial.begin(115200);
    pinMode(trigPin,OUTPUT);
    pinMode(echoPin,INPUT);
    esc.attach(escPin,1000,2000);
    esc.writeMicroseconds(1000);
    delay(5000);
                 }
    
    void loop(){
    
    digitalWrite(trigPin,LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin,HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin,LOW);
    duration = pulseIn(echoPin,HIGH);
    distance = duration * 0.034 / 2;
    
    if(distance < 10){
    esc.writeMicroseconds(1000);
    }
    else if(distance < 20){
    esc.writeMicroseconds(1150);
    }
    else{
    esc.writeMicroseconds(1300);
    }
    delay(500);
              }
      

    Step 2 ·Code

    This code allowed the distance to be measured and the motor speed to be controlled:

    System implementation

    In this section we will implement our system with the actual electronic components

    VSCode interface overview

    Step 1 · Used items.

    The materials used for the implementation of the system were the following:

  • ESP32 DevKit
  • Brushless motor
  • Electronic Speed Controller(ESC)
  • Ultrasonic sensor HC-SR04
  • LiPo batery 3S
  • Protoboard
  • Dupont cables
  • USB cable
  • Visual consistency

    Step 2 · Energy source.

    The system was powered by an 11.1V LiPo battery for the motor, while the ESP32 was powered via the computer's USB port to ensure stability during programming and testing.The LiPo battery must be fully charged for the activity to work correctly.

    Content workflow

    Step 3 · Connection diagram

    The system connections were made as follows:

    Workflow.

    The procedure followed during the lab was as follows:

  • The condition of the electronic components was checked.
  • The ESC was connected to the brushless motor.
  • The ultrasonic sensor was connected to the ESP32.
  • A voltage divider was implemented to protect the ECHO pin.
  • The ESP32 was programmed using the Arduino IDE.
  • The code was tested with the ESC controller.
  • The LiPo battery was connected after uploading the program.
  • The ESC's operation was verified using audible signals.
  • Tests were performed varying the sensor distance.
  • Once the development environment was configured, the program responsible for measuring the distance using the HC-SR04 ultrasonic sensor and controlling the speed of the brushless motor through the ESC, using servo-type PWM signals generated by the ESP32, was loaded.

    System operation.

    The system works by continuously measuring the distance detected by the ultrasonic sensor. The ESP32 sends a pulse to the sensor and measures the response time to calculate the distance. Depending on the detected distance:

  • Distance less than 10 cm → motor stopped
  • Distance between 10 and 20 cm → motor at low speed
  • Distance greater than 20 cm → motor at medium speed
  • The ESC interprets the PWM signal sent by the ESP32 as a speed control signal, allowing the brushless motor's revolutions per minute (RPM) to be varied.

    Visual consistency

    Results obtained.

    During the experiment, the correct operation of the automatic control system was verified. The ultrasonic sensor accurately detected the distance to objects, and the ESP32 processed this information to control the motor speed. It was observed that the motor responded appropriately to changes in distance, decreasing its speed when an object approached and increasing it when it moved away. The importance of proper ESC assembly and correct system power supply was also confirmed. As a final result, a functional sensor-based automatic control system was obtained, demonstrating the practical application of microcontrollers in industrial automation and control systems.

    Download files