AS5040/45 library

For the AS5040 and AS5045 rotary encoder.

AS5040/45 library

Postby john » Thu Jul 24, 2014 10:45 pm

I wrote an actual library for the AS5040 and AS5045.
The most recent version is on github: and
But here's the current working version for the 5040.
#ifndef AS5040_h
#define AS5040_h

#include "Arduino.h"

class AS5040
AS5040(uint16_t DataPin, uint16_t ClockPin, uint16_t ChipSelectPin);
uint32_t encoder_degrees(void);
uint32_t encoder_value(void);
uint32_t encoder_error(void);
struct err_struct{
bool DECn;
bool INCn;
bool OCF;
bool COF;
bool LIN; } err_value;
uint32_t read_chip(void);
const uint16_t _clock; // clock pin: output from arduino to as5040
const uint16_t _cs; // chip select: output
const uint16_t _data; // data pin: input
#include "Arduino.h"
#include "AS5040.h"

AS5040::AS5040(uint16_t DataPin, uint16_t ClockPin, uint16_t ChipSelectPin)
: _data(DataPin), _clock(ClockPin), _cs(ChipSelectPin)
pinMode(_data, INPUT);
pinMode(_clock, OUTPUT);
pinMode(_cs, OUTPUT);

uint32_t AS5040::encoder_degrees(void)
return ((encoder_value() * 360)/1024);

uint32_t AS5040::encoder_value(void)
return (read_chip() >> 6);

uint32_t AS5040::encoder_error(void)
uint16_t error_code; // not yet implemented
uint32_t raw_value;
raw_value = read_chip();
error_code = raw_value & 0b0000000000111111;
err_value.DECn = error_code & 2;
err_value.INCn = error_code & 4;
err_value.LIN = error_code & 8;
err_value.COF = error_code & 16;
err_value.OCF = error_code & 32;
return error_code;

uint32_t AS5040::read_chip(void)
uint32_t raw_value = 0;
uint16_t inputstream = 0;
uint16_t c;
digitalWrite(_cs, HIGH);
digitalWrite(_clock, HIGH);
digitalWrite(_cs, LOW);
digitalWrite(_clock, LOW);
for (c = 0; c < 16; c++)
digitalWrite(_clock, HIGH);
inputstream = digitalRead(_data);
raw_value = ((raw_value << 1) + inputstream);
digitalWrite(_clock, LOW);
return raw_value;
demo Arduino program:
#include <AS5040.h>

AS5040 myAS5040(2,3,4); // data, clock, chip select, pins 7, 8, and 9 on the board.

void setup()

void loop()
long value;
value = myAS5040.encoder_value();
Serial.print("measured value: ");
value = myAS5040.encoder_degrees();
Serial.print("measured degrees: ");
if (myAS5040.encoder_error());
Serial.println("error detected.");
if (myAS5040.err_value.DECn) Serial.println("DECn error");
if (myAS5040.err_value.INCn) Serial.println("INCn error");
if (myAS5040.err_value.COF) Serial.println("COF error");
if (myAS5040.err_value.OCF) Serial.println("OCF error");
if (myAS5040.err_value.LIN) Serial.println("LIN error");

I should have done this YEARS ago.

I'm working on writing libraries for other Austrian Microsystems encoders right now.

As always, I'd love to hear about problems. I've tested the 5040 on actual hardware and it works fine, but haven't yet tested the 5045 on hardware.

To use this, put the .cpp and .h files together in a directory (probably called AS5040) in the same directory as your sketches. It should show up under tools > add library, which will automatically drop in the include at the beginning of the program.
Site Admin
Posts: 7
Joined: Tue Sep 21, 2010 9:20 am

AS5040/45 library



Return to 10/12Bit Rotary Encoder

Who is online

Users browsing this forum: No registered users and 0 guests