Sunday, August 3, 2014

2x BH1750FVI Digital Light Intensity Sensor Module 3V 5V

  
     These are the two BH1750FVI Light Sensors that were attached outside of the payload facing opposite directions. 




     This particular device uses the I2C communications bus and the pinout is as follows:
VCC     to 3.3V
GND    to GND
SDA     to A4
SCL     to A5
ADDR  to 3.3V or unconnected (there are two possible ID addresses)

     After experimenting with a few different codes, I came up with one that worked well enough with both sensors and did not require the use of a library (for the sake of saving sketch space). The output of these sensors using this particular code is in the unit of lux, which is is the SI unit of illuminance equivalent to one lumen per square meter. The code for simply displaying the output to the serial monitor is shown below.

#include<Wire.h>
#include<math.h>

int BH1750_Device=0x23;
int BH1750_Device2=0x5c;//Light sensor with address pin connected to 3.3V
unsigned int Lux;
unsigned int Lux2;

void setup() { 
  Wire.begin();
  Serial.begin(9600);
  Configure_BH1750();
  Serial.println("Starting");
}

void loop() {
  Lux=BH1750_Read();
  Lux2=BH1750_Read2();
  Serial.print(Lux,DEC);
  Serial.print("[lx]");
  Serial.print(",");
  Serial.print(Lux2,DEC);
  Serial.println("[lx]");
  delay(1000);
}
//////////////////////////////////
unsigned int BH1750_Read() {
  unsigned int i=0;
  Wire.beginTransmission(BH1750_Device);
  Wire.requestFrom(BH1750_Device,2);
  while (Wire.available()) {
    i <<=8; 
    i |=Wire.read();
  }
  Wire.endTransmission();
  return i/1.2;
}
////////////////////////////////////
unsigned int BH1750_Read2() {
  unsigned int i=0;
  Wire.beginTransmission(BH1750_Device2);
  Wire.requestFrom(BH1750_Device2,2);
  while (Wire.available()) {
    i <<=8; 
    i |=Wire.read();
  }
  Wire.endTransmission();
  return i/1.2;
}
/////////////////////////////////////////
void Configure_BH1750() {
  Wire.beginTransmission(BH1750_Device);
  Wire.write(0x10);
  Wire.endTransmission();
  Wire.beginTransmission(BH1750_Device2);//
  Wire.write(0x10);//
  Wire.endTransmission();//
}

     This is the code for logging this data to the SD card using the LC studio SD card reader.



//High Altitude Ballon Project DataLogger
//Mt. San Antonio College Operation Get High

#include<SD.h>
#include<Wire.h>





#define LOG_INTERVAL 1000
#define SYNC_INTERVAL 1000
uint32_t syncTime=0;



int chipSelect=10; //SD card
int BH1750_Device=0x23; //Light Sensor#1
int BH1750_Device2=0x5c; //Light Sensor#2
unsigned int Lux;
unsigned int Lux2;
unsigned long time;

File logfile;

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
  while(1);
}

void setup() {
  Serial.begin(115200);
  
  
  pinMode(10, OUTPUT);
  Configure_BH1750(); //Configure Light Sensor
  

  Serial.println("Initializing the SD card.");
  delay(100);
  //Check to see if SD card is operational
  if (!SD.begin(chipSelect)) {
    Serial.println("Initialization was unsuccessful.");
    return;
  }
  else {
    Serial.println("Initialization is complete.");
  }
    
  //Create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE);
      break; // leave the loop!
    }
  }
  
  if (! logfile) {
    error("couldnt create file");
  }
  
  Serial.print("Logging to: ");
  Serial.println(filename);
  
  if (logfile) {
    String header = "Time, Lux1, Lux2"; //Add sensor headings here
    logfile.println(header);
    Serial.println(header);
  }
  
}
  void loop() {
    delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL)); //maybe
    
    time=millis();
    Lux=BH1750_Read(); //Light Sensor data
    Lux2=BH1750_Read2(); //Light Sensor2 data

    if (logfile) {
      logfile.print(time);
      logfile.print(",");
      logfile.print(Lux,DEC);
      logfile.print(",");
      logfile.println(Lux2,DEC);
    

      Serial.print(time);
      Serial.print(",");
      Serial.print(Lux,DEC);
      Serial.print(",");
      Serial.println(Lux2,DEC);


      
      
      
  
    }
  
    else {
      Serial.println("Could not write to file.");
    }
  
  if ((millis() - syncTime) < SYNC_INTERVAL) return;
  syncTime = millis();
  

    logfile.flush();
  }
    ////////////////////////////////////////////////////////////////////////////////////
  // Code for Light Sensor
  ////////////////////////////////////////////////////////////////////////////////////
 unsigned int BH1750_Read() {  //Read Light Sensor#1
    unsigned int i=0;
    Wire.beginTransmission(BH1750_Device);
    Wire.requestFrom(BH1750_Device,2);
    while (Wire.available()) {
      i <<=8; 
      i |=Wire.read();
    }
    Wire.endTransmission();
    return i/1.2;
}

unsigned int BH1750_Read2() { //Read Light Sensor#2
  unsigned int i=0;
  Wire.beginTransmission(BH1750_Device2);
  Wire.requestFrom(BH1750_Device2,2);
  while (Wire.available()) {
    i <<=8; 
    i |=Wire.read();
  }
  Wire.endTransmission();
  return i/1.2;
}

 void Configure_BH1750() {
  Wire.beginTransmission(BH1750_Device); //Light Sensor#1
  Wire.write(0x10);
  Wire.endTransmission();
  Wire.beginTransmission(BH1750_Device2); //Light Sensor#2
  Wire.write(0x10);
  Wire.endTransmission(); 
}

     In continuous high resolution mode, these sensors have a range (units of lux) of 0 to 65,535. A table displaying typical values of lux found on wikipedia is shown below.

Examples
IlluminanceSurfaces illuminated by:
0.0001 luxMoonless, overcast night sky (starlight)[3]
0.002 luxMoonless clear night sky with airglow[3]
0.27–1.0 luxFull moon on a clear night[3][4]
3.4 luxDark limit of civil twilight under a clear sky[5]
50 luxFamily living room lights (Australia, 1998)[6]
80 luxOffice building hallway/toilet lighting[7][8]
100 luxVery dark overcast day[3]
320–500 luxOffice lighting[9][10][11]
400 luxSunrise or sunset on a clear day.
1000 luxOvercast day;[3] typical TV studio lighting
1000025000 luxFull daylight (not direct sun)[3]
32000100000 luxDirect sunlight
    The obvious downside to using these particular light sensors is that only two can be implemented into a single sensor system. I am not saying it is impossible, but I was unable to figure out how to do so. The science team determined that two would be sufficient to determine directionality of the payload during flight. That has yet to be confirmed.
     

No comments:

Post a Comment