esphome / feature-requests

ESPHome Feature Request Tracker
https://esphome.io/
417 stars 26 forks source link

Add support of distance TOF vl6180x #1624

Open zigomatichub opened 2 years ago

zigomatichub commented 2 years ago

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

Time of flight sensor can use VL6180 chip, it would be great to add this one in the actual list of TOF and distance sensor

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

There is this: https://github.com/pololu/vl6180x-arduino Link to datasheet: https://www.st.com/resource/en/datasheet/vl6180.pdf

Additional context Use I2C platform

wazoo666 commented 2 years ago

I also join to this request

please, could you create code to this short distance/light ToF ?? thankU

I found 2 samples on github , but none is fully working

Raelx commented 1 year ago

I too have a use for this.

ryan-durbin commented 1 year ago

Following, would also like official support for VL6180

DocDoo commented 1 year ago

Also following 👀 would love to have support for this combined time-of-flight distance and ambient light sensor ❤

medivb commented 9 months ago

Definitely following this as well. The short range would be great to have and the custom component (besides being deprecated) is not working for me.

mikulik86 commented 9 months ago

I second that.

NonaSuomy commented 8 months ago

Definitely following this as well. The short range would be great to have and the custom component (besides being deprecated) is not working for me.

In case someone is confused about this comment, the hardware sensor is ACTIVE and not depreciated. The custom component that was made is being depreciated in the future https://github.com/exxamalte/esphome-customisations/issues/10

You can see the hardware sensor status on top of the product pages where it says Active on both:

https://www.st.com/en/imaging-and-photonics-solutions/vl6180.html https://www.st.com/en/imaging-and-photonics-solutions/vl6180x.html

Also following 👀 would love to have support for this combined time-of-flight distance and ambient light sensor ❤

Just to note: Most breakout boards that say VL6180X on them are not X just VL6180's ie they have no ambient light sensor on them (I fell for this as I wanted the ambient light sensor as well but was sad when I got the module and tested it out).

medivb commented 8 months ago

Definitely following this as well. The short range would be great to have and the custom component (besides being deprecated) is not working for me.

In case someone is confused about this comment, the hardware sensor is ACTIVE and not depreciated. The custom component that was made is being depreciated in the future exxamalte/esphome-customisations#10

You can see the hardware sensor status on top of the product pages where it says Active on both:

https://www.st.com/en/imaging-and-photonics-solutions/vl6180.html https://www.st.com/en/imaging-and-photonics-solutions/vl6180x.html

Also following 👀 would love to have support for this combined time-of-flight distance and ambient light sensor ❤

Just to note: Most breakout boards that say VL6180X on them are not X just VL6180's ie they have no ambient light sensor on them (I fell for this as I wanted the ambient light sensor as well but was sad when I got the module and tested it out).

I did not say the sensor was deprecated, but the custom component. It works, but a warning about this is shown on https://esphome.io/custom/custom_component.html.

NonaSuomy commented 8 months ago

I just wanted to clarify. Didn't want someone who may quickly read that and wanted to tackle the code to be like why add this if the hardware is depreciated confusion.

NonaSuomy commented 5 months ago

We just built a component for this but my sensor is a vl6180 (non x) anyone have a confirmed X version with the ALS (Ambient Light Sensor)? image

wazoo666 commented 5 months ago

We just built a component for this but my sensor is a vl6180 (non x) anyone have a confirmed X version with the ALS (Ambient Light Sensor)? image

Please, can you share the code ??

I am just too crazy, because some time ago I was able to get working vl6180 module under ESPhome, but my computer crashed and lost the code. All the code I have tested does not compile (I have used the Pololu, DFRobot,... libraries). I must admit that I am not a programming expert. The custom sensor created in ESPhome has calls that are not recognised by the compiler. I have tried to use AI but I get always same error response by AI, "folder location" of the .h and .cpp files, and I am sure they are well pointed from the "external_component" in YAML.

** Sorry, can not discover hot to edit below "big" text in HTML format to plain text. :(

If no sensor code added, compiler finish correctly, but my problem is when custom sensor and lambda code is used.

This error code received by ESPhome compiler:

/config/pw-dds238.yaml: In lambda function: /config/pw-dds238.yaml:387:29: error: could not convert '{vl6180x_sensor}' from '' to 'std::vector<esphome::sensor::Sensor*>' return {vl6180x_sensor};

NonaSuomy commented 4 months ago

What are you using the sensor for? Yes you can have it soon enough. I'm just trying to finish some simple hand gesture control. Is your sensor the X version or V with no ALS?

Use 3 backticks ` at the top and bottom of your code / logs for it to not interfere with the github markdown. After the 3 first backticks you can also add the coding syntax like yaml, cpp, python etc for it to colour it.

image

AridRayne commented 4 months ago

@NonaSuomy I have a vl6180x, with the ALS. I'd be more than happy to help out.

NonaSuomy commented 4 months ago

Please contact me on ESPHome discord https://discord.gg/syUMbGXW same name.

wazoo666 commented 4 months ago

Please contact me on ESPHome discord https://discord.gg/syUMbGXW same name.

OK, let me some time because need to recover my Discord account :D :D

I am using a common Aliexp TOF050C module with IR filter cover, and it is vl6180 model w/o ligth sensor. :( I am using as a door lock detector. I mean, on a security door it has diferent deep positioning, so I am reading and translating to ESPhome to generate different Automations in HA. That is because a buy this model because I only need precise readings from 5mm to 60mm :D

NonaSuomy commented 4 months ago

@AridRayne can I get another ALS test if you have another free moment.

wazoo666 commented 4 months ago

What are you using the sensor for? Yes you can have it soon enough. I'm just trying to finish some simple hand gesture control. Is your sensor the X version or V with no ALS?

Finally I get it working. I am not a great coding expert, just try and error methode :) I think my problem was includes and libraries typing sequence. Now is working for my needs. I only use distance measurement, my module does not include light sensor. It is basic vl6180 (w/o "X")

esphome:
  name: pw-dds238
  friendly_name: pw-dds238
   includes:
      - ./custom_components/vl6180x_sensor/vl6180x_sensor.h
  libraries:
    - Wire
    - pololu/VL6180X  #@1.4.0
# -----------------------------------------------
esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: arduino
# -----------------------------------------------
(logger, debug, API , pta, wifi, ...  definitions)
.
.
# ----------------------------------------------
i2c:                            # COMUNICACION CON SENSOR I2C (VL6180) para ver cerradura de puerta
  id: i2c_bus
  scl: GPIO04
  sda: GPIO05
  frequency: 400kHz
  scan: true
# ---------------
.
.
.
# ---------------
sensor:
# Sensor puerta
  - platform: custom
    id: dds238_cerradura
    lambda: |-
      auto vl6180x_sensor = new esphome::vl6180x::VL6180XSensor();
      App.register_component(vl6180x_sensor);
      return {vl6180x_sensor};
    sensors:
      id: cerradura_distancia
      name: "DDS238 cerradura mm"
      unit_of_measurement: "mm"
      accuracy_decimals: 1
      icon: "mdi:arrow-expand-horizontal"
      filters:
        - multiply: 1.5
        - sliding_window_moving_average:
            window_size: 8
            send_every: 5
        - delta: 1.5
NonaSuomy commented 3 months ago
external_components:
#   - source:
#     type: local
#     path: components
  - source: github://NonaSuomy/esphome@vl6180x
    components: [ vl6180x ]
    refresh: 30s
i2c:
  - id: i2cbus_001
    sda: 23
    scl: 22
sensor:
  - platform: vl6180x
    address: 0x29
    update_interval: 5s
    distance:
      id: vl6180x_distance
      name: "VL6180X Distance"
      unit_of_measurement: mm
    als:
      name: "VL6180X Ambient Light"
      unit_of_measurement: lux
      accuracy_decimals: 1
      gain: '2.5x'
      #under_glass: true
      #lux_without_glass: 500.0  # Add this line to specify the lux value without the glass

Be careful how much you poll your sensor I did 100ms to use with gestures and not sure if that was the issue but it stopped working after that.

This works with both arduino and esp-idf framework.

The gesture stuff looked like this but didn't get a chance to finish it before the device failed

on_swipe_gesture:
  then:
    - lambda: ESP_LOGD("vl6180x", "Swiped");
    - light.toggle: my_light
on_double_tap_gesture:
   then:
     - lambda: ESP_LOGD("vl6180x", "Double tap triggered");
     - light.toggle: my_light
on_hover_gesture:
   then:
     - lambda: ESP_LOGD("vl6180x", "Hover triggered");
     - light.toggle: my_light
AridRayne commented 3 months ago

Every now and then, I'd start getting a vcsel error after I changed my config. Completely removing power from the board and restarting fixed it.

NonaSuomy commented 3 months ago

If you don't change the config is it stable after the power reset?

I no longer have a board to test it until I buy another one.

I have always had issue with I2C boards that keep the power always on even if the UC restarts and the only way to get them working again was to reset the power fully to them. I started building power switches into the power lines of the I2C boards. Maybe try using the enable pin on the board (shutx, etc) to a gpio pin.

AridRayne commented 3 months ago

It only send to ever happen after flashing. But it's been stable after every power reset so far.

dmsergeevich63 commented 3 months ago

How to change the sensor address using the gpio pin? I need to use three sensors on one bus i2c.

I used the following settings with fast polling, the sensor works and does not heat up:

i2c:
  sda: GPIO21
  scl: GPIO22
  frequency: 400kHz

sensor:
  - platform: vl6180x
    address: 0x29
    update_interval: 0.1s
    distance:
      id: vl6180x_distance
      name: "VL6180X Distance"
      unit_of_measurement: mm
    als:
      name: "VL6180X Ambient Light"
      unit_of_measurement: lux
      accuracy_decimals: 1
      gain: '2.5x'
NonaSuomy commented 3 months ago

Maybe check out TCA9548A 1-to-8 I2C multiplexers

https://forums.adafruit.com/viewtopic.php?t=119485

Their documentation for the other model says this:

The VL53L0X has a default I2C address of 0x29!

You can change it, but only in software. That means you have to wire the SHUTDOWN pin and hold all but one sensor in reset while you reconfigure one sensor at a time
ImNewty commented 3 months ago

Using your code my sensor limits to 255mm on distance what could cause that?

NonaSuomy commented 2 months ago

The range for the vl6180x is 5mm to 100mm depending on your light conditions with a MAXIMUM of 200mm in pristine condition not sure why you are expecting more than that from this unit. If you need more get the model up as it won't be very accurate otherwise. image

ImNewty commented 2 months ago

The sensor I got said it can measure up to 50cm so 500mm and also when used with the default arduino library it had no such issues. And I'm also using this in complete darkness.

NonaSuomy commented 2 months ago

You probably used the pololu library, This is based on the adafruit library that doesn't make those claims. 255 feels like a limitation of the data type or a limitation on how I read the I2C data. What are you using it for?

NonaSuomy commented 2 months ago

Attempted a fix it compiled, try now let me know. Sorry if it explodes. I have no hardware to test with. Changed the data type to a 16 instead of 8. You may have to deal with overflow now if you go out of range.

srvoets commented 2 months ago

I am trying to implement a hiletgo vl6180x, I am using the reccomended configs, I inirially was getting readings of 0 but now am getting "VCSEL Continuity Test; No measurement possible"

Board is a D1 Mini 8266

`i2c:

NonaSuomy commented 2 months ago

There's a comment here about that not sure if the same error? https://github.com/esphome/feature-requests/issues/1624#issuecomment-2201941401 Did you try power cycling both the module and the esp after a successful upload (removing the power completely from both units) I can't remember the VCSEL error I was getting but it meant the sensor is dead.

I never tested on a esp8266 only esp32 if that has some unknown to me effect for you.

NonaSuomy commented 2 months ago

I get this error currently

[17:03:45][E][vl6180x.sensor:154]: Unknown error
[17:03:50][E][vl6180x.sensor:154]: Unknown error
[17:03:55][E][vl6180x.sensor:154]: Unknown error
[17:08:54][154681][E][Wire.cpp:513] requestFrom(): i2cRead returned Error 263
[17:08:59][E][vl6180x.sensor:099]: System error; VCSEL Continuity Test; No measurement possible

So will have to see if it works for any one with a working module

srvoets commented 2 months ago

I get this error currently

[17:03:45][E][vl6180x.sensor:154]: Unknown error
[17:03:50][E][vl6180x.sensor:154]: Unknown error
[17:03:55][E][vl6180x.sensor:154]: Unknown error
[17:08:54][154681][E][Wire.cpp:513] requestFrom(): i2cRead returned Error 263
[17:08:59][E][vl6180x.sensor:099]: System error; VCSEL Continuity Test; No measurement possible

So will have to see if it works for any one with a working module

I just wired up my last VL16180x, and am receiving this in logs, below is the relevant yaml

[18:06:47][W][vl6180x:060]: Raw reading underflow [18:06:47][D][sensor:093]: 'Distance': Sending state nan mm with 0 decimals of accuracy

`esphome: name: tip-jar friendly_name: Tip-Jar includes:

esp8266: board: d1_mini

sensor:

NonaSuomy commented 2 months ago

Looks like it is a problem with my component code. I just tested with Arduino and my sensor I thought was dead is working fine. Note the Pololu library example is only showing up to 255 as well.

/* This minimal example shows how to get single-shot range
measurements from the VL6180X.

The range readings are in units of mm. */

#include <Wire.h>
#include <VL6180X.h>

#define I2C_SDA 33
#define I2C_SCL 32

VL6180X sensor;

void setup() 
{
  Serial.begin(9600);
  Wire.begin(I2C_SDA, I2C_SCL);

  sensor.init();
  sensor.configureDefault();
  sensor.setTimeout(500);
}

void loop() 
{ 
  Serial.print(sensor.readRangeSingleMillimeters());
  if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }

  Serial.println();
}
96
89
91
92
105
173
255
255
255

Looks like they use this scaling factor to do larger distance

/* This example shows how to change the range scaling factor
of the VL6180X. The sensor uses 1x scaling by default,
giving range measurements in units of mm. Increasing the
scaling to 2x or 3x makes it give raw values in units of 2
mm or 3 mm instead. In other words, a bigger scaling factor
increases the sensor's potential maximum range but reduces
its resolution. */

#include <Wire.h>
#include <VL6180X.h>

#define I2C_SDA 33
#define I2C_SCL 32

// To try different scaling factors, change the following define.
// Valid scaling factors are 1, 2, or 3.
#define SCALING 2

VL6180X sensor;

void setup()
{
  Serial.begin(9600);
  Wire.begin(I2C_SDA, I2C_SCL);

  sensor.init();
  sensor.configureDefault();
  sensor.setScaling(SCALING);
  sensor.setTimeout(500);
}

void loop()
{
  Serial.print("(Scaling = ");
  Serial.print(sensor.getScaling());
  Serial.print("x) ");

  Serial.print(sensor.readRangeSingleMillimeters());
  if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }

  Serial.println();
}
(Scaling = 2x) 330
(Scaling = 2x) 338
(Scaling = 2x) 346
(Scaling = 2x) 358
(Scaling = 2x) 366
(Scaling = 2x) 368
(Scaling = 2x) 376
(Scaling = 2x) 374
(Scaling = 2x) 510
(Scaling = 2x) 510
(Scaling = 3x) 369
(Scaling = 3x) 363
(Scaling = 3x) 366
(Scaling = 3x) 360
(Scaling = 3x) 354
(Scaling = 3x) 351
(Scaling = 3x) 354
(Scaling = 3x) 357
(Scaling = 3x) 765

Adafruit Library also doesn't default to higher than 200 goes into reading overflow past that.

#include <Wire.h>
#include "Adafruit_VL6180X.h"

#define I2C_SDA 33
#define I2C_SCL 32

Adafruit_VL6180X vl = Adafruit_VL6180X();

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

  // wait for serial port to open on native usb devices
  while (!Serial) {
    delay(1);
  }

  // Initialize I2C with custom SDA and SCL pins
  Wire.begin(I2C_SDA, I2C_SCL);

  Serial.println("Adafruit VL6180x test!");
  if (!vl.begin()) {
    Serial.println("Failed to find sensor");
    while (1);
  }
  Serial.println("Sensor found!");
}

void loop() {
  float lux = vl.readLux(VL6180X_ALS_GAIN_5);

  Serial.print("Lux: "); Serial.println(lux);

  uint8_t range = vl.readRange();
  uint8_t status = vl.readRangeStatus();

  if (status == VL6180X_ERROR_NONE) {
    Serial.print("Range: "); Serial.println(range);
  }

  // Some error occurred, print it out!

  if ((status >= VL6180X_ERROR_SYSERR_1) && (status <= VL6180X_ERROR_SYSERR_5)) {
    Serial.println("System error");
  }
  else if (status == VL6180X_ERROR_ECEFAIL) {
    Serial.println("ECE failure");
  }
  else if (status == VL6180X_ERROR_NOCONVERGE) {
    Serial.println("No convergence");
  }
  else if (status == VL6180X_ERROR_RANGEIGNORE) {
    Serial.println("Ignoring range");
  }
  else if (status == VL6180X_ERROR_SNR) {
    Serial.println("Signal/Noise error");
  }
  else if (status == VL6180X_ERROR_RAWUFLOW) {
    Serial.println("Raw reading underflow");
  }
  else if (status == VL6180X_ERROR_RAWOFLOW) {
    Serial.println("Raw reading overflow");
  }
  else if (status == VL6180X_ERROR_RANGEUFLOW) {
    Serial.println("Range reading underflow");
  }
  else if (status == VL6180X_ERROR_RANGEOFLOW) {
    Serial.println("Range reading overflow");
  }
  delay(50);
}
Range: 190
Lux: 0.00
Raw reading overflow
Lux: 0.00

Which library were you using?

ImNewty commented 2 months ago

With arduino I used the pololu library with scaling to get a longer range because I don't really care much about the resolution since I'm using this sensor so see how much food for animals I have left in an enclosed box.

NonaSuomy commented 2 months ago

I wonder if this has to do with the vcsel need power cycle thing https://github.com/adafruit/Adafruit_VL6180X/issues/15

ImNewty commented 2 months ago

For me the vcsel error only happened when I didn't include the light sensor I only needed the distance and it went into that error but after adding light sensor as well it worked

ImNewty commented 2 months ago

Any other updates on this?