A wrapper for driving the cheap but great magnetic rotary sensor AS5601 over I²C on an Arduino. The library aims to offer clean high-level access to the sensor's capabilities and to provide convenience functions for common tasks. The current implementation supports only a subset of the sensor's functionality, more functions may be added in future versions.
Initialize the driver, which sets up the I²C bus:
AS5601 Sensor;
Find out if the magnet is in range:
if ( Sensor.magnetDetected() ) { … }
Query the sensor to get the magnet's current angle:
int angle = Sensor.getAngle();
The sensor's register addresses are available as static constants as specified in the datasheet. The struct ByteRegister includes the 8-bit registers, WordRegister contains the starting addresses of the 16-bit registers.
unsigned char
The address to read from. For example, AS5601::ByteRegister::STATUS.unsigned char
.unsigned char
The address to begin reading from. For example, AS5601::WordRegister::ANGLE.unsigned int
.unsigned char
The address to write to. For example, AS5601::ByteRegister::ABN.unsigned char
The address to begin writing to. For example, AS5601::WordRegister::ZPOS.bool
.unsigned int
.unsigned char
.unsigned int
.unsigned int
An integer angle ∈ [0,4095] as returned by getRawAngle(). Can be omitted for convenience to use the sensor's current raw angle.unsigned int
An integer number of angle steps in a full circle. Supported values are 8, 16, 32, …, 2048.unsigned int
.Functionality not covered by the above higher-level methods can be easily added using the low-level communication methods. The already included register address maps facilitate this. In any case, giving the datasheet a read is highly recommended to understand how the sensor works.