DHT11 Interfacing Code
#include <DHT.h> // Include the DHT sensor library
#define DHTPIN 2 // Define the pin where the sensor is connected
#define DHTTYPE DHT11 // Define the sensor type as DHT11
DHT dht(DHTPIN, DHTTYPE); // Create an instance of the DHT class
void setup() {
[Link](9600); // Start serial communication at 9600 baud rate
[Link](); // Initialize the DHT11 sensor
[Link]("DHT11 Sensor Initialized");
void loop() {
delay(2000); // Wait for 2 seconds between readings
// Read humidity and temperature values
float humidity = [Link]();
float temperature = [Link]();
// Check if any reads failed and exit early (to try again)
if (isnan(humidity) || isnan(temperature)) {
[Link]("Failed to read from DHT sensor!");
return; // If true then exit the loop
// Print the values to the Serial Monitor
[Link]("Humidity: ");
[Link](humidity);
[Link](" %\t");
[Link]("Temperature: ");
[Link](temperature);
[Link](" *C");
}
HC-SR04 Interfacing Diagram
// Define the pins for the trigger and echo
const int trigPin = 9;
const int echoPin = 12;
// Variables to store the duration and distance
long duration;
int distance;
void setup() {
// Start the serial communication
[Link](9600);
// Set the trigger pin as an output and the echo pin as an input
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
void loop() {
// Clear the trigger pin by setting it LOW
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigger pin HIGH for 10 microseconds to start the measurement
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the duration of the pulse from the echo pin
duration = pulseIn(echoPin, HIGH); // Got the value in microsecond
// Calculate the distance in centimeters
distance = duration * 0.034 / 2; // distance calculated in cm
// Print the distance to the Serial Monitor
[Link]("Distance: ");
[Link](distance);
[Link](" cm");
// Wait for 50 milliseconds before the next measurement
delay(50);
}
HC-SR501 Interfacing Code
const int pirPin = 2; // The OUT pin of the PIR sensor
const int ledPin = 13; // The pin for the indicator LED
// --- State Tracking Variables ---
// We need to keep track of the sensor's state to detect changes.
int pirState = LOW; // Assume no motion initially
int lastPirState = LOW; // Variable to store the last known state
void setup() {
// This code runs once when the Arduino is powered on.
[Link](9600);
// Configure the pins.
pinMode(pirPin, INPUT); // The PIR sensor is an input device.
pinMode(ledPin, OUTPUT); // The LED is an output device.
[Link]("PIR Motion Sensor Initialized. Awaiting motion...");
void loop() {
// Read the current state of the PIR sensor.
pirState = digitalRead(pirPin);
// 2. Compare the current state to the last known state.
if (pirState != lastPirState) {
// --- A change in state has occurred ---
if (pirState == HIGH) {
digitalWrite(ledPin, HIGH); // Turn the LED ON.
[Link]("Motion detected!");
else {
// Motion has just ended.
digitalWrite(ledPin, LOW); // Turn the LED OFF.
[Link]("Motion ended.");
// This is crucial so we only print the message once per event.
lastPirState = pirState;
}
LDR Interfacing Code
const int ldrPin = A0; // The LDR is connected to analog pin A0
const int ledPin = 11; // The LED is connected to PWM pin 11
int ldrValue = 0; // Variable to store the raw value from the LDR (0-1023)
int brightness = 0; // Variable to store the calculated brightness for the LED (0-255)
void setup() {
// This code runs once when the Arduino is powered on or reset.
// Initialize the LED pin as an output.
pinMode(ledPin, OUTPUT);
[Link](9600);
void loop() {
ldrValue = analogRead(ldrPin);
brightness = map(ldrValue, 0, 1023, 0, 255);
analogWrite(ledPin, brightness);
// 4. Print values to the Serial Monitor for debugging
[Link]("LDR Value: ");
[Link](ldrValue);
[Link](" | LED Brightness: ");
[Link](brightness);
// Add a small delay to make the serial output readable
delay(100);
}
SG90/MG996R Servo Motor Interfacing Code
#include <Servo.h>
Servo myServo;
void setup() {
[Link](9600);
[Link](9);
void loop() {
for (int i = 0; i < 180; i++) {
[Link](i);
[Link](i); // Print the current angle to the monitor
delay(10);
for (int i = 179; i > 0; i--) {
[Link](i);
[Link](i); // Print the current angle to the monitor
delay(10);
}
Relay Interfacing Code
int RelayPin = 7;
void setup() {
// Set RelayPin as an output pin
pinMode(RelayPin, OUTPUT);
[Link](9600); // We need [Link](9600) if any communication between Arduino and computer happens.
Like [Link]/ plotting in monitor etc. It is safe to write the [Link](9600).
void loop() {
// Let's turn on the relay...
digitalWrite(RelayPin, LOW); delay(3000);
// Let's turn off the relay...
digitalWrite(RelayPin, HIGH); delay(3000);