0% found this document useful (0 votes)
17 views22 pages

IoT Lab Experiments for CSE Students

Uploaded by

heddie82
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views22 pages

IoT Lab Experiments for CSE Students

Uploaded by

heddie82
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Government Engineering College, Khagaria

(Dept. of Science & Technology, Govt. of Bihar)

LAB FILE: IoT Architecture & Design


PAPER CODE: 155501P

Submitted by: Submitted to:


Name : Sohan Kumar Das Mr. Nityanand Kumar
Reg. no : 22155154019 (Asst. Professor)
Branch : CSE(IOT) Dept. of CSE

Signature:
INDEX
Subject: IoT Architecture & Design Lab Code : 155501P

Semester: 5th Discipline : CSE(IoT)

S. No. Details of Experiments (s) Faculty Signature

Introduction to IoT Platforms: Setting Up Arduino, Raspberry Pi, and


NodeMCU
1.
a) Familiarization with hardware, software installation, and basic
connectivity
Interfacing Ultrasonic Sensor for Distance Measurement Using Arduino,
Raspberry Pi, and NodeMCU
2. a) Measure distance and display the data on a serial monitor

IoT-Based LED Control Using Arduino, Raspberry Pi, and NodeMCU


3. a) Implement LED control through GPIO pins and cloud-based
control.

Temperature Monitoring and Notification System Using LM35 and NodeMCU


4. a) Measure temperature and send alerts via IoT cloud services

Intrusion Detection System Using Ultrasonic Sensor and Arduino


5. a) Detect motion and trigger an alarm for security applications.

IoT-Based Smart Lighting System Using LDR and Arduino


6. a) Implement an automatic night lamp based on ambient light
conditions.
Air Quality Monitoring Using MQ135 Sensor and IoT Cloud Integration
a) Measure pollution levels and display data on an LCD or IoT
7.
dashboard.

Wi-Fi-Based IoT Connectivity: Connecting Arduino to Available


Networks
8.
a) Establish a Wi-Fi connection and send sensor data to an IoT cloud
platform.
Bluetooth-Based Home Automation Using Arduino and Relay Module
a) Control home appliances using a mobile app and Bluetooth
9.
module.

Smart Agriculture: Sensor-Based Monitoring Using NodeMCU.


10. a) Implement soil moisture and environmental sensors for precision
farming.s
Experiment 1: Introduction to IoT Platforms: Setting Up Arduino,
Raspberry Pi, and NodeMCU
a. Familiarization with hardware, software installation, and basic connectivity

Aim: To get familiar with the hardware, software installation, and basic connectivity of
popular IoT platforms: Arduino, Raspberry Pi, and NodeMCU.

Components Required:

• Hardware: Arduino UNO, Raspberry Pi (any model), NodeMCU (ESP8266), USB


cables, Breadboard, LEDs, 220Ω resistors, Jumper wires.
• Software: Arduino IDE, Python 3, Raspberry Pi Imager.

Theory:

• Arduino UNO: A microcontroller board based on the ATmega328P. It is ideal for


simple, repetitive tasks like reading sensors and controlling motors. It is programmed
using the Arduino IDE with a simplified version of C++.

• Raspberry Pi: A single-board computer (SBC) that runs a full-fledged operating


system (like Raspberry Pi OS, a Debian-based Linux distribution). It has a powerful
processor, RAM, and graphics capabilities, making it suitable for complex tasks like
image processing, running a web server, and multitasking. It is typically programmed
using Python.

• NodeMCU (ESP8266): A low-cost microcontroller with built-in Wi-Fi capabilities. It


is highly popular for IoT projects due to its integrated wireless connectivity. It can be
programmed using the Arduino IDE or other platforms like MicroPython.

Procedure:
1. Arduino UNO Setup:
• Download and install the Arduino IDE from the official website.
• Connect the Arduino UNO to the computer via USB.
• In the IDE, go to Tools > Board and select "Arduino Uno".
• Go to Tools > Port and select the COM port to which the Arduino is connected.
• Open the "Blink" example sketch from File > Examples > [Link] > Blink.
Click the "Upload" button to compile and upload the code to the board.

2. NodeMCU Setup:
• In the Arduino IDE, go to File > Preferences.
• Add the ESP8266 board manager URL to "Additional Boards Manager
URLs": [Link]
• Go to Tools > Board > Boards Manager, search for "esp8266" and install it.
• Select "NodeMCU 1.0 (ESP-12E Module)" from the Tools > Board menu.
• Connect the NodeMCU and select the correct COM port.
• Upload the "Blink" sketch. Note that the built-in LED is usually on pin D0
(GPIO16) or D4 (GPIO2).
3. Raspberry Pi Setup:
• Use the Raspberry Pi Imager to flash Raspberry Pi OS onto a microSD card.
• Insert the card into the Raspberry Pi, connect peripherals (monitor, keyboard,
mouse), and power it on.
• Complete the initial setup.
• Open the Thonny Python IDE (pre-installed).
• Write a Python script to blink an external LED connected to a GPIO pin.

Arduino/NodeMCU Blink Sketch:


void setup() { C++
pinMode(LED_PIN, OUTPUT);
}

void loop() {
digitalWrite(LED_PIN, HIGH); // turn the LED on
delay(1000);
digitalWrite(LED_PIN, LOW); // turn the LED off
delay(1000);
}

Raspberry Pi Blink Script (Python):


import [Link] as GPIO Python
import time

LED_PIN = 17
[Link]([Link])
[Link](LED_PIN, [Link])

try:
while True:
[Link](LED_PIN, [Link])
[Link](1)
[Link](LED_PIN, [Link])
[Link](1)
except KeyboardInterrupt:
[Link]()

Result: The LEDs connected to each board blinked with a one-second interval, confirming
the environments are set up correctly.

Conclusion: Successfully configured the development environments for Arduino,


NodeMCU, and Raspberry Pi and verified their basic functionality.
Experiment 2: Interfacing Ultrasonic Sensor for Distance Measurement
Using Arduino, Raspberry Pi, and NodeMCU
a. Measure distance and display the data on a serial monitor.

Aim: To interface an HC-SR04 ultrasonic sensor with a NodeMCU to measure distance and
display the reading on the serial monitor.

Components Required:
• Hardware: NodeMCU, HC-SR04 Ultrasonic Sensor, Breadboard, Jumper wires.
• Software: Arduino IDE.

Theory: The HC-SR04 ultrasonic sensor measures distance by emitting an ultrasonic pulse
and measuring the time it takes for the echo to return. The distance is calculated using the
formula: Distance = (Echo Time × Speed of Sound) / 2. The NodeMCU's GPIO pins are
used to trigger the sensor and read the echo pulse duration.

Procedure:

1. Assemble the circuit, connecting the sensor's VCC, GND, Trig, and Echo pins to the
NodeMCU.
2. Write the code in the Arduino IDE to trigger the sensor, measure the pulse duration
from the Echo pin using pulseIn(), and convert it to distance.
3. Upload the code and open the Serial Monitor to observe the distance readings.

Code:
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
[Link](9600);
}
void loop() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

long duration = pulseIn(ECHO_PIN, HIGH);


int distance = duration * 0.034 / 2; // Calculate distance in cm

[Link]("Distance: ");
[Link](distance);
[Link](" cm");
delay(500);
}

Result: The serial monitor displayed the distance of an object from the sensor in centimeters.

Conclusion: Successfully interfaced an HC-SR04 ultrasonic sensor with a NodeMCU to


perform accurate distance measurements.
Experiment 3: IoT-Based LED Control Using Arduino, Raspberry Pi, and
NodeMCU
a. Implement LED control through GPIO pins and cloud-based control.
Aim: To control an LED connected to a NodeMCU from a cloud-based dashboard using an
IoT platform.

Components Required:
• Hardware: NodeMCU, LED, 220Ω resistor, Breadboard, Jumper wires.
• Software: Arduino IDE, Blynk IoT Platform account.

Theory: IoT platforms like Blynk allow remote control of hardware over the internet. The
NodeMCU connects to your Wi-Fi and the Blynk server using an authentication token. A
widget (like a button) on the Blynk dashboard can send commands to the NodeMCU to
control its GPIO pins, thereby turning the LED on or off.

Procedure:

1. Blynk Setup: Create a Blynk account and a new device template. Note the template
ID and authentication token. Add a "Switch" widget assigned to a Virtual Pin (e.g.,
V0).
2. Hardware & Code: Assemble the LED circuit. In the Arduino IDE, install the Blynk
library. Write the code using your Wi-Fi credentials and Blynk token.
3. Execution: Upload the code. Once the NodeMCU connects to Blynk, use the switch
on the dashboard to control the LED.
Code:
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = BLYNK_AUTH_TOKEN;


char ssid[] = "YOUR_WIFI_SSID";
char pass[] = "YOUR_WIFI_PASSWORD";
#define LED_PIN D5 // GPIO14

BLYNK_WRITE(V0) { // Function executes when V0 widget changes


int pinValue = [Link]();
digitalWrite(LED_PIN, pinValue);
}
void setup() {
[Link](9600);
pinMode(LED_PIN, OUTPUT);
[Link](auth, ssid, pass);
}
void loop() {
[Link]();
}

Result: The LED was successfully controlled remotely from the Blynk web dashboard.

Conclusion: Demonstrated a fundamental IoT application by implementing cloud-based


control of a hardware component.
Experiment 4: Temperature Monitoring and Notification System Using
LM35 and NodeMCU
a. Measure temperature and send alerts via IoT cloud services.

Aim:
To measure temperature using an LM35 sensor with a NodeMCU and send alerts via IoT
cloud services.

Components Required:
• Hardware: NodeMCU, LM35 Temperature Sensor, Breadboard, Jumper wires.
• Software: Arduino IDE, Blynk IoT Platform.

Theory:
The LM35 is an analog temperature sensor whose output voltage is linearly proportional to
the temperature in Celsius (10mV per degree). The NodeMCU's Analog-to-Digital Converter
(ADC) reads this voltage. The reading is then converted to a temperature value and can be
sent to an IoT platform like Blynk to trigger a notification if it exceeds a certain threshold.

Procedure:
1. Connect the LM35 sensor's VCC, GND, and Vout pins to the NodeMCU.
2. Set up a new device and a "Notification" widget in your Blynk project.
3. Write code to read the analog value from the LM35, convert it to Celsius, and send it
to Blynk.
4. Add logic to trigger [Link]() when the temperature crosses a set limit.
5. Upload the code and test by warming the sensor.

Code:
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = BLYNK_AUTH_TOKEN;


char ssid[] = "YOUR_WIFI_SSID";
char pass[] = "YOUR_WIFI_PASSWORD";

#define LM35_PIN A0 // Analog pin A0


#define TEMP_THRESHOLD 30.0 // Alert threshold in Celsius
void setup() {
[Link](9600);
[Link](auth, ssid, pass);
}
void loop() {
int adcVal = analogRead(LM35_PIN);
float voltage = adcVal * (3.3 / 1024.0);
float temperatureC = voltage * 100.0;

[Link]("Temperature: ");
[Link](temperatureC);
[Link](" C");

[Link](V1, temperatureC); // Send temp to a display widget on V1

if (temperatureC > TEMP_THRESHOLD) {


[Link]("High Temperature Alert!");
}
[Link]();
delay(2000);
}

Result:
The system displayed the current temperature on the serial monitor and sent a push
notification to the Blynk app when the temperature exceeded 30°C.

Conclusion:
Successfully built an IoT-based temperature monitoring and notification system using a
NodeMCU and the Blynk platform.
Experiment 5: Intrusion Detection System Using Ultrasonic Sensor and
Arduino
a. Detect motion and trigger an alarm for security applications.

Aim:
To detect motion using an ultrasonic sensor with an Arduino and trigger a local alarm.

Components Required:
• Hardware: Arduino UNO, HC-SR04 Ultrasonic Sensor, Active Buzzer, Breadboard,
Jumper wires.
• Software: Arduino IDE.

Theory:
This system uses the HC-SR04 sensor to continuously monitor the distance to the nearest
object. A "safe" distance is established. If an object (an "intruder") enters this range and the
measured distance drops below a predefined threshold, the Arduino triggers an alarm by
activating a buzzer.

Procedure:

1. Assemble the Hardware Circuit:


• Place the Arduino, HC-SR04, and buzzer on the breadboard.
• Connect VCC of HC-SR04 to 5V on Arduino, GND to GND.
• Connect Trig to digital pin 9, Echo to pin 10.
• Connect the positive pin of the active buzzer to pin 7, negative to GND.
• Verify connections with a multimeter if available to ensure no shorts.

2. Prepare the Software:


• Open Arduino IDE and select Tools > Board > Arduino AVR Boards > Arduino
Uno.
• Select the correct port.

3. Write and Upload the Code:


• Copy the code into a new sketch.
• Adjust ALARM_DISTANCE if needed (e.g., based on your testing area).
• Verify and upload.
4. Test the System:
• Open Serial Monitor (baud 9600) to see distance readings.
• Position the sensor facing an open area and note the baseline distance.
• Move an object closer than the threshold; the buzzer should activate.
• Move it away; the buzzer should stop.
• Troubleshoot: If no sound, check buzzer polarity or use tone() function for
passive buzzers.

Code:
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
[Link](9600);
}

void loop() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

long duration = pulseIn(ECHO_PIN, HIGH);


int distance = duration * 0.034 / 2;

[Link]("Distance: ");
[Link](distance);

if (distance < ALARM_DISTANCE && distance > 0) {


digitalWrite(BUZZER_PIN, HIGH); // Turn buzzer on
} else {
digitalWrite(BUZZER_PIN, LOW); // Turn buzzer off
}
delay(200);
}

Result: The buzzer sounded whenever an object entered the 50 cm range of the sensor.

Conclusion: A simple but effective intrusion detection system was created using an Arduino
and an ultrasonic sensor.
Experiment 6: IoT-Based Smart Lighting System Using LDR and Arduino
a. Implement an automatic night lamp based on ambient light conditions.

Aim: To implement an automatic night lamp using an LDR and an Arduino.

Components Required:
• Hardware: Arduino UNO, LDR (Light Dependent Resistor), 10kΩ Resistor, LED,
Breadboard, Jumper wires.
• Software: Arduino IDE.

Theory:
An LDR is a variable resistor whose resistance decreases as the intensity of light falling on it
increases. By using it in a voltage divider circuit with a fixed resistor, we can create a
voltage that changes with the ambient light level. The Arduino's ADC reads this voltage, and
if it falls below a certain threshold (indicating darkness), it turns on an LED.

Procedure:
1. Assemble the Voltage Divider Circuit:
• Connect one leg of the LDR to 5V on Arduino.
• Connect the other leg of the LDR to one end of the 10kΩ resistor and to analog
pin A0.
• Connect the other end of the 10kΩ resistor to GND.
• Connect the LED: Anode to digital pin 8 via a 220Ω resistor (optional for
protection), cathode to GND.

2. Prepare the Software:


• Open Arduino IDE, select Arduino Uno board and port

3. Write and Upload the Code:


• Copy the code into a new sketch.
• Calibrate LIGHT_THRESHOLD: Run a test with Serial Monitor to read LDR
values in light/dark conditions and set the threshold midway.
• Verify and upload.
4. Test the System:
• Open Serial Monitor to view LDR readings.
• Cover the LDR (simulate darkness); the LED should turn on.
• Expose to light; the LED should turn off.
• Troubleshoot: If unresponsive, adjust threshold or check resistor values.

Code:
void setup() {
pinMode(LED_PIN, OUTPUT);
[Link](9600);
}

void loop() {
int ldrStatus = analogRead(LDR_PIN);
[Link]("LDR Value: ");
[Link](ldrStatus);

if (ldrStatus < LIGHT_THRESHOLD) {


digitalWrite(LED_PIN, HIGH); // It's dark, turn LED on
} else {
digitalWrite(LED_PIN, LOW); // It's light, turn LED off
}
delay(100);
}

Result: The LED turned on automatically when the room was darkened and turned off when
it was lit.

Conclusion: Successfully created a smart lighting system that responds automatically to


ambient light conditions.
Experiment 7: Air Quality Monitoring Using MQ135 Sensor and IoT Cloud
Integration
a. Measure pollution levels and display data on an LCD or IoT dashboard.

Aim: To measure pollution levels using an MQ135 sensor with a NodeMCU and display
data on an IoT dashboard.

Components Required:
• Hardware: NodeMCU, MQ135 Gas Sensor Module, Breadboard, Jumper wires.
• Software: Arduino IDE, ThingSpeak or Blynk IoT Platform.

Theory: The MQ135 is a semiconductor sensor for air quality. It is sensitive to various
gases, including smoke, CO2, and alcohol. The sensor module provides an analog output
voltage that corresponds to the concentration of these gases. The NodeMCU reads this
analog value, which can be interpreted as a general air quality index and sent to an IoT
platform like ThingSpeak for logging and visualization.

Procedure:
1. Assemble the Hardware Circuit:
• Connect VCC of MQ135 to 5V (VIN) on NodeMCU, GND to GND, and analog
out (A0) to A0 on NodeMCU.
• Power on the setup and let the sensor preheat for 5-10 minutes (it may smell
slightly during initial burn-in).

2. Set Up ThingSpeak Account:


• Go to [Link] and create a free account.
• Create a new channel, enable Field 1 for air quality data, and note the Write API
Key.

3. Write and Upload the Code:


• In Arduino IDE, select NodeMCU board and port.
• Copy the code, replacing placeholders with your Wi-Fi and API details.
• Add Wi-Fi connection logic in setup (e.g., while([Link]() !=
WL_CONNECTED) { delay(500); }).
• Verify and upload.
4. Test and Monitor:
• Open Serial Monitor to see raw readings.
• Expose the sensor to smoke or breath (for CO2) and check if values change.
• View the graph on your ThingSpeak channel; data updates every 20 seconds.
• Troubleshoot: If no data posts, check API key or increase delay to meet
ThingSpeak's 15-second limit.

Code:
#include <ESP8266WiFi.h>
String apiKey = "YOUR_THINGSPEAK_API_KEY";
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* server = "[Link]";

WiFiClient client;

void setup() {
[Link](9600);
[Link](ssid, password);
while ([Link]() != WL_CONNECTED) {
delay(500);
[Link](".");
}
[Link]("Connected to WiFi");
}
void loop() {
int airQualityValue = analogRead(MQ_PIN);
[Link]("Air Quality: ");
[Link](airQualityValue);

if ([Link](server, 80)) {
String postStr = apiKey;
postStr += "&field1=";
postStr += String(airQualityValue);
postStr += "\r\n\r\n";

[Link]("POST /update HTTP/1.1\n");


[Link]("Host: [Link]\n");
[Link]("Connection: close\n");
[Link]("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
[Link]("Content-Type: application/x-www-form-urlencoded\n");
[Link]("Content-Length: ");
[Link]([Link]());
[Link]("\n\n");
[Link](postStr);
}
[Link]();
delay(20000); // ThingSpeak has a 15-second update limit }
Result: Air quality sensor readings were successfully logged and visualized in real-time on a
ThingSpeak channel.
Conclusion: An IoT air quality monitoring system was developed, demonstrating how to
stream sensor data to a cloud platform for analysis.
Experiment 8: Wi-Fi-Based IoT Connectivity: Connecting Arduino to
Available Networks
a. Establish a Wi-Fi connection and send sensor data to an IoT cloud platform.

Aim: To connect an Arduino to a Wi-Fi network using an ESP8266 module and send sensor
data to an IoT platform.

Components Required:
• Hardware: Arduino UNO, ESP8266 (ESP-01) Wi-Fi Module, Logic Level Shifter
(recommended), Breadboard, Jumper wires.
• Software: Arduino IDE.

Theory:
The ESP8266 ESP-01 module can be used as a cost-effective Wi-Fi "shield" for an Arduino.
The two devices communicate via a serial (UART) connection. The Arduino sends AT
commands to the ESP-01 to configure it, connect to a network, and establish a TCP
connection to a server to send data. A logic level shifter is recommended because the
Arduino operates at 5V logic while the ESP-01 uses 3.3V.

Procedure:
1. Assemble the Hardware Circuit:
• Use a logic level shifter: Connect Arduino TX (pin 3) to shifter HV, ESP-01 RX
to LV; Arduino RX (pin 2) to shifter HV, ESP-01 TX to LV.
• Power ESP-01: Connect 3.3V from Arduino (or external supply) to VCC and
CH_PD on ESP-01, GND to GND.
• Ensure no direct 5V connections to ESP-01 to avoid damage.

2. Prepare the Software:


• In Arduino IDE, install SoftwareSerial library if needed.
• Select Arduino Uno board and port.
3. Write and Upload the Code:
• Copy the code into a new sketch, replacing Wi-Fi details.
• Note: Baud rate may need adjustment (e.g., from 115200 to 9600 using AT
commands if incompatible).
• Verify and upload to Arduino.
4. Test the Connection:
• Open Serial Monitor (baud 9600).
• Type AT commands manually if needed (e.g., AT to test response).
• Monitor for successful Wi-Fi join (OK responses).
• Expand for data sending: Add AT+CIPSTART and AT+CIPSEND in loop for
full IoT integration.
• Troubleshoot: If no response, check wiring, baud rate, or reset ESP-01 (connect
GPIO0 to GND briefly).
Code:

Code:
#include <SoftwareSerial.h>
SoftwareSerial esp8266(2, 3); // RX, TX

void setup() {
[Link](9600);
[Link](115200); // Default baud rate for ESP-01 can vary

// Send AT commands to connect


[Link]("AT+RST");
delay(1000);
[Link]("AT+CWMODE=1");
delay(1000);
[Link]("AT+CWJAP=\"YOUR_WIFI_SSID\",\"YOUR_WIFI_PASSWORD\"");
delay(5000);
}

void loop() {
// This example just shows connection. A full implementation
// would send data using AT+CIPSTART and AT+CIPSEND.
if ([Link]()) {
[Link]([Link]());
}
if ([Link]()) {
[Link]([Link]());
}
}

Result: The serial monitor showed the responses from the ESP-01, confirming a successful
connection to the Wi-Fi network.

Conclusion: Successfully demonstrated how to provide Wi-Fi connectivity to a standard


Arduino board using an ESP-01 module.
Experiment 9: Bluetooth-Based Home Automation Using Arduino and Relay
Module
a. Control home appliances using a mobile app and Bluetooth module.

Aim: To control a relay module using a smartphone app via a Bluetooth connection to an
Arduino.

Components Required:
• Hardware: Arduino UNO, HC-05 Bluetooth Module, 1-Channel Relay Module,
Breadboard, Jumper wires.
• Software: Arduino IDE, a Bluetooth serial terminal app on a smartphone.

Theory: The HC-05 Bluetooth module provides two-way serial communication. It can be
paired with a smartphone, which then sends character commands (e.g., '1' for ON, '0' for
OFF) through a terminal app. The Arduino listens for these characters on its serial port.
Upon receiving a specific character, it toggles a digital pin connected to a relay module. The
relay acts as an electronically controlled switch that can turn a high-voltage appliance on or
off.

Procedure:
1. Assemble the Hardware Circuit:
• Connect HC-05: VCC to 5V, GND to GND, TX to Arduino RX (pin 0), RX to
Arduino TX (pin 1). Note: This uses the main serial, so disconnect during
upload.
• Connect relay: VCC to 5V, GND to GND, IN to pin 7.
• For safety, do not connect high-voltage devices yet; test with an LED first.
2. Prepare the Software and App:
• Install a Bluetooth terminal app (e.g., "Bluetooth Serial Terminal" on Android).
• In Arduino IDE, select Uno board (disconnect HC-05 TX/RX during upload).

3. Write and Upload the Code:


• Copy the code.
• Upload while HC-05 is disconnected, then reconnect.

4. Test the System:


• Power on, pair your phone with "HC-05" (default password 1234 or 0000).
• Open the app, connect to HC-05.
• Send '1' to turn relay ON, '0' to OFF; observe the relay click and Serial Monitor
feedback.
• Troubleshoot: If no connection, check pairing or baud rate (HC-05 default is
9600).

Code:
#define RELAY_PIN 7
char command;

void setup() {
[Link](9600); // HC-05 default baud rate
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Default state is OFF
[Link]("Bluetooth Relay Control Ready.");
}

void loop() {
if ([Link]() > 0) {
command = [Link]();

if (command == '1') {
digitalWrite(RELAY_PIN, HIGH); // Turn relay ON
[Link]("Relay ON");
} else if (command == '0') {
digitalWrite(RELAY_PIN, LOW); // Turn relay OFF
[Link]("Relay OFF");
}
}
}

Result: The relay clicked on and off in response to the '1' and '0' commands sent from the
smartphone app.
Conclusion: Successfully implemented a local home automation system using Bluetooth,
demonstrating wireless control of an electrical switch.
Experiment 9: Smart Agriculture: Sensor-Based Monitoring Using
NodeMCU
a. Implement soil moisture and environmental sensors for precision farming.

Aim: To monitor soil moisture and environmental conditions for precision farming using a
NodeMCU.

Components Required:
• Hardware: NodeMCU, Capacitive Soil Moisture Sensor, DHT11/DHT22
(Temperature & Humidity Sensor), Breadboard, Jumper wires.
• Software: Arduino IDE.

Theory: This system integrates multiple sensors to provide a comprehensive view of farm
conditions. A capacitive soil moisture sensor measures the water content in the soil by
detecting changes in capacitance, providing a more durable solution than resistive sensors. A
DHT sensor measures ambient temperature and humidity. The NodeMCU collects data from
both sensors and can transmit it via Wi-Fi for remote monitoring, enabling data-driven
decisions for irrigation and crop care.

Procedure:
1. Assemble the Hardware Circuit:
• Connect soil sensor: VCC to 3.3V, GND to GND, signal to A0.
• Connect DHT11: VCC to 3.3V, GND to GND, data to D4 (GPIO2). Add a
10kΩ pull-up resistor between VCC and data if not built-in.
• Insert the soil sensor probe into moist/dry soil for testing.

2. Prepare the Software:


• In Arduino IDE, install "DHT sensor library" via Tools > Manage Libraries.
• Select NodeMCU board and port.
3. Write and Upload the Code:
• Copy the code, choosing DHTTYPE (DHT11 or DHT22).
• Calibrate map() values for soil moisture based on dry (e.g., 1023) and wet (e.g.,
0) readings.
• Verify and upload.

4. Test the System:


• Open Serial Monitor (baud 9600).
• Water the soil or breathe on DHT to change readings; observe updates every 2
seconds.
• For optional IoT: Add Blynk or Wi-Fi code to send data remotely.
• Troubleshoot: If NaN errors, check DHT wiring or library version.

Code:
#include "DHT.h"

#define SOIL_MOISTURE_PIN A0
#define DHTPIN D4 // GPIO2
#define DHTTYPE DHT11 // or DHT22
DHT dht(DHTPIN, DHTTYPE);

void setup() {
[Link](9600);
[Link]();
}
void loop() {
delay(2000); // Delay between measurements

int soilMoistureValue = analogRead(SOIL_MOISTURE_PIN);


// Map the raw value to a percentage
int soilMoisturePercent = map(soilMoistureValue, 1023, 0, 0, 100); // Calibrate these
values

float h = [Link]();
float t = [Link]();

if (isnan(h) || isnan(t)) {
[Link]("Failed to read from DHT sensor!");
return;
}
[Link]("Soil Moisture: ");
[Link](soilMoisturePercent);
[Link]("%\t");
[Link]("Humidity: ");
[Link](h);
[Link]("%\t");
[Link]("Temperature: ");
[Link](t);
[Link]("°C");
}

Result: Real-time data for soil moisture, ambient temperature, and humidity were displayed
on the serial monitor.

Conclusion: A foundational smart agriculture monitoring system was developed, capable of


collecting key environmental data for precision farming applications.

You might also like