#include <NewPing.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <DHT11.h>
// ================= PIN DEFINITIONS =================
#define TRIG_PIN 6
#define ECHO_PIN 7
#define SERVO_PIN 5
#define DHT_PIN 8
#define MAX_DISTANCE 500
// ================= OBJECTS =================
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myServo;
DHT11 dht11(DHT_PIN);
// ================= VARIABLES =================
int temperature;
int humidity;
float speedOfSound;
float distance;
int servo_pos = 0;
int servo_direction = 1;
// ==================================================
// SETUP
// ==================================================
void setup() {
Serial.begin(115200);
// LCD
lcd.init();
lcd.backlight();
// Servo
myServo.attach(SERVO_PIN);
myServo.write(servo_pos);
// Startup message
lcd.setCursor(0, 0);
lcd.print("Hello, System");
delay(2000);
lcd.clear();
}
// ==================================================
// LOOP
// ==================================================
void loop() {
// ================================================
// READ DHT11
// ================================================
int result = dht11.readTemperatureHumidity(
temperature,
humidity
);
// ================================================
// CHECK DHT11 RESULT
// ================================================
if (result != 0) {
Serial.print("DHT11 Error: ");
Serial.println(result);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("DHT11 Error");
delay(1000);
return;
}
// ================================================
// CALCULATE SPEED OF SOUND
// ================================================
/*
Speed of sound:
v = 331.3 + (0.606 × T)
T = temperature in Celsius
v = speed in meters/second
*/
speedOfSound = 331.3 + (0.606 * temperature);
// ================================================
// READ ULTRASONIC SENSOR
// ================================================
unsigned int duration = sonar.ping();
if (duration == 0) {
distance = -1;
}
else {
/*
Distance:
distance = time × speed / 2
duration:
microseconds
speedOfSound:
meters/second
×100:
meters → centimeters
/1,000,000:
microseconds → seconds
/2:
because the ultrasonic wave travels
to the object and back
*/
distance =
(duration * speedOfSound * 100.0)
/ (2.0 * 1000000.0);
}
// ================================================
// SERVO MOVEMENT
// ================================================
myServo.write(servo_pos);
servo_pos += servo_direction;
// Reached 180°
if (servo_pos >= 180) {
servo_pos = 180;
servo_direction = -10;
}
// Reached 0°
if (servo_pos <= 0) {
servo_pos = 0;
servo_direction = 10;
}
// ================================================
// SERIAL MONITOR
// ================================================
Serial.println("---------------------------");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
Serial.print("Speed of Sound: ");
Serial.print(speedOfSound);
Serial.println(" m/s");
Serial.print("Distance: ");
if (distance < 0) {
Serial.println("No Echo");
}
else {
Serial.print(distance, 2);
Serial.println(" cm");
}
Serial.print("Servo: ");
Serial.print(servo_pos);
Serial.println(" degrees");
// ================================================
// LCD
// ================================================
lcd.clear();
// First row
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(temperature);
lcd.print("C H:");
lcd.print(humidity);
// Second row
lcd.setCursor(0, 1);
if (distance < 0) {
lcd.print("No Echo");
}
else {
lcd.print("D:");
lcd.print(distance, 1);
lcd.print("cm");
}
// ================================================
// DHT11 READING INTERVAL
// ================================================
delay(1000);
}