10. Output devices¶
Group assignment:¶
-
Measure the power consumption of an output device.
-
Document your work to the group work page and reflect on your individual page what you learned
To see our group assignment click here
Individual assignment:¶
- Add an output device to a microcontroller board you've designed and program it to do something.
Introduction to the project¶
Following our exploration of data acquisition, this week focuses strictly on Output Devices. I have designed a system that translates logical commands into tangible physical responses. The project utilizes an I2C LCD screen for a rich visual interface and a servomotor for precise motion control. The technical challenge this week lies in managing serial communication protocols for the display and generating PWM (Pulse Width Modulation) signals to control the motor's angle.
The 16x2 LCD Display (via I2C)¶
The 16x2 LCD display (via I2C) serves as a Human-Machine Interface (HMI). Its role is to translate the processor’s internal data into readable text messages, allowing the user to monitor the system status in real time.

The LCD display acts as the bridge between the computer program (invisible) and the user. In automation systems, a machine should always indicate what it is doing.
The LCD allows the system to display:
-
System status:
(e.g., "Initialization", "Ready", "Error"). -
Current actions:
When the servo motor moves, the screen displays its exact angle (e.g., "Angle: 45°").
1. Precision Monitoring¶
Since the project uses a servo motor, the LCD also serves to validate the command sent to the motor.
It displays the setpoint value transmitted by the ESP32, making it possible to verify that the mechanical movement correctly matches the logical value calculated by the microcontroller.
2. Space Management (I2C Optimization)¶
Thanks to the I2C module soldered on the back of the display, the LCD also simplifies the hardware architecture.
- It converts a complex data stream into readable characters while using only two pins (SDA and SCL) of the ESP32.
- This demonstrates the ability to use industrial serial communication protocols to control data-intensive peripherals without occupying all the input/output pins of the microcontroller board.
Servomotor¶
The servomotor is the driving force behind our project. Unlike a conventional motor, which runs continuously, the servomotor is designed to move to a specific position and hold it there.
In our system, it acts as the mechanical actuator. Its role is to convert a logical command (a digit in the code) into an actual physical movement.


How a Servo Motor Works¶
A servo motor is an actuator capable of controlling its angular position with high precision. Unlike a standard DC motor that rotates continuously, a servo motor moves to a specific angle based on a control signal.
1. Internal Components¶
A typical servo motor contains:
- A DC motor that generates rotation
- A gear reduction system that increases torque and reduces speed
- A potentiometer connected to the output shaft to measure the current angle
- An internal control circuit that compares the desired position with the actual position
2. Control Principle¶
The servo motor is controlled using a PWM signal (Pulse Width Modulation) sent by the microcontroller.
- The ESP32 sends periodic pulses to the servo.
- The width of each pulse determines the target angle.
Typical values are:
| Pulse Width | Approximate Angle |
|---|---|
| 1 ms | 0° |
| 1.5 ms | 90° |
| 2 ms | 180° |
The signal is repeated approximately every 20 ms.
3. Feedback System¶
The potentiometer continuously measures the current shaft position.
- If the shaft is not at the requested angle,
the control circuit powers the motor until the correct position is reached. - Once the target angle is achieved,
the motor stops automatically.
This closed-loop control system gives the servo motor its precision and stability.
The assembly¶

Installing the Library in Arduino IDE¶
To add a library in the Arduino IDE:
- Go to the menu Tools → Manage Libraries
- Search for "LiquidCrystal_I2C and ESP32Servo.h"
- Click Install


Project code¶
markdown id="system"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
// 1. Configuration de l'écran LCD (Adresse 0x27 ou 0x3F, 16 colonnes, 2 lignes)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// 2. Configuration du Servomoteur
Servo monServo;
#define SERVO_PIN 18 // Broche de sortie pour le servomoteur
void setup() {
// Initialisation du LCD
lcd.init();
lcd.backlight();
// Initialisation du Servomoteur
ESP32PWM::allocateTimer(0); // Allocation du timer pour l'ESP32
monServo.setPeriodHertz(50); // Fréquence standard de 50Hz pour un servo
monServo.attach(SERVO_PIN, 500, 2400); // Attache la broche avec les limites de signal (min, max)
// Message d'accueil
lcd.setCursor(0, 0);
lcd.print("SYSTEME OUTPUT");
lcd.setCursor(0, 1);
lcd.print("INITIALISATION...");
delay(2000);
lcd.clear();
}
void loop() {
// --- SEQUENCE 1 : Position 0 degres ---
lcd.setCursor(0, 0);
lcd.print("ETAT: REPOS ");
lcd.setCursor(0, 1);
lcd.print("ANGLE: 0 deg ");
monServo.write(0);
delay(2000);
// --- SEQUENCE 2 : Position 90 degres ---
lcd.setCursor(0, 0);
lcd.print("ETAT: MI-OUVERT ");
lcd.setCursor(0, 1);
lcd.print("ANGLE: 90 deg ");
monServo.write(90);
delay(2000);
// --- SEQUENCE 3 : Position 180 degres ---
lcd.setCursor(0, 0);
lcd.print("ETAT: MAXIMAL ");
lcd.setCursor(0, 1);
lcd.print("ANGLE: 180 deg ");
monServo.write(180);
delay(2000);
}
Brief Explanation of the Code¶
1. Library Inclusion¶
```cpp
include ¶
include ¶
include ¶
```
These libraries are used to: - Communicate with the LCD display using the I2C protocol - Control the servo motor using PWM signals on the ESP32
2. LCD Initialization¶
cpp
LiquidCrystal_I2C lcd(0x27, 16, 2);
This line creates an LCD object with:
- I2C address 0x27
- 16 columns
- 2 rows
3. Servo Initialization¶
cpp
Servo myServo;
This creates a servo motor object that will later be attached to a GPIO pin.
4. Setup Function¶
cpp
void setup()
The setup function runs once when the ESP32 starts.
It: - Initializes the LCD display - Turns on the LCD backlight - Attaches the servo motor to a GPIO pin - Prepares the system for operation
5. LCD Management¶
cpp
lcd.setCursor();
lcd.print();
These instructions are used to: - Define the text position on the LCD - Display messages and system information
The LCD acts as a Human-Machine Interface (HMI).
6. Servo Control¶
cpp
myServo.write(angle);
This command sends a target angle to the servo motor.
The ESP32 generates PWM signals that position the servo shaft precisely.
7. Feedback Display¶
cpp
lcd.print("Angle: 45");
For every servo movement, the LCD displays the current angle.
This provides visual feedback to the user and confirms that the command was correctly executed.
8. Timing Control¶
cpp
delay(1000);
The delay gives the servo motor enough time to physically reach its target position before executing the next instruction.
9. Infinite Loop¶
cpp
void loop()
The loop function continuously repeats the program sequence: - Move the servo motor - Update the LCD display - Wait before the next action
This creates a continuous automated control cycle.