mbed OS 2 and mbed OS 5

This is the handbook for mbed OS 2. If you’re working with mbed OS 5, please see the new handbook and API References. For the latest CAN API, please see CAN.

CAN or Controller-area Network is a bus standard designed to allow microcontrollers and devices to communicate with each other without a host computer.

Hello World!

This example sends from one CAN bus (can1) an counter while it is listen on the other CAN bus (can2) to receive a packet. Each bus controller should be connected to a CAN bus transceiver. These should be connected together at a CAN bus.

In order to use CAN, you need transceivers, which change the digital signal (RD/TD), outputted by the LPC1768 into a differential signal, which is transmitted along the CAN bus to the other nodes. As well as this, you need two 120 Ohm terminating resistors at either end of the bus. Without both the transceivers and the terminating resistors, the bus will not work properly. Below is an example circuit, which demonstrates how to set up a CAN bus, which simply loops the data between the mbed LPC1768's two CAN buses.



If using the LPC1768, or the LPC11C1X series, the transceivers are not included on the chip, so the two transceivers are required. However, if you are using the LPC11C2X series, you do not need transceivers, as they are already present on the chip. In both cases, you need terminating resistors.


The mbed's pins are 5V tolerant, so this example will work. However, do not assume that the chip you are using has 5V tolerant pins, so consult with the user manual/datasheet of your device to check this. If it is not 5V tolerant, a logic level converter can be used, to lower the logic levels to 3.3V logic.

Loopback example

#include "mbed.h"

Ticker ticker;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
CAN can1(p9, p10);
CAN can2(p30, p29);
char counter = 0;

void send() {
    if(can1.write(CANMessage(1337, &counter, 1))) {
        printf("Message sent: %d\n", counter);
    led1 = !led1;

int main() {
    ticker.attach(&send, 1);
    CANMessage msg;
    while(1) {
        if( {
            printf("Message received: %d\n",[0]);
            led2 = !led2;


API summary

Import library

Public Member Functions

  CAN (PinName rd, PinName td)
  Creates an CAN interface connected to specific pins.
  CAN (PinName rd, PinName td, int hz)
  Initialize CAN interface and set the frequency.
int  frequency (int hz)
  Set the frequency of the CAN interface.
int  write ( CANMessage msg)
  Write a CANMessage to the bus.
int  read ( CANMessage &msg, int handle=0)
  Read a CANMessage from the bus.
void  reset ()
  Reset CAN interface.
void  monitor (bool silent)
  Puts or removes the CAN interface into silent monitoring mode.
int  mode (Mode mode)
  Change CAN operation to the specified mode.
int  filter (unsigned int id, unsigned int mask, CANFormat format=CANAny, int handle=0)
  Filter out incomming messages.
unsigned char  rderror ()
  Detects read errors - Used to detect read overflow errors.
unsigned char  tderror ()
  Detects write errors - Used to detect write overflow errors.
void  attach (Callback< void()> func, IrqType type=RxIrq)
  Attach a function to call whenever a CAN frame received interrupt is generated.
template<typename T >
  MBED_DEPRECATED_SINCE ("mbed-os-5.1","The attach function does not support cv-qualifiers. Replaced by ""attach(callback(obj, method), type).") void attach(T *obj
  Attach a member function to call whenever a CAN frame received interrupt is generated.


The CAN Interface can be used on mbed pins p9/p10 and p30/p29

See the Pinout page for more details

The CAN Interface can be used to write data words out of a CAN port and will return the data received from another CAN device. The CAN clock frequency can be configured.