Skip to content

Commit 67da3e9

Browse files
committed
add(i2c): AS5600 magnetic angle sensor
1 parent 967b2ae commit 67da3e9

File tree

3 files changed

+170
-2
lines changed

3 files changed

+170
-2
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 INA237 and INA238 Library, Adafruit INA260 Library, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit MLX90632 Library, 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 QMC5883P Library, 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, RTClib, StreamUtils, Adafruit SHT4x Library, Adafruit GPS Library, Adafruit uBlox
10+
depends=OmronD6T - Community Fork, SdFat - Adafruit Fork, Adafruit AS5600 Library, Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit INA237 and INA238 Library, Adafruit INA260 Library, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit MLX90632 Library, 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 QMC5883P Library, 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, RTClib, StreamUtils, Adafruit SHT4x Library, Adafruit GPS Library, Adafruit uBlox

platformio.ini

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
; PlatformIO Defaults
77
[platformio]
88
description = "Adafruit.IO WipperSnapper Firmware"
9-
default_envs = adafruit_feather_esp32s3_tft, adafruit_magtag29_esp32s2, adafruit_feather_esp32s3, featheresp32s2, adafruit_feather_esp32s2_reversetft, adafruit_metro_esp32s2, adafruit_qtpy_esp32s2, adafruit_feather_esp32s3_reversetft, adafruit_feather_esp32s3_nopsram, adafruit_feather_esp32s2_tft, adafruit_qtpy_esp32, adafruit_qtpy_esp32c3, featheresp32, adafruit_qtpy_esp32s3_nopsram, featheresp32v2, huzzah, adafruit_pyportal_m4, adafruit_pyportal_m4_titano
9+
; default_envs = adafruit_feather_esp32s3_tft, adafruit_magtag29_esp32s2, adafruit_feather_esp32s3, featheresp32s2, adafruit_feather_esp32s2_reversetft, adafruit_metro_esp32s2, adafruit_qtpy_esp32s2, adafruit_feather_esp32s3_reversetft, adafruit_feather_esp32s3_nopsram, adafruit_feather_esp32s2_tft, adafruit_qtpy_esp32, adafruit_qtpy_esp32c3, featheresp32, adafruit_qtpy_esp32s3_nopsram, featheresp32v2, huzzah, adafruit_pyportal_m4, adafruit_pyportal_m4_titano
10+
default_envs = adafruit_metro_esp32s3, raspberypi_picow_debug
1011

1112

1213
; Common Build Environment
@@ -60,6 +61,7 @@ lib_deps =
6061
adafruit/Adafruit MCP9808 Library
6162
adafruit/Adafruit MCP9600 Library
6263
adafruit/Adafruit MLX90632 Library
64+
adafruit/Adafruit AS5600 Library
6365
adafruit/Adafruit MPL115A2
6466
adafruit/Adafruit MPRLS Library
6567
adafruit/Adafruit MS8607
@@ -275,6 +277,10 @@ build_flags = -DARDUINO_METRO_ESP32S3 -DBOARD_HAS_PSRAM
275277
;set partition to tinyuf2-partitions-16MB.csv as of idf 5.1
276278
board_build.partitions = tinyuf2-partitions-16MB.csv
277279
extra_scripts = pre:rename_usb_config.py
280+
debug_tool = jlink
281+
; debug_init_break = tbreak ws_sdcard.cpp:1000
282+
; upload_protocol = jlink
283+
278284

279285
; Adafruit Funhouse ESP32-S2
280286
[env:adafruit_funhouse_esp32s2]
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*!
2+
* @file drvAS5600.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 DRV_AS5600_H
16+
#define DRV_AS5600_H
17+
18+
#include "drvBase.h"
19+
#include <Adafruit_AS5600.h>
20+
21+
/*!
22+
@brief Class that provides a driver interface for an AS5600 sensor.
23+
*/
24+
class drvAS5600 : public drvBase {
25+
public:
26+
/*******************************************************************************/
27+
/*!
28+
@brief Constructor for the AS5600 sensor.
29+
@param i2c
30+
The I2C interface.
31+
@param sensorAddress
32+
7-bit device address.
33+
@param mux_channel
34+
The I2C multiplexer channel.
35+
@param driver_name
36+
The name of the driver.
37+
*/
38+
/*******************************************************************************/
39+
drvAS5600(TwoWire *i2c, uint16_t sensorAddress, uint32_t mux_channel,
40+
const char *driver_name)
41+
: drvBase(i2c, sensorAddress, mux_channel, driver_name) {
42+
_as5600 = nullptr;
43+
_angle = 0;
44+
}
45+
46+
/*******************************************************************************/
47+
/*!
48+
@brief Destructor for an AS5600 sensor.
49+
*/
50+
/*******************************************************************************/
51+
~drvAS5600() {
52+
if (_as5600) {
53+
delete _as5600;
54+
_as5600 = nullptr;
55+
}
56+
}
57+
58+
/*******************************************************************************/
59+
/*!
60+
@brief Initializes the AS5600 sensor and begins I2C.
61+
@returns True if initialized successfully, False otherwise.
62+
*/
63+
/*******************************************************************************/
64+
bool begin() override {
65+
_as5600 = new Adafruit_AS5600();
66+
if (!_as5600->begin((uint8_t)_address, _i2c)) {
67+
WS_DEBUG_PRINTLN("Failed to find AS5600 chip");
68+
return false;
69+
}
70+
71+
if (!configureSensor()) {
72+
WS_DEBUG_PRINTLN("Failed to configure AS5600 sensor");
73+
return false;
74+
}
75+
return true;
76+
}
77+
78+
/*******************************************************************************/
79+
/*!
80+
@brief Configures the AS5600 sensor.
81+
@returns True if the sensor was configured successfully, False otherwise.
82+
*/
83+
/*******************************************************************************/
84+
bool configureSensor() {
85+
return _as5600->enableWatchdog(false) &&
86+
// Normal (high) power mode
87+
_as5600->setPowerMode(AS5600_POWER_MODE_NOM) &&
88+
// No Hysteresis
89+
_as5600->setHysteresis(AS5600_HYSTERESIS_OFF) &&
90+
// analog output (0-VCC for 0-360 degrees)
91+
_as5600->setOutputStage(AS5600_OUTPUT_STAGE_ANALOG_FULL) &&
92+
// setup filters
93+
_as5600->setSlowFilter(AS5600_SLOW_FILTER_16X) &&
94+
_as5600->setFastFilterThresh(AS5600_FAST_FILTER_THRESH_SLOW_ONLY) &&
95+
// Reset position settings to defaults
96+
_as5600->setZPosition(0) && _as5600->setMPosition(4095) &&
97+
_as5600->setMaxAngle(4095);
98+
}
99+
100+
/*******************************************************************************/
101+
/*!
102+
@brief Reads the Angle sensor.
103+
@returns True if the sensor was read successfully, False otherwise.
104+
*/
105+
/*******************************************************************************/
106+
bool readSensor() {
107+
if (!_as5600->isMagnetDetected()) {
108+
WS_DEBUG_PRINTLN("Magnet not detected!");
109+
return false;
110+
}
111+
112+
// Continuously read and display angle values
113+
uint16_t rawAngle = _as5600->getRawAngle();
114+
uint16_t angle = _as5600->getAngle();
115+
116+
WS_DEBUG_PRINT("AS5600 Raw: ");
117+
WS_DEBUG_PRINT(rawAngle);
118+
WS_DEBUG_PRINT(" | Scaled: ");
119+
WS_DEBUG_PRINT(angle);
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+
_angle = ((float)angle / 4095.0) * 360.0;
131+
return true;
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+
if (!readSensor()) {
145+
return false;
146+
}
147+
rawEvent->data[0] = _angle;
148+
return true;
149+
}
150+
151+
void ConfigureDefaultSensorTypes() override {
152+
_default_sensor_types_count = 1;
153+
_default_sensor_types[0] =
154+
wippersnapper_sensor_SensorType_SENSOR_TYPE_RAW;
155+
}
156+
157+
protected:
158+
float _angle; ///< Current angle reading from the AS5600 sensor
159+
Adafruit_AS5600 *_as5600; ///< Pointer to AS5600 sensor object
160+
};
161+
162+
#endif // DRV_AS5600_H

0 commit comments

Comments
 (0)