IoTA Unit 3
IoTA Unit 3
IoTA Unit 3
Actuators
Programming and Interfacing with sensors – Temperature, Humidity,
Water, MQ2,PIR and Ultrasonic – Magnetic relay switches, Actuators –
Servo motor, Stepper Motor
1
Programming and Interfacing with sensors
Temperature Sensor
2
Temperature Sensor
3
Temperature Sensor
float temp;
int tempPin = A1;
void setup() {
Serial.begin(9600);
}
void loop() {
temp = analogRead(tempPin);
// takes analog volt from sensor and save to
variable temp
temp = temp * 0.48828125;
// change the analog volt to its temperature
equivalent
Serial.print("TEMPERATURE = ");
Serial.print(temp); // display temperature
value
Serial.print("*C");
Serial.println();
delay(1000); // update sensor reading each one
second
}
4
Humidity Sensor
5
Humidity Sensor
6
Humidity Sensor
#include "DHT.h"
#define DHTPIN 2 // what digital pin we're connected
to
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
dht.begin();
}
7
Humidity Sensor
void loop() {
delay(2000); // Wait a few seconds between
measurements
float h = dht.readHumidity();
// Reading temperature or humidity takes about 250
milliseconds!
float t = dht.readTemperature();
// Read temperature as Celsius (the default)
float f = dht.readTemperature(true);
// Read temperature as Fahrenheit (isFahrenheit = true)
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return; 8
Humidity Sensor
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print ("Humidity: ");
Serial.print (h);
Serial.print (" %\t");
Serial.print ("Temperature: ");
Serial.print (t);
Serial.print (" *C "); Serial.print (f);
Serial.print (" *F\t"); Serial.print ("Heat index: "); Serial.print (hic); Serial.print (" *C ");Serial.print (hif);
Serial.println (" *F");
}
9
PIR Sensor
10
PIR Sensor
11
PIR Sensor
#define pirPin 2
int calibrationTime = 30;
long unsigned int lowIn;
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int PIRValue = 0;
void setup() {
Serial.begin(9600);
pinMode(pirPin, INPUT);
}
12
PIR Sensor
void loop() {
PIRSensor();
}
void PIRSensor() {
if(digitalRead(pirPin) == HIGH) {
if(lockLow) {
PIRValue = 1;
lockLow = false;
Serial.println("Motion detected.");
delay(50);
}
takeLowTime = true;
}
13
PIR Sensor
if(digitalRead(pirPin) == LOW) {
if(takeLowTime){
lowIn = millis();takeLowTime = false;
}
if(!lockLow && millis() - lowIn > pause) {
PIRValue = 0;
lockLow = true;
Serial.println("Motion ended.");
delay(50);
}
}
}
14