Sensor Codes
Sensor Codes
Code:
/*** Arduino with IR Sensor ***/
int SensorPin = 2;
void setup() {
pinMode(OutputPin, OUTPUT);
pinMode(SensorPin, INPUT);
Serial.begin(9600);
void loop() {
Serial.println(SensorValue);
delay(1000);
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);
}
Output:
Temperature & Humidity sensor
DHT11
dht11 DHT11;
void setup()
{
Serial.begin(9600);
void loop()
{
Serial.println();
delay(2000);
Output: