0% found this document useful (0 votes)
350 views12 pages

All Sensor Code

This document contains code for several Arduino sensors and components: 1) An LED blinks on and off every 2 seconds. 2) An IR sensor detects obstacles and lights an LED if one is detected. 3) A PIR sensor detects motion and lights an LED, printing a message to the serial monitor. 4) An LM35 temperature sensor reads temperature and lights an LED if it exceeds 30 degrees Celsius. 5) A gas sensor detects gas and uses LEDs and an LCD to display the status. 6) Additional sensors and components described include a flame sensor, ultrasonic sensor, LCD display, moisture sensor, and a relay to control two lamps.

Uploaded by

Enchantress
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)
350 views12 pages

All Sensor Code

This document contains code for several Arduino sensors and components: 1) An LED blinks on and off every 2 seconds. 2) An IR sensor detects obstacles and lights an LED if one is detected. 3) A PIR sensor detects motion and lights an LED, printing a message to the serial monitor. 4) An LM35 temperature sensor reads temperature and lights an LED if it exceeds 30 degrees Celsius. 5) A gas sensor detects gas and uses LEDs and an LCD to display the status. 6) Additional sensors and components described include a flame sensor, ultrasonic sensor, LCD display, moisture sensor, and a relay to control two lamps.

Uploaded by

Enchantress
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
You are on page 1/ 12

LED

int led 11
void setup()
{
pinMode(led,OUTPUT);
}
void loop()
{
digitalWrite(led,HIGH);
delay(2000);
digitalWrite(led,LOW);
delay(2000);
}

IR sensor

int LEDpin = 13;


int IRsensor = 2;
int hasObstacle = LOW; // LOW MEANS NO OBSTACLE

void setup() {
pinMode(LEDpin, OUTPUT);
pinMode(IRsensor, INPUT);
Serial.begin(9600);
}

void loop() {
hasObstacle = digitalRead(IRsensor);

if (hasObstacle == HIGH) {
Serial.println("Stop something is ahead!!");
Serial.println(hasObstacle);
digitalWrite(LEDpin, HIGH);
}
else {
Serial.println("Path is clear");
digitalWrite(LEDpin, LOW);
}
delay(200);
}

PIR SENSOR

int IRSensor = 2; // connect ir sensor to arduino pin 12


int LED = 13; // conect Led to arduino pin 13

void setup() {

pinMode(2, INPUT);
pinMode(13, OUTPUT);
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
int pir=digitalRead(2);

if (pir==HIGH)
{
digitalWrite(LED, HIGH);
Serial.println("Object Detected");
delay(3000);
}
else
{
digitalWrite(LED, LOW);
Serial.println("No Detection");
delay(1000);
}
// put your main code here, to run repeatedly:

LM 35 TEMPERATURE SENSOR
int val;
int temp = A2;
int LED = 12;

void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
}

void loop() {
val = analogRead(temp);

float mv = (val / 1024.0) * 5000;


float cel = mv / 10;
float farh = (cel * 9) / 5 + 32;

Serial.print("Temperature = ");
Serial.print(cel);
Serial.print("c");
Serial.println();
if (cel > 30)
{ digitalWrite(LED, HIGH);
}
else
{
digitalWrite(LED, LOW);
}
delay(1000); // update sensor reading each one second
}
GAS SENSOR

#include <LiquidCrystal.h>

LiquidCrystal lcd(2,3,4,5,6,7);

int MQPin = 10;


int redLed = 12;
int greenLed = 13;

void setup() {

pinMode(MQPin, INPUT_PULLUP);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
lcd.begin(16, 2);
lcd.setCursor(5, 0);
lcd.print("GAS");
lcd.setCursor(3, 1);
lcd.print("DETECTOR");
delay(3000);
//lcd.clear();
Serial.begin(9600);
}

void loop() {
int gas_value = digitalRead(MQPin);

if(gas_value==HIGH)
{
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
lcd.setCursor(6, 0);
lcd.print("GAS");
lcd.setCursor(3, 1);
lcd.print("DETECTED");
delay(2000);
lcd.clear();
delay(200);

}
else
{
lcd.clear();
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);

delay(100);
}
FLAME SENSOR

int Flame = 10;


int LED = 13;
int val;

void setup() {
Serial.begin(9600);
pinMode(Flame, INPUT);
pinMode(LED, OUTPUT);
}

void loop() {

val=digitalRead(Flame);

if (val == HIGH) {
Serial.println("Fire!!!");
digitalWrite(LED, HIGH);
}
else
{
Serial.println("No worries");
digitalWrite(LED, LOW);
}
delay(1000); // update sensor reading each one second
}
ULTRASONIC

#define trigPin 11
#define echoPin 12

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

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop()
{
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1; //Sound can travel a distance of 0.03435
centimetres in one microsecond.
Serial.print("Distance : ");
Serial.print(distance);
Serial.println(" cm ");
delay(1000);
}

LCD

/*
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
*/

// include the library code:


#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
// set up the lcd's number of columns and rows:
lcd.begin(16, 2);
}

void loop() {
lcd.setCursor(3,0); //sets the cursor at row 0 column 3
lcd.print("Welcome to "); // Welcome to
lcd.setCursor(5,1); //sets the cursor at row 1 column 5
lcd.print("LJIET"); // prints LJIET
delay(2000);

MOISTURE

int SensorPin = A1;


int LED = 13;
int limit = 300;
int output;

void setup() {
Serial.begin(9600);
pinMode(SensorPin, INPUT);
pinMode(LED, OUTPUT);

}
void loop() {

int SensorValue = analogRead(SensorPin);


output = map(SensorValue, 1023, 255, 0, 100);
Serial.print("Analog output value : ");
Serial.println(SensorValue);
Serial.print("Moisture in %: ");
Serial.println(output);

if (SensorValue > limit) {


digitalWrite(LED, HIGH);
}
else {
digitalWrite(LED, LOW);
}
delay(1000);
}

RELAY
int Lamp_1 = 12;
int Lamp_2 = 13;

void setup() {
pinMode(Lamp_1, OUTPUT);
pinMode(Lamp_2, OUTPUT);
Serial.begin(9600);
}

void loop() {

digitalWrite(Lamp_1, HIGH);
Serial.println("Lamp-1 is on");
delay(5000);
digitalWrite(Lamp_1, LOW);
digitalWrite(Lamp_2, HIGH);
Serial.println("Lamp-2 is on");
delay(5000);
digitalWrite(Lamp_2, LOW);

You might also like