Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

IOT Practical Codes

Practical No. 5: Write a program using Arduino to control LED (One or more ON/OFF). Or Blinking

void setup()
{
pinMode(2, OUTPUT);
}
void loop()
{
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, LOW);
delay(1000);
}

Practical No. 6: Create a program that illuminates the green LED if the counter is less than 100,
illuminates the yellow LED if the counter is between 101 and 200 and illuminates the red LED if the counter
is greater than 200.

int counter=0;
void setup()
{
Serial.begin(9600);
pinMode(10, OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
}
void loop()
{
if(counter==280)
{
counter=0;
}
counter=counter+1;
delay(10);
11
Serial.print(counter);
Serial.print("\n");
if(counter<100)
{
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
}
else if(counter>101 && counter<201)
{
digitalWrite(10,LOW);
digitalWrite(11,HIGH);
digitalWrite(12,LOW);
}
else if(counter>201)
{
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(12,HIGH);
}
}

Practical No. 7: Create a program so that when the user enters "b" the green light blinks, "g" the green
light is illuminated "Y" the yellow light is illuminated and "r" the red light is Illuminated.

int user=0;
void setup()
{
Serial.begin(9600);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
11
}
void loop()
{
// send data only when you receive data:
if (Serial.available() > 0)
{
// read the incoming byte:
user = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(user);
}
if(user==98)
{
digitalWrite(7,HIGH);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
delay(1000);
}
else if(user==103)
{
digitalWrite(7,LOW);
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
delay(1000);
}
else if(user==121)
{
digitalWrite(7,LOW);
digitalWrite(8,LOW);

11
digitalWrite(9,HIGH);
digitalWrite(10,LOW);
delay(1000);
}
else if(user==114)
{
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,HIGH);
delay(1000);
}
else
{
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
}
}

Practical No. 8: Write a program that asks the user for a number and outputs the number squared that is
entered.

int squ;
int user;
void setup()
{
Serial.begin(115200);
}
void loop()
{
if(Serial.available()>0)
11
{
user=Serial.readString().toInt();
Serial.print("The Number is ");
Serial.println(user);
Serial.print("The square of entered number is ");
squ=user*user;
Serial.println(squ);
}
}

Practical No. 9: Write a program to control the color of the LED by turning 3 different potentiometers.
One will be read for the value of Red, one for the value of Green, and one for the value of Blue.

void setup()
{
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}

void loop()
{
int redValue = analogRead(A0) / 4;//Map the value to 0-255
int greenValue = analogRead(A1) / 4;
int blueValue = analogRead(A2) / 4;
analogWrite(9, redValue);
analogWrite(10, greenValue);
analogWrite(11, blueValue);
delay(10);
}

11
Practical No. 10: Write a program read the temperature sensor and send the values to the serialmonitor
on the computer.

float temp;
void setup()
{
Serial.begin(9600);
}
void loop()
{
temp=analogRead(A0);
temp = temp * 0.48828125;
Serial.print("Tempearture = ");
Serial.print(temp);
Serial.print("*C");
Serial.println();
delay(1000);
}

Practical No. 11: Write a program so it displays the temperature in Fahrenheit as well as the maximum
and minimum temperatures it has seen.
#include <LiquidCrystal.h>
int t;
int fahr;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
}
void loop()
{
int temp=analogRead(A0);
t=temp*0.48828;
11
fahr= (t*9/5)+32;
lcd.print("Tempearture in Fahrenheit:");
lcd.print(fahr);
lcd.println();
if(fahr<-110)
{
lcd.print("It's to cold");
}
else if(fahr>122)
{
lcd.print("It's to hot");
}
}

Practical No. 12: Write a program to show the temperature and shows a graph of the recent
measurements

#include <LiquidCrystal.h>

int t;
int fahr;
bool outputDisplayed = false;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
}

void loop()
{

11
if (!outputDisplayed)
{
lcd.clear();
int temp = analogRead(A0);
t = temp * 0.48828;
fahr = (t * 9 / 5) + 32;
lcd.print("C:");
lcd.print(t);
lcd.println();
lcd.print("F:");
lcd.print(fahr);
lcd.println();
if (fahr < -110) {
lcd.print("Cold");
} else if (fahr > 122) {
lcd.print("Hot");
}
outputDisplayed = true;
}
}

OR

Practical No. 12: Understanding the connectivity of Arduino with IR sensor. Write an application to
detect obstacle and notify user using LEDs.

void setup() {
pinMode(A2, INPUT);
pinMode(8, OUTPUT);
Serial.begin(9600);
}
void loop()
{

11
int obstacle = digitalRead(A2);
if (obstacle == HIGH)
{
digitalWrite(8, HIGH);
Serial.println("Obstacle detected!");
}
else
{
digitalWrite(8, LOW);
}
delay(100);
}

11

You might also like