Book
Book
IOT in the cloud using NodeMCU and InfraRed sensor: integrate NodeMCU with an Ultrasonic
sensor to enable Iot cloud-based functionalities.
Apparatus:
Theory: The NodeMCU ESP8266 is a powerful microcontroller board with built-in Wi-Fi
connectivity, making it an excellent choice for IoT applications. It allows for seamless integration
with various sensors and cloud platforms to collect, process, and visualize data in real time.
The Ultrasonic sensor (HC-SR04) operates on the principle of emitting high-frequency sound
waves and measuring the time taken for the echo to return after bouncing off an object. The
formula used to compute the distance is:
In this experiment, the Ultrasonic sensor acts as a data source, measuring distances and sending
the data to the NodeMCU. The NodeMCU processes this data and transmits it over Wi-Fi to a
cloud platform such as ThingSpeak or Firebase for further analysis and visualization. This setup
enables remote monitoring, making it useful for applications like object detection, obstacle
avoidance, and real-time monitoring in smart devices.
1. Real-time Data Access: Enables users to monitor data from anywhere with an internet
connection.
2. Centralized Monitoring: Allows multiple sensors to transmit data to a single cloud
platform for easy visualization.
3. Scalability: IoT systems can be easily expanded with additional devices and sensors.
Code:
#include <ESP8266WiFi.h>
#include <ThingSpeak.h>
#define TRIG_PIN D5
#define ECHO_PIN D6
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
ThingSpeak.begin(client);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
long duration, distance;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);
ThingSpeak.setField(1, distance);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(15000);
Procedure:
1. Hardware Connections:
• Connect the VCC pin of the Ultrasonic sensor to the 3.3V pin of the NodeMCU.
• Connect the GND pin of the Ultrasonic sensor to the GND pin of the NodeMCU.
• Connect the TRIG pin of the Ultrasonic sensor to D5 (GPIO14) of the NodeMCU.
• Connect the ECHO pin of the Ultrasonic sensor to D6 (GPIO12) of the NodeMCU.
2. Software Setup:
• Install the required libraries in Arduino IDE, including ESP8266 and relevant IoT
platform libraries.
• Write a program to read distance values from the Ultrasonic sensor, process them, and
upload data to the IoT platform.
• Configure the cloud platform (e.g., ThingSpeak, Firebase) to receive and visualize data.
• Upload the program to the NodeMCU and establish a Wi-Fi connection.
Verify that distance readings are successfully transmitted to the cloud and displayed on the
platform.
Precautions:
- Ensure proper power supply to the NodeMCU.
- Avoid miswiring, as it may damage components.
- Test the IR sensor in a stable environment for accurate readings.
Result: