Prince IOT File
Prince IOT File
Prince IOT File
Practical File
GGSIPU, Delhi
Semester- VI
(2021-2024)
Practical 1
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.
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.
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); }
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:
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
SOLUTION:
#include <dht11.h>
#define DHT11PIN 4
dht11 DHT11;
void setup()
{
Serial.begin(9600);
void loop()
{
Serial.println();
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);
}
OUTPUT:
9
Practical 7
SOLUTION:
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
SOLUTION:
#define ECHOpin 5 // it defines the ECHO pin of the sensor to pin 5 of Arduino #define
TRIGpin 6
setup()
} void
loop()
digitalWrite(TRIGpin, LOW);
delayMicroseconds(4);
digitalWrite(TRIGpin,
11
LOW);
// It will read the ECHO pin and will return the time duration
= pulseIn(ECHOpin, HIGH);
= duration*(0.034/2);
// (speed in microseconds)
Serial.print("Distance: ");
Serial.print(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:
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