0% found this document useful (0 votes)
11 views

IOT AND ITS APPLICATIONS ESP8266

The document provides an overview of various applications of the ESP8266 module in IoT projects, including connections and code examples for controlling LEDs, IR sensors, DHT11 temperature and humidity sensors, I2C LCDs, potentiometers, ultrasonic sensors, servo motors, and rain sensors. Each section includes wiring diagrams and code snippets for implementing the functionalities. The document serves as a practical guide for integrating these components with the ESP8266 for IoT applications.

Uploaded by

Anup Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

IOT AND ITS APPLICATIONS ESP8266

The document provides an overview of various applications of the ESP8266 module in IoT projects, including connections and code examples for controlling LEDs, IR sensors, DHT11 temperature and humidity sensors, I2C LCDs, potentiometers, ultrasonic sensors, servo motors, and rain sensors. Each section includes wiring diagrams and code snippets for implementing the functionalities. The document serves as a practical guide for integrating these components with the ESP8266 for IoT applications.

Uploaded by

Anup Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

NSTI RAMANTHAPUR

IOT AND ITS APPLICATIONS

ESP8266 :-
The ESP8266 module enables microcontrollers to connect to 2.4 GHz Wi-Fi, using IEEE
802.11 bgn. It can be used with ESP-AT firmware to provide Wi-Fi connectivity to
external host MCUs, or it can be used as a self-sufficient MCU by running an RTOS-
based SDK. The module has a full TCP/IP stack and provides the ability for data
processing, reads and controls of GPIOs.
LED TO ESP8266 :-

CODE
#define LED_PIN 01 // Use GPIO4 (D2 on NodeMCU)

void setup() {
pinMode(LED_PIN, OUTPUT); // Set LED pin as output

void loop() {
digitalWrite(LED_PIN, HIGH); // Turn LED ON
delay(500); // Wait 1 second
digitalWrite(LED_PIN, LOW); // Turn LED OFF
delay(500);
// Wait 1 second
}

2.IR SENSOR TO ESP8266 :-


CODE
#define IR_SENSOR_PIN 4 // D2 (GPIO4)
#define BUZZER_PIN 14 // D5 (GPIO14)

void setup() {
pinMode(IR_SENSOR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
int sensorValue = digitalRead(IR_SENSOR_PIN);

if (sensorValue == LOW) { // Object detected (Active LOW)


digitalWrite(BUZZER_PIN, HIGH);
Serial.println("Object Detected! Buzzer ON");
} else {
digitalWrite(BUZZER_PIN, LOW);
Serial.println("No Object Detected. Buzzer OFF");
}
delay(200); // Small delay to avoid rapid triggering
}
CONNECTIONS :
ESP8266
IR Sensor
(NodeMCU)
VCC (5V) Vin (or 3.3V)
GND GND
OUT
D2 (GPIO4)
(Digital)
ESP8266
Buzzer
(NodeMCU)
Positive
D5 (GPIO14)
(+)
Negative GND
(-)
3.DHT11 TO ESP8266 :

CONNECTIONS :
ESP8266
DHT11 Pin
(NodeMCU)
VCC (5V or
3.3V
3.3V)
GND GND
DATA D4 (GPIO2)
Between DATA and
10kΩ Resistor
VCC

CODE
#include <DHT.h>

#define DHTPIN 2 // D4 (GPIO2)


#define DHTTYPE DHT11 // Define sensor type

DHT dht(DHTPIN, DHTTYPE);

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

void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();

if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print("°C | Humidity: ");
Serial.print(humidity);
Serial.println("%");

delay(2000); // Read every 2 seconds


}
4.ESP8266 TO LCDI2C :

CONNECTIONS :

I2C LCD ESP8266


Module (NodeMCU)
VCC 3.3V or VV
(Power) (5V)
GND (Ground) GND
SDA (Serial D2
Data) (GPIO4)
SCL (Serial D1
Clock) (GPIO5)

CODE
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x27 // Change this to your detected I2C address

LiquidCrystal_I2C lcd(I2C_ADDR, 16, 2); // 16x2 LCD


void setup() {
lcd.begin();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Hello, NETHAJI!");
lcd.setCursor(0, 1);
lcd.print("I2C LCD Working");
}

void loop() {
lcd.setCursor(0, 1);
lcd.print(millis() / 1000); // Display uptime in seconds
delay(1000);
}

5.POTENTIOMETER TO ESP8266 :

CONNECTIONS :
CODE
#define POTENTIOMETER_PIN A0

float floatMap(float x, float in_min, float in_max, float out_min, float


out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) +
out_min;
}
// The setup function runs once on reset or power-up
void setup() {
// Initialize the Serial to communicate with the Serial Monitor.
Serial.begin(9600);
}
void loop() {
// read the input on analog pin:
int analog_value = analogRead(POTENTIOMETER_PIN);
// Rescale to potentiometer's voltage (from 0V to 5V):
float voltage = floatMap(analog_value, 0, 1023, 0, 5);
// print out the value you read:
Serial.print("Analog: ");
Serial.print(analog_value);
Serial.print(", Voltage: ");
Serial.println(voltage);
delay(1000);
}
6.ULTRASONIC SENSOR TO ESP8266 :
CODE
const int trigPin = 12;
const int echoPin = 14;
//define sound velocity in cm/uS
#define SOUND_VELOCITY 0.034
#define CM_TO_INCH 0.393701

long duration;
float distanceCm;
float distanceInch;

void setup() {
Serial.begin(115200); // Starts the serial communication
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in
microseconds
duration = pulseIn(echoPin, HIGH);

// Calculate the distance


distanceCm = duration * SOUND_VELOCITY/2;

// Convert to inches
distanceInch = distanceCm * CM_TO_INCH;

// Prints the distance on the Serial Monitor


Serial.print("Distance (cm): ");
Serial.println(distanceCm);
Serial.print("Distance (inch): ");
Serial.println(distanceInch);
delay(1000);
}

7.SERVO MOTOR TO ESP8266 :

CODE
#include <Servo.h>
Servo servo;
void setup() {
servo.attach(2); //D4
servo.write(0);
delay(2000);
}
void loop() {
servo.write(0);
delay(1000);

servo.write(180);
delay(1000);
}

8.RAIN SENSOR TO ESP8266 :

CODE
#define RAIN_SENSOR_PIN 4 // D2 (GPIO4)

void setup() {
Serial.begin(115200);
pinMode(RAIN_SENSOR_PIN, INPUT);
}

void loop() {
int rainState = digitalRead(RAIN_SENSOR_PIN);

if (rainState == LOW) { // Rain detected (Active LOW)


Serial.println("⚠️Rain Detected!");
} else {
Serial.println("✅ No Rain.");
}

delay(1000);
}

You might also like