Need help for using the TSL2591 sensor

Hello
Here I am a beginner on Arduino and it has been three weeks since I tried to operate an LED with the TSL2591 Light sensor so that when the brightness drops below a certain value the LED lights up. I have tried everything and its dont work. I think my problem is rather simple to solve but I cannot compare the value given by the sensor to the value I give for the IF loop

Thank you for your help

For informed help, please read and follow the instructions in the "How to get the best out of this forum" post, at the head of every topic.

Post the code, using code tags and explain what you expected to happen, and what happened instead.

I am having a problem with my eyes, I cannot see your schematic, it was a schematic and not a frizzy thing I hope. Please post it that will help us help you. When yo do that do not forget to post the code.

here the code normaly when the light level is under the thresholdvalue the led have switch on

#include <Adafruit_Sensor.h>

#include <Adafruit_TSL2591.h>

int tempo=1;
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_TSL2591.h"
int thresholdvalue=400;
const int pinLed = 13;
const int pinlight = SDA;

Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591);

void displaySensorDetails(void)
{
  sensor_t sensor;
  tsl.getSensor(&sensor);
  Serial.println(F("------------------------------------"));
  Serial.print  (F("Sensor:       ")); Serial.println(sensor.name);
  Serial.print  (F("Driver Ver:   ")); Serial.println(sensor.version);
  Serial.print  (F("Unique ID:    ")); Serial.println(sensor.sensor_id);
  Serial.print  (F("Max Value:    ")); Serial.print(sensor.max_value); Serial.println(F(" lux"));
  Serial.print  (F("Min Value:    ")); Serial.print(sensor.min_value); Serial.println(F(" lux"));
  Serial.print  (F("Resolution:   ")); Serial.print(sensor.resolution, 4); Serial.println(F(" lux"));  
  Serial.println(F("------------------------------------"));
  Serial.println(F(""));
  delay(500);
}

void configureSensor(void)
{
  // You can change the gain on the fly, to adapt to brighter/dimmer light situations
  //tsl.setGain(TSL2591_GAIN_LOW);    // 1x gain (bright light)
  tsl.setGain(TSL2591_GAIN_MED);      // 25x gain
  //tsl.setGain(TSL2591_GAIN_HIGH);   // 428x gain
  

  //tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS);  
  // tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS);
  tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
  // tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
  // tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
  // tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS);  


  Serial.println(F("------------------------------------"));
  Serial.print  (F("Gain:         "));
  tsl2591Gain_t gain = tsl.getGain();
  switch(gain)
  {
    case TSL2591_GAIN_LOW:
      Serial.println(F("1x (Low)"));
      break;
    case TSL2591_GAIN_MED:
      Serial.println(F("25x (Medium)"));
      break;
    case TSL2591_GAIN_HIGH:
      Serial.println(F("428x (High)"));
      break;
    case TSL2591_GAIN_MAX:
      Serial.println(F("9876x (Max)"));
      break;
  }
  Serial.print  (F("Timing:       "));
  Serial.print((tsl.getTiming() + 1) * 100, DEC); 
  Serial.println(F(" ms"));
  Serial.println(F("------------------------------------"));
  Serial.println(F(""));
}



void setup(void) 
{
  Serial.begin(9600);
  
  Serial.println(F("Starting Adafruit TSL2591 Test!"));
  
  if (tsl.begin()) 
  {
    Serial.println(F("Found a TSL2591 sensor"));
  } 
  else 
  {
    Serial.println(F("No sensor found ... check your wiring?"));
    while (1);
  }
    

  displaySensorDetails();
  

  configureSensor();

{
    pinMode(4, OUTPUT); 
    pinMode(5, OUTPUT);
    pinMode(pinLed, OUTPUT);
}

}


void advancedRead(void)
{
  uint32_t lum = tsl.getFullLuminosity();
  uint16_t ir, full;
  ir = lum >> 16;
  full = lum & 0xFFFF;
  Serial.print(F("[ ")); Serial.print(millis()); Serial.print(F(" ms ] "));
  Serial.print(F("IR: ")); Serial.print(ir);  Serial.print(F("  "));
  Serial.print(F("Full: ")); Serial.print(full); Serial.print(F("  "));
  Serial.print(F("Visible: ")); Serial.print(full - ir); Serial.print(F("  "));
  Serial.print(F("Lux: ")); Serial.println(tsl.calculateLux(full, ir), 6); 
  int analogValue = tsl.getFullLuminosity();
}

void loop() 
{
  int sensorValue = analogRead(pinlight);
    delay(1000);
    
  if(analogValue < ANALOG_THRESHOLD)
  {
      digitalWrite(LED, HIGH);
      delay(1000);
      digitalWrite(4, HIGH); 
      for (int i=0; i <= 200; i++)
      {
        digitalWrite(5, LOW);
        delay(tempo); 
        digitalWrite(5, HIGH); 
        delay(tempo);
      }
  }
  else
  {
    digitalWrite(LED, LOW);
    delay(1000);

    digitalWrite(4, LOW); 
    for (int i=0; i <= 200; i++)
    {
        digitalWrite(5, LOW);
        delay(tempo); 
        digitalWrite(5, HIGH); 
        delay(tempo);
    }

  }
}

That code won't even compile, because analogValue is not defined for the loop function.

void loop() 
{
  int sensorValue = analogRead(pinlight);
    delay(1000);
    
  if(analogValue < ANALOG_THRESHOLD)
  {

Start with one of the example programs and make sure you understand how it works before making any changes.

Hint: the function advancedRead() is a bit useless, because it prints stuff but returns no values.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.