1.
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.
int ldrPin = A0;
int ledPin1 = 9;
int ledPin2 = 10;
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
Serial.begin(9600);
}
void loop() {
int ldrValue = analogRead(ldrPin);
Serial.print("LDR Value: ");
Serial.println(ldrValue);
int brightness = map(ldrValue, 0, 1023, 0, 255);
analogWrite(ledPin1, brightness);
if (ldrValue < 200) {
digitalWrite(ledPin2, HIGH);
delay(500);
digitalWrite(ledPin2, LOW);
delay(500);
} else {
digitalWrite(ledPin2, LOW);
}
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);
if (entryState == HIGH && exitState == LOW) {
count += 1;
Serial.print("Person entered. Count: ");
Serial.println(count);
delay(1000);
}
if (entryState == LOW && exitState == HIGH) {
count -= 1;
Serial.print("Person exited. Count: ");
Serial.println(count);
delay(1000);
}
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
int rainfallSensorPin = A0;
int ledPin1 = 13;
int ledPin2 = 12;
void setup() {
pinMode(rainfallSensorPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
Serial.begin(9600);
}
void loop() {
int rainfallLevel = analogRead(rainfallSensorPin);
if (rainfallLevel < 500) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
Serial.println("No rain");
} else if (rainfallLevel < 800) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
Serial.println("Light rain");
} else {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
Serial.println("Heavy rain");
}
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”.
const int clapPin = 2;
int clapCount = 0;
void setup() {
Serial.begin(9600);
pinMode(clapPin, INPUT_PULLUP);
}
void loop() {
if (digitalRead(clapPin) == LOW) {
clapCount++;
Serial.print("Claps: ");
Serial.println(clapCount);
if (clapCount >= 10) {
Serial.println("THAT IS ENOUGH");
clapCount = 0;
}
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”.
const int sensorPin = A0;
const int triggerPin = 2;
const int echoPin = 3;
const int distanceThreshold = 10;
const unsigned long durationThreshold = 60000;
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);
long duration = pulseIn(echoPin, HIGH);
float distance = duration * 0.034 / 2;
if (distance < distanceThreshold) {
static unsigned long startTime = millis();
unsigned long elapsedTime = millis() - startTime;
if (elapsedTime >= durationThreshold) {
Serial.println("PLEASE MOVE");
startTime = millis();
}
} else {
startTime = millis();
}
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.
const int sensorPin = A0;
const int ledPin = 9;
const int maxDistance = 100;
const int minBrightness = 0;
const int maxBrightness = 255;
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.
const int soilHumidityPin = A0;
const int ledPin = 9;
const int maxHumidity = 1023;
const int minBrightness = 0;
const int maxBrightness = 255;
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.
const int tempSensorPin = A0;
const int ledPin = 9;
const int maxTemp = 50;
const int minBrightness = 0;
const int maxBrightness = 255;
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.
const int piezo1Pin = A0;
const int piezo2Pin = A1;
const int ledPin = 9;
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);
}