Modular Hexagonal Cell – TPS-Based Assembly Station
Controlling a 5V DC Motor using HC-SR04 with XIAO ESP32-C3
The objective of this group assignment was to measure the power consumption of an output device and document the complete process. For this purpose, a 5V DC motor controlled by a XIAO ESP32-C3 and an L298N driver was used as the test system.
The practical work began with the assembly of the circuit on a protoboard, ensuring correct connections between the microcontroller, motor driver, and power supply. A program was developed and uploaded to the XIAO ESP32-C3 to control the motor, allowing it to rotate in both directions at fixed time intervals.
Once the system was operating correctly, electrical measurements were performed using a multimeter. Voltage was measured in parallel across the motor terminals, and current was measured in series. These values were then used to calculate the power consumption of the motor using fundamental electrical formulas.
This assignment allowed the group to understand how real electrical systems behave under operation, reinforcing concepts such as voltage polarity, current flow, and energy consumption in embedded systems.
Through this activity, we learned how to properly use measurement instruments such as a multimeter, differentiate between voltage and current measurement techniques, and apply theoretical concepts to real-world systems. Additionally, we gained experience in analyzing how control logic affects the behavior and energy consumption of output devices.
In this project, a proximity detection system is developed using the HC-SR04 ultrasonic sensor and the XIAO ESP32-C3 microcontroller to control a 5V DC motor. The system uses distance measurements to activate or deactivate the motor through an L298N motor driver module.
When an object is detected within a defined range, the motor is activated. Otherwise, it remains off. This demonstrates how input devices can be used to control output devices in real-time embedded systems.
| Component | Description |
|---|---|
| XIAO ESP32-C3 | Microcontroller used for processing and control |
| HC-SR04 | Ultrasonic sensor for distance measurement |
| L298N Module | Motor driver used to control the 5V DC motor |
| DC Motor (5V) | Output device activated based on proximity |
| Power Supply | Provides voltage for motor and circuit |
| Jumper Wires | Connections between components |
The individual task involved designing, simulating, programming, and implementing a system where a DC motor is controlled by a proximity sensor.
For this project, a custom FabXIAO board based on the XIAO ESP32-C3 was used as the main microcontroller. This board is designed and fabricated as part of the digital fabrication workflow, allowing a compact and efficient integration of embedded systems.
The FabXIAO board acts as the central controller of the system. It executes the program logic, processes signals, and controls the output device (DC motor) through the L298N driver. The board generates digital signals to define the direction and timing of the motor’s operation.
Using a FabXIAO board reinforces the concept of designing and manufacturing custom electronics, enabling full control over both hardware and software components of the system.
The circuit includes:
Important: The motor cannot be connected directly to the microcontroller.
The schematic design defines how all components are electrically connected. This system integrates an ultrasonic sensor (HC-SR04), a motor driver (L298N), a DC motor, and the XIAO ESP32-C3 microcontroller.
Important: Use a voltage divider on the ECHO pin if needed to reduce from 5V to 3.3V.
The DC motor is connected to the output terminals of the L298N driver module. This module acts as an interface between the microcontroller and the motor, allowing safe control of direction and power.
The motor does not have polarity restrictions for basic operation. However, swapping the wires will reverse the rotation direction.
| IN1 | IN2 | Motor Behavior |
|---|---|---|
| HIGH | LOW | Rotation Direction 1 |
| LOW | HIGH | Rotation Direction 2 |
| LOW | LOW | Motor Stop |
The circuit was simulated to verify behavior before hardware implementation.
This program uses an ultrasonic sensor (HC-SR04) to measure distance and control a DC motor based on proximity. When an object is detected within a defined range, the motor is activated.
| Pin | Function |
|---|---|
| TRIG_PIN (4) | Sends ultrasonic pulse |
| ECHO_PIN (5) | Receives reflected signal |
| MOTOR_PIN (6) | Controls motor activation |
The distance is calculated using the time it takes for the ultrasonic wave to travel to the object and return:
Distance = (Time × Speed of Sound) / 2
| Condition | Motor State |
|---|---|
| Distance < 15 cm | Motor ON |
| Distance ≥ 15 cm | Motor OFF |
// Pin Configuration
const int TRIG_PIN = 4;
const int ECHO_PIN = 5;
const int MOTOR_PIN = 6;
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(MOTOR_PIN, OUTPUT);
}
void loop() {
long duration;
float distance;
// Trigger pulse
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read echo
duration = pulseIn(ECHO_PIN, HIGH);
// Distance calculation
distance = (duration * 0.0343) / 2;
// Motor control
if (distance < 15) {
digitalWrite(MOTOR_PIN, HIGH); // Motor ON
} else {
digitalWrite(MOTOR_PIN, LOW); // Motor OFF
}
Serial.print("Distance: ");
Serial.println(distance);
delay(100);
}
This implementation demonstrates how a DC motor can rotate in both directions (left and right) by controlling the logic states of a motor driver (L298N). By alternating the control signals with defined time intervals, the motor changes direction automatically every few seconds.
This approach is commonly used in automation systems where periodic motion is required, such as conveyor belts, oscillating mechanisms, or scanning devices.
| Component | Pin | XIAO ESP32-C3 |
|---|---|---|
| L298N | IN1 | D4 (GPIO6) |
| L298N | IN2 | D5 (GPIO7) |
| L298N | ENA | 5V (Enable) |
| IN1 | IN2 | Motor Direction |
|---|---|---|
| HIGH | LOW | Rotation Direction 1 |
| LOW | HIGH | Rotation Direction 2 |
// DC Motor Bidirectional Control with Time Intervals
const int IN1 = 6; // D4
const int IN2 = 7; // D5
void setup() {
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
}
void loop() {
// Rotate in one direction
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
delay(3000);
// Rotate in opposite direction
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
delay(3000);
}
By alternating the logic levels of IN1 and IN2 with time delays, the motor reverses its rotation direction. This creates a continuous left-to-right motion cycle without requiring additional sensors or feedback systems.
This assignment provided a comprehensive understanding of how output devices behave in real electrical systems, particularly in terms of power consumption. By integrating a DC motor with a microcontroller and a motor driver, we were able to move beyond theoretical concepts and directly observe how voltage, current, and control logic interact in practice.
One of the key learnings was the correct use of measurement instruments. We reinforced the importance of measuring voltage in parallel and current in series, as well as understanding how polarity changes when controlling bidirectional systems using an H-bridge. This helped us interpret negative values not as errors, but as indicators of direction changes.
Additionally, we developed a clearer understanding of how power is calculated and how even small variations in voltage and current can impact overall system performance. The process of calculating real power consumption allowed us to validate theoretical formulas with actual measurements.
From a system integration perspective, we also learned the importance of proper wiring, stable power supply, and the role of drivers like the L298N in protecting the microcontroller while enabling control of higher power devices.
Overall, this assignment strengthened our ability to analyze and design embedded systems, bridging the gap between electronics theory and practical implementation. It also emphasized the importance of accurate measurement and documentation in engineering workflows.