How to use the AS5147 magnetic absolute rotation sensor

The AS5147 magnetic absolute rotation sensor is perfect for measuring robot joint angles. The key here is absolute! A relative rotation sensor like an encoder is very cheap but when you need to always totally unquestionably need to know where your thing is at then you need absolute sensing. In this post I’m going to show you my test setup, wiring, and code so you can get started easy peasy.

The setup

The plan is to make a stepper motor turn the magnet and have the sensor read the magnet. Then the Arduino can report the values and Arduino app can use the Serial Plotter to graph the results. If the motor moves smoothly back and forth through a full turn we should see a “saw tooth” on the plot.

The datasheet for the AS5147 and it says the magnet must be centered over the chip and it must be within 1.1mm from the center of the chip and it must be within 2mm of the chip.

In the above picture I’ve got…

  • …a NEMA17 stepper motor 1.8 degree per step (top right)
  • …a 3×8 magnet on the end of the stepper motor shaft.
  • …an AS5147 sensor with the chip centered over the magnet.
  • …an A4988 stepper driver IC on a RUMBA board. You could also use a RAMPS shield on an Arduino MEGA 2560 – they’re nearly the same thing.
  • …and a 12v power supply (connected to the blue terminal blocks on the left). The minimum power in this setup is 1 amp.

The wiring

The datasheet for the AS5147 has several ways to communicate. I’ve chosen to use the SPI interface. That means I need at least four digital pins. Since I have absolute sensors I don’t need limit switches, so the six limit switch pins are available.

AS5147 pinRUMBA pinArduino pin
5vXpos +
GNDXpos –
CSELXpos input37
CLKXneg input36
MOSIYpos input35
MISOYneg input34
Motor Direction16
Motor Step17
Motor Enble48

The code

Here’s a link to all the AS5147 code, ready to run. There are three projects here: one for a single sensor, one for six sensors at once, and one for a sensor with a stepper motor. AS5147-stepper is the one you want.

The demo

Final thoughts

Please like, share, and subscribe for more. Thank you!