Sem IV IoT Lab Manual BCA402P/BSC204P
DEPARTMENT OF
COMPUTER SCIENCE
INTERNET OF THINGS LAB MANUAL
(COMMON FOR BCA/BSC)
SEMESTER : 4 CIA MARKS : 50
COURSE TITLE INTERNET OF THINGS PRACTICAL
: SEE MARKS : 50
TOTAL
COURSE CODE : BCA402P/BSC402P MARKS : 100
HOURS/WEEK : 2 CREDITS : 1
COURSE COORDINATOR: Mrs Roopa Ms. Alisha Jabeen
Shivshankar Mrs. Anu Joel
Mr. Bellappu
Ramakrishna
Dr. Usman Aijaz
Mrs. Roopa Shivsankar
Dr. Roselin
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
Experiments
1 Controlling LEDs using an Arduino Microcontroller.
2 Set Up the DHT 11 Humidity sensor on an Arduino.
3 Detecting obstacle with IR sensor and Arduino
4 LDR (Light Dependent Resistor) / Light Sensor
5 Interfacing LCD_I2C (4 pins) with the Arduino Microcontroller.
6 Interfacing Servo Motor with the Arduino.
7 Interfacing Ultrasonic Sensor with the Arduino Microcontroller to
measure the distance of the object.
8 Interfacing Smoke Sensor (MQ2) with the Arduino
Microcontroller to detect the smoke from the surrounding.
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
Exercise 1:
1. Controlling LEDs using an Arduino Microcontroller.
a. Blink
Aim:
To control the state of an LED using a microcontroller (Arduino).
Theory:
In the Blink operation, the Arduino board is programmed to turn an LED on and off at predefined intervals. This
fundamental program introduces the concept of digital output. The onboard LED (often connected to pin 13 on
Arduino Uno) is commonly used for this purpose.
The Arduino sketch for Blink utilizes the digitalWrite function to set the state of a specified digital pin. The LED
is turned on by setting the pin to HIGH and turned off by setting it to LOW. delay functions introduce the concept
of timing. The delay between turning the LED on and off creates the blinking effect. Understanding timing is
crucial for more complex applications.
Application:
Traffic Light Simulation: The Blink concept can be expanded to simulate a traffic light system. By connecting
LEDs to different digital pins, one can create a program that mimics the changing states of a traffic light.
Emergency Indicator: In real-world applications, Blink logic can be used for emergency indicators or warning
lights, where an LED blinks to draw attention.
Heartbeat Indicator: In medical devices or wearable technology, a Blink-like operation could be applied to
create a heartbeat indicator, providing a visual representation of a simulated heartbeat
Circuit Diagram
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
Procedure:
[Write the procedure]
Code:
int led=12 ;
void setup()
{
pinMode(led, OUTPUT);
}
void loop()
{
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}
Result:
● Led turns ON for 1 second.
● Led turns OFF for 1 second.
● The cycle repeats continuously.
Conclusion:
The Blink operation serves as the foundation for understanding digital output, timing, and basic programming
concepts using Arduino. These principles become building blocks for more advanced projects, including
robotics, automation, and IoT applications.
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
b. Fade
Aim:
To control the brightness of an LED using a microcontroller (Arduino) through the Fade operation.
Theory:
In the Fade operation, the Arduino board is programmed to smoothly vary the brightness of an LED, creating a
fading effect. This program introduces the concept of analog output and Pulse Width Modulation (PWM). PWM
is a technique where the LED is rapidly switched on and off at varying duty cycles to control the average power
delivered, thereby adjusting the brightness.
The analogWrite function in the Arduino sketch is employed to set the intensity of the LED. This function allows
for a range of values between 0 (completely off) and 255 (maximum brightness). The gradual transition between
these values generates the fading effect.
Application:
Dimmable Lighting System: The Fade concept can be extended to simulate a dimmable lighting system. By
connecting LEDs to different digital pins and applying Fade logic, one can create a program that mimics the
smooth transition of light intensity, providing a customizable ambient lighting experience.
Mood Lighting Controller: In home automation or entertainment systems, Fade logic can be employed to create
a mood lighting controller. The LED's gradual change in brightness can be synchronized with different moods or
scenarios, enhancing the overall atmosphere.
Sunrise/Sunset Simulator: Fade operation can be applied in projects like sunrise/sunset simulators, where the
LED imitates the natural transition of light during dawn and dusk. This can be particularly useful in smart home
applications or for creating natural lighting environments.
Circuit Diagram:
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
Code:
int led = 9;
int brightness = 0;
int fadeAmount = 5;
void setup()
{
pinMode(led, OUTPUT);
}
void loop()
{
analogWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255)
{
fadeAmount = -fadeAmount;
}
delay(10);
}
Result:
• The LED connected to pin 9 fades in and out smoothly, creating a visually pleasing transition of brightness.
• The fadeAmount variable controls the rate of brightness change, and the delay(10) introduces a short delay
between each intensity adjustment, contributing to the smooth fading effect.
• The LED's brightness transitions from 0 to 255 and vice versa in a continuous loop.
Conclusion:
Just like the Blink operation, the Fade program is a key learning tool. It helps users understand analog output
and PWM (Pulse Width Modulation). As users progress, these concepts become crucial for creating various
projects, from smart lighting systems to fun mood controllers in the world of microcontroller programming.
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
2. Set Up the Humidity sensor and Temprature on an Arduino.
Aim:
To utilize a microcontroller (Arduino) to interface with a Digital Humidity and Temperature sensor, and obtain
accurate readings of humidity, temperature, and heat index.
Theory:
In the DHT Sensor experiment, the Arduino board is programmed to communicate with a DHT sensor (DHT11
in this case) connected to pin 2. The DHT library is employed to read humidity, temperature.
Application:
Climate Monitoring System: DHT sensors are commonly used in climate monitoring systems. The experiment
can be extended to create a device that continuously monitors and reports changes in temperature, humidity, and
heat index, providing valuable information for environmental control.
Greenhouse Automation: In greenhouse applications, DHT sensors play a crucial role in maintaining optimal
growing conditions. The experiment's principles can be applied to automate climate control systems for plants,
ensuring the right balance of temperature and humidity.
Circuit Diagram:
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
Code:
const int tempSensorPin = A0; // Temperature sensor pin
const int humiditySensorPin = A1; // Humidity sensor pin
int rawValue = 0;
double voltage = 0;
double tempC = 0;
double tempF = 0;
int humiditySensorOutput = 0;
void setup() {
Serial.begin(9600);
pinMode(humiditySensorPin, INPUT);
}
void loop() {
// Read temperature sensor
rawValue = analogRead(tempSensorPin);
voltage = (rawValue / 1023.0) * 5000; // Convert to millivolts
tempC = (voltage - 500) * 0.1; // Convert mV to Celsius (for LM35-like sensor)
tempF = (tempC * 1.8) + 32; // Convert Celsius to Fahrenheit
// Print temperature
Serial.print("Raw Value = ");
Serial.print(rawValue);
Serial.print("\tMillivolts = ");
Serial.print(voltage, 0);
Serial.print(" mV\tTemperature = ");
Serial.print(tempC, 1);
Serial.print(" °C / ");
Serial.print(tempF, 1);
Serial.println(" °F");
// Read humidity sensor
humiditySensorOutput = analogRead(humiditySensorPin);
int humidityPercent = map(humiditySensorOutput, 0, 1023, 10, 70);
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
// Print humidity
Serial.print("Humidity: ");
Serial.print(humidityPercent);
Serial.println("%");
delay(1000); // Delay 1 second
}
Result:
• Serial monitor displays humidity, temperature in Celsius and Fahrenheit, and the calculated heat index every
2 seconds.
• The readings provide real-time information about the environmental conditions sensed by the DHT sensor.
Conclusion:
The DHT Sensor experiment provides foundational knowledge in interfacing with digital humidity and
temperature sensors. The acquired data, covering humidity, temperature, and heat index, lays the groundwork for
a range of applications, including climate monitoring systems and greenhouse automation.
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
3. Detecting obstacle with IR sensor and Arduino.
Aim:
To interface an Infrared (IR) sensor with a microcontroller (Arduino) and detect the obstacle with it.
Circuit Diagram:
Code:
int irsensor = 0;
void setup()
{
pinMode(5, INPUT);
Serial.begin(9600);
}
void loop()
{
irsensor = digitalRead(5);
Serial.println(irsensor);
if (irsensor == HIGH) {
Serial.println(" Stop something is ahead");
} else {
Serial.println("path is clear");
}
delay(10); // Delay a little bit to improve simulation performance
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
Result:
Whenever an obstacle is present in front of the IR sensor it will detect and display that “Stop something is
ahed” on the serial monitor otherwise it will display that “ Path is clear”.
Conclusion:
IR sensor it will detect and display that “Stop something is ahed” on the serial monitor otherwise it will
display that “ Path is clear”.
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
4. LDR (Light Dependent Resistor) / Light Sensor
Aim:To interface a Light-Dependent Resistor (LDR) sensor with a microcontroller (Arduino), to demonstrate
how the LED can be turned on or off based on the amount of light detected by the LDR.
Theory: In the LDR Sensor experiment, the Arduino board is programmed to read digital values from the LDR
sensor connected to pin 2.The digital values represent the presence or absence of light.
Application:
Ambient Light Control: This experiment serves as the foundation for creating systems that adjust ambient
lighting based on the surrounding light intensity. For example, it can be applied to automatically control indoor
lighting based on natural light conditions.
Energy-Efficient Systems: LDR sensors can be used in energy-efficient systems to regulate the brightness of
displays, streetlights, or outdoor lighting. This contributes to energy conservation by adapting to changing light
conditions.
Sunlight Harvesting: In solar energy applications, LDR sensors can be utilized for sunlight harvesting. The
information obtained can be used to optimize the positioning of solar panels for maximum exposure to sunlight.
Circuit Diagram:
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
Code :
int ldr = 0;
void setup()
{
pinMode(A0, INPUT);
Serial.begin(9600);
pinMode(10, OUTPUT);
}
void loop()
{
ldr = analogRead(A0);
Serial.println(ldr);
if (ldr >= 500) {
digitalWrite(10, HIGH);
} else {
digitalWrite(10, LOW);
}
delay(10); // Delay a little bit to improve simulation performance
}
Result:
• Serial monitor displays “it is dark time , LED is turned ON” if digital value is HIGH(if it is dark)
• The system continuously monitors the LDR sensor and turns ON or OFF the LED
conclusion: The LDR Sensor experiment measures the light intensity based on the resistance. If it is dark
time it will turn on the LED else turn off the LED.
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
5. LCD_I2C (4 pins)
Aim:
To interface a Liquid Crystal Display (LCD) with an I2C connector to a microcontroller (Arduino), and display
dynamic content on the screen.
Theory:
In this LCD with I2C experiment, the Arduino board is programmed to communicate with the LCD screen
through the I2C interface. The LCD_I2C library is utilized to simplify the interaction. The LCD is initialized
with its specific I2C address (0x3F) and the dimensions of the display (16 columns and 2 rows).
Application:
Information Display System: This experiment serves as the foundation for creating information display systems.
The LCD with I2C connectivity can be integrated into projects that require real-time information presentation,
such as weather stations, monitoring devices, or smart home control panels.
User Interface in Embedded Systems: The LCD can act as a user interface in embedded systems, offering a
means to display relevant information to users interacting with a device.
Circuit Diagram:
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
Code:
#include <Adafruit_LiquidCrystal.h>
Adafruit_LiquidCrystal lcd_1(0);
void setup()
{
lcd_1.begin(16, 2);
}
void loop()
{
lcd_1.setCursor(0, 0);
lcd_1.print("hello world");
delay(10); // Delay a little bit to improve simulation performance
}
Result:
• The LCD displays "Hello" on the first line and "World!" on the second line. • The backlight blinks for a
visually appealing effect.
• The LCD is cleared, and the cycle repeats every 500 milliseconds
Conclusion:
The LCD with I2C experiment demonstrates the seamless integration of an LCD screen with an Arduino using
I2C communication. It provides a foundation for creating diverse projects, from informational display systems to
user interfaces in embedded systems.
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
6. Interfacing Servo Motor with the Arduino.
Aim:
To control the position of a servo motor using a microcontroller (Arduino) through the Servo Motor operation.
Theory:
In the Servo Motor operation, the Arduino board is programmed to precisely control the angular position of a
servo motor. A servo motor is a device that incorporates a feedback mechanism, allowing for accurate positioning.
It is widely used in robotics and various automation applications.
The Servo library in Arduino facilitates the control of servo motors. The attach and write functions are
utilized to assign a pin to the servo motor and set its position, respectively. The servo motor operates within a
specified range, typically 0 to 180 degrees.
Application:
Door/Gate Control System: The Servo Motor concept can be extended to control doors or gates in home
automation systems. By connecting the servo to a door or gate mechanism, the Arduino can precisely control
the position of the servo motor to open or close the door.
Camera Pan-and-Tilt Mechanism: In robotics or surveillance applications, Servo Motor logic can be employed
to create a pan-and-tilt system for a camera. The servo motors control the horizontal and vertical movements,
allowing the camera to cover a wide area.
Automated Window Blinds: Servo Motor operation can be applied to automate window blinds. The servo motor
adjusts the slats' angle to control the amount of light entering a room, enhancing energy efficiency.
Circuit Diagram:
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
code:
#include <Servo.h>
int myServo = 0;
int i = 0;
int j = 0;
int k = 0;
int variabl = 0;
int m = 0;
int n = 0;
int servo = 0;
int o = 0;
Servo servo_9;
void setup()
{
servo_9.attach(9, 500, 2500);
}
void loop()
{
for (servo = 1; servo <= 180; servo += 1) {
servo_9.write(servo);
}
delay(1000); // Wait for 1000 millisecond(s)
for (servo = 180; servo >= 1; servo -= 1) {
servo_9.write(servo);
}
delay(1000); // Wait for 1000 millisecond(s)
}
Result:
• The servo motor connected to pin 9 smoothly sweeps from 0 to 180 degrees in one direction.
• After reaching 180 degrees, the servo motor smoothly sweeps back from 180 to 0 degrees.
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
This sweeping motion repeats continuously, creating a visually perceivable back-and-forth movement.
Conclusion:
The Servo Motor control experiment is crucial in microcontroller programming, offering a foundational
understanding of precise motor control. This hands-on experience forms the basis for diverse applications in
robotics, automation, and smart home systems.
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
7. Ultrasonic Sensor
Aim:
To interface an ultrasonic sensor with a microcontroller (Arduino), aiming to measure and display the distance
of an object from the sensor.
Theory:
In the Ultrasonic Sensor experiment, the Arduino board is programmed to communicate with the ultrasonic sensor
using two pins: trigPin for triggering the sensor and echoPin for receiving the echo signal. The sensor works by
emitting ultrasonic waves, and the time taken for the waves to bounce back is used to calculate the distance. The
formula distance = duration * 0.034 / 2 converts the time into distance in centimeters.
Application:
Distance Measurement System: This experiment serves as the foundation for creating distance measurement
systems. Ultrasonic sensors are commonly used in robotics, parking assistance systems, and object detection
applications.
Smart Security Systems: Ultrasonic sensors can be applied in smart security systems for proximity detection.
This includes alerting or triggering actions based on the distance of an object from the sensor.
Automated Navigation in Robotics: In robotics, ultrasonic sensors are often employed for obstacle avoidance
and navigation. The distance data obtained can be used to guide the robot's movements.
Circuit Diagram:
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
Code:
int varobl = 0;
int yen = 0;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop()
{
varobl = 0.01723 * readUltrasonicDistance(A0, A0);
Serial.println(varobl);
if (varobl <= 100) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
delay(10); // Delay a little bit to improve simulation performance
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
}
Result:
• Serial monitor displays "Distance: [Distance in cm]" every 500 milliseconds. • The ultrasonic sensor
continuously measures the distance, updating the value based on the object's proximity.
Conclusion:
The Ultrasonic Sensor experiment demonstrates the practical application of an ultrasonic sensor for distance
measurement. It forms the basis for creating systems that can intelligently respond to objects in their vicinity.
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
8. Smoke Sensor (MQ2)
Aim:
To interface a Smoke Sensor (MQ2) with a microcontroller (Arduino), with the aim of monitoring and
responding to smoke levels.
Theory:
In the Smoke Sensor (MQ2) experiment, the Arduino board is programmed to read analog values from the smoke
sensor connected to pin A1. The analog values represent the concentration of smoke or other gasses in the
environment. A threshold value is set, and if the sensor reading falls below this threshold, an external LED
connected to pin 2 is turned on.
Application:
Smoke Detection System: This experiment serves as the basis for creating a smoke detection system. The Smoke
Sensor (MQ2) is commonly used in applications where the detection of smoke or harmful gasses is crucial, such
as in fire alarm systems.
Air Quality Monitoring: The MQ2 sensor can be applied to monitor air quality by detecting various gasses.
This is useful in indoor environments to ensure a healthy and safe atmosphere.
Security Systems: Smoke sensors can be integrated into security systems to detect unauthorized access,
triggering actions such as turning on lights or sounding alarms when smoke is detected.
Circuit Diagram:
Dept., of Computer Science, YIASCM
Sem IV IoT Lab Manual BCA402P/BSC204P
Code:
int gassensor = 0;
void setup()
{
pinMode(A0, INPUT);
Serial.begin(9600);
}
void loop()
{
gassensor = analogRead(A0);
Serial.println(gassensor);
delay(10); // Delay a little bit to improve simulation performance
Result:
• Serial monitor displays "smoke value is [Analog Reading]" continuously.
• The external LED turns on if the smoke concentration falls below the set threshold (condition: ss > 400),
indicating a potential smoke presence.
Conclusion:
The Smoke Sensor (MQ2) experiment showcases the practical application of a smoke sensor in monitoring
environmental conditions. It provides the foundation for developing smoke detection systems that can be
integrated into safety and security solutions.
Dept., of Computer Science, YIASCM