Using a piicodev colour sensor veml6040 with ESP32

Hi
I’m interested in using the piicodev colour sensor with and ESP32 as part of an IOT project I have.

I think that I will ditch the adapter for Raspberry Pi and go with CE07796 - the prototyping cable and a breadboard to connect the ESP32 and the sensor.

I was just wondering if anyone has any tips/tricks/traps that might help me?

2 Likes

Hi Ben,

The prototyping cable is perfect for the ESP32’s.

As for tips and tricks, if you are using micropython I’d look up the GitHub docs for the PiicoDev sensors, all of the parameters you need to input can be found there (bus, freq, sda, scl)

1 Like

Hi Ben,

That’s the way to go. I’ve used the same cable with my PiicoDev magnetometer with one of my ESP32s. Liam is bang on here if you’re using micropython we have a great range of documentation and sample code that may be of use to you.

If you’re using C I’ve had luck using existing libraries for the same chips before but it can be a bit finicky at times.

Let us know how you go!

2 Likes

Thanks guys - I have had a look at the Git hub page and also the video at https://youtu.be/Qz4lFLdr7Rk?feature=shared

I’ve been working on this today and I now have a couple of better questions. I’m using a EWP32-C3-DevKitC-2U.

The first relates to the required libraries to run the CEML6040 on the ESP32-C3. In the main.py you use the lines

from PiicoDev_VEML6040 import PiicoDev_VEML6040
from PiicoDev_Unified import sleep_ms #

this seems to be different to the way that libraries are included normally

#include <PubSubClient.h> 

I haven’t been able to find a piicodev library in my arduino IDE library manager. Its a bit confusing.

What libraries do I need to include and how should I do this?

Second question;
I want to map the yellow SCL jumper from the VEML6040 to GPIO21 and the blue SDA jumper to GPIO20, how do I include this in the code? And do I need to do anything else when calling the colourSensor function?

Hi Ben,

The PiicoDev examples are in Micropython where as Arduino uses C++. Most devices that use Micropython can also be programmed in C++. The issue that you will run into here is you won’t be able to use the PiicoDev libraries.

Try using some other libraries for the VEML6040. You should be able to find one that works for you.

The SCL and SDA pins are for the I2C comms protocol. You will need to include the wire library. In your setup include Wire.begin(); to initialise the I2C bus. From here it will depend on which library you find for the sensor.

Which ESP32 board are you using?

1 Like

I’m using the ESP32-C3-DevKit-C2
https://docs.espressif.com/projects/esp-dev-kits/en/latest/esp32c3/esp32-c3-devkitc-02/index.html#esp32-c3-devkitc-02

Hey @Ben280022,

So to confirm, You are using a ESP32-C3-DevKit-C2 and are planning on programming it using the Arduino IDE?

The Arduino IDE and the libraries it contains will be written in the C++ language. As the piicodev sensors have been designed with micropython in mind the libraries will not be able to used interchangeably.

If you are using C++ inside the Arduino IDE with this sensor you will need to find the corresponding arduino library implementations that relate to veml6040 and I2C.

Jack has already tracked these down for you:

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

Once you have these libraries imported you can confirm this is all working for you by testing some of the example code included in the sensor library.

Hope this helps!

Thanks Guys - that was helpful. I have been able to get the sensor working using those libraries. Once they were sorted out the biggest drama I had was getting the ESP32 to connect to the MQTT broker. THB it is still a bit flaky.

You can use a number of .get functions to read the sensor. Eg RGBWSensor.getRed(), and you can pull red, green, blue, and white. The first 3 return four digit integers as values, white returns a five digit integer. There is also colour temperature - a 4 digit integer and a .getAmbientLight function which is producing a float.

Does anyone have any information about what these numbers represent? I expect that the “red” number represents a part of the spectrum and the same for ‘green’ and ‘blue’ are these discrete or do they overlap? Maybe the manufacturer provides a data sheet?

1 Like

Oh, just one thing on the I2C communication.
Working out which pins to use on the EWP32 was a bit unclear. My understanding is you can actually use any of the pins, but some pins have dedicated functions so it is better to avoid them. I set it up like this;

//sensor

int sda_pin = 18; // GPIO18 as I2C SDA Blue

int scl_pin = 19; // GPIO19 as I2C SCL Yellow

and set up the sensor by including this in my variable delcarations.
VEML6040 RGBWSensor;

Then during the setup I used,

Wire.setPins(sda_pin, scl_pin); // Set the I2C pins before begin
Wire.begin(); 

Hi Ben,

Pins on the ESP32 can be configured differently for different use cases and you are very right that some pins should be avoided as they serve specific functions.

Just some updates to your code. By defining your sda and scl pins as integers you’re allocating memory on your ESP to store that variable. Sometimes this is ok if you are planning on changing the pins in your code at some point.

For pins that you don’t plan on changing I recommend using #define. This lets the preprocessor make the changes for you when you upload your code. For most project this amount of optimisation doesn’t matter but its good to get into these habits for when you may need it.

#define sda_pin 18 // GPIO18 as I2C SDA Blue

#define scl_pin 19 // GPIO19 as I2C SCL Yellow```
1 Like

Thanks Jack - I appreciate the feedback :smiley:

1 Like