Skip to content

Commit 0a43f57

Browse files
committed
Add AS5600
1 parent e06ebab commit 0a43f57

File tree

5 files changed

+171
-1
lines changed

5 files changed

+171
-1
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper
77
category=Communication
88
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
99
architectures=*
10-
depends=OmronD6T - Community Fork, SdFat - Adafruit Fork, Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit INA260 Library, Adafruit INA237 and INA238 Library, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, Sensirion I2C SEN66, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VCNL4200 Library, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS28, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork, Adafruit BusIO, Adafruit Unified Sensor, Sensirion Core, Adafruit GFX Library, Adafruit LED Backpack Library, Adafruit LiquidCrystal, Adafruit SH110X, Adafruit SSD1306
10+
depends=OmronD6T - Community Fork, SdFat - Adafruit Fork, Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit INA260 Library, Adafruit INA237 and INA238 Library, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit AS5600 Library, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, Sensirion I2C SEN66, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VCNL4200 Library, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS28, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork, Adafruit BusIO, Adafruit Unified Sensor, Sensirion Core, Adafruit GFX Library, Adafruit LED Backpack Library, Adafruit LiquidCrystal, Adafruit SH110X, Adafruit SSD1306

platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ lib_deps =
3232
adafruit/ENS160 - Adafruit Fork
3333
adafruit/Adafruit SleepyDog Library
3434
adafruit/Adafruit AHTX0
35+
adafruit/Adafruit AS5600 Library
3536
adafruit/Adafruit BME280 Library
3637
adafruit/Adafruit BMP280 Library
3738
adafruit/Adafruit BMP3XX Library

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,16 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
236236
}
237237
_ahtx0->configureDriver(msgDeviceInitReq);
238238
drivers.push_back(_ahtx0);
239+
} else if (strcmp("as5600", msgDeviceInitReq->i2c_device_name) == 0) {
240+
_as5600 = new WipperSnapper_I2C_Driver_AS5600(this->_i2c, i2cAddress);
241+
if (!_as5600->begin()) {
242+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize AS5600 chip!");
243+
_busStatusResponse =
244+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
245+
return false;
246+
}
247+
_as5600->configureDriver(msgDeviceInitReq);
248+
drivers.push_back(_as5600);
239249
} else if (strcmp("bh1750", msgDeviceInitReq->i2c_device_name) == 0) {
240250
_bh1750 = new WipperSnapper_I2C_Driver_BH1750(this->_i2c, i2cAddress);
241251
if (!_bh1750->begin()) {

src/components/i2c/WipperSnapper_I2C.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "drivers/WipperSnapper_I2C_Driver.h"
2323
#include "drivers/WipperSnapper_I2C_Driver_ADT7410.h"
2424
#include "drivers/WipperSnapper_I2C_Driver_AHTX0.h"
25+
#include "drivers/WipperSnapper_I2C_Driver_AS5600.h"
2526
#include "drivers/WipperSnapper_I2C_Driver_BH1750.h"
2627
#include "drivers/WipperSnapper_I2C_Driver_BME280.h"
2728
#include "drivers/WipperSnapper_I2C_Driver_BME680.h"
@@ -157,6 +158,7 @@ class WipperSnapper_Component_I2C {
157158
_drivers_out; ///< List of i2c output drivers
158159
// Sensor driver objects
159160
WipperSnapper_I2C_Driver_AHTX0 *_ahtx0 = nullptr;
161+
WipperSnapper_I2C_Driver_AS5600 *_as5600 = nullptr;
160162
WipperSnapper_I2C_Driver_D6T1A *_d6t1a = nullptr;
161163
WipperSnapper_I2C_Driver_DPS310 *_dps310 = nullptr;
162164
WipperSnapper_I2C_Driver_DS2484 *_ds2484 = nullptr;
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_AS5600.h
3+
*
4+
* Device driver for the AS5600 Magnetic Angle sensor.
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Tyeth Gundry 2024 for Adafruit Industries.
11+
*
12+
* MIT license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
#ifndef WipperSnapper_I2C_Driver_AS5600_H
16+
#define WipperSnapper_I2C_Driver_AS5600_H
17+
18+
#include "WipperSnapper_I2C_Driver.h"
19+
#include "Wippersnapper.h"
20+
#include <Adafruit_AS5600.h>
21+
22+
/**************************************************************************/
23+
/*!
24+
@brief Class that provides a driver interface for a AS5600 sensor.
25+
*/
26+
/**************************************************************************/
27+
class WipperSnapper_I2C_Driver_AS5600 : public WipperSnapper_I2C_Driver {
28+
public:
29+
/*******************************************************************************/
30+
/*!
31+
@brief Constructor for the AS5600 sensor.
32+
@param i2c
33+
The I2C interface.
34+
@param sensorAddress
35+
7-bit device address.
36+
*/
37+
/*******************************************************************************/
38+
WipperSnapper_I2C_Driver_AS5600(TwoWire *i2c, uint16_t sensorAddress)
39+
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
40+
_i2c = i2c;
41+
_sensorAddress = sensorAddress;
42+
_as5600 = nullptr;
43+
_angle = 0;
44+
}
45+
46+
/*******************************************************************************/
47+
/*!
48+
@brief Destructor for an AS5600 sensor.
49+
*/
50+
/*******************************************************************************/
51+
~WipperSnapper_I2C_Driver_AS5600() { delete _as5600; }
52+
53+
/*******************************************************************************/
54+
/*!
55+
@brief Initializes the AS5600 sensor and begins I2C.
56+
@returns True if initialized successfully, False otherwise.
57+
*/
58+
/*******************************************************************************/
59+
bool begin() {
60+
_as5600 = new Adafruit_AS5600();
61+
if (!_as5600->begin((uint8_t)_sensorAddress, _i2c)) {
62+
WS_DEBUG_PRINTLN("Failed to find AS5600 chip");
63+
return false;
64+
}
65+
66+
if (!configureSensor()) {
67+
WS_DEBUG_PRINTLN("Failed to configure AS5600 sensor");
68+
return false;
69+
}
70+
return true;
71+
}
72+
73+
/*******************************************************************************/
74+
/*!
75+
@brief Configures the AS5600 sensor.
76+
@returns True if the sensor was configured successfully, False otherwise.
77+
*/
78+
/*******************************************************************************/
79+
bool configureSensor() {
80+
return _as5600->enableWatchdog(false) || // Normal (high) power mode
81+
_as5600->setPowerMode(AS5600_POWER_MODE_NOM) ||
82+
// No Hysteresis
83+
_as5600->setHysteresis(AS5600_HYSTERESIS_OFF) ||
84+
85+
// analog output (0-VCC for 0-360 degrees)
86+
_as5600->setOutputStage(AS5600_OUTPUT_STAGE_ANALOG_FULL) ||
87+
88+
// setup filters
89+
_as5600->setSlowFilter(AS5600_SLOW_FILTER_16X) ||
90+
_as5600->setFastFilterThresh(AS5600_FAST_FILTER_THRESH_SLOW_ONLY) ||
91+
92+
// Reset position settings to defaults
93+
_as5600->setZPosition(0) ||
94+
_as5600->setMPosition(4095) ||
95+
_as5600->setMaxAngle(4095);
96+
97+
return true;
98+
}
99+
100+
101+
102+
bool readSensor() {
103+
if (!_as5600->isMagnetDetected()) {
104+
return false;
105+
}
106+
107+
// Continuously read and display angle values
108+
uint16_t rawAngle = _as5600->getRawAngle();
109+
uint16_t angle = _as5600->getAngle();
110+
111+
WS_DEBUG_PRINT("Raw: ");
112+
WS_DEBUG_PRINT(rawAngle);
113+
WS_DEBUG_PRINT(" (0x");
114+
WS_DEBUG_PRINT(rawAngle, HEX);
115+
WS_DEBUG_PRINT(") | Scaled: ");
116+
WS_DEBUG_PRINT(angle);
117+
WS_DEBUG_PRINT(" (0x");
118+
WS_DEBUG_PRINT(angle, HEX);
119+
WS_DEBUG_PRINT(")");
120+
121+
// Check status conditions
122+
if (_as5600->isAGCminGainOverflow()) {
123+
WS_DEBUG_PRINTLN(" | MH: magnet too strong");
124+
return false;
125+
}
126+
if (_as5600->isAGCmaxGainOverflow()) {
127+
WS_DEBUG_PRINTLN(" | ML: magnet too weak");
128+
return false;
129+
}
130+
return true;
131+
}
132+
133+
134+
/*******************************************************************************/
135+
/*!
136+
@brief Reads the Angle sensor with short wait for data.
137+
@param rawEvent
138+
Angle sensor reading
139+
@returns True if the sensor event was obtained successfully, False
140+
otherwise.
141+
*/
142+
/*******************************************************************************/
143+
bool getEventRaw(sensors_event_t *rawEvent) {
144+
ulong start = millis();
145+
if (!readSensor()) {
146+
return false;
147+
}
148+
rawEvent->data[0] = _angle;
149+
return true;
150+
}
151+
152+
protected:
153+
float _angle; ///< Current angle reading from the AS5600 sensor
154+
Adafruit_AS5600 *_as5600; ///< Pointer to AS5600 sensor object
155+
};
156+
157+
#endif // WipperSnapper_I2C_Driver_AS5600

0 commit comments

Comments
 (0)