0% found this document useful (0 votes)
7 views5 pages

Arduino Sensor Projects Guide

The document contains multiple Arduino code examples for various sensors and displays, including the DHT11 for temperature and humidity, HC-SR04 for distance measurement, 7-segment displays in both common cathode and common anode configurations, and a PIR sensor for motion detection. Each section provides setup and loop functions to read sensor data and display or respond accordingly. The examples include serial output for monitoring sensor readings and control of outputs like LEDs.

Uploaded by

anupamam249
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Arduino Sensor Projects Guide

The document contains multiple Arduino code examples for various sensors and displays, including the DHT11 for temperature and humidity, HC-SR04 for distance measurement, 7-segment displays in both common cathode and common anode configurations, and a PIR sensor for motion detection. Each section provides setup and loop functions to read sensor data and display or respond accordingly. The examples include serial output for monitoring sensor readings and control of outputs like LEDs.

Uploaded by

anupamam249
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

## DHT11 WITH ARDUINO

#include "DHT.h"

#define DHTPIN 2 // Data pin connected to D2


#define DHTTYPE DHT11 // DHT11 sensor

DHT dht(DHTPIN, DHTTYPE);

void setup() {
[Link](9600);
[Link]("DHT11 Sensor Test");
[Link]();
}

void loop() {
// Reading temperature or humidity takes ~250ms
float humidity = [Link]();
float temperature = [Link](); // Celsius

// Check if any reads failed


if (isnan(humidity) || isnan(temperature)) {
[Link]("Failed to read from DHT11 sensor!");
return;
}

// Print results
[Link]("Humidity: ");
[Link](humidity);
[Link](" %\t");

[Link]("Temperature: ");
[Link](temperature);
[Link](" °C");

delay(2000); // Wait 2 seconds between readings


}
##ULTRASONIC WITH ARDUINO

// Ultrasonic Sensor with Arduino UNO


// HC-SR04 distance measurement

#define TRIG 9 // Trigger pin


#define ECHO 10 // Echo pin

long duration;
int distance;

void setup() {
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
[Link](9600); // Start Serial Monitor
}

void loop() {
// Send a 10µs HIGH pulse to TRIG
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);

// Measure the duration of the ECHO pulse


duration = pulseIn(ECHO, HIGH);

// Calculate distance in cm
distance = duration * 0.034 / 2;

// Print to Serial Monitor


[Link]("Distance: ");
[Link](distance);
[Link](" cm");

delay(500); // half-second delay


}
## 7 SEGMENT WITH ARDUINO

COMMON CATHODE

// 7-Segment Display with Arduino


// Common Cathode type

int segPins[] = {2, 3, 4, 5, 6, 7, 8}; // a, b, c, d, e, f, g

// Digit patterns for 0-9 (1 = ON, 0 = OFF)


byte digits[10][7] = {
{1,1,1,1,1,1,0}, // 0
{0,1,1,0,0,0,0}, // 1
{1,1,0,1,1,0,1}, // 2
{1,1,1,1,0,0,1}, // 3
{0,1,1,0,0,1,1}, // 4
{1,0,1,1,0,1,1}, // 5
{1,0,1,1,1,1,1}, // 6
{1,1,1,0,0,0,0}, // 7
{1,1,1,1,1,1,1}, // 8
{1,1,1,1,0,1,1} // 9
};

void setup() {
for (int i = 0; i < 7; i++) {
pinMode(segPins[i], OUTPUT);
}
}

void loop() {
for (int num = 0; num < 10; num++) { // Count 0 → 9
displayDigit(num);
delay(1000); // hold each number for 1 second
}
}

void displayDigit(int num) {


for (int i = 0; i < 7; i++) {
digitalWrite(segPins[i], digits[num][i]);
}
}
COMMON ANODE

int segPins[] = {2,3,4,5,6,7,8}; // a,b,c,d,e,f,g

// Digits for common ANODE (0 = ON, 1 = OFF)


byte digits[10][7] = {
{0,0,0,0,0,0,1}, //0
{1,0,0,1,1,1,1}, //1
{0,0,1,0,0,1,0}, //2
{0,0,0,0,1,1,0}, //3
{1,0,0,1,1,0,0}, //4
{0,1,0,0,1,0,0}, //5
{0,1,0,0,0,0,0}, //6
{0,0,0,1,1,1,1}, //7
{0,0,0,0,0,0,0}, //8
{0,0,0,0,1,0,0} //9
};

void setup() {
for(int i=0;i<7;i++) pinMode(segPins[i],OUTPUT);
}

void loop() {
for(int num=0; num<10; num++) {
showDigit(num);
delay(1000);
}
}

void showDigit(int num) {


for(int i=0;i<7;i++) {
digitalWrite(segPins[i], digits[num][i]);
}
}
##PIR WITH ARDUINO

int pirPin = 2; // PIR sensor output


int ledPin = 13; // Onboard LED

void setup() {
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
[Link](9600);
}

void loop() {
int pirValue = digitalRead(pirPin);

if (pirValue == HIGH) {
digitalWrite(ledPin, HIGH);
[Link]("Motion detected!");
} else {
digitalWrite(ledPin, LOW);
[Link]("No motion");
}

delay(500);
}

You might also like