Prince IOT File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

BACHELOR OF COMPUTER APPLICATION

Submitted in the Partial Fulfillment of requirement

Fort the award of the degree

Practical File

GGSIPU, Delhi

Semester- VI
(2021-2024)

Subject: Practical-XII IOT Lab


Subject Code: BCA 372

Submitted To: Submitted


By:
Ms. Meena Sachdeva Name: Prince Michel
Assistant Professor Roll No: 03825502021
List of Practicals

S.No. Detailed Statement Sign


1. Study and Install IDE of Arduino.
2. Write the steps to add libraries in Arduino and setup
Arduino IDE for programming.
3. e a program using Arduino for Blink LED.
4. Write a program for monitoring Temperature using
Arduino and LM35 Temperature Sensors.
5. Write a program for monitoring Temperature and
Humidity using Arduino and DHT11 Sensor.
6. Write a program that shows how to fade an LED on pin 9
using the Analog Write() function.
7. Write a program using Arduino for Servo Motor
(HCSR04).
8. Write a program using Arduino for Ultrasonic sensor.
9. Write a program using Arduino for fading of LED in
accordance to distance (using Ultrasonic sensor)
10. Write a program using Arduino for movement of Servo
Motor in accordance to distance (using Ultrasonic
sensor)
11. Write a program for Arduino by Ultrasonic sensor and
servo motor (HC-SR04), and make a smart dustbin.

Practical 1

Q1. Study and Install IDE of Arduino.

SOLUTION:

2
Get the latest version from the download page. You can choose between the Installer (.exe) and
the Zip packages. We suggest you use the first one that installs directly everything you need to
use the Arduino Software (IDE), including the drivers. With the Zip package you need to install
the drivers manually. The Zip file is also useful if you want to create a portable installation.

When the download finishes, proceed with the installation and please allow the driver installation
process when you get a warning from the operating system.

Choose the components to install. Choose the installation directory.

3
Installation in progress

The process will extract and install all the required files to execute properly the Arduino
Software (IDE)

The text of the Arduino getting started guide is licensed under a Creative Commons
AttributionShareAlike 3.0 License. Code samples in the guide are released into the public
domain.

Practical 2
Q2. Write the steps to add libraries in Arduino and setup Arduino IDE for
programming.

SOLUTION:
1. Open the Library Manager:
In the menu bar, select Tools > Manage Libraries…
In IDE 2, you can also click on the button in the sidebar.
2. Filter the available libraries by typing something (such as a library name) in the text field
above the listed libraries.

IDE 1.x also has options to filter by Type and Topic.

3. Find the library in the search results. The results are listed alphabetically, so you may
need to scroll down the list.

To find more information about the library, click More info. This will usually
take you to a reference page or repository for the library

4
4. Find a library you want to install. You can review the description and author. When
you’ve found a library you want to install, click Install. The latest version is selected by
default.
5. The Library Manager looks slightly different depending on what version of the IDE we
are using. 6. Wait for the installation to complete.

Practical 3
Q3. Write a program using Arduino for Blink LED.

SOLUTION:
void setup ()
{
// initialize digital pin 13 as an output. pinMode(10, OUTPUT); }

// the loop function runs over and over again forever

void loop()
{ digitalWrite(10, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000);
// wait for a second
digitalWrite(10, LOW);
// turn the LED off by making the voltage LOW
delay(1000); // wait for a second

5
}

OUTPUT:

Practical 4
Q4. Write a program for monitoring Temperature using Arduino and LM35
Temperature Sensors.

SOLUTION:

const int lm35_pin = A1; /* LM35 O/P pin */

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

void loop()
{
int temp_adc_val;
float temp_val;
temp_adc_val = analogRead(lm35_pin);
/* Read Temperature */
temp_val = (temp_adc_val * 4.88);

6
/* Convert adc value to equivalent voltage */ temp_val = (temp_val/10);
/* LM35 gives output of 10mv/°C */
Serial.print("Temperature = ");
Serial.print(temp_val);
Serial.print(" Degree Celsius\n"); delay(1000); }
OUTPUT:

7
Practical 5

Q5. Write a program for monitoring Temperature and Humidity using


Arduino and DHT11 Sensor.

SOLUTION:

#include <dht11.h>
#define DHT11PIN 4

dht11 DHT11;

void setup()
{
Serial.begin(9600);
void loop()
{
Serial.println();

int chk = DHT11.read(DHT11PIN);

Serial.print("Humidity (%): ");


Serial.println((float)DHT11.humidity, 2);

Serial.print("Temperature (C): ");


Serial.println((float)DHT11.temperature, 2);

delay(2000);
}

OUTPUT:
Practical 6

Q6. Write a program that shows how to fade an LED on pin 9 using the
Analog Write() function.

SOLUTION:
int led = 9; // the PWM pin the LED is attached to int
brightness = 0; // how bright the LED is int fadeAmount = 5; //
how many points to fade the LED by // the setup routine runs
once when you press reset:

void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:


void loop() {
// set the brightness of pin 9: analogWrite(led,
brightness);
// change the brightness for next time through the loop: brightness
= brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade: if
(brightness <= 0 || brightness >= 255) { fadeAmount *= 1;
}
delay(30);
// wait for 30 milliseconds to see the dimming effect }

OUTPUT:

9
Practical 7

Q7. Write a program using Arduino for Servo Motor (HC-SR04).

SOLUTION:

Servo myservo; // create servo object to control a servo


// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() { myservo.attach(9); // attaches the servo on pin 9 to


the servo object }

void loop() { for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to
180 degrees // in steps of 1 degree myservo.write(pos); // tell
servo to go to position in variable 'pos' delay(15); // waits
15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position } }

OUTPUT:

10
Practical 8

Q8 Write a program using Arduino for Ultrasonic sensor.

SOLUTION:
#define ECHOpin 5 // it defines the ECHO pin of the sensor to pin 5 of Arduino #define
TRIGpin 6

// we have defined the variable long duration;

// variable for the duration of sound wave travel int distance;

// variable for the distance measurement void

setup()

pinMode(TRIGpin, OUTPUT); // It sets the ECHO pin as OUTPUT

pinMode(ECHOpin, INPUT); // It sets the TRIG pin as INPUT

Serial.begin(9600); // // Serial Communication at the rate of 9600 bps

Serial.println("Test of the Ultrasonic Sensor HCSR04");

// It will appear on Serial Monitor

Serial.println("with the Arduino UNO R3 board");

} void

loop()

// It first sets the TRIG pin at LOW for 2 microseconds

digitalWrite(TRIGpin, LOW);

delayMicroseconds(4);

// It now sets TRIG pin at HIGH for 15 microseconds

digitalWrite(TRIGpin, HIGH); delayMicroseconds(15);

digitalWrite(TRIGpin,

11
LOW);
// It will read the ECHO pin and will return the time duration

= pulseIn(ECHOpin, HIGH);

// distance formula distance

= duration*(0.034/2);

// (speed in microseconds)

// Speed of sound wave (340 m/s)divided by 2 (forward and backward bounce)


// To display the distance on Serial Monitor

Serial.print("Distance: ");

Serial.print(distance);

Serial.println(" cm"); //specified unit of distance }

OUTPUT:

12
Practical 9
Q9 Write a program using Arduino for fading of LED in accordance to
distance(using Ultrasonic sensor)

SOLUTION:
const int LEDPin = 9; //PWM pin, included a capacitor to help smooth out the
signal, has a 330 Ohm resistor int Bright = 0; //bright max 255
int fadeAmount = 5; //how many points to fade the LED by int
DT = 30; //delay time
const int trigPin = 5; //ultra sonic pin
const int echoPin = 6; //ultra sonic pin
float duration, distance; void setup()
{
pinMode(LEDPin, OUTPUT); //declare LED as output
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600); } void
loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2); digitalWrite(trigPin,
HIGH); delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration * .0343) / 2; if
(distance >= 2.80 || Bright > 5) {
analogWrite(LEDPin, Bright); //turn LED on to 0
Bright = Bright + fadeAmount; } if (Bright <= 0
|| Bright >= 255) { fadeAmount = -fadeAmount;
} delay (DT);
Serial.print("distance: ");
Serial.println(distance);
delay(100); }

OUTPUT:

13
Practical 10
Q10 Write a program using Arduino for movement of Servo Motor in
accordance to distance(using Ultrasonic sensor)

SOLUTION:
#include <Servo.h> // constants won't change
const int TRIG_PIN = 6; // Arduino pin connected to Ultrasonic Sensor's TRIG pin const
int ECHO_PIN = 7; // Arduino pin connected to Ultrasonic Sensor's ECHO pin
const int SERVO_PIN = 9; // Arduino pin connected to Servo Motor's pin const int
DISTANCE_THRESHOLD = 50; // centimeters
Servo servo; // create servo object to control a servo // variables will change:
float duration_us,
distance_cm;
void setup() {
Serial.begin (9600); // initialize serial port
pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode
pinMode(ECHO_PIN, INPUT); // set arduino pin to input mode
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
servo.write(0); }
void loop() {
// generate 10-microsecond pulse to TRIG pin
digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// measure duration of pulse from ECHO pin
duration_us = pulseIn(ECHO_PIN, HIGH);
// calculate the distance distance_cm = 0.017
* duration_us; if (distance_cm <
DISTANCE_THRESHOLD)
servo.write(180); // rotate servo motor to 90 degree else
servo.write(0); // rotate servo motor to 0 degree
Serial.print("distance: "); // print the value to Serial Monitor
Serial.print(distance_cm);
Serial.println(" cm");
delay(500); }

OUTPUT:

14
Practical 11
Q11. Write a program for Arduino by Ultrasonic sensor and servo
motor(HCSR04), and make a smart dustbin.

SOLUTION:

#include Servo servo; int


trigPin = 5; int echoPin = 6;
int servoPin = 7; int led= 10;
long duration, dist, average;
long aver[3]; //array for
average

void setup()
{Serial.begin(9600);
servo.attach(servoPin); pinMode(trigPin,
OUTPUT); pinMode(echoPin, INPUT);
servo.write(0); //close cap on power on
delay(100);
servo.detach();
} void
measure() {
digitalWrite(10,HIGH);
digitalWrite(trigPin, LOW);
delayMicroseconds(5); digitalWrite(trigPin,
HIGH); delayMicroseconds(15);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT); duration =
pulseIn(echoPin, HIGH); dist =
(duration/2) / 29.1; //obtain distance
} void
loop()

15
{ for (int i=0;i<=2;i++) { //average distance
measure(); aver[i]=dist;
delay(10); //delay between
measurements }

dist=(aver[0]+aver[1]+aver[2])/3;

if ( dist<50 ) {
//Change distance as per
your need
servo.attach(servoPin);
delay(1); servo.write(0);
delay(3000);
servo.write(150);
delay(1000); servo.detach();
}
Serial.print(dist);
}

OUTPUT:

16
17

You might also like