Sensor Lab
Sensor Lab
Sensor Lab
Write a program to count the number of people entering a room, the count should be displayed on the serial
window. The count should stop when the value reaches 10 and show the massage “ROOM FULL”.
int count = 0;
int sensorPin = 2;
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = digitalRead(sensorPin);
if (sensorValue == HIGH) {
count++;
Serial.println("Person entered the room");
Serial.print("Count: ");
Serial.println(count);
delay(1000);
}
if (count >= 10) {
Serial.println("ROOM FULL");
while (1) {}
}
}
2. Write a program to measure the distance of an object from a given location. Whenever the distance is less
than 8cm a LED should turn ON. Whenever distance is less than 4cm 2 two LEDs should turn ON. When no
object is detected both LEDs should keep blinking.
#define trigPin 9
#define echoPin 10
#define ledPin1 5
#define ledPin2 6
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration / 58.2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (distance < 8) {
digitalWrite(ledPin1, HIGH);
} else {
digitalWrite(ledPin1, LOW);
}
if (distance < 4) {
digitalWrite(ledPin2, HIGH);
} else {
digitalWrite(ledPin2, LOW);
}
if (distance == 0) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
delay(500);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
delay(500);
}
delay(500);
}
3. Write a program which will control the intensity of brightness of a LED based on the amount of light falling
on LDR. Also connect another LED which will blink continuously as long as the LDR is kept in dark.
void loop() {
int ldrValue = analogRead(ldrPin);
Serial.print("LDR Value: ");
Serial.println(ldrValue);
delay(100);
}
5. Use two IR sensors to detect entry and exit people from the room. A counter should increment when a
person enters the room and it should decrement when a person exits the room. The count should be
displayed on the serial monitor.
int entrySensorPin = 2;
int exitSensorPin = 3;
int count = 0;
void setup() {
pinMode(entrySensorPin, INPUT);
pinMode(exitSensorPin, INPUT);
Serial.begin(9600);
}
void loop() {
int entryState = digitalRead(entrySensorPin);
int exitState = digitalRead(exitSensorPin);
Serial.print("Count: ");
Serial.println(count);
delay(100);
}
7. A rainfall sensor is detecting the amount of rain falling. Use a two LED binary code to show the following
conditions
No rain 0 0
Light Rain 0 1
HeavyRain 1 0
void setup() {
pinMode(rainfallSensorPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
Serial.begin(9600);
}
void loop() {
int rainfallLevel = analogRead(rainfallSensorPin);
delay(100);
}
8. Use three LEDs and a IR sensor to show the count of people entering a room in binary. For example one
person enters the count should be 001, second person enters the count should be 010 .
int irSensorPin = 2;
int ledPin1 = 13;
int ledPin2 = 12;
int ledPin3 = 11;
int count = 0;
void setup() {
pinMode(irSensorPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
Serial.begin(9600);
}
void loop() {
int irSensorValue = digitalRead(irSensorPin);
if (irSensorValue == HIGH) {
count++;
Serial.println(count);
}
if (count == 0) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
} else if (count == 1) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
} else if (count == 2) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, LOW);
} else if (count == 3) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, LOW);
} else if (count == 4) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
} else if (count == 5) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
} else if (count == 6) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
} else if (count == 7) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
}
delay(100);
}
10. Count the number of claps sensed by the clap sensor. Every clap detected should be displayed as a count on
the serial window. If 10 or more claps are detected a message should be displayed as “THAT IS ENOUGH”.
void setup() {
Serial.begin(9600);
pinMode(clapPin, INPUT_PULLUP);
}
void loop() {
if (digitalRead(clapPin) == LOW) {
clapCount++;
Serial.print("Claps: ");
Serial.println(clapCount);
delay(500);
}
}
11. Write a program to detect if an object is present in front of the sensor at a distance of less than 10 cm for
more than one minute continuously. If yes, it should display a message “PLEASE MOVE”.
void setup() {
Serial.begin(9600);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
delay(100);
}
void setup() {
void loop() {
12. Write a program to control the brightness of an LED based on the distance of an object from the sensor.
Closer the object, brighter the LED should glow.
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(sensorPin);
float distance = (1024 - sensorValue) * 5.0 / 1024.0;
int brightness = map(distance, 0, maxDistance, maxBrightness, minBrightness);
analogWrite(ledPin, brightness);
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm, Brightness: ");
Serial.println(brightness);
delay(100);
}
13. Write a program to control the brightness of an LED based on the soil humidity. Higher the humidity,
brighter the LED should glow.
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int soilHumidityValue = analogRead(soilHumidityPin);
int brightness = map(soilHumidityValue, 0, maxHumidity, minBrightness, maxBrightness);
analogWrite(ledPin, brightness);
Serial.print("Soil Humidity: ");
Serial.print(soilHumidityValue);
Serial.print(", Brightness: ");
Serial.println(brightness);
delay(100);
}
14. Write a program to control the brightness of an LED based on temperature. Higher the temperature,
brighter the LED should glow.
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int tempSensorValue = analogRead(tempSensorPin);
float temperature = (5.0 * tempSensorValue * 100.0) / 1024.0;
int brightness = map(temperature, 0, maxTemp, minBrightness, maxBrightness);
analogWrite(ledPin, brightness);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" C, Brightness: ");
Serial.println(brightness);
delay(100);
}
15. Write a program to compare the output of two Piezoelectric generators. An LED should turn ON when both
the Piezoelectric generators generate the same voltage.
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int piezo1Value = analogRead(piezo1Pin);
int piezo2Value = analogRead(piezo2Pin);
if (piezo1Value == piezo2Value) {
digitalWrite(ledPin, HIGH);
Serial.println("Both Piezoelectric generators are generating the same voltage");
} else {
digitalWrite(ledPin, LOW);
}
delay(100);
}