Arduino - Fényerősségmérés (fotóellenálás, BH1750, MAX44009)

Fotóellenállás


Fényerősségre változó ellenállás. Ha nő a fény, akkor növekszik a vezetőképesség is, így az ellenállás pedig csökken. Egy lehetséges bekötés (az ellenállás 10 kOhm):

Példakód:

const int sensorPin = A0;
int sensorValue;

void setup() {
  Serial.begin(9600)
}

void loop() {
  sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue);
  delay(1000);
}

BH1750

Ez a digitális eszköz I2C busz kommunikációt használ, valamint nagy felbontással és széles mérési tartománnyal (1-65535 Lux) rendelkezik. Alacsony energiafelhasználású. Felbontása szerint két címmel is rendelkezik:

  1. 0x5C: nagy felbontás, ADD magas szintre, felbontás: 1 Lux
  2. 0x23: alacsony felbontás, ADD alacsony (GND) szintre, felbontás: 4 Lux
Üzemi feszültség 3.3 V!

Példakód (le kell tölteni hozzá a BH1750 library-t):


#include <BH1750FVI.h> // Sensor Library
#include <Wire.h> // I2C Library

BH1750FVI LightSensor;


void setup() {   // put your setup code here, to run once:
   Serial.begin(9600);
  LightSensor.begin();
  /*
Set the address for this sensor
you can use 2 different address
Device_Address_H "0x5C"
Device_Address_L "0x23"
you must connect Addr pin to A3 .
*/
  LightSensor.SetAddress(Device_Address_H);//Address 0x5C
// To adjust the slave on other address , uncomment this line
// lightMeter.SetAddress(Device_Address_L); //Address 0x5C
//-----------------------------------------------
  /*
   set the Working Mode for this sensor
   Select the following Mode:
    Continuous_H_resolution_Mode
    Continuous_H_resolution_Mode2
    Continuous_L_resolution_Mode
    OneTime_H_resolution_Mode
    OneTime_H_resolution_Mode2
    OneTime_L_resolution_Mode
   
    The data sheet recommanded To use Continuous_H_resolution_Mode
  */

  LightSensor.SetMode(Continuous_H_resolution_Mode);
  Serial.println("Running...");
}


void loop() {
  // put your main code here, to run repeatedly:
  uint16_t lux = LightSensor.GetLightIntensity();// Get Lux value
  Serial.print("Light: ");
  Serial.print(lux);
  Serial.println(" lux");
  delay(1000);
}

MAX44009 (GY-49)

Szintén I2C kommunikációt használ 22 bites felbontással. A mérési tartománya 0.045 - 188000 Lux, áramfelvétele pedig csupán 1uA!! Fontos még, hogy ez a modul kiszűri az IR és UV sugárzást is.

Példakód:

#include<Wire.h>

#define Addr 0x4A

void setup()
{

  Wire.begin();
  // Initialise serial communication
  Serial.begin(9600);

  Wire.beginTransmission(Addr);
  Wire.write(0x02);
  Wire.write(0x40);
  Wire.endTransmission();
  delay(300);
}

void loop()
{
  unsigned int data[2];
  Wire.beginTransmission(Addr);
  Wire.write(0x03);
  Wire.endTransmission();

  // Request 2 bytes of data
  Wire.requestFrom(Addr, 2);

  // Read 2 bytes of data luminance msb, luminance lsb
  if (Wire.available() == 2)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
  }

  // Convert the data to lux
  int exponent = (data[0] & 0xF0) >> 4;
  int mantissa = ((data[0] & 0x0F) << 4) | (data[1] & 0x0F);
  float luminance = pow(2, exponent) * mantissa * 0.045;

  Serial.print("Ambient Light luminance :");
  Serial.print(luminance);
  Serial.println(" lux");
  delay(500);
}


Megjegyzések