This circuit is designed to interface an Arduino UNO with various sensors and modules, including a DHT11 temperature and humidity sensor, an MKE-S01 ultrasonic distance sensor, a PIR motion sensor, a red LED, an LCD Display (16x4) with I2C interface, a servo motor, and an RFID-RC522 module. The Arduino UNO acts as the central processing unit, reading sensor data, controlling the LED and servo, and communicating with the RFID reader and LCD display. The provided code is responsible for reading data from the ultrasonic and DHT11 sensors and outputting the information to the Serial Monitor.
/*
* This Arduino sketch reads data from an ultrasonic distance sensor and a
* DHT11 temperature and humidity sensor. The distance is measured in
* centimeters and printed to the Serial Monitor. The temperature and
* humidity values are also read and printed to the Serial Monitor.
*/
#include <DHT.h>
// Define pins for the ultrasonic sensor
const int trigPin = 3; // Trigger pin connected to D3
const int echoPin = 2; // Echo pin connected to D2
// Define pins for the DHT11 sensor
#define DHTPIN 4 // DHT11 data pin connected to D4
#define DHTTYPE DHT11 // DHT11 sensor type
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
// Variables to store duration and distance
long duration;
int distance;
void setup() {
// Setup serial communication to print values to the Serial Monitor
Serial.begin(9600);
// Set the trigPin as OUTPUT (to send the pulse)
pinMode(trigPin, OUTPUT);
// Set the echoPin as INPUT (to receive the pulse)
pinMode(echoPin, INPUT);
// Initialize the DHT sensor
dht.begin();
}
void loop() {
// Clear the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigPin HIGH for 10 microseconds to send the pulse
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the pulse duration from the echoPin
duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
distance = duration * 0.034 / 2;
// Read temperature and humidity from DHT11 sensor
float h = dht.readHumidity();
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again)
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Print the temperature and humidity to the Serial Monitor
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
// Short delay before the next measurement
delay(2000);
}
This code is designed to be uploaded to the Arduino UNO microcontroller. It initializes the sensors, reads their data, and outputs the information to the Serial Monitor. The ultrasonic sensor measures distance, and the DHT11 sensor measures temperature and humidity. The code includes error checking for failed sensor reads and prints the results every two seconds.