Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

Light Sensor Module

Introduction: Light sensor modules are essential components in various electronic systems, as they allow for the detection and measurement of light levels. These modules are widely used in applications such as automatic lighting control, security systems, and energy-saving devices. In this article, we will explore the functionality and applications of light sensor modules, along with example codes and a list of components used in the examples.

Project: For this example, we will create a simple light sensing system using an Arduino board and a light sensor module. The objective is to measure the ambient light level and display it on the Arduino's serial monitor.

Components: To build this project, you will need the following components:

  • Arduino board (Uno, Nano, or any compatible board)
  • Light sensor module (BH1750, TSL2561, or any compatible module)
  • Jumper wires
  • Breadboard (optional)

You can find these components at online stores like Adafruit, SparkFun, or Amazon.

Examples: Example 1: Reading the light level

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

BH1750 lightSensor;

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

void loop() {
  float lightLevel = lightSensor.readLightLevel();
  Serial.print("Light Level: ");
  Serial.print(lightLevel);
  Serial.println(" lux");
  delay(1000);
}

Explanation:

  • We include the required libraries for the BH1750 light sensor module and the Wire library for I2C communication.
  • In the setup function, we initialize the serial communication and the light sensor module.
  • In the loop function, we read the light level using the readLightLevel() function provided by the BH1750 library.
  • We then print the light level in lux (luminous flux per unit area) to the serial monitor.
  • The delay of 1000 milliseconds (1 second) ensures that the light level is read and displayed once every second.

Example 2: Controlling an LED based on light level

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

BH1750 lightSensor;
const int ledPin = 13;

void setup() {
  Serial.begin(9600);
  lightSensor.begin();
  pinMode(ledPin, OUTPUT);
}

void loop() {
  float lightLevel = lightSensor.readLightLevel();
  Serial.print("Light Level: ");
  Serial.print(lightLevel);
  Serial.println(" lux");

  if (lightLevel < 100) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

  delay(1000);
}

Explanation:

  • In this example, we add an LED connected to pin 13 of the Arduino board.
  • We set the LED pin as an output in the setup function.
  • In the loop function, we read the light level and print it to the serial monitor, similar to Example 1.
  • We then check if the light level is below a threshold (100 lux in this case) and turn on the LED if it is, or turn it off otherwise.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.