Has anyone been able to connect and manage VEML7700 Ambient Light Sensor Module with ESPHome ? If so, could you share your code please.
I’ve done just that. Works like a charm.
(I’m assuming that you’re using the VEML device sold by AdaFruit)
Follow the directions in the example tutorial for Custom Components.
Here’s the .h file I use. Most of it is commented-out - those are features I didn’t need.
// veml7700.h - for ESPHome, a Custom Sensor
#include <Adafruit_VEML7700.h>
#define VEML_POLL_RATE 17000 // 27000 makes a good rate
#define VEML_GAIN VEML7700_GAIN_1 // GAIN_1 is 'default'
#define VEML_IT VEML7700_IT_100MS // 100MS is 'default'
// gain 1, IT 100mS, means the max light level reported will be 3775
class MyVEML7700Sensor : public PollingComponent, public Sensor {
protected:
bool VEML7700Present = false, VEML7700FirstRead = true;
public:
Adafruit_VEML7700 veml = Adafruit_VEML7700(); // constructor
MyVEML7700Sensor() : PollingComponent(VEML_POLL_RATE) {}
Sensor *veml7700_lux = new Sensor();
// Sensor *veml7700_white = new Sensor();
// Sensor *veml7700_raw_als = new Sensor();
// Sensor *veml7700_gain = new Sensor();
// Sensor *veml7700_it = new Sensor();
// Sensor *veml7700_interrupt_status = new Sensor();
float get_setup_priority() const override { return esphome::setup_priority::DATA; }
void setup() override {
if (veml.begin()) {
VEML7700Present = true;
} else {
return;
}
veml.setGain(VEML_GAIN); // in example is VEML7700_GAIN_1
veml.setIntegrationTime(VEML_IT); // in example is VEML7700_IT_800MS
veml.readLux(); // fetch the first entry, but discard it - it will be wrong
}
void update() override { // This will be called every "update_interval" milliseconds.
if (VEML7700Present) {
veml7700_lux->publish_state(veml.readLux());
// veml7700_white->publish_state(veml.readWhite());
// veml7700_raw_als->publish_state(veml.readALS());
// veml7700_gain->publish_state(veml.getGain());
// veml7700_it->publish_state(veml.getIntegrationTime());
// veml7700_interrupt_status->publish_state(veml.interruptStatus());
// uint16_t irq = veml.interruptStatus();
// if (irq & VEML7700_INTERRUPT_LOW) {
// // Serial.println("** Low threshold");
// }
// if (irq & VEML7700_INTERRUPT_HIGH) {
// // Serial.println("** High threshold");
// }
}
}
// void loop() override { // This will be called by App.loop(), very rapidly
// }
};
In your YAML file, you’ll need a few things, too:
sensor:
- platform: custom
lambda: |-
auto my_veml7700_sensor = new MyVEML7700Sensor();
App.register_component(my_veml7700_sensor);
return {
my_veml7700_sensor->veml7700_lux
// , my_veml7700_sensor->veml7700_white
// , my_veml7700_sensor->veml7700_raw_als
// , my_veml7700_sensor->veml7700_gain
// , my_veml7700_sensor->veml7700_it
// , my_veml7700_sensor->veml7700_interrupt_status
};
sensors:
- name: "${node_name} VEML7700 Lux"
# filters:
# - median:
# # window_size: 5
# send_every: 1
# # send_first_at: 2
# - exponential_moving_average:
# # window_size: 5
# send_every: 3
# # send_first_at: 5
accuracy_decimals: 1
unit_of_measurement: lux
# poll_frequency: see veml7700.h, VEML_POLL_RATE
# - name: "${node_name} VEML7700 White"
# accuracy_decimals: 1
# unit_of_measurement: lux
# - name: "${node_name} VEML7700 raw ALS"
# accuracy_decimals: 1
# unit_of_measurement: raw
esphome:
includes:
- veml7700.h
libraries:
- "Wire"
- "Adafruit Unified Sensor"
- "SPI"
- "Adafruit BusIO"
- "Adafruit VEML7700 Library"
I got my unit from AliExpress. Link https://www.aliexpress.com/item/1005002600208149.html?spm=a2g0o.order_list.0.0.21ef1802POXkwl.
They should work the same way using I2C.
I never used common components, I will investigate and give it a try.
Thanks
Got it to work. Thank you very much, I learn a bit more.
Took me a while, there is 2 voltage input: (VIN and 3Vo). I used VIN and ESPHOME could not find the device, giving me error at compilation time. I installed another device (BMP885) and ESPHOME was able to find it. I was then sure to have the right connection. Then I connected to 3Vo, and success.
Thank very much for providing the code.
I am trying to get this same sensor working with my ESPHOME/HA. I had to switch to the ESP-IDF framework for another reason and this VEML7700 is written for Arduino so it is throwing errors. Has anyone seen an ESP-IDF implementation? Mainly the includes right now but I think other errors will follow. like #include and the Wire and others.