Industrial FABLAB UCuenca

Week 10 – Output devices

Modular Hexagonal Cell – TPS-Based Assembly Station

Week 20 – Output Devices

Controlling a 5V DC Motor using HC-SR04 with XIAO ESP32-C3

Group Assignment – Power Consumption Measurement

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.

Learning Reflection

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.

View Group Assignment Page

Individual Assignment

Project Overview – Motor Control with HC-SR04

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.

System Functionality

  • Distance < 15 cm → Motor ON
  • Distance ≥ 15 cm → Motor OFF

Components Used

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

Key Considerations

  • The motor is controlled via the L298N driver, not directly from the microcontroller
  • Ensure common ground between all components
  • Verify correct voltage levels for safe operation

The individual task involved designing, simulating, programming, and implementing a system where a DC motor is controlled by a proximity sensor.

Microcontroller Board – FabXIAO

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.

Key Features
  • Based on ESP32-C3 architecture
  • Compact form factor suitable for prototyping
  • Digital I/O pins for sensor and actuator control
  • Low power consumption
  • USB interface for programming and communication
Role in the System

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.

Advantages in This Project
  • Custom fabrication aligns with Fab Academy methodology
  • Compact design reduces wiring complexity
  • Reliable performance for real-time control
  • Easy integration with sensors and drivers

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.

1️⃣ Schematic Design

The circuit includes:

  • HC-SR04 connected to digital pins
  • DC motor controlled via transistor
  • Flyback diode to protect circuit

Important: The motor cannot be connected directly to the microcontroller.

Schematic Design – Step by Step

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.

Step 1: Power Distribution

  • Connect GND of all components together (common ground)
  • Provide 5V to the HC-SR04 and L298N module
  • The XIAO ESP32-C3 operates at 3.3V logic

🔌 Step 2: HC-SR04 Connections

HC-SR04 Pin Connection XIAO ESP32-C3
VCC Power Supply 5V
GND Common Ground GND
TRIG Digital Output D2 (GPIO4)
ECHO Digital Input D3 (GPIO5)

Important: Use a voltage divider on the ECHO pin if needed to reduce from 5V to 3.3V.

Step 3: L298N Motor Driver Connections

L298N Pin Function XIAO ESP32-C3
IN1 Motor Control D4 (GPIO6)
IN2 Motor Control D5 (GPIO7)
ENA Enable Motor / PWM 5V or PWM Pin
GND Common Ground GND
VCC Motor Power 5V External

Step 4: Motor Connection

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.

Connection Steps
  • Locate the output terminals on the L298N labeled OUT1 and OUT2
  • Connect one wire of the motor to OUT1
  • Connect the other wire of the motor to OUT2

The motor does not have polarity restrictions for basic operation. However, swapping the wires will reverse the rotation direction.

How Direction Control Works
IN1 IN2 Motor Behavior
HIGH LOW Rotation Direction 1
LOW HIGH Rotation Direction 2
LOW LOW Motor Stop
Important Considerations
  • The motor must be powered through the L298N module, not directly from the microcontroller
  • Ensure the motor power supply (5V) is properly connected to the L298N
  • All grounds (XIAO, L298N, power supply) must be connected together
  • Verify that the ENA jumper is enabled or connected to 5V for activation

Step 5: Logic Behavior

  • IN1 = HIGH, IN2 = LOW → Motor rotates forward
  • IN1 = LOW, IN2 = HIGH → Motor rotates reverse
  • IN1 = LOW, IN2 = LOW → Motor stops

Key Design Considerations

  • Ensure all grounds are connected together
  • Do not power the motor directly from the microcontroller
  • Use stable external power for the motor if required
  • Verify voltage compatibility between components

2️⃣ Simulation in Wokwi

The circuit was simulated to verify behavior before hardware implementation.

  • Added XIAO ESP32-C3
  • Configured HC-SR04
  • Simulated motor using output pin

3️⃣ Arduino IDE Code

Code Description – Ultrasonic Sensor + Motor Control

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 Configuration
Pin Function
TRIG_PIN (4) Sends ultrasonic pulse
ECHO_PIN (5) Receives reflected signal
MOTOR_PIN (6) Controls motor activation
Program Workflow
  • Initialize serial communication for monitoring data
  • Configure TRIG as output and ECHO as input
  • Set motor control pin as output
  • Send a 10 microsecond pulse to trigger the sensor
  • Measure the duration of the echo signal
  • Calculate the distance using the speed of sound
  • Activate the motor if the object is closer than 15 cm
  • Print distance data to the serial monitor
Distance Calculation

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

Motor Control Logic
Condition Motor State
Distance < 15 cm Motor ON
Distance ≥ 15 cm Motor OFF
Key Considerations
  • Ensure correct timing for the trigger pulse (10 microseconds)
  • Use a common ground for all components
  • Verify stable power supply for the motor
  • Adjust distance threshold based on application needs

Code Implementation

// 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);
}
    

Bidirectional Motor Control Using Time Intervals

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.

Pin Configuration
Component Pin XIAO ESP32-C3
L298N IN1 D4 (GPIO6)
L298N IN2 D5 (GPIO7)
L298N ENA 5V (Enable)
Connection Details
  • Connect motor terminals to OUT1 and OUT2 of the L298N module
  • Connect IN1 to D4 and IN2 to D5 on the XIAO ESP32-C3
  • Ensure the ENA pin is connected to 5V or enabled with a jumper
  • Provide 5V external power to the L298N motor supply
  • Connect all grounds together (XIAO, L298N, and power supply)
Motor Control Logic
IN1 IN2 Motor Direction
HIGH LOW Rotation Direction 1
LOW HIGH Rotation Direction 2
Code Implementation
// 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);
}
    
Key Concept

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.

Group Reflection

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.