AS5600 Driver (Rust)
A comprehensive, low-level, platform-agnostic Rust driver for the AS5600 magnetic rotary encoder (12-bit contactless potentiometer). Built on embedded-hal 1.0, it provides direct access to all device registers and OTP programming functions.
๐ Table of Contents
- Features
- Installation
- Usage Examples
- Interface Abstraction
- I2C Bus Sharing
- Safety Warning (OTP)
- Support
- License
๐ Key Features
- no_std Support: Ready for bare-metal microcontrollers (ESP32, STM32, nRF, etc.).
- Full Register Map: Complete coverage of ZPOS, MPOS, MANG, CONF, STATUS, RAW_ANGLE, ANGLE, AGC, and MAGNITUDE.
- Hardware Configuration: Support for Hysteresis, Power Modes, PWM settings, and Fast/Slow Filters.
- Diagnostics: Methods to monitor magnet detection, magnetic field strength, and Automatic Gain Control (AGC).
- OTP Programming: Secure methods for permanent burning of settings (marked
unsafe). - Mocking Support: Built-in hardware emulator for testing and simulation (behind the
mockfeature). - Trait-based Interface:
AS5600Interfacetrait allows easy swapping between real hardware and mocks. - Cross-Platform: Support for Linux (SBCs like Raspberry Pi), ESP32 (std & no_std), and any other platform implementing
embedded-hal.
๐ฆ Installation
Add this to your Cargo.toml:
[]
# Basic no_std version
= "0.1.0"
# Or with std and anyhow support (recommended for ESP32/Linux)
= { = "0.1.0", = ["std", "anyhow"] }
โ๏ธ Features
std: Enables standard library support.anyhow: Enables integration withanyhowcrate for easier error handling (requiresstd).mock: Enables the hardware mock emulator (requiresstd).
๐ Usage Examples
All examples provide a real-time monitoring dashboard as shown below:
Typical real-time diagnostic output from the provided examples.
We provide several ready-to-use examples for different environments:
- ESP32 Dashboard (std): A real-time terminal dashboard for ESP32 using the
stdlibrary andesp-idf-hal. - ESP32 Dashboard (no_std): Bare-metal implementation for ESP32 using
esp-hal(no operating system). - Linux Dashboard: Using the sensor on Linux-based SBCs (Raspberry Pi, etc.) via
/dev/i2c-x. - Mock Simulation: Hardware-free simulation for testing UI and logic on your PC.
Quick Start: Decoupled Interface (Traits)
Using AS5600Interface allows your application logic to be independent of the specific I2C implementation.
use AS5600Interface;
// This function works with ANY sensor implementation (Real or Mock)
๐ Sharing the I2C Bus
The AS5600 has a fixed I2C address (0x36).
Multiple Sensors
To use multiple AS5600 sensors on the same bus, you must use an I2C multiplexer (e.g., TCA9548A).
Shared Bus with Other Devices
To share the bus with other device types, use a bus manager like embedded-hal-bus:
// Using a reference (&mut) to the I2C bus
let mut bus = new?;
let mut encoder1 = new;
// Other sensors on the same bus must have different addresses
let mut other_sensor = new;
โ ๏ธ Safety Warning: OTP Programming
The AS5600 has One-Time Programmable (OTP) memory. These methods perform permanent, irreversible hardware changes:
danger_permanent_burn_settings(): Programs ZPOS and MPOS. Max 3 times.danger_permanent_burn_config(): Programs CONF register. ONLY ONCE.
Support the Project / ะัะดััะธะผะฐัะธ ะฟัะพะตะบั
If you find this extension useful and want to support development or speed up new features:
Donate via:
- ๐บ๐ฆ Donatello โ Ukrainian service supporting:
- ๐ณ Visa/Mastercard
- ๐ช Cryptocurrency (USDT)
- ๐ฆ Other payment methods
- ๐ PayPal: pavvers1@gmail.com
Your support helps keep this project alive and growing. Thank you! / ะัะบัั ะทะฐ ะฟัะดััะธะผะบั! ๐๐
You can contact me via telegram or pavvers1@gmail.com.
๐ License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) at your option.