In acest articol vom folosi senzorul BH1750 (GY-302) pentru a detecta nivelul de luminozitate a mediului ambiant.

Modulul se alimenteaza cu o tensiune de 3.3V si conexiunile cu Arduino se fac pe portul I2C.

Pentru a folosi acest modul va trebui sa instalam libraria BH1750. Pentru acest lucru vom accesa in Arduino IDE meniul Schita > Incarcare biblioteca > Gestionare biblioteci. In fereastra care se va deschide vom tasta in casuta de cautare BH1750 si vom selecta libraria dorita (de obicei primul rezultat este cel cautat).

O poza cu modulul folosi este mai jos:

senzor luminozitate bh1750

 

Codul sursa pentru a testa modulul este foarte simplu, poate fi gasit si ca exemplu oferit de catre aceasta librarie si este cel de mai jos:

/*

  Example of BH1750 library usage.

  This example initialises the BH1750 object using the default high resolution
  continuous mode and then makes a light level reading every second.

  Connection:

    VCC -> 3V3 or 5V
    GND -> GND
    SCL -> SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on esp8266 free selectable)
    SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on esp8266 free selectable)
    ADD -> (not connected) or GND

  ADD pin is used to set sensor I2C address. If it has voltage greater or equal to
  0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be
  0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will
  be 0x23 (by default).

*/


#include <Wire.h>
#include <BH1750.h>

BH1750 lightMeter;


void setup(){

  Serial.begin(9600);

  // Initialize the I2C bus (BH1750 library doesn't do this automatically)
  Wire.begin();
  // On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3);
  // For Wemos / Lolin D1 Mini Pro and the Ambient Light shield use Wire.begin(D2, D1);

  lightMeter.begin();

  Serial.println(F("BH1750 Test begin"));

}


void loop() {

  float lux = lightMeter.readLightLevel();
  Serial.print("Light: ");
  Serial.print(lux);
  Serial.println(" lx");
  delay(1000);

}

Rezultatele se pot observa deschizand terminalul serial.

Nu sunt comentarii

Acest website foloseste cookie-uri pentru o experienta placuta in timpul navigarii.