Site Information

 Loading... Please wait...

Blog

ESP32 TWAI Driver Explained: A Practical Guide to CAN Bus Programming

Posted by Wilfried Voss on

ESP32 TWAI Driver ExplainedThe ESP32 has become one of the most popular microcontrollers for CAN bus applications. Whether you are building industrial controllers, SAE J1939 devices, NMEA 2000 products, robotics systems, or automotive prototypes, the ESP32 provides a powerful and cost-effective platform with a built-in CAN controller.

Yet, many developers are confused when they encounter the term TWAI in the ESP-IDF documentation. Is TWAI different from CAN? Does it require special hardware? Can it be used with SAE J1939 or NMEA 2000?

This article explains the ESP32 TWAI driver, how it relates to CAN bus communication, and how to get started with CAN programming on the ESP32.

What Is TWAI?

TWAI stands for Two-Wire Automotive Interface.

In reality, TWAI is simply Espressif's name for a CAN controller. The functionality is essentially identical to the CAN controller found in earlier ESP32 devices. Espressif adopted the term TWAI primarily for naming and trademark reasons, while maintaining compatibility with standard CAN communication.

When developers speak about ESP32 CAN programming, they are usually referring to the ESP-IDF TWAI driver.

TWAI Versus CAN: Is There a Difference?

From an application perspective, the answer is simple:

TWAI is CAN.

The ESP32 TWAI controller supports:

  • Standard 11-bit CAN identifiers
  • Extended 29-bit CAN identifiers
  • Classical CAN communication up to 1 Mbps
  • CAN error detection and recovery
  • Acceptance filtering
  • Listen-only monitoring mode

Because extended 29-bit identifiers are supported, the ESP32 TWAI driver can be used for:

  • SAE J1939
  • NMEA 2000
  • CANopen
  • DeviceNet
  • Proprietary CAN protocols

The controller is fully compatible with Classical CAN communication and supports both standard and extended frame formats.

One Important Detail: You Still Need a CAN Transceiver

One of the most common misconceptions is that the ESP32 can be connected directly to a CAN network.

It cannot.

The ESP32 contains only the CAN controller. To communicate on a physical CAN bus, an external CAN transceiver is required. The transceiver converts the ESP32's logic-level TX and RX signals into the differential CAN-H and CAN-L signals used on the network.

This is similar to Ethernet. A microcontroller may include an Ethernet MAC, but it still requires a PHY chip to connect to the network cable.

ESP32 Development Boards with Integrated CAN Hardware

If you prefer to avoid wiring separate transceiver modules, several ESP32-S3 development boards already include the necessary CAN hardware:

All of these boards can be programmed using the standard ESP-IDF TWAI driver for Classical CAN applications.

For developers working with SAE J1939, NMEA 2000, industrial automation, or general CAN bus monitoring, integrated CAN hardware significantly simplifies development and testing.

TWAI Driver Architecture

The ESP-IDF TWAI driver provides a software layer between your application and the hardware CAN controller.

The basic architecture looks like this:

Application Software

TWAI Driver

TWAI Controller

CAN Transceiver

CAN Network

The driver handles:

  • Message transmission
  • Message reception
  • Hardware filtering
  • Error management
  • Bus state monitoring
  • Interrupt handling

This allows application software to focus on the actual data being exchanged rather than low-level controller details.

Driver Configuration

A typical TWAI application starts by configuring three structures:

General Configuration

The general configuration defines:

  • TX pin
  • RX pin
  • Operating mode
  • Queue sizes
  • Alert settings

Typical operating modes include:

  • Normal Mode
  • Listen-Only Mode
  • No-Acknowledge Mode

Listen-Only Mode is especially useful when monitoring existing CAN networks because the ESP32 will not influence bus traffic or transmit acknowledgments.

Timing Configuration

The timing configuration determines the CAN bit rate.

Common values include:

  • 125 kbps
  • 250 kbps
  • 500 kbps
  • 1 Mbps

All nodes on the network must use the same bit rate.

Filter Configuration

Acceptance filters determine which messages are received.

For heavily loaded networks such as SAE J1939 systems, filtering can dramatically reduce processor workload.

Transmitting Messages

Sending a CAN frame is straightforward.

The application fills a message structure containing:

  • Identifier
  • Data length
  • Data bytes
  • Frame type

The message is then placed into the transmit queue.

The TWAI driver handles:

  • Arbitration
  • Frame formatting
  • Error checking
  • Retransmissions

In most applications, transmitting a message requires only a few lines of code.

Receiving Messages

Reception works similarly.

Incoming frames are placed into a receive queue where they can be processed by the application.

The TWAI driver automatically handles:

  • CRC verification
  • Frame validation
  • Acceptance filtering
  • Queue management

For monitoring applications such as CAN analyzers or J1939 data loggers, the receive function often becomes the core of the application.

Understanding Error States

One of the strengths of CAN technology is its sophisticated error handling.

The TWAI driver exposes the same error states found in standard CAN controllers.

Error Active

This is the normal operating state.

The node can transmit and receive messages without restrictions.

Error Passive

The node has detected excessive communication errors.

It can still participate on the network but with reduced influence.

Bus Off

If communication errors continue to accumulate, the controller enters Bus Off state.

At this point, transmissions are disabled until recovery is initiated. The TWAI driver includes alerts and recovery mechanisms to detect and manage these situations.

TWAI Alerts

The ESP-IDF driver includes an alert system that reports important bus events.

Examples include:

  • Transmission successful
  • Transmission failed
  • Error warning
  • Error passive
  • Error active
  • Bus recovery
  • Bus off

For industrial and automotive applications, these alerts are invaluable for diagnostics and troubleshooting.

Can TWAI Be Used with SAE J1939?

Absolutely.

Because SAE J1939 uses extended 29-bit identifiers, many developers initially assume special hardware is required.

In reality, the TWAI controller natively supports 29-bit identifiers and works very well with J1939 applications.

Typical applications include:

  • Engine monitoring
  • Vehicle diagnostics
  • Fleet management
  • Agricultural equipment
  • Construction machinery

The same applies to NMEA 2000, which is also based on extended CAN identifiers.

What About CAN FD?

This is where many developers become confused.

The TWAI controller found in traditional ESP32 and ESP32-S3 devices supports Classical CAN only. CAN FD frames are not supported by the TWAI peripheral and will be treated as errors.

If your application requires CAN FD communication, additional hardware support is necessary.

However, for many industrial, marine, and heavy-duty vehicle applications, Classical CAN remains the dominant protocol and is fully supported by the TWAI driver.

Common Beginner Mistakes

After helping engineers with CAN networks for many years, I frequently see the same problems:

Missing Transceiver

The ESP32 cannot be connected directly to CAN-H and CAN-L.

A transceiver is always required.

Missing Termination

Every CAN network requires proper 120-ohm termination resistors at both ends of the bus.

Incorrect Bit Rate

A network operating at 250 kbps will not communicate with a node configured for 500 kbps.

Using MCP2515 Libraries

The built-in TWAI controller does not require MCP2515 libraries.

Those libraries are intended for external MCP2515 CAN controllers connected via SPI.

Conclusion

The ESP32 TWAI driver provides a powerful and surprisingly easy-to-use interface for CAN bus communication. Despite the unusual name, TWAI is simply Espressif's implementation of a standard CAN controller.

Combined with the ESP32's processing power, wireless connectivity, and low cost, the TWAI driver makes the ESP32 an excellent platform for CAN-based applications ranging from industrial automation and robotics to SAE J1939 and NMEA 2000 development.

For developers using ESP32-S3 CAN hardware such as the Copperhill Technologies CAN FD, CAN/LIN, or NMEA 2000 development boards, getting started with CAN communication can be as simple as installing the driver, configuring the bit rate, and transmitting the first message.


Programming the ESP32 In C Using the Arduino LibraryProgramming the ESP32 In C Using the Arduino Library

The Espressif ESP32 is a remarkably versatile microcontroller. Despite its low cost, it integrates a wide range of peripherals and processing capabilities, making it suitable for everything from simple hobby projects to sophisticated embedded applications. Programming the ESP32 in C provides the speed and efficiency required for real-time systems, while the Arduino framework offers an accessible development environment. Built on top of Espressif’s official ESP-IDF (IoT Development Framework), the Arduino layer simplifies development without preventing access to the advanced features of the underlying platform when greater control is needed.

This book explores the ESP32’s capabilities through practical, hands-on examples that focus on understanding the hardware and developing essential embedded programming skills. Topics include GPIO, interrupts, PWM, SPI, I²C, 1-Wire, UART communications, Wi-Fi networking, SD card interfaces, low-power operation, real-time clock functions, touch sensors, RMT peripherals, and FreeRTOS-based multitasking. Rather than presenting a collection of finished projects, the book emphasizes the concepts and techniques needed to design and build your own applications with confidence, minimizing trial and error and helping you bring ideas to completion efficiently. More information...

ESP32S3 CAN & LIN-Bus Board – Accelerating CAN-to-LIN Gateway Development

Modern vehicles and industrial machines increasingly rely on multiple communication networks. While CAN Bus serves as the backbone for critical control systems, LIN (Local Interconnect Network) provides a low-cost solution for intelligent sensors, switches, actuators, and body electronics. In many applications, engineers need a reliable method to exchange data between these two networks. The ESP32S3 CAN [...]

Read More »


Adafruit HUZZAH32 ESP32 Feather Board – WiFi, Bluetooth & Battery Power for IoT Projects

The Adafruit HUZZAH32 Feather Board brings Espressif’s powerful ESP32-WROOM32 module into Adafruit’s popular Feather ecosystem. It offers built-in WiFi and Bluetooth connectivity, dual-core performance, and efficient battery management — making it an excellent choice for Internet of Things (IoT) and embedded applications that demand portability, wireless communication, and reliability. Compact Power for Wireless Projects The HUZZAH32 is [...]

Read More »


Learning Python and Electronics with the ESP32

In an age where smart devices are seamlessly integrated into our daily lives, the demand for accessible tools to understand and build electronics projects has never been greater. This book aims to demystify both Python programming and basic electronics, serving as a comprehensive introduction for beginners. Whether you’re a hobbyist, student, educator, or simply curious [...]

Read More »


Harnessing the Power of the ESP32-S3: A Look at Copperhill’s CAN FD Development Board

In the evolving landscape of IoT, industrial automation, and automotive communication, the demand for a versatile, wireless-enabled microcontroller with CAN bus capabilities has never been greater. Copperhill Technologies rises to the occasion with its latest innovation—a development board based on the ESP32-S3-WROOM-1-N8R8, engineered to support both Classical CAN and CAN FD protocols. At the heart of [...]

Read More »


Exploring the ESP32 Processor and Its CAN Interface Programming

The ESP32 processor, developed by Espressif Systems, has rapidly emerged as a favorite among developers and hobbyists alike due to its robust performance, integrated wireless connectivity, and versatility in handling various tasks. Among its many features is a built-in CAN (Controller Area Network) interface—technically implemented as a TWAI (Two-Wire Automotive Interface) controller—that has opened up [...]

Read More »


ESP32 Processor: CAN Bus Topology and Termination Resistors

This post is an excerpt from our application note Controller Area Network (CAN) Development with ESP32. It is my experience that newcomers to the technology overlook the importance of termination resistors. Missing or misplaced resistors can lead to transmission errors or even prevent transmission altogether. The general rule is that if you connect to an existing, fully [...]

Read More »


ESP32 Development Kits with Onboard CAN Bus Controller

The ESP32 is a low-cost, low-power system-on-chip microcontroller with integrated WiFi and dual-mode Bluetooth. It is equipped with a Tensilica Xtensa LX6 microprocessor in dual-core and single-core versions. The microcontroller features built-in antenna switches, RF balun, power amplifiers, low-noise receive amplifiers, filters, and power management modules. It is the successor to the ESP8266 SoC. There are [...]

Read More »


ESP32 Processor: Adding CAN/CAN-FD Controllers per SPI Port

This post is an excerpt from our application note Controller Area Network (CAN) Development with ESP32. The internal CAN controller SJA1000 does not support CAN-FD and is not CAN-FD tolerant. To use CAN-FD with the ESP32, you need to employ CAN-FD breakout boards that are accessible per the ESP32’s SPI ports. Note: The SPI ports are not [...]

Read More »


ESP32 Processor: Internal SJA 100 CAN Bus Controller

This post is an excerpt from our application note Controller Area Network (CAN) Development with ESP32.The ESP32 integrates a CAN Bus controller compatible with the NXP SJA1000. Thus, it is CAN 2.0B (ISO 11898, a.k.a. Classical CAN) specification-compliant.As with the SJA1000, the ESP32 CAN Bus controller provides only the data link layer and the physical layer [...]

Read More »