0% found this document useful (0 votes)
2 views2 pages

Sensor Programs Arduino

The document contains several Arduino programs for different sensors and components. It includes code for an LED blink program, a light sensor (LDR) program, a temperature sensor (LM35) program, an ultrasonic sensor program, and a smoke sensor (MQ-2) program. Each program initializes the respective sensor, reads data, and performs actions based on the readings, such as turning on an LED or printing values to the serial monitor.

Uploaded by

lasrireddy5
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)
2 views2 pages

Sensor Programs Arduino

The document contains several Arduino programs for different sensors and components. It includes code for an LED blink program, a light sensor (LDR) program, a temperature sensor (LM35) program, an ultrasonic sensor program, and a smoke sensor (MQ-2) program. Each program initializes the respective sensor, reads data, and performs actions based on the readings, such as turning on an LED or printing values to the serial monitor.

Uploaded by

lasrireddy5
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/ 2

1.

LED Blink Program

void setup() {
pinMode(13, OUTPUT); // Set pin 13 as output (LED)
}

void loop() {
digitalWrite(13, HIGH); // Turn LED on
delay(1000); // Wait 1 second
digitalWrite(13, LOW); // Turn LED off
delay(1000); // Wait 1 second
}

2. LDR (Light Sensor) Program

int ldr = A0; // LDR connected to A0


int led = 13; // LED on pin 13

void setup() {
pinMode(led, OUTPUT);
}

void loop() {
int value = analogRead(ldr); // Read light level

if (value < 500) { // If it's dark


digitalWrite(led, HIGH); // Turn on LED
} else {
digitalWrite(led, LOW); // Turn off LED
}

delay(500);
}

3. Temperature Sensor (LM35) Program

int sensor = A0; // LM35 on A0

void setup() {
Serial.begin(9600); // Start serial monitor
}

void loop() {
int value = analogRead(sensor);
float temp = value * 0.488; // Convert to Celsius

Serial.print("Temp: ");
Serial.println(temp);

delay(1000);
}

4. Ultrasonic Sensor Program

int trig = 9;
int echo = 10;

void setup() {
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
Serial.begin(9600);
}

void loop() {
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);

long time = pulseIn(echo, HIGH);


int distance = time * 0.034 / 2;

Serial.print("Distance: ");
Serial.println(distance);

delay(500);
}

5. Smoke Sensor (MQ-2) Program

int smoke = A0; // MQ-2 on A0


int led = 13;

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

void loop() {
int value = analogRead(smoke); // Read smoke level

if (value > 400) {


digitalWrite(led, HIGH); // Turn on LED if smoke is high
} else {
digitalWrite(led, LOW);
}

Serial.println(value);
delay(1000);
}

You might also like