0% found this document useful (0 votes)
13 views9 pages

Sensor Codes

The document provides code examples for interfacing various sensors with an Arduino Uno, including an IR sensor, PIR sensor, ultrasonic sensor, and DHT11 temperature and humidity sensor. Each section includes setup and loop functions demonstrating how to read sensor values and output results via serial communication. The examples illustrate basic sensor functionality and data retrieval using Arduino programming.

Uploaded by

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

Sensor Codes

The document provides code examples for interfacing various sensors with an Arduino Uno, including an IR sensor, PIR sensor, ultrasonic sensor, and DHT11 temperature and humidity sensor. Each section includes setup and loop functions demonstrating how to read sensor values and output results via serial communication. The examples illustrate basic sensor functionality and data retrieval using Arduino programming.

Uploaded by

harshitrawat3125
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

IR Sensor

IR Sensor interfacing with Arduino Uno

Code:
/*** Arduino with IR Sensor ***/

int SensorPin = 2;

int OutputPin = 13;

void setup() {

pinMode(OutputPin, OUTPUT);

pinMode(SensorPin, INPUT);

Serial.begin(9600);

void loop() {

int SensorValue = digitalRead(SensorPin);


Serial.print("SensorPin Value: ");

Serial.println(SensorValue);

delay(1000);

if (SensorValue==LOW){ // LOW MEANS Object Detected

digitalWrite(OutputPin, HIGH);

else

digitalWrite(OutputPin, LOW);

Output:

PIR Sensor
PIR Sensor interfacing with Arduino Uno

Code:
const int PIR_SENSOR_OUTPUT_PIN = 4; /* PIR sensor O/P pin */
int warm_up;

void setup() {
pinMode(PIR_SENSOR_OUTPUT_PIN, INPUT);
Serial.begin(9600); /* Define baud rate for serial communication */
delay(20000); /* Power On Warm Up Delay */
}

void loop() {
int sensor_output;
sensor_output = digitalRead(PIR_SENSOR_OUTPUT_PIN);
if( sensor_output == LOW )
{
if( warm_up == 1 )
{
Serial.print("Warming Up\n\n");
warm_up = 0;
delay(2000);
}
Serial.print("No object in sight\n\n");
delay(1000);
}
else
{
Serial.print("Object detected\n\n");
warm_up = 1;
delay(1000);
}
}

Output:

Ultrasound Sensor
Ultrasound Sensor interfacing with Arduino Uno

Code:
const int pingPin = 2; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 4; // Echo Pin of Ultrasonic Sensor

void setup() {
Serial.begin(9600); // Starting Serial Terminal
}

void loop() {
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}

long microsecondsToInches(long microseconds) {


return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {


return microseconds / 29 / 2;
}

Output:
Temperature & Humidity sensor
DHT11

Temperature & Humidity sensor interfacing with Arduino Uno


Code:
#include <dht11.h>
#define DHT11PIN 2

dht11 DHT11;

void setup()
{
Serial.begin(9600);

void loop()
{
Serial.println();

int chk = DHT11.read(DHT11PIN);

Serial.print("Humidity (%): ");


Serial.println((float)DHT11.humidity, 2);

Serial.print("Temperature (C): ");


Serial.println((float)DHT11.temperature, 2);

delay(2000);

Output:

You might also like