m4 r5 o Level Iot Practical Classes Chapter 4 Diffrent Type of Sensors and Actuators

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

O-Level (M4-

UPCISS
R5)
Internet of Things and its
Applications

Chapter-4
Interfacing Different Types
of Sensor and Actuators
Video Tutorial
O-Level M4-R5 Full
Video Playlist Available
on YouTube Channel
UPCISS

Free Online Computer Classes on


YouTube Channel UPCISS
www.youtube.com/upciss
For free PDF Notes
Our Website: www.upcissyoutube.com

Copyright © 2022 UPCISS


Contents
Interfacing Different Types of Sensor and Actuators-----------------------------2
Push Button................................................................................................2
Buzzer (Piezo)............................................................................................ 2
Touch Sensor............................................................................................. 3
RGB LED Light.............................................................................................4
Ultrasonic Sensor (HC-SR04)......................................................................5
LDR Sensor.................................................................................................5
7 Segment LED Display...............................................................................6
MQ135 Gas Sensor....................................................................................9
PIR Motion Sensor (Passive Infrared Sensor).............................................9
DHT 11 Humidity and Temperature Sensor.............................................10
Interfacing Keypad 4X4............................................................................10
Interfacing LCD 16X2................................................................................11
DHT11 Sensor Print on LCD......................................................................12
IR (Infrared) Sensor..................................................................................12
HC-05 Bluetooth Module.........................................................................13
Servo Motor.............................................................................................13

1
Interfacing Different Types of Sensor and
Actuators
Push Button
int led = 7, pb =
8; void setup() {
Serial.begin(9600);
pinMode(led,
OUTPUT); Resistor- 300Ω
pinMode(pb, INPUT);
}
void loop() {
int signal =
digitalRead(pb);
Serial.println(signal);
if (signal == 1) {
digitalWrite(led,
1); delay(200);
} else {
digitalWrite(led,
0);
}
}

Buzzer (Piezo)
int buz = 7, pb =
8; void setup() {
Serial.begin(9600);
pinMode(buz,
OUTPUT);
pinMode(pb, INPUT);
}
void loop() {
int signal = Resistor- 300Ω
digitalRead(pb);
Serial.println(signal);
if (signal == 1) {
digitalWrite(buz,
1); delay(200);
} else {
digitalWrite(buz,
0);
}
}

2
Touch Sensor

int buz = 7, tch =


8; void setup() {
Serial.begin(9600);
pinMode(buz,
OUTPUT);
pinMode(tch,
INPUT);
}
void loop() {
int signal =
digitalRead(tch);
Serial.println(signal);
if (signal == 1) {
digitalWrite(buz,
1); delay(200);
} else {
digitalWrite(buz,
0);
}
}

3
RGB LED Light
int red = 6, blue = 5, green = 3, pb = 8, c
= 0; void setup() {
pinMode(red, OUTPUT);
pinMode(blue,
OUTPUT);
pinMode(green,
OUTPUT); pinMode(pb,
INPUT);
}

void loop() {
int signal =
digitalRead(pb); if
(signal == 1) { c++; }
if (c == 1) {
setcolor(255, 0, 0);
delay(100);
setcolor(160, 110,
90); delay(100);
setcolor(200, 70,
190); delay(100);
setcolor(255, 170,
80); delay(100);
} else if (c == 2) {
setcolor(0, 255, 0);
delay(100);
setcolor(60, 210, Common Cathode RGB LED
50); delay(100);
setcolor(100, 240,
90); delay(100);
setcolor(110, 255,
50); delay(100);
} else {
setcolor(0, 0,
0); delay(200);
c = 0;
}
}

void setcolor(int r, int b, int


g) { analogWrite(red, r);
analogWrite(blue, b);
analogWrite(green, g);
}

4
Ultrasonic Sensor (HC-SR04)
int trig =
13; int echo
= 12; int
duration;
int
distance;
void setup()
{
Serial.begin(9600);
pinMode(trig,
OUTPUT); Minimum Range = 2 CM Maximum Range = 4 Meters Accuracy = 3
pinMode(echo,
INPUT);
}
void loop() {
digitalWrite(trig,
0);
delayMicroseconds(2)
; digitalWrite(trig,
1);
delayMicroseconds(10
);
digitalWrite(trig,
0);
duration = pulseIn(echo, 1); // in Microseconds
distance = duration * 0.034 / 2;
Serial.print("Distance in CM: ");
Serial.println(distance);
}

LDR Sensor

void setup() {
pinMode(13,
OUTPUT);
Serial.begin(9600)
5
;
}
void loop() {
int ldr =
analogRead(A0);
Serial.println(ldr);
delay(100);
if (ldr > 900) { digitalWrite(13,
1);} else {digitalWrite(13, 0);}
}

6
7 Segment LED Display

Common Cathode Resistor- 300Ω

Common

F B

E C
DP
D

Common

7
int a = 8, b = 7, c = 6, d = 5, e = 4, f = 3, g
= 2; int dt = 1000;
void setup() {
pinMode(a,
OUTPUT);
pinMode(b,
OUTPUT);
pinMode(c,
OUTPUT);
pinMode(d,
OUTPUT);
pinMode(e,
OUTPUT);
pinMode(f,
OUTPUT);
pinMode(g,
OUTPUT);
}
void loop() {
// Write 0
digitalWrite(a, 1);
digitalWrite(b, 1);
digitalWrite(c, 1);
digitalWrite(d, 1);
digitalWrite(e, 1);
digitalWrite(f, 1);
digitalWrite(g,
0); delay(dt);
// Write 1
digitalWrite(a, 0);
digitalWrite(b, 1);
digitalWrite(c, 1);
digitalWrite(d, 0);
digitalWrite(e, 0);
digitalWrite(f, 0);
digitalWrite(g,
0); delay(dt);
// Write 2
digitalWrite(a, 1);
digitalWrite(b, 1);
digitalWrite(c, 0);
digitalWrite(d, 1);
digitalWrite(e, 1);
digitalWrite(f, 0);
digitalWrite(g,
1); delay(dt);
// Write 3
digitalWrite(a, 1);
digitalWrite(b, 1);
digitalWrite(c, 1);
digitalWrite(d, 1);
digitalWrite(e, 0);
digitalWrite(f, 0);

8
digitalWrite(g,
1); delay(dt);
// Write 4
digitalWrite(a, 0);
digitalWrite(b, 1);
digitalWrite(c, 1);

9
digitalWrite(d, 0);
digitalWrite(e, 0);
digitalWrite(f, 1);
digitalWrite(g,
1); delay(dt);
// Write 5
digitalWrite(a, 1);
digitalWrite(b, 0);
digitalWrite(c, 1);
digitalWrite(d, 1);
digitalWrite(e, 0);
digitalWrite(f, 1);
digitalWrite(g,
1); delay(dt);
// Write 6
digitalWrite(a, 1);
digitalWrite(b, 1);
digitalWrite(c, 1);
digitalWrite(d, 1);
digitalWrite(e, 1);
digitalWrite(f, 0);
digitalWrite(g,
1); delay(dt);
// Write 7
digitalWrite(a, 1);
digitalWrite(b, 1);
digitalWrite(c, 1);
digitalWrite(d, 0);
digitalWrite(e, 0);
digitalWrite(f, 0);
digitalWrite(g,
0); delay(dt);
// Write 8
digitalWrite(a, 1);
digitalWrite(b, 1);
digitalWrite(c, 1);
digitalWrite(d, 1);
digitalWrite(e, 1);
digitalWrite(f, 1);
digitalWrite(g,
1); delay(dt);
// Write 9
digitalWrite(a, 1);
digitalWrite(b, 1);
digitalWrite(c, 1);
digitalWrite(d, 1);
digitalWrite(e, 0);
digitalWrite(f, 1);
digitalWrite(g,
1); delay(dt);
}

10
MQ135 Gas Sensor

Resistor- 10kΩ to 47kΩ

void setup() {
Serial.begin(9600
);
}
void loop() {
int val = analogRead(A0);
int air = map(val, 0, 1023, 0,
255); Serial.print("Air Quality
"); Serial.println(air);
delay(1000);
}

PIR Motion Sensor (Passive Infrared Sensor)


void setup() {
pinMode(2,
OUTPUT);
pinMode(5,
INPUT);
Serial.begin(9600
);
}
void loop() {
int value =
digitalRead(5);
Serial.println(value);
if (value == 1) {
digitalWrite(2,
1);

11
} else {
digitalWrite(2,
0);
}
}

12
DHT 11 Humidity and Temperature Sensor
#include <DHT.h>
#include
<DHT_U.h>

DHT dht(2, DHT11);

void setup() {
Serial.begin(9600
); dht.begin();
}
void loop()
{ delay(200
0);
float h = dht.readHumidity();
float tc =
dht.readTemperature();
float tf = dht.readTemperature(true);

Serial.print(h);
Serial.print(" % Humidity \
t"); Serial.print(tc);
Serial.print(" °C Temperature \
t"); Serial.print(tf);
Serial.print(" °F Temperature \
t"); Serial.println("");
}

Interfacing Keypad 4X4

13
#include
<Keypad.h> byte
row = 4;
byte col = 4;
char keys[4][4] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
byte rowpin[4] = { 9, 8, 7, 6 };
byte colpin[4] = { 5, 4, 3, 2 };
Keypad mykeypad = Keypad(makeKeymap(keys), rowpin, colpin, row,
col); void setup() {
Serial.begin(9600);
}
void loop() {
char presskey =
mykeypad.getKey(); if
(presskey) {
Serial.println(presskey);
}
}

Interfacing LCD 16X2

Resistor- 1kΩ Resistor- 300Ω

14
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6,
7); void setup() {
lcd.begin(16, 2);
lcd.print("Hello
UPCISS");
}
void loop() {
lcd.setCursor(0,
1);
lcd.print(millis() / 1000);
}

DHT11 Sensor Print on LCD


#include <DHT.h>
#include
<DHT_U.h>
#include
<LiquidCrystal.h> DHT
dht(8, DHT11);
LiquidCrystal lcd(2, 3, 4, 5, 6,
7); void setup() {
Serial.begin(9600
); dht.begin();
lcd.begin(16, 2);
}
void loop() {
float h = dht.readHumidity();
float tc =
dht.readTemperature();
lcd.print(h);
lcd.print(" %
Humidity");
lcd.setCursor(0, 1);
lcd.print(tc);
lcd.print(" °C
Temperature");
delay(2000);
lcd.clear();
}

IR (Infrared) Sensor
#include
<IRremote.h> int
irs = 5;
int c = 1;
void setup()
{
Serial.begin(9600
); pinMode(2,

15
OUTPUT);
IrReceiver.begin(irs, ENABLE_LED_FEEDBACK);
}
void loop() {
if (IrReceiver.decode()) {
String ircode = String(IrReceiver.decodedIRData.command,
HEX); delay(300);
Serial.println(ircode);
IrReceiver.resume();

16
if ((ircode == "f") && (c % 2 ==
1)) { digitalWrite(2, 1);
c++;
} else if ((ircode == "f") && (c % 2 ==
0)) { digitalWrite(2, 0);
c++;
}
}
}

HC-05 Bluetooth Module


char val;
void setup() {
Serial.begin(9600
); pinMode(2,
OUTPUT);
}
void loop() {
if
(Serial.available())
{ val =
Serial.read(); if
(val == 'A') {
digitalWrite(2, 1);
} else if (val == 'a')
{ digitalWrite(2,
0);
}
}
}

Servo Motor
#include
<Servo.h> Servo GND
VCC
myservo; void Signal
setup() {
myservo.attach(7);
}
void loop() {
for (int i = 0; i <= 180; i+
+) { myservo.write(i);
delay(10);
}
for (int i = 180; i > 0;
i--) { myservo.write(i);
delay(10);
}
}

17
18
It takes a lot of hard work to make notes, so if you can pay some fee 100,

नोट्स बनाने में बहुत मेहनत लगी है , इसललए यलि आप कु छ शुल्क 100, 200 रूपए जो आपको उलि

19

You might also like