Marcio Cunha

How to build an IoT controller board with ESP32 and CAN bus

Learn how to design an IoT controller board combining the ESP32 and CAN bus for cheap, robust, and fault-tolerant industrial communication.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • The ESP32 integrates native Wi-Fi and Bluetooth, but lacks physical robustness for noisy industrial environments.
  • The CAN bus standard uses voltage differentiation to ensure reliable communication even under heavy electromagnetic interference.
  • MCP2551 or TJA1050 controllers act as essential transceivers to translate microcontroller logic signals into the physical network.
  • Board power supply design requires strict galvanic isolation and filtering to prevent crashes from motor noise.
  • Designing dedicated integrated circuits reduces costs and enables decentralized automation in legacy machinery.

Why combine the ESP32 with an industrial CAN network?

In automation, connecting devices to the internet usually requires expensive hardware and complex systems. However, combining the ESP32 microcontroller—a cheap chip with built-in Wi-Fi—with a CAN bus interface (a robust communication protocol used in cars and factories) solves this problem elegantly. In practice, this means you can gather data from factory floor sensors and send them to the cloud without spending a fortune on traditional programmable logic controllers.

Often, Internet of Things projects suffer from Wi-Fi unreliability in industrial warehouses full of metal barriers. This is where the CAN bus shines, as it was originally designed to connect automotive computers under severe conditions of vibration and electrical noise. When we build a custom board, we eliminate messy breadboard setups and create a product ready for the real world.

Understanding the physics and protocol of the CAN bus

Controller Area Network (CAN bus) works similarly to a radio system where all devices listen to the same frequency, but only the correct recipient replies. It uses two twisted wires, called CAN High and CAN Low, which transmit data based on the voltage difference between them. In practice, if strong electromagnetic interference is generated by a neighboring electric motor, it affects both wires equally, canceling out the error.

For the ESP32, which operates at 3.3-volt logic levels, to converse with this differential network, we need an intermediate chip called a CAN transceiver. Components like the TJA1050 or MCP2551 take digital signals from the microcontroller and convert them to the physical network standard. Furthermore, much like strategies we discuss for diagnosing packet loss and high latency in networks, understanding the physical integrity of the transmission medium prevents mysterious packet drops on the factory floor.

Designing the printed circuit board and galvanic isolation

Printed circuit board design is where being cheap can backfire if you ignore the golden rules of industrial electronics. Motors, frequency inverters, and relays generate voltage spikes that can fry your ESP32 instantly if there is no protection. Therefore, the controller board must include galvanic isolation, using optocouplers or isolated power supplies to physically separate the microcontroller power from industrial grid power.

Another critical detail in board layout is CAN bus termination, which requires 120-ohm resistors at both physical ends of the transmission line to prevent signal reflection. In practice, neglecting these resistors makes communication work on a workbench with half a meter of wire, but fail miserably when installed in a real assembly line with dozens of meters of cable.

Writing code to send and receive CAN messages

The ESP32 features a built-in CAN controller in its silicon, known as TWAI (Two-Wire Automotive Interface), which makes programming quite straightforward. Below is a functional code snippet using the Arduino framework to initialize the bus and send a simple message with sensor readings:

#include <driver/twai.h> void setup() { Serial.begin(115200); twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t)5, (gpio_num_t)4, TWAI_MODE_NORMAL); twai_timing_config_t t_config = TWAI_TIMING_CONFIG_50KBPS(); twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL(); if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) { Serial.println("CAN driver installed successfully!"); } else { Serial.println("Failed to install CAN driver."); return; } if (twai_start() == ESP_OK) { Serial.println("CAN bus started."); } } void loop() { twai_message_t message; message.identifier = 0x541; message.data_length_code = 4; message.data[0] = 0x12; message.data[1] = 0x34; message.data[2] = 0x56; message.data[3] = 0x78; if (twai_transmit(&message, pdMS_TO_TICKS(1000)) == ESP_OK) { Serial.println("Message sent on network!"); } else { Serial.println("Error sending message."); } delay(1000); }

This code configures transmit and receive pins, sets the network speed to 50 kilobits per second, and attempts to transmit a packet every second. Just as when managing state in distributed systems, keeping error handling robust in code ensures the controller automatically recovers if the bus suffers a momentary short circuit.

Integrating local data with cloud systems

Once machine data arrives at our controller board via CAN bus, the next logical step is to make this information available to modern monitoring systems. The ESP32 can take packets received from the industrial bus and publish them to an MQTT broker or stream them via WebSockets for real-time dashboards. Much like we investigated solutions to understand why WebSocket connections drop and how to ensure resilience, keeping automatic reconnection active is vital to prevent telemetry loss.

This hybrid approach turns an old, isolated industrial network into an intelligent IoT asset. Maintenance engineers can view temperature and energy consumption alerts directly on their smartphone or control room computer without needing expensive software licenses from major automation corporations.

Final considerations

Building your own IoT controller board based on the ESP32 and CAN bus opens up immense possibilities to modernize factories and machinery with reduced investment. Mastering physical design, electrical isolation, and protocol programming ensures the hardware you create will bravely withstand the harshest industrial environment conditions. With careful planning and rigorous workbench testing, you eliminate commercial middlemen and gain total control over your operation's technology.