Async Rust driver for the AS5048A 14-bit magnetic rotary position sensor using the SPI interface.
This driver is designed for embedded systems using embedded-hal-async and works seamlessly with async runtimes like Embassy.
- Fully async using
embedded-hal-asynctraits - 14-bit angular position reading (0.022° resolution)
- Optional
defmtlogging for debugging no_stdcompatibleforbid(unsafe_code)- 100% safe Rust
The AS5048A is a 14-bit rotary position sensor with SPI interface:
- Resolution: 14-bit (16384 positions, 0.022°/LSB)
- SPI Mode: Mode 1 (CPOL=0, CPHA=1)
- Max Clock: 10 MHz (recommend 1-3 MHz for initial testing)
- Supply: 3.3V or 5V
For more details read the full AS5048A Datasheet.
Add to your Cargo.toml:
[dependencies]
as5048a-async = "0.1"
embedded-hal-async = "1.0"use as5048a_async::As5048a;
// Create sensor with an SpiDevice (bus + CS pin)
let mut sensor = As5048a::new(spi_device);
// Read angle (0-16383, representing 0-360°)
let angle = sensor.angle().await?;
let degrees = (angle as f32) * 360.0 / 16384.0;For complete examples see the examples folder.
// Check magnetic field strength and sensor status
let diag = sensor.diagnostics().await?;
if diag.is_valid() {
// Read AGC value, check COMP flags, etc.
}
// Clear error flags
sensor.clear_error_flag().await?;See the API documentation for complete details on available methods and types.
Main methods:
angle()- Read 14-bit angle (0-16383)angle_degrees()- Read angle in degrees (0-359) using integer mathmagnitude()- Read CORDIC magnitudediagnostics()- Read diagnostics with AGC value and status flagsclear_error_flag()- Clear error flag
Constants:
ANGLE_MAX- Maximum angle value (16384) for custom conversions
This crate requires Rust 1.87 or later.
Contributions are welcome! Please run cargo test and cargo clippy before submitting.