Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
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:
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:
readLightLevel()
function provided by the BH1750 library.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: