Project 1
1.Switching ON/OFF Lights based on light intensity:
Hardware Required
- NodeMCU
- LDR
- Resistor
- LED
- Breadboard
- Micro USB cable
- Connecting Wires
LDR : Output is analog in nature, so it gets connected to the A0 pin of the NodeMCU.
LED : It is connected to D1 pin and to Ground(GND) pin of NodeMCU.
Program:
const int ledPin = 5; //Pin no 5 to led
const int ldrPin = A0;//ldr pin to analog pin
void setup()
{
Serial.begin(9600);//start the serial transmission
pinMode(ledPin, OUTPUT);//setting the ledpin to output mode
pinMode(ldrPin, INPUT);//setting ldr pin to input mode
}
void loop() {
int ldrStatus = analogRead(ldrPin);//reading the ldr value and storing it in a variable
if (ldrStatus <=300)//checking the condition if it is less than the threshold(300)
{
digitalWrite(ledPin, HIGH);//turning on the light
Serial.print(ldrStatus);//printing the value of ldr on serial monitor
Serial.println("It is DARK, LED is ON");//print a message
}
else//if condition is not satisfied
{
digitalWrite(ledPin, LOW);//turn off the light
Serial.println("LED is OFF");//print a message
}
}
2.Switching ON/OFF fan based on temperature:
Hardware Required
- NodeMCU
- Fan
- Resistor
- DHT11 sensor
- Breadboard
- Micro USB cable
- Connecting Wires
Hardware Required
- NodeMCU
- Smoke sensor
- Resistor
- Buzzer
- Breadboard
- Micro USB cable
- Connecting Wires
int redLed = 10;//components are assigned to the pins red led to pin 10,green to pin 12,buzzer to 8 and smoke sensor to A0 int greenLed = 12; int buzzer = 8; int smokeA0 = A0; // Your threshold value int sensorThres = 100; void setup()
{ pinMode(redLed, OUTPUT);//setting pin modes pinMode(buzzer, OUTPUT);//redled and green led as output pinMode(smokeA0, INPUT);//smoke sensor as input Serial.begin(9600);//serial data transmission starts at baud rate of 9600 } void loop() { int analogSensor = analogRead(smokeA0);//read the smoke sensor value // Checks if it has reached the threshold value if (analogSensor > sensorThres) { digitalWrite(redLed, HIGH); //if yes then turn the red light and turn on the buzzer digitalWrite(12, LOW); tone(buzzer, 1000, 200); } else { digitalWrite(redLed, LOW);//if no then turn on the green light and turn off the buzzer digitalWrite(12, HIGH); noTone(buzzer); } delay(500); }