# 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:** 1\
**Showing post:** 2

<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()) { }
}

```

---

_[View the full topic](https://forum.opencyphal.org/t/libcanard-examples-starters-tutorials/935)._
