# Libcanard: Examples, Starters, Tutorials

**URL:** <https://forum.opencyphal.org/t/libcanard-examples-starters-tutorials/935>\
**Category:** Libcanard\
**Created:** [September 7, 2020, 7:33am UTC](https://forum.opencyphal.org/t/libcanard-examples-starters-tutorials/935 "2020-09-07T07:33:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![hugo](https://forum.opencyphal.org/letter_avatar_proxy/v4/letter/h/b38774/32.png) [@hugo](https://forum.opencyphal.org/u/hugo)\
**Post date:** [September 7, 2020, 7:33am UTC](https://forum.opencyphal.org/t/libcanard-examples-starters-tutorials/935/1 "2020-09-07T07:33:38Z")

</div>

Good day 🙂

This topic is dedicated to collect and provide links supplied by the community. What you will find here are:

- Examples: Gists, posts links to other forum like Stack Overflow which present a whole solution or partial solution.
- Starters: for example a Github template project to get you started.
- Tutorials: Full explanations like those found in Instructables or Medium and/or videos in YouTube.
- Open Source Libraries: what better examples than a whole open source library or implementation?
- Example Hardware Setups: There are plenty of affordable and some open source hardware to start exploring UAVCAN.

This topic prime goal is to be informational. We strongly advise and suggest that lengthy discussion or debate of any of the links presented here be carried out at the respective forum dedicated to that link please.

Happy Coding

---

<div class="post-metadata">

**Author:** ![aentinger](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/aentinger/32/1428_2.png) [@aentinger](https://forum.opencyphal.org/u/aentinger)\
**Post date:** [September 7, 2020, 10:22am UTC](https://forum.opencyphal.org/t/libcanard-examples-starters-tutorials/935/2 "2020-09-07T10:22:31Z")

</div>

[107-Arduino-UAVCAN](https://github.com/107-systems/107-Arduino-UAVCAN) is a Arduino library providing a convenient C++ interface for accessing [UAVCAN](https://uavcan.org/) ([v1.0-beta](https://uavcan.org/specification/UAVCAN_Specification_v1.0-beta.pdf)) via [libcanard](https://github.com/UAVCAN/libcanard). This library works for

- [ArduinoCore-samd](https://github.com/arduino/ArduinoCore-samd): Arduino Zero, MKR 1000, MKR WiFi 1010, Nano 33 IoT, MKR GSM 1400, MKR NB 1500, MKR WAN 1300/1310 ✔
- [ArduinoCore-mbed](https://github.com/arduino/ArduinoCore-mbed): Portenta H7, Nano 33 BLE, Nano RP2040 Connect, Edge Control ✔
- [arduino-esp32](https://github.com/espressif/arduino-esp32): `ESP32 Dev Module`, `ESP32 Wrover Module`, … ✔

There’s a [dedicated thread](https://forum.opencyphal.org/t/arduino-uavcan-library/905) on this forum for discussing anything related to the library, of course the default GitHub mechanisms of [issues](https://github.com/107-systems/107-Arduino-UAVCAN/issues) and [pull requests](https://github.com/107-systems/107-Arduino-UAVCAN/pulls) can also be employed (choose the right medium 😉).

Here’s a [short example](https://github.com/107-systems/107-Arduino-UAVCAN/blob/master/examples/UAVCAN-Heartbeat-Publish/UAVCAN-Heartbeat-Publish.ino) of a node publishing a heartbeat using the **107-Arduino-UAVCAN** :

```auto
/ **************************************************************************************
 * INCLUDE
 ************************************************************************************** /

#include <SPI.h>

#include <ArduinoUAVCAN.h>
#include <ArduinoMCP2515.h>

/ **************************************************************************************
 * CONSTANTS
 ************************************************************************************** /

static int const MKRCAN_MCP2515_CS_PIN = 3;
static int const MKRCAN_MCP2515_INT_PIN = 7;

/ **************************************************************************************
 * FUNCTION DECLARATION
 ************************************************************************************** /

void spi_select()
{
  digitalWrite(MKRCAN_MCP2515_CS_PIN, LOW);
}

void spi_deselect()
{
  digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH);
}

uint8_t spi_transfer(uint8_t const data)
{
  return SPI.transfer(data);
}

void onExternalEvent()
{
  mcp2515.onExternalEventHandler();
}

bool transmitCanFrame(uint32_t const id, uint8_t const * data, uint8_t const len)
{
  return mcp2515.transmit(id, data, len);
}

/ **************************************************************************************
 * GLOBAL VARIABLES
 ************************************************************************************** /

ArduinoMCP2515 mcp2515(spi_select,
                       spi_deselect,
                       spi_transfer,
                       nullptr,
                       nullptr);

ArduinoUAVCAN uavcan(13, micros, transmitCanFrame);

Heartbeat_1_0 hb(0, Heartbeat_1_0::Health::NOMINAL, Heartbeat_1_0::Mode::INITIALIZATION, 0);

/ **************************************************************************************
 * SETUP/LOOP
 ************************************************************************************** /

void setup()
{
  Serial.begin(9600);
  while(!Serial) { }

  /* Setup SPI access */
  SPI.begin();
  pinMode(MKRCAN_MCP2515_CS_PIN, OUTPUT);
  digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH);

  /* Attach interrupt handler to register MCP2515 signaled by taking INT low */
  pinMode(MKRCAN_MCP2515_INT_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(MKRCAN_MCP2515_INT_PIN), onExternalEvent, FALLING);

  /* Initialize MCP2515 */
  mcp2515.begin();
  mcp2515.setBitRate(CanBitRate::BR_250kBPS);
  mcp2515.setNormalMode();
}

void loop()
{
  /* Update the heartbeat object */
  hb.uptime(millis() / 1000);
  hb.mode(Heartbeat_1_0::Mode::OPERATIONAL);

  /* Publish the heartbeat once/second */
  static unsigned long prev = 0;
  unsigned long const now = millis();
  if(now - prev > 1000) {
    uavcan.publish(hb);
    prev = now;
  }

  /* Transmit all enqeued CAN frames */
  while(uavcan.transmitCanFrame()) { }
}

```

---

<div class="post-metadata">

**Author:** ![hugo](https://forum.opencyphal.org/letter_avatar_proxy/v4/letter/h/b38774/32.png) [@hugo](https://forum.opencyphal.org/u/hugo)\
**Post date:** [September 14, 2020, 3:33pm UTC](https://forum.opencyphal.org/t/libcanard-examples-starters-tutorials/935/4 "2020-09-14T15:33:08Z")

</div>

**Starter Template C Project with CMake for Visual Studio Code**

[https://github.com/bluecorn/uavcan-template-c](https://github.com/bluecorn/uavcan-template-c) is a Visual Studio Code specific starter temple. The code is meant to get you started quickly in building your own libcanard C node. It is meant executed withing a Linux system. Since it used CMake to build then it might work with other IDE’s.

Cheers.

---

<div class="post-metadata">

**Author:** ![hugo](https://forum.opencyphal.org/letter_avatar_proxy/v4/letter/h/b38774/32.png) [@hugo](https://forum.opencyphal.org/u/hugo)\
**Post date:** [September 23, 2020, 10:34am UTC](https://forum.opencyphal.org/t/libcanard-examples-starters-tutorials/935/5 "2020-09-23T10:34:21Z")

</div>

## Raspberry Pi with ultrasound example in C

The code is meant to be run using two Raspberry Pi. Each Pi is connected to a MCP2515 module. One Pi is connected to a HC-SR04 ultrasound sensor. The sensor calculated distance to an obstacle is transmitted as a 32bit float. The float is serialized using canard\_dsdl.h. At the reciving end the float is deserialized and the distance is printed to the console.

The code can be found at:

### [raspberry-can-HC-SR04](https://github.com/bluecorn/raspberry-can-HC-SR04)

### [raspberry-can-master-ultrasound](https://github.com/bluecorn/raspberry-can-master-ultrasound)

---

<div class="post-metadata">

**Author:** ![generationmake](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/generationmake/32/611_2.png) [@generationmake](https://forum.opencyphal.org/u/generationmake)\
**Post date:** [December 29, 2020, 8:39am UTC](https://forum.opencyphal.org/t/libcanard-examples-starters-tutorials/935/6 "2020-12-29T08:39:22Z")

</div>

Hi @hugo  
I just want to let you know that I rebuilt your raspberry pi example and it just worked out of the box. Thanks for it.

I also managed to receive the data using Yakut. The right command is:

```auto
yakut -i='CAN(can.media.socketcan.SocketCANMedia("can0",8),59)' sub 1610:uavcan.primitive.scalar.Real32.1.0

```

The output should be like:

```auto
1610:
  _metadata_:
    timestamp:
      system: 1609230618.943262
      monotonic: 976.584537
    priority: nominal
    transfer_id: 13
    source_node_id: 42
  value: 18.762100219726562

```

---

<div class="post-metadata">

**Author:** ![noxuz](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/noxuz/32/397_2.png) [@noxuz](https://forum.opencyphal.org/u/noxuz)\
**Post date:** [April 20, 2021, 2:38pm UTC](https://forum.opencyphal.org/t/libcanard-examples-starters-tutorials/935/7 "2021-04-20T14:38:39Z")

</div>

**Libcanard example for the NXP S32K1 automotive-grade microcontroller.**

This demo transmits the uavcan/node/heartbeat message between a pair of [UCANS32K146](https://www.nxp.com/design/development-boards/automotive-development-platforms/s32k-mcu-platforms/can-fd-development-system-for-drones-rovers-and-mobile-robotics:UCANS32K146) development boards, in a interrupt-driven approach both for Tx and Rx.

> **[noxuz/libcanard\_S32K1](https://github.com/noxuz/libcanard_S32K1)**
>
> Libcanard for UAVCAN v1 media layer for NXP S32K1 Automotive-grade microcontrollers - noxuz/libcanard\_S32K1

---

<div class="post-metadata">

**Author:** ![pavel.kirienko](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/pavel.kirienko/32/9_2.png) [@pavel.kirienko](https://forum.opencyphal.org/u/pavel.kirienko)\
**Post date:** [May 10, 2021, 9:46am UTC](https://forum.opencyphal.org/t/libcanard-examples-starters-tutorials/935/8 "2021-05-10T09:46:02Z")

</div>

We have recently made our official demos public; at the time of writing this, they are all libcanard-based:

> **[UAVCAN/demos](https://github.com/UAVCAN/demos)**
>
> Demo applications and reference implementations. Contribute to UAVCAN/demos development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![pavel.kirienko](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/pavel.kirienko/32/9_2.png) [@pavel.kirienko](https://forum.opencyphal.org/u/pavel.kirienko)\
**Post date:** [September 25, 2021, 8:08pm UTC](https://forum.opencyphal.org/t/libcanard-examples-starters-tutorials/935/9 "2021-09-25T20:08:06Z")

</div>

This presentation from Landon Haugh deserves a place here:

https://www.youtube.com/embed/wyMcb8T7S9M?feature=oembed&wmode=opaque&list=PLYy2pGCdhu7xlnSL29qYUfIBaiQZR3LCC

---

<div class="post-metadata">

**Author:** ![pavel.kirienko](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/pavel.kirienko/32/9_2.png) [@pavel.kirienko](https://forum.opencyphal.org/u/pavel.kirienko)\
**Post date:** [May 16, 2025, 4:54pm UTC](https://forum.opencyphal.org/t/libcanard-examples-starters-tutorials/935/10 "2025-05-16T16:54:29Z")

</div>


