Industrial FABLAB UCuenca

Week 10 – Output devices

Modular Hexagonal Cell – TPS-Based Assembly Station

Week 10 - 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.

Group Assignment Development

To strengthen the group assignment, the test was organized as a complete measurement workflow instead of only a functional motor test. The group first identified the output device, the driver stage, and the power supply path. Then the system was tested in different operating states so that voltage, current, and power could be compared under real conditions.

Test Condition What Was Observed Learning
Motor stopped The circuit consumed very low current. A system can still consume energy even when the actuator is not moving.
Motor rotating forward Current increased when the L298N activated the motor. The driver protects the microcontroller from the motor load.
Motor changing direction The current changed during the transition between directions. Direction changes create transient electrical demand.
Motor under mechanical resistance The current demand increased when the motor had more load. Mechanical load must be considered when selecting power supplies and drivers.

The power was calculated using P = V x I. This made the group exercise more useful for the final project because the modular manufacturing cell will require actuators that move reliably without overloading the controller or the power distribution system.

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 sensor signals can be used to control output devices in real-time embedded systems.

System Functionality

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

Bill of Materials

Component Qty. System Role Power / Signal Why It Is Used
FabXIAO / XIAO ESP32-C3 1 Controller 3.3V logic / USB power Reads sensor data and sends digital control signals to the driver.
HC-SR04 1 Input sensor 5V power / TRIG-ECHO signals Measures distance and defines when the motor should activate.
L298N Module 1 Motor driver External motor supply / IN1-IN2 logic Allows the microcontroller to control motor direction and power safely.
DC Motor 1 Output actuator 5V test motor / 12V planned motor Produces movement; the final system will use a 12V DC motor for the conveyor belt.
Power Supply 1 Energy source Separated logic and motor power Prevents the actuator from overloading the microcontroller board.
Jumper Wires Several Interconnections Signal, VCC, and GND Connect the FabXIAO, sensor, driver, and external power reference.
Multimeter 1 Measurement tool Continuity / voltage / current Used to check the PCB and wiring before powering the actuator.

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, 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.

Custom PCB Construction for Output Devices

For this output devices assignment, the electronic board used in the Input Devices assignment was documented again because it became the control base for the actuator tests. The same FabXIAO PCB organizes the XIAO ESP32-C3, power rails, ground reference, and signal pins. This is important because an output device such as a motor or servo should not be connected directly to the microcontroller without checking the control and power paths.

The board fabrication process started with the schematic and PCB layout for the XIAO ESP32-C3 connections. The traces were fabricated using the fiber laser workflow documented in Input Devices, then the board was cleaned, inspected, soldered, and tested with a multimeter before connecting the output driver.

Fabrication and Assembly Steps
  1. Schematic review: The XIAO pins, 5V line, GND, and signal outputs were identified before connecting the actuator circuit.
  2. PCB engraving: The copper traces were produced with the fiber laser process used in Week 09.
  3. Cleaning: Residues were removed so that small copper bridges could be detected visually.
  4. Soldering: Headers and connection points were soldered carefully to avoid lifted traces.
  5. Continuity test: Power, ground, and signal lines were checked before energizing the board.
  6. Output integration: The PCB was connected to the L298N driver and motor test setup.
Relation to the Final Project

This board is related to the final project because the Smart Lean Cell needs a compact controller for modular stations. The same logic used here can be expanded to control conveyor movement, object detection, and actuator timing in the assembly cell. The output device test therefore became a first functional step toward a custom electronics module for the final project.

PCB Problems Reused as Learning for Outputs

The PCB construction also showed that output devices demand more careful verification than simple signal tests. Any short circuit, weak ground, or lifted trace can affect the driver and the actuator. For that reason, the board was checked before connecting the motor power stage.

1️⃣ Schematic Design

The circuit includes:

  • HC-SR04 connected to digital pins
  • DC motor controlled through the L298N driver
  • Common ground between the controller, driver, and power supply

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

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

FabXIAO Output Connections

Before running the bidirectional motor test, the FabXIAO board was connected to the L298N driver and checked as the control interface for the output device. The XIAO ESP32-C3 sends only logic signals, while the L298N receives external motor power and switches the motor terminals safely.

FabXIAO / XIAO Pin Connected To Purpose
D4 / GPIO6 L298N IN1 Controls one side of the H-bridge direction logic
D5 / GPIO7 L298N IN2 Controls the opposite side of the H-bridge direction logic
GND L298N GND and power supply GND Creates a common reference for the control signals
USB / 5V logic supply FabXIAO board power Powers the controller during programming and testing
External motor supply L298N motor voltage input Feeds the actuator separately from the microcontroller

This wiring evidence shows the custom PCB being used as the controller board for the output test. The motor driver is separated from the FabXIAO so that the high-current side of the system does not pass through the microcontroller.

FabXIAO PCB connected to L298N motor driver for output device test

Evidence of the FabXIAO PCB used as the control board for the output device test.

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.

Problems During Servo Tests and Lessons Learned

During the output device tests, several servos were damaged while trying to validate actuator movement for the final project. This was an important part of the process because it showed that an actuator can fail even when the code logic seems correct. The problem was related to power distribution, wiring verification, and the difference between a control signal and the current required by the actuator.

After these tests, the decision was made not to continue using servos for this mechanism. The final project requires moving a conveyor belt, so the actuator selected for the next development stage is a 12V DC motor. This change is also the preamble for the Machines Design assignment, where the belt system, motor power, transmission, and mechanical integration will be developed in more detail.

What Happened
  • Some servos overheated during repeated movement tests.
  • In some trials, the servo stopped responding after being connected to an unstable power setup.
  • The tests showed that powering an actuator from an inadequate source can damage the component.
  • Shared ground was necessary, but the power line for the actuator also needed to be checked independently.
Probable Causes
Issue Effect Correction
Unstable power supply Servo overheating or erratic motion Use an external regulated supply sized for the actuator current.
Incorrect wiring order Risk of damaging the servo electronics Verify VCC, GND, and signal before powering the circuit.
No current verification The actuator demand was underestimated Measure current during movement and stall-like conditions.
Mechanical resistance Higher load and higher current draw Move to a 12V DC motor for the conveyor belt mechanism and size the power system correctly.
Learning Outcome

The damaged servos helped define a safer workflow for future actuator tests: first test the board without the actuator, then verify voltage and continuity, then connect the actuator with an external power supply, and finally test the movement with short commands before running a full cycle. This lesson is directly connected to the final project, where the modular cell will require reliable actuation and safe power distribution. For the next stage, the work will focus on controlling a 12V DC motor because it is more appropriate for driving a conveyor belt than a small servo.

Burned servo after output device testing

Evidence of the damaged servo used during the output device tests.

Reflections

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.