Skip to main content

AS5600 - Direction

This page contains an example demonstrating initialization, direction configuration, and angle reading using the AS5600 magnetic position sensor.


Initialization

Include the library and create the sensor object:

#include "Position-sensor-AS5600-breakout-SOLDERED.h"

PositionSensor sensor;

In setup(), initialize the sensor and wait for a magnet to be detected:

void setup()
{
Serial.begin(115200);

Wire.begin();

if (!sensor.begin())
{
Serial.println("AS5600 not found! Check wiring. Freezing.");
while (true)
{
delay(1000);
}
}

Serial.println("AS5600 found!");

while (!sensor.magnetDetected())
{
Serial.println("Magnet not detected!");
delay(1000);
}

Serial.println("Magnet detected!");
}

sensor.begin()

Initializes the AS5600 sensor and establishes I2C communication.

Returns value: Boolean value, true if the sensor is detected and communication succeeds, false otherwise.

sensor.magnetDetected()

Checks whether a magnet is positioned correctly above the AS5600 sensor.

Returns value: Boolean value, true if a magnet is detected, false otherwise.

Serial Monitor - magnet not detected
Serial Monitor output when magnet is not detected

Direction configuration

In setup(), after initializing the sensor, call setDirection() to configure which rotation direction increases the angle:

// Set direction of angle increment to counterclockwise
sensor.setDirection(AS5600_COUNTERCLOCK_WISE);

sensor.setDirection()

Sets the direction in which the angle value increases when the magnet is rotated.

Returns value: None

Function parameters:

TypeNameDescription
intdirectionUse AS5600_COUNTERCLOCK_WISE for counterclockwise increase, or AS5600_CLOCK_WISE for clockwise increase.
ConstantDescription
AS5600_COUNTERCLOCK_WISEAngle increases counterclockwise
AS5600_CLOCK_WISEAngle increases clockwise

Full example

#include <Wire.h>
#include "Position-sensor-AS5600-breakout-SOLDERED.h"

PositionSensor sensor;

void setup()
{
Serial.begin(115200);

Wire.begin();

if (!sensor.begin())
{
Serial.println("AS5600 not found! Check wiring. Freezing.");
while (true)
{
delay(1000);
}
}

Serial.println("AS5600 found!");

while (!sensor.magnetDetected())
{
Serial.println("Magnet not detected!");
delay(1000);
}

Serial.println("Magnet detected!");

// Set direction of angle increment to counterclockwise
sensor.setDirection(AS5600_COUNTERCLOCK_WISE);
}

void loop()
{
Serial.print(sensor.readAngle()); // Angle as integer
Serial.print("\t");
Serial.println(sensor.rawAngle() * AS5600_RAW_TO_DEGREES); // Angle in degrees

delay(1000);
}
Serial Monitor - direction output
Serial Monitor output for direction detection

Direction.ino

Full example for direction detection using the AS5600 sensor