Unleashing the Power of Embedded Development 🤖
Welcome to an exciting deep dive into my latest hand-vacuum project, Pleco! This project is a testament to how embedded development can bring hardware and software together in innovative ways. From sophisticated PID control to automated tuning, it integrates many facets of modern engineering in a single system. In this project, I bring together STM32L432KC hardware, the STM32CubeIDE, FreeRTOS concurrency, KiCad wiring schematics, and more. Whether you’re a developer, an aspiring embedded engineer, or a technology enthusiast, read on to learn about the project’s features, the tooling used, and some lessons learned along the way.
Overview of Pleco 🌟
Pleco is a small hand-vac system I created for fun. It leverages:
- STM32L432KC microcontroller for handling the logic and real-time tasks via FreeRTOS.
- A 12V PWM-controlled fan to provide the suction necessary for a handheld vacuum.
- I2C-driven LCD to display speed, battery voltage, and other stats.
- 3D printing for the mechanical enclosure.
- KiCad for wiring schematics and hardware design.
To get started exploring pleco, you can go directly to the latest stable release of my code on GitHub:
https://github.com/blakelton/pleco/releases/tag/V1.0.0
Prepare for A Mess – Wiring is FUN! Schematics can be found in the repo.
The combination of hardware and software is what makes embedded development so rewarding. Now, let’s explore the features and functionalities in detail.
Core Features and Functionalities
1. PID Control 🎛️
At the heart of Pleco is a PID loop (Proportional-Integral-Derivative). This algorithm adjusts the fan’s PWM duty cycle based on the error between the target speed and the measured speed (via the fan’s tachometer). Key highlights:
- Smooth Operation: Minimizes overshoot (fan accelerating too fast) and undershoot (fan never reaching setpoint).
- Adaptability: Suits a wide range of speeds, ensuring stable vacuum power.
2. Automated PID Tuning 🧪
Tuning PID gains by hand can be tedious. So, Pleco comes with an automated PID test:
- Predefined Gains: The system tries multiple values of Kp, Ki, and Kd.
- Performance “Score”: Each set of gains is tested for a certain time, and a performance score is calculated based on how well the fan speed stays on target.
- Best Gains: The code adopts whichever set yields the highest score for stable control.
3. PWM Reading and Generating 🎚️
- PWM Generation: The STM32L432KC generates PWM signals to control the fan’s speed. The duty cycle can range from near-zero to 100%, adjusting fan speed smoothly.
- PWM Reading: The fan’s tachometer output is read by the MCU to measure current speed in RPM. This reading is crucial for the PID loop to adjust the duty cycle effectively.
4. Power Throttling for Low Power ⚡
To prevent deep discharge of the LiPo battery:
- Voltage Monitoring: The system regularly checks battery voltage via the ADC.
- Threshold: If the battery voltage drops below 10.5V, the firmware shuts off the fan (sets duty cycle to zero) and blinks a “PLEASE RECHARGE!” message on the LCD.
- Safe Operation: Protects battery health and extends its lifecycle.
5. I2C Communication with the LCD 📺
- LCD 1602: An I2C interface is used for communication with the display.
- Task Scheduling: A dedicated FreeRTOS task updates the display with real-time fan RPM, battery voltage, and other stats.
6. 3D Printing and Mechanical Design 🏗️
- Custom Enclosure: Printed an enclosure specifically sized for the fan, battery, and control board.
- CAD Tools: 3D files in
.STL
or other standard formats, enabling easy replicability and modifications.
7. KiCad for Wiring Schematics 🏷️
- Schematic Clarity: All wiring—battery, fan, potentiometer, and LCD—was laid out in KiCad to avoid confusion.
- Scalability: Simple enough for a small project, yet robust for expansions.
8. Using the STM32 IDE for Embedded Development 🛠️
- STM32CubeIDE for coding, building, and debugging FreeRTOS tasks and the PID logic.
Why PID Control Loops Are Helpful
PID loops (Proportional–Integral–Derivative) are pivotal in precise control systems. They measure the difference (error) between a desired target (fan RPM) and the actual measured RPM, then adjust the PWM duty cycle accordingly. Why is this so beneficial?
- Smooth Operation
Without a control strategy, you might overcorrect and see wild oscillations in fan speed. A PID loop helps the fan reach and maintain the target RPM gracefully. - Adaptive Response
The PID controller constantly adjusts how much drive signal (PWM) it sends out. Changes in load or voltage are countered quickly, so the system adapts and remains stable. - Reduced Overshoot
A well-tuned PID means the fan won’t spin out of control, overshoot its target speed, or sag under load. Instead, it naturally converges around the desired setpoint.
Noise in Embedded Systems—and an Offset Solution
In real hardware, noise is practically inevitable. Wires can pick up interference from the fan’s motor, from the environment, or from voltage fluctuations. This can skew ADC readings—particularly for battery voltage—leading to inaccurate measurements.
In Pleco, you might see the system measure one voltage at lower RPMs and a noticeably different voltage at higher RPMs, even though the battery is actually quite stable. To handle this:
- Measured Noise: I observed that at higher RPMs, the battery ADC reading tended to appear artificially higher due to interference.
- Offset Approach: I add an offset—a small numerical correction—to the ADC reading when fan speed is high. This compensates for the noise-induced spike in measured voltage.
- Why It Matters: If the microcontroller thinks the battery is more charged than it really is, it won’t shut down in time. Our offset logic helps ensure the reported voltage remains accurate across various RPM conditions.
Importance of Battery Monitoring and Power Throttling
A significant aspect of Pleco is to protect the LiPo battery from damage. These batteries can degrade or become unsafe if discharged too deeply. Hence, Pleco includes:
- Periodic Voltage Checks
The system regularly reads battery voltage through an ADC channel. This data determines if the fan can continue running or needs to shut down. - Low-Power Threshold
If the battery voltage falls below 10.5V, Pleco immediately cuts fan power. This “power throttle” prevents over-discharge. - Visual Warning
The LCD blinks a “PLEASE RECHARGE!” message when the battery voltage is dangerously low, alerting users to plug in or replace the battery.
This approach ensures the longevity of the battery pack and maintains a safe operation.
Why FreeRTOS is Perfect for Embedded Development
FreeRTOS (Real-Time Operating System) is critical for the smooth operation of Pleco. It enables multitasking, precise timing, and efficient resource use, making it ideal for managing a system with various components like fans, LCDs, and batteries. Why is FreeRTOS so beneficial?
- Task-Based Multitasking
FreeRTOS breaks the firmware into independent tasks, ensuring each function operates seamlessly:- Fan Control Task adjusts PWM signals and implements PID loops for stable speed control.
- ADC Monitoring Task reads potentiometer and battery voltage inputs periodically.
- LCD Update Task refreshes the display with system information like RPM and battery level.
- Tachometer Monitoring Task measures fan RPM via pulse feedback and sets RPM to zero if no pulses are detected.
- Efficient Resource Use
FreeRTOS prevents any single function from dominating the processor, optimizing performance:- Tasks like fan control or ADC monitoring execute only when necessary.
- Lower-priority tasks, such as LCD updates, run when resources are free.
- Real-Time Responsiveness
Critical tasks like battery voltage checks or fan control are prioritized, ensuring they are completed without delays. This ensures Pleco adapts quickly to changing conditions, such as load variations or user input. - Scalable for Future Features
FreeRTOS makes it easy to expand Pleco’s functionality. For example, adding BLE control or advanced sensors would only require new tasks, which can be seamlessly integrated into the existing system.
Project Takeaways
- Embedded Magic: This project merges hardware (PWM fan, LiPo battery, 3D enclosure) with software (PID logic, I2C updates, concurrency in FreeRTOS).
- Safety & Reliability: Battery monitoring ensures the device doesn’t over-discharge. Tuning ensures fans don’t spin out of control.
- Reusability: The code structure—particularly the tasks for ADC, Display, Tach reading—can be adapted to other embedded projects.
- Fun Factor: Building something tangible that can be used daily (like a mini hand-vac) is incredibly rewarding!
Thanks for reading, and keep on building cool stuff in the embedded world! 😄
Happy Tinkering and Stay Powered Up!
— The Pleco Hand-Vac Project