feature-requests icon indicating copy to clipboard operation
feature-requests copied to clipboard

Support for AS5600 I2C Position Sensor

Open RGN01 opened this issue 4 years ago • 8 comments

Describe the problem you have/What new integration you would like

It would be fantastic to see the AS5600 module supported in ESPHome to allow single-chip support for all manner of 360° contactless rotational position sensing.

Please describe your use case for this integration and alternatives you've tried:

I wish to use this initially in a self-designed weather station for wind direction but expect to see it used for blinds, as a digital potentiometer equivalent for position inputs for dimmers and many similar use cases. The low cost adds to the attraction!

Additional context

More details at https://ams.com/as5600

RGN01 avatar Oct 13 '21 19:10 RGN01

I join the request.

I was just looking for a way to build a weather vane and for the price this sensor is perfect.

fran6120 avatar Dec 06 '21 18:12 fran6120

I join the request.

I was just looking for a way to build a weather vane and for the price this sensor is perfect.

It's not the most elegant way, but I managed to do this by using the 'analogue' (stepped digital) output of the AS5600. It works very well. I'd love to use I2C and will keep trying to do that but, for now, this works.

RGN01 avatar Dec 12 '21 21:12 RGN01

Is it possible you could drop an explained on how to did this on the post you made in the Forum?

Drew765 avatar Jan 13 '22 22:01 Drew765

Hi all, in the meantime is there a solution as a custom component ?

beaTejakulator avatar Jul 10 '22 20:07 beaTejakulator

+1 here, I would be interested in a solution too

whizkid79 avatar Jul 11 '22 08:07 whizkid79

Here I have found a library. I just can't get it to work via custom i2c on esphome...

https://registry.platformio.org/libraries/robtillaart/AS5600 https://github.com/RobTillaart/AS5600

beaTejakulator avatar Jul 12 '22 16:07 beaTejakulator

+1 here, I would also be interested in a solution. Used for weather station

G-R-O avatar Sep 02 '22 05:09 G-R-O

My test code might be useful for someone. This is just a quick hack, so it will need to be polished. And changed if you don't want the value pushed to HA every 15 seconds...

First, create a file called (for example) as5600_sensor.h in directory /config/esphome/ with this content:

#include "esphome.h"
#include <Wire.h>
#include "AS5600.h"

#define TAG "AS5600"

AS5600 as5600; // Default i2c address 0x36

class AS5600Sensor: public PollingComponent, public Sensor {
  public: AS5600 as5600;
  AS5600Sensor(): PollingComponent(15000) {}

  void setup() override {
    as5600.begin();
    as5600.setDirection(AS5600_CLOCK_WISE); // Or AS5600_COUNTERCLOCK_WISE
  }

  void update() override {
    if (!as5600.isConnected()) {
      ESP_LOGE(TAG, "Not connected");
      return;
    }
    if (!as5600.detectMagnet()) {
      ESP_LOGE(TAG, "No magnet detected");
      return;
    }
    if (as5600.magnetTooStrong()) {
      ESP_LOGE(TAG, "Magnet too strong");
      return;
    }
    if (as5600.magnetTooWeak()) {
      ESP_LOGE(TAG, "Magnet too weak");
      return;
    }
    float angle = as5600.rawAngle() * AS5600_RAW_TO_DEGREES;
    publish_state(angle);
  }
};

Then in your ESPHome yaml, add something like this:

esphome:
  includes:
    - "as5600_sensor.h"
  libraries:
    - Wire
    - "AS5600"

i2c:
  sda: 21 # update as needed
  scl: 22

sensor:
  - platform: custom
    lambda: |-
      auto my_as5600_sensor = new AS5600Sensor();
      App.register_component(my_as5600_sensor);
      return {my_as5600_sensor};
    sensors:
      name: "AS5600 Test"
      accuracy_decimals: 2
# If needed, add filters...
#      filters:
#      - calibrate_linear:
#          - 211.9 -> 0
#          - 240.8 -> 2

The lib for AS5600 is automagically pulled from https://registry.platformio.org/libraries/robtillaart/AS5600, thank you ESPHome! See that link for documentation.

My experience: Do not forget to wire the DIR to GND or else readings will be strange. Putting the magnet directly on a metal resulted in bad readings, but seem to work for others. 5 mm plastic in between fixed this for me.

Hope it can help someone.

wt4711 avatar Sep 17 '22 12:09 wt4711

Hello, I tried to implement code above, but it seems that AS5600 library is missing during compilation. I don't know how to implement it, also tried to download manualy RobTillaart/AS5600 library, but no luck with commisioning. Will be AS5600 included in ESPHome in future? Did I miss something? Thank you for support.

gorjiri avatar Oct 21 '22 13:10 gorjiri

It is actually just as described above. I would say you delete the build path once. Or you create it again as a new device. The best thing is to show what exactly does not work, or what message you get.

beaTejakulator avatar Oct 21 '22 16:10 beaTejakulator

After connection sensor to i2c, autoscan found new device with 0x36 addres. - Thats ok. The file "as5600_sensor.h" is in folder "/config/esphome/" Code in ESPHome yaml is same as described.

During OTA there is error 1:

INFO Reading configuration /config/esphome/weatherstation.yaml... INFO Generating C++ source... INFO Compiling app... Processing weatherstation (board: esp01_1m; framework: arduino; platform: platformio/espressif8266 @ 3.2.0)

HARDWARE: ESP8266 80MHz, 80KB RAM, 1MB Flash LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf Library Manager: Installing AS5600 Warning! Please upgrade to the PlatformIO Core 6 Dependency Graph |-- <ESPAsyncTCP-esphome> 1.2.3 |-- <Wire> 1.0 |-- <ESPAsyncWebServer-esphome> 2.1.0 | |-- <ESPAsyncTCP-esphome> 1.2.3 | |-- <Hash> 1.0 | |-- <ESP8266WiFi> 1.0 |-- <DNSServer> 1.1.1 |-- <ESP8266WiFi> 1.0 |-- <ESP8266mDNS> 1.2 |-- <ArduinoJson> 6.18.5 Linking /data/weatherstation/.pioenvs/weatherstation/firmware.elf /data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: /data/weatherstation/.pioenvs/weatherstation/src/main.cpp.o:(.text._ZNSt17_Function_handlerIFSt6vectorIPN7esphome6sensor6SensorESaIS4_EEvEZ5setupEUlvE10_E9_M_invokeERKSt9_Any_data+0x8): undefined reference to _ZN6AS5600C1EP7TwoWire' /data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: /data/weatherstation/.pioenvs/weatherstation/src/main.cpp.o: in function _ZNSt17_Function_handlerIFSt6vectorIPN7esphome6sensor6SensorESaIS4_EEvEZ5setupEUlvE10_E9_M_invokeERKSt9_Any_data': main.cpp:(.text._ZNSt17_Function_handlerIFSt6vectorIPN7esphome6sensor6SensorESaIS4_EEvEZ5setupEUlvE10_E9_M_invokeERKSt9_Any_data+0x45): undefined reference to _ZN6AS5600C1EP7TwoWire' /data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: /data/weatherstation/.pioenvs/weatherstation/src/main.cpp.o:(.text._ZN12AS5600Sensor6updateEv[_ZN12AS5600Sensor6updateEv]+0x18): undefined reference to _ZN6AS560011isConnectedEv' /data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: /data/weatherstation/.pioenvs/weatherstation/src/main.cpp.o:(.text._ZN12AS5600Sensor6updateEv[_ZN12AS5600Sensor6updateEv]+0x1c): undefined reference to _ZN6AS560012detectMagnetEv' /data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: /data/weatherstation/.pioenvs/weatherstation/src/main.cpp.o:(.text._ZN12AS5600Sensor6updateEv[_ZN12AS5600Sensor6updateEv]+0x20): undefined reference to _ZN6AS560015magnetTooStrongEv' /data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: /data/weatherstation/.pioenvs/weatherstation/src/main.cpp.o:(.text._ZN12AS5600Sensor6updateEv[_ZN12AS5600Sensor6updateEv]+0x24): undefined reference to _ZN6AS560013magnetTooWeakEv' /data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: /data/weatherstation/.pioenvs/weatherstation/src/main.cpp.o:(.text._ZN12AS5600Sensor6updateEv[_ZN12AS5600Sensor6updateEv]+0x28): undefined reference to _ZN6AS56008rawAngleEv' /data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: /data/weatherstation/.pioenvs/weatherstation/src/main.cpp.o: in function _ZN12AS5600Sensor6updateEv': main.cpp:(.text._ZN12AS5600Sensor6updateEv[_ZN12AS5600Sensor6updateEv]+0x3f): undefined reference to _ZN6AS560011isConnectedEv' /data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: main.cpp:(.text._ZN12AS5600Sensor6updateEv[_ZN12AS5600Sensor6updateEv]+0x4f): undefined reference to _ZN6AS560012detectMagnetEv' /data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: main.cpp:(.text._ZN12AS5600Sensor6updateEv[_ZN12AS5600Sensor6updateEv]+0x5f): undefined reference to _ZN6AS560015magnetTooStrongEv' /data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: main.cpp:(.text._ZN12AS5600Sensor6updateEv[_ZN12AS5600Sensor6updateEv]+0x70): undefined reference to _ZN6AS560013magnetTooWeakEv' /data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: main.cpp:(.text._ZN12AS5600Sensor6updateEv[_ZN12AS5600Sensor6updateEv]+0x8b): undefined reference to _ZN6AS56008rawAngleEv' /data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: /data/weatherstation/.pioenvs/weatherstation/src/main.cpp.o:(.text._ZN12AS5600Sensor5setupEv[_ZN12AS5600Sensor5setupEv]+0x0): undefined reference to _ZN6AS56005beginEh' /data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: /data/weatherstation/.pioenvs/weatherstation/src/main.cpp.o:(.text._ZN12AS5600Sensor5setupEv[_ZN12AS5600Sensor5setupEv]+0x4): undefined reference to _ZN6AS560012setDirectionEh' /data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: /data/weatherstation/.pioenvs/weatherstation/src/main.cpp.o: in function _ZN12AS5600Sensor5setupEv': main.cpp:(.text._ZN12AS5600Sensor5setupEv[_ZN12AS5600Sensor5setupEv]+0x1a): undefined reference to _ZN6AS56005beginEh' /data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: main.cpp:(.text._ZN12AS5600Sensor5setupEv[_ZN12AS5600Sensor5setupEv]+0x24): undefined reference to _ZN6AS560012setDirectionEh' /data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: /data/weatherstation/.pioenvs/weatherstation/src/main.cpp.o: in function _GLOBAL__sub_I_logger_logger': main.cpp:(.text.startup._GLOBAL__sub_I_logger_logger+0xf): undefined reference to `_ZN6AS5600C1EP7TwoWire' collect2: error: ld returned 1 exit status *** [/data/weatherstation/.pioenvs/weatherstation/firmware.elf] Error 1 ========================== [FAILED] Took 6.26 seconds ==========================

gorjiri avatar Oct 21 '22 20:10 gorjiri

@gorjiri

I have made the same settings as you with the same board and come up with no problems. I use homeassistant with ESPHome Current version: 2022.10.0.

I tried to add the sensor on its own. I don't think it's really right, but it works. Copy the folder to your ESPHome directory "/esphome/custom_components".

Then copy the following into your .yaml:

i2c:
  sda: 4
  scl: 5

sensor:
  - platform: as5600_custom
    name: AS5600
    update_interval: 5s
    accuracy_decimals: 2

custom_components.zip

beaTejakulator avatar Oct 23 '22 05:10 beaTejakulator

Now it is working great, thank you!

gorjiri avatar Oct 23 '22 14:10 gorjiri

Unfortunately not compile with framework esp-idf, only with framework Arduino.

Can someone guide me or help me to make it work with esp-idf?

Thanks in advance!

fran6120 avatar Apr 15 '23 10:04 fran6120

Been waiting for this one. Everything is working perfectly on an ESP8266 right out of the gate. This community is awesome. Thanks for all the work.

CobraDunn avatar Jan 10 '24 22:01 CobraDunn