Using a LTR-329 Ambient Light sensor with an ESP8266

In this article we look at an Ambient Light sensor, the LTR-329 and connect it to an ESP8266 board – a Wemos D1 Mini pro was what I had at hand

This sensor is very similar to the LTR-303 one we looked at in a previous article

Sensor Information

The LTR-329 provides 16-bit light measurements for both Infrared and Visible+IR spectrums. Subtract one from the other to get human-visible light. Thanks to configurable gain and integration time settings, the LTR-329 can measure from 0 to 65K+ lux.

I bought an Adafruit breakout board, so the LTR-329 comes integrated with a voltage regulator and level-shifting circuitry to allow it to be used with 3.3V devices or 5V devices.

Along with a header, there is a SparkFun Qwiic compatible STEMMA QT connectors for the I2C bus so you just need to fit a suitable cable

Features

I2C interface with up to Fast Mode @ 400kbit/s with address 0x29
Ultra-small 4-pin ChipLED package 2.0mm(L), 2.0mm(B), 0.7mm(H)
Built-in temperature compensation circuit
Low active power consumption with standby mode
Supply voltage range from 2.4V to 3.6V capable of 1.7V logic voltage
Operating temperature range from -30C to +70C
RoHS and Halogen free compliant
Close to human eye spectral response
Immunity to IR / UV Light Source
Automatically rejects 50 / 60 Hz lightings flicker
Full dynamic range from 0.01 lux to 64k lux
16-bit effective resolution

Parts Required

You can connect to the sensor using dupont style jumper wire.

Name Link
Arduino Uno Aliexpress Link
Ebay Link
Amazon Link
LTR329 https://learn.adafruit.com/products/5591/guides
Connecting cables Aliexpress Link
Ebay Link
Amazon Link

Schematic/Connection

I used 3.3v from the Wemos Mini Pro – 5v should be ok as the adafruit sensor has a voltage regulator on it.

 

Code Example

I installed the Adafruit library using the Arduino ide which also installed the dependency library

Click the Manage Libraries … menu item, search for LTR329_LTR303, and select the Adafruit LTR329 LTR303 library

If asked about dependencies, click on the “Install all” option

If the Dependencies window does appear, you already have the required dependencies installed.

If the dependencies are already installed, make sure that you have the latest ones installed using the Arduino Library Manager

This is the default ltr329_simpletest example with the delay set to 1000ms rather than 100ms which seemed a bit too short a delay for simple testing. I also removed code from the setup routine which basically displayed the settings that you are able to change in the setup.

These are

ltr.setGain(LTR3XX_GAIN_2);
ltr.setIntegrationTime(LTR3XX_INTEGTIME_100);
ltr.setMeasurementRate(LTR3XX_MEASRATE_200);

#include "Adafruit_LTR329_LTR303.h"

Adafruit_LTR329 ltr = Adafruit_LTR329();

void setup() {
  Serial.begin(115200);
  Serial.println("Adafruit LTR-329 advanced test");

  if ( ! ltr.begin() ) {
    Serial.println("Couldn't find LTR sensor!");
    while (1) delay(10);
  }
  Serial.println("Found LTR sensor!");

  ltr.setGain(LTR3XX_GAIN_2);
  ltr.setIntegrationTime(LTR3XX_INTEGTIME_100);
  ltr.setMeasurementRate(LTR3XX_MEASRATE_200);

}

void loop() {
  bool valid;
  uint16_t visible_plus_ir, infrared;

  if (ltr.newDataAvailable()) {
    valid = ltr.readBothChannels(visible_plus_ir, infrared);
    if (valid) {
      Serial.print("CH0 Visible + IR: ");
      Serial.print(visible_plus_ir);
      Serial.print("\t\tCH1 Infrared: ");
      Serial.println(infrared);
    }
  }

  delay(1000);
}

 

Output

Here is  an example of what I saw in the serial monitor window – you may see some different results

This was covering and uncovering the sensor

CH0 Visible + IR: 2 CH1 Infrared: 2
CH0 Visible + IR: 7 CH1 Infrared: 1
CH0 Visible + IR: 125 CH1 Infrared: 57
CH0 Visible + IR: 176 CH1 Infrared: 85
CH0 Visible + IR: 16 CH1 Infrared: 10
CH0 Visible + IR: 16 CH1 Infrared: 10

Links

https://learn.adafruit.com/products/5591/guides