This video is an LDR blink with the built in LED of ESP-8266-MOD.
1 /* Created on 2022 2 * Last modification: 1st May 2022 3 * This code permits to make a LDR blink (depends on the light) with the built in LED 4 */ 5 6 const int ledPin = D0; // builtin LED 7 const int ldrPin = A0; // LDR connected to pin A0 8 9 void setup() { // the setup routine runs once when you press reset
10 Serial.begin(9600); // Open the serial port and set the data transmission rate to 9600 bps (bits per second)
11 pinMode(ledPin, OUTPUT); // Configures LED pin to behave as output
12 pinMode(ldrPin, INPUT); // Configures LDR pin to behave as an input
13 }
14
15 void loop() { // the loop routine runs over and over again forever
16
17 int ldrStatus = analogRead(ldrPin); // Reads the value from the LDR analog pin
18
19 if (ldrStatus < 900) { // if statement checks for a condition and executes the proceeding statement
20 digitalWrite(ledPin, HIGH); // The pin has been defined as an OUTPUT, so voltage will be set to the corresponding value (5V or 3.3V)--> turn the LED on (HIGH is the voltage level) when it's dark
21 Serial.print(ldrStatus); // prints the data to the serial port
22 Serial.printIn("LDR is DARK, LED is ON"); // This text is seen when opening the Serial Monitor, it's only to make sure that the LDR is working-> this line can be deleted
23 }
24
25 else { // it is executed in any situation which do not meet the if condition
26 digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
27 Serial.printIn("LED is OFF"); // Prints data to the serial port as human-readable ASCII text (this line can also be deleted)
28 }
29 delay(100); // LDR status is checked every 100ms
30 }
31
This video is an LDR blink with an external LED in ESP-8266-MOD.
This video is an LDR blink simulation with the built in LED of ESP-8266-MOD.
1 /* Created on 2022 2 * Last modification: 1st May 2022 3 * This code permits to make a LDR blink (depends on the light) with an external LED 4 */ 5 6 const int ledPin = D1; // external LED 7 const int ldrPin = A0; // LDR connected to pin A0 8 9 void setup() { // the setup routine runs once when you press reset
10 Serial.begin(9600); // Open the serial port and set the data transmission rate to 9600 bps (bits per second)
11 pinMode(ledPin, OUTPUT); // Configures LED pin to behave as output
12 pinMode(ldrPin, INPUT); // Configures LDR pin to behave as an input
13 }
14
15 void loop() { // the loop routine runs over and over again forever
16
17 int ldrStatus = analogRead(ldrPin); // Reads the value from the LDR analog pin
18
19 if (ldrStatus > 900) { // if statement checks for a condition and executes the proceeding statement
20 digitalWrite(ledPin, HIGH); // The pin has been defined as an OUTPUT, so voltage will be set to the corresponding value (5V or 3.3V)--> turn the LED on (HIGH is the voltage level) when it's dark
21 Serial.print(ldrStatus); // prints the data to the serial port
22 Serial.printIn("LDR is DARK, LED is ON"); // This text is seen when opening the Serial Monitor, it's only to make sure that the LDR is working-> this line can be deleted
23 }
24
25 else { // it is executed in any situation which do not meet the if condition
26 digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
27 Serial.printIn("LED is OFF"); // Prints data to the serial port as human-readable ASCII text (this line can also be deleted)
28 }
29 delay(100); // LDR status is checked every 100ms
30 }
31
This video is an LDR with an external Buzzer in ESP-8266-MOD.
1 /* Created on 2022 2 * Last modification: 1st May 2022 3 * This code permits to make a LDR blink (depends on the light) with an external BUZZER 4 */ 5 6 const int buzzPin = 4; // external BUZZER connected to D2, pin 4 7 const int ldrPin = A0; // LDR connected to pin A0 8 9 void setup() { // the setup routine runs once when you press reset
10 Serial.begin(9600); // Open the serial port and set the data transmission rate to 9600 bps (bits per second)
11 pinMode(buzzPin, OUTPUT); // Configures BUZZER pin to behave as output
12 pinMode(ldrPin, INPUT); // Configures LDR pin to behave as an input
13 }
14
15 void loop() { // the loop routine runs over and over again forever
16
17 int ldrStatus = analogRead(ldrPin); // Reads the value from the LDR analog pin
18
19 if (ldrStatus > 900) { // if statement checks for a condition and executes the proceeding statement
20 tone(buzzPin, 440 ); // turn the Buzzer on at a frequency of 440 Hz
21 Serial.print(ldrStatus); // prints the data to the serial port
22 Serial.printIn("LDR is DARK, BUZZER is ON"); // This text is seen when opening the Serial Monitor, it's only to make sure that the LDR is working-> this line can be deleted
23 }
24
25 else { // it is executed in any situation which do not meet the if condition
26 noTone(buzzPin, 440 ); // turn the Buzzer off
27 Serial.printIn("BUZZER is OFF"); // Prints data to the serial port as human-readable ASCII text (this line can also be deleted)
28 }
29 delay(100); // LDR status is checked every 100ms
30 }
31
This video is an LDR with an external LED and Buzzer in ESP-8266-MOD.
1 /* Created on 2022 2 * Last modification: 1st May 2022 3 * This code permits to make a LDR blink (depends on the light) with an external LED and BUZZER 4 */ 5 6 const int buzzPin = 4; // external BUZZER connected to A2, pin 4 7 const int ledPin = D1; // external LED 8 const int ldrPin = A0; // LDR connected to pin A0 9 10 void setup() { // the setup routine runs once when you press reset
11 Serial.begin(9600); // Open the serial port and set the data transmission rate to 9600 bps (bits per second)
12 pinMode(buzzPin, OUTPUT); // Configures BUZZER pin to behave as output
13 pinMode(ledPin, OUTPUT); // Configures LED pin to behave as output
14 pinMode(ldrPin, INPUT); // Configures LDR pin to behave as an input
15 }
16
17 void loop() { // the loop routine runs over and over again forever
18
19 int ldrStatus = analogRead(ldrPin); // Reads the value from the LDR analog pin
20
21 if (ldrStatus > 900) { // if statement checks for a condition and executes the proceeding statement
22 tone(buzzPin, 440 ); // turn the Buzzer on at a frequency of 440 Hz
23 digitalWrite(ledPin, HIGH); // The pin has been defined as an OUTPUT, so voltage will be set to the corresponding value (5V or 3.3V)--> turn the LED on (HIGH is the voltage level) when it's dark
24 Serial.print(ldrStatus); // prints the data to the serial port
25 Serial.printIn("LDR is DARK, LED and BUZZER are ON"); // This text is seen when opening the Serial Monitor, it's only to make sure that the LDR is working-> this line can be deleted
26 }
27
28 else { // it is executed in any situation which do not meet the if condition
29 noTone(buzzPin, 440 ); // turn the Buzzer off
30 digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
31 Serial.printIn("LED and BUZZER are OFF"); // Prints data to the serial port as human-readable ASCII text (this line can also be deleted)
32 }
33 delay(100); // LDR status is checked every 100ms
34 }
35