-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLight_Sensor.ino
More file actions
126 lines (49 loc) · 3.43 KB
/
Copy pathLight_Sensor.ino
File metadata and controls
126 lines (49 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <Wire.h> // I2C Library
#include <BH1750.h> // BH1750 (Light Sensor) Library
BH1750 lightMeter; // Initialize the Sensor
const int led0 = 2; // Initialize the Arduino Digital Pin 2 for LED 0
const int led1 = 3; // Initialize the Arduino Digital Pin 3 for LED 1
const int led2 = 4; // Initialize the Arduino Digital Pin 4 for LED 2
const int led3 = 5; // Initialize the Arduino Digital Pin 5 for LED 3
/* The setup() function is called when a sketch starts. It is used to initialize variables, pin modes, start using libraries, etc. This function will only run once, after each power up or reset of the Arduino board */
void setup()
{
Serial.begin(9600); // Init Baud/Serial Rate
// 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);
lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE); // Select the working
Serial.println(F("BH1750 One-Time Test")); // Print the value of Sensor One-Time
pinMode(led0, OUTPUT); // Arduino Digital Pin 2 (LED 0) acts as Output Pin
pinMode(led1, OUTPUT); // Arduino Digital Pin 3 (LED 1) acts as Output Pin
pinMode(led2, OUTPUT); // Arduino Digital Pin 4 (LED 2) acts as Output Pin
pinMode(led3, OUTPUT); // Arduino Digital Pin 5 (LED 3) acts as Output Pin
}
/* This Particular Function is used for Repeated Execution of the Circuit until Specified. */
void loop()
{
uint16_t lux = lightMeter.readLightLevel(); // Get the Lux Value from LED 0
uint16_t lux1 = lightMeter.readLightLevel(); // Get the Lux Value from LED 1
uint16_t lux2 = lightMeter.readLightLevel(); // Get the Lux Value from LED 2
uint16_t lux3 = lightMeter.readLightLevel(); // Get the Lux Value from LED 3
Serial.print("Light: "); // Print Light:
Serial.print(lux); // Print lux Value
Serial.println(" lx"); // Print lx in Next Line
delay(100); // Hold the program by 100 ms
if (lux < 70) // LED 0 will be High when Lux Value exceed 70 Luxens
digitalWrite(led0, HIGH);
else // LED 0 will be Low if Lux Value is less than 70 Luxens
digitalWrite(led0, LOW);
if (lux1 < 50) // LED 1 will be High when Lux Value exceed 50 Luxens
digitalWrite(led1, HIGH);
else // LED 1 will be Low when Lux Value is less than 50 Luxens
digitalWrite(led1, LOW);
if (lux2 < 25) // LED 2 will be High when Lux Value exceed 25 Luxens
digitalWrite(led2, HIGH);
else // LED 2 will be Low when Lux Value is less than 25 Luxens
digitalWrite(led2, LOW);
if (lux3 < 10) // LED 3 will be High when Lux Value exceed 10 Luxens
digitalWrite(led3, HIGH);
else // LED 3 will be Low when Lux Value is less than 10 Luxens
digitalWrite(led3, LOW);
}