Marcio Cunha

Building Custom Automation Rack Controllers with ESP32 and FreeRTOS

Learn how to design dedicated circuit boards and controllers for industrial and residential racks using the ESP32 microcontroller and the FreeRTOS real-time operating system to ensure stability and instant response.

Marcio Cunha•6 min
Also available in:EspañolPortuguês
Summary
  • Affordable microcontrollers like the ESP32 enable the creation of robust controllers outside traditional industrial standards.
  • Splitting tasks into distinct priorities using FreeRTOS prevents sensor failures from freezing the entire system.
  • Proper electrical isolation and physical enclosure prevent noise and surges from destroying hardware in the field.
  • Stable communication via bus protocols ensures reliability when exchanging data between different automation racks.
  • Rigorous validation under prolonged load guarantees that the design supports years of continuous, crash-free operation.

Why Replace Traditional PLCs with Custom ESP32 Solutions

In the world of building and industrial automation, PLCs (Programmable Logic Controllers, which are rugged computers used to control machinery) have dominated the market for decades. However, the high cost and rigidity of these commercial platforms often make smaller projects unfeasible or require clumsy integration workarounds. This is where the ESP32 microcontroller comes in—a compact, inexpensive chip packed with wireless communication features that, when combined with custom code, transforms into a versatile control center. Building custom controllers for automation racks means designing tailor-made printed circuit boards capable of reading sensors, activating high-power relays, and talking to other devices without depending on closed, expensive ecosystems.

In practice, this means you gain total engineering freedom, paying only for the components you will actually use on your panel. However, this freedom brings crucial responsibilities. While an industrial PLC comes factory-shielded against electrical surges and severe interference, a custom design requires extra attention to physical board layout, power supplies, and cable organization inside the metal rack. Choosing the ESP32 as the brain of this system is justified by its dual-core processor and ease of integration with Wi-Fi and Bluetooth networks, as well as traditional serial ports used in automation.

Managing Chaos with the FreeRTOS Operating System

When we write simple code for microcontrollers, we usually use the traditional structure where everything runs in a single, endless sequential loop. In rack automation, this approach is a recipe for disaster; if a temperature reading routine freezes for half a second, the entire system stops responding to a critical emergency shutdown command. To solve this concurrency problem, we use FreeRTOS, which is a real-time operating system (a lightweight software that manages the division of processing time among different tasks). In practice, it allows us to slice up the processor execution so that multiple routines happen seemingly simultaneously.

With FreeRTOS, we can create isolated tasks and assign priorities to each one. For example, the digital input monitoring task can have top priority to react instantly to a panic button, while the routine sending data to a panel display runs in the background with lower priority. The operating system takes care of pausing and resuming these tasks down to the microsecond, ensuring no single routine monopolizes the chip. This brings operational determinism that is unreachable in common monolithic code, making the rack controller extremely reliable under stress conditions.

Hardware Architecture and Real-World Shielding

The environment inside an automation rack is physically aggressive for sensitive electronics. Starting motors, frequency inverters generating electromagnetic noise, and sharp power grid variations create a hostile scenario that can reset or fry the ESP32 if the design ignores basic hardware rules. The first line of defense is galvanic isolation, which involves using components like optocouplers (small chips that transmit electrical signals using light instead of direct physical contact) to separate the 3.3V logic part of the ESP32 from the power circuits driving 24V or 220V solenoids and motors.

Another critical point is the power supply. Industrial racks suffer from voltage drops and fluctuations. Instead of using cheap phone charger power supplies, the custom controller must be powered by good quality switched-mode industrial supplies, complemented by robust decoupling capacitors and efficient voltage regulators that dissipate heat well. Furthermore, using high-quality screw terminal connectors and thick copper traces on the printed circuit board ensures electrical current flows without unwanted heating, preventing mechanical failure points over years of operation.

Implementing Concurrent Tasks in the Base Code

To illustrate how FreeRTOS organizes ESP32 behavior in practice, let's look at a structured C code snippet designed to run independent tasks. In this example, we create two separate routines: one simulating the continuous reading of an air pressure sensor in the rack, and another blinking a system health indicator LED to ensure the processor hasn't locked up.

#include <stdio.h> "freertos/FreeRTOS.h" "freertos/task.h" "driver/gpio.h"  #define LED_PIN GPIO_NUM_2  void sensor_task(void *pvParameters) {     while (1) {         // Simulates reading a sensor in the rack         printf("Reading system pressure...
");         vTaskDelay(pdMS_TO_TICKS(1000)); // Pauses for 1 second without blocking the chip     } }  void indicator_task(void *pvParameters) {     gpio_pad_select_gpio(LED_PIN);     gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);     while (1) {         gpio_set_level(LED_PIN, 0);         vTaskDelay(pdMS_TO_TICKS(500));         gpio_set_level(LED_PIN, 1);         vTaskDelay(pdMS_TO_TICKS(500));     } }  void app_main() {     xTaskCreate(sensor_task, "SensorTask", 2048, NULL, 2, NULL);     xTaskCreate(indicator_task, "BlinkTask", 2048, NULL, 1, NULL); }

The code above demonstrates the simplicity and elegance of structuring a multi-task program on the ESP32. The xTaskCreate functions allocate memory space and register each routine in the FreeRTOS scheduler, while the vTaskDelay function is the key to energy efficiency and multitasking. Instead of using the traditional blocking delay() command that freezes the entire processor in a sterile loop, vTaskDelay tells the operating system: 'I am free for one second, give CPU control to another task'. This organized cooperation is the secret to keeping the rack controller agile and responsive.

Communication Protocols and Field Network Integration

A custom controller rarely operates in isolation; it needs to talk to supervisory systems, legacy PLCs, or cloud platforms. In the physical environment of an automation rack, the most common protocols include Modbus RTU over an RS-485 bus for long-distance noise-resistant wired communication, and MQTT over Wi-Fi or Ethernet networks for modern telemetry. The ESP32 has native support for serial ports and wireless networking, allowing the designer to implement complete communication stacks without needing complex, expensive converter chips.

In practice, structuring the communication network requires clearly defining who commands and who obeys on the bus, preventing data collisions that corrupt messages. Moreover, it is crucial to program the controller to handle connection drops gracefully. If the network goes down, the ESP32 must continue executing its local safety routines in the rack and attempt automated background reconnection, preventing infrastructure network failures from paralyzing the physical operation of connected equipment.

Final Considerations on Reliability and Bench Maintenance

Designing and building a custom controller for automation racks using the ESP32 and FreeRTOS requires a mindset shift away from amateur prototype assembly. It is not enough to just make the code work on a workbench with loose wires; you must validate thermal behavior, electrical noise immunity, and long-term stability under extreme conditions. Clear pinout documentation, proper DIN-rail mount enclosures, and careful board layout ensure the system can be easily maintained or repaired by any technician in the future.

When well-executed, this type of project delivers unbeatable cost-effectiveness and total expansion flexibility, allowing engineers and integrators to create robust, tailor-made solutions for specific demands that off-the-shelf products simply cannot meet. The union of affordable hardware and concurrent real-time software raises the bar for custom automation, proving that high-performance systems are within reach for anyone who masters control engineering fundamentals.