Task - Reading the input from the temperature sensor and then display it in the serial monitor.
LM35
TMP36
- 3 pins (gnd, vcc (5 V), signal - Vout (A0)))
//name variables
int temp = A0;
int switch = 9;
void setup()
Serial.begin(9600)
pinMode(9, OUTPUT); // pin 9 is a digital pin, where the switch is connected
void loop()
int read = analogRead(temp);
float voltage = (read * 5)/1024
serial.println("Temperature")
serial.print(voltage)
float tempC = (voltage - 0.5) * 100;
Serial.print(tempC);
if (tempC > 28)
digitalWrite(9, HIGH)
else
digitalWrite(9, LOW)
suppose if the temperature goes high (above 28 degree C) then switch on the AC.
Ultrasonic sensor
Task - Find the distance of any obstacle and print the distance in the serial monitor
HC-SR04
- 4 pins (trigger, echo, gnd, vcc)
Pulse - HIGH/LOW
int trgpin = 8;
int echopin = 9;
void setup()
pinMode(trgpin, OUTPUT);
pinMode(echopin, INPUT);
Serial.begin(9600);
void loop()
digitalWrite(trgpin, LOW);
delay(25);
digitalWrite(trgpin, High);
delay(25);
digitalWrite(trgpin, LOW);
duration = pulseIn(echopin, HIGH);
distance = (duration * 0.0034)/2;
serial.print(distance)
tigger - > obstacle
Echo <-
Calculate the distance of the obstacle, if the distance is < 80cm produce a beep sound
DHT Sensors - DHT11/DHT22
Humidity and temperature
Task - Read the humidity value and print in the serial monitor
#include <dht.h>
//initialize the dht sensor
DHT22 dht(A0);
void setup()
Serial.begin(9600);
Serial.println("DHT activated")
dht.begin();
void loop()
val = dht.readHumidity();
valt = dht.readTemperature();
Serial.println("Humidity:")
Serial.print(val);
delay(3000);
Serial.println("Temperature:")
Serial.print(valt);
delay(3000);
Output
humidity : 36.0% temperature: 26 degree C
humidity : 36.0% temperature: 26 degree C
Actuator - Servo motor
mechanical
coverting to a motion
how to activate the servo motor
#include <Servo.h>
int servopin = 12
Servo ser1;
void setup()
ser1.attach(servopin)
void loop()
ser1.write(0);
delay(1000);
ser1.write(90);
delay(1000);
ser1.write(180);
delay(1000);
// use potentiometer sensor (distance or displacement - linear/rotation)
potentionmeter = value based on that you are mapping to the servo motor and based on that you
are making an LED to glow.
#include <Servo.h>
int servopin = 12
Servo ser1;
int led = 6;
int pot = A0;
void setup()
ser1.attach(servopin)
pinMode(6, OUTPUT);
}
Void loop()
int val = analogRead(pot); // 1ms = 0 , 1.5 ms = 90, 2ms = 180
val1 = map(val, 0, 1023, 0, 180);
ser1.write(val);
if (val == 0)
digitalWrite(led, LOW);
if(val == 180)
digitalWrite(led, HIGH);
0 to 5V -> 0 to 1023