How to use an Arduino library in a Rust project?

Overview

Example of an Arduino library usage in a Rust project

The project tested with Arduino UNO on Fedora 35.
It demonstrates the usage of LiquidCrystal_I2C with rust project to control I2C Text Display from Rust. It also shows how to combine it with existing Arduino rust crates. arduino_hal crate is used to blink the LED.

This is the source code for Five simple steps to use any Arduino C++ library in a Rust project 🦀 . Demo video.

Project setup

  • Install avr toolchain and avrdude
sudo dnf install avrdude avr-gcc avr-libc
  • Install dependencies to compile ravedude
sudo dnf install systemd-devel pkgconf-pkg-config
  • Install ravedude:
cargo install ravedude
  • Install bindgen dependencies:
sudo dnf install clang-devel
  • Install arduino IDE and validate it is working by compiling a simple sketch.
  • Install LiquidCrystal_I2C library to Arduino Libraries folder (manually or using Arduino IDE like you would normally do)
  • Edit arduino.yaml to configure your arduino installation and version of core library
  • Ensure to have rustfmt installed
rustup component add rustfmt
  • Plug in your Arduino UNO and run the project:
cargo run

If you see the following error

> avrdude: ser_open(): can't open device "/dev/ttyACM0": Permission denied

then try editing the permission or adding you user to dialout group (require re-login):

sudo chmod a+rw /dev/ttyACM0
sudo usermod -aG dialout $USER

Windows

If you are running on Windows, consider different way of system dependencies installation.

  • Instead of install avr tools using dnf, locate them in your Arduino IDE installation and add the folders containing avr-gcc and avrdude to the PATH. I had to add the following:
    • %LOCALAPPDATA%\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7\bin\
    • %LOCALAPPDATA%\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17\bin\
  • Ensure avr-gcc and avrdude are found.
  • Download and install clang for Windows here
  • Setup LIBCLANG_PATH to point to <CLANG_INSTALL_DIR>\bin
  • Setup AR to point to %LIBCLANG_PATH%\llvm-ar.exe
  • Don't forget to update arduino.yaml with correct paths.
  • cargo build and cargo run should work now!
You might also like...
A cli utility written in Rust that allows fetching all the labels of a project, save those as a YAML file

A cli utility written in Rust that allows fetching all the labels of a project, save those as a YAML file that you can easily edit or save as backup and apply a saved preset to new repositories.

Watches changes in a rust project, runs test and shows friendly notification
Watches changes in a rust project, runs test and shows friendly notification

Cargo testify Automatically runs tests on your Rust project and notifies about the result. Install Install prerequisites (for Debian/Ubuntu): apt-get

Rust implementation of Project Fluent

Fluent fluent-rs is a collection of Rust crates implementing Project Fluent. The crates perform the following functions: fluent Umbrella crate combini

Automatically build a Rust module tree from the project directory structure

Hypermod An even lazier version of automod and supermod. Searches the src/ directory recursively for .rs files, then builds a module tree using the di

cargo, make me a project
cargo, make me a project

cargo-generate cargo, make me a project cargo-generate is a developer tool to help you get up and running quickly with a new Rust project by leveragin

Devmode is a project management utility for developers.
Devmode is a project management utility for developers.

Dev(mode) Dev(mode) is a project management utility for developers.

Generate a THIRDPARTY file with all licenses in a cargo project.

cargo-bundle-licenses Bundle all third-party licenses into a single file. NOTE This tools is not a lawyer and no guarantee of correctness can be made

A tool that generates a Sublime Text project file that helps you get started using Scoggle.

README A tool that generates a Sublime Text project file that helps you get started using Scoggle. While Scoggle-Gen may not find every single source

An ND812 decoder implemented as part of the yaxpeax project

yaxpeax-nd812 an ND812 decoder implemented as part of the yaxpeax project, including traits provided by yaxpeax-arch. the ND812 is a 12-bit microcompu

Owner
Konstantin
Konstantin
Garden monitoring system using m328p Arduino Uno boards. 100% Rust [no_std] using the avr hardware abstraction layer (avr-hal)

uno-revive-rs References Arduino Garden Controller Roadmap uno-revive-rs: roadmap Components & Controllers 1-2 Uno R3 m328p Soil moisture sensor: m328

Ethan Gallucci 1 May 4, 2022
Arduino Uno 9 axis acceleration sensor (BMX055) reader implemented in Rust.

Arduino Uno Accelaration reader in Rust Components Arduino Uno (Probably possible with other AVR microcontrollers) BMX055 (Japanese website) Datasheet

Katsu Uchiyama 3 Dec 15, 2022
Arduino Nano frequency counter with atomic clock accuracy

Arduino Nano frequency counter with atomic clock accuracy Project description and test setup With this project you can measure a frequency from less t

Frank Buss 24 Apr 3, 2022
🖥 Simple Arduino Serial Monitor

Born out of the desire to connect to an Arduino without having to run the whole Arduino suite.

Robin Schroer 2 Mar 19, 2022
Garden monitoring system using m328p and m2560 Arduino Uno boards

Garden monitoring system using m328p and m2560 Arduino Uno boards. 100% Rust [no_std] using the avr hardware abstraction layer (avr-hal)

Ethan Gallucci 1 May 4, 2022
A Rust framework to develop and use plugins within your project, without worrying about the low-level details.

VPlugin: A plugin framework for Rust. Website | Issues | Documentation VPlugin is a Rust framework to develop and use plugins on applications and libr

VPlugin 11 Dec 31, 2022
Easy to use Rust i18n library based on code generation

rosetta-i18n rosetta-i18n is an easy-to-use and opinionated Rust internationalization (i18n) library powered by code generation. rosetta_i18n::include

null 38 Dec 18, 2022
ffizz is a library of utilities for exporting Rust libs for use in other languages

ffizz ffizz is a library of utilities for exporting Rust libs for use in other languages. FFI generally requires a lot of unsafe code, which in turn r

Dustin J. Mitchell 2 Aug 29, 2022
bevy_blender is a Bevy library that allows you to use assets created in Blender directly from the .blend file

bevy_blender bevy_blender is a Bevy library that allows you to use assets created in Blender directly from the .blend file.

Jerald Thomas 45 Jan 4, 2023
Forked rust translation of the popular dump1090 project for adsb demodulation

Forked rust translation of the popular dump1090 project for adsb demodulation

wcampbell 64 Dec 24, 2022