CYCLE 2
ARDUINO LAB
EXPERIMENT 1 Built-in LED Blinking, LED Interfacing with Traffic light simulation
a) Built-in LED Blinking
void setup() { // put your setup code here, to run once:
pinMode(LED_BUILTIN,OUTPUT);
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED_BUILTIN,HIGH);
delay(2000);
digitalWrite(LED_BUILTIN,LOW);
delay(2000);
b) LED Interfacing with Traffic light simulation
void setup() {
// put your setup code here, to run once:
pinMode(11,OUTPUT);
pinMode(10,OUTPUT);
pinMode(9,OUTPUT);
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(11,HIGH);
delay(6000);
digitalWrite(11,LOW);
delay(2000);
digitalWrite(10,HIGH);
delay(4000);
digitalWrite(10,LOW);
delay(2000);
digitalWrite(9,HIGH);
delay(2000);
digitalWrite(9,LOW);
delay(2000);
EXPERIMENT 2- IR Sensor Interfacing and Home automation
a) IR Sensor Interfacing
// Arduino IR Sensor Code
int IRSensor = 9; // connect ir sensor module to Arduino pin 9
int LED = 13; // conect LED to Arduino pin 13
void setup()
[Link](115200); // Init Serila at 115200 Baud
[Link]("Serial Working"); // Test to check if serial is working or not
pinMode(IRSensor, INPUT); // IR Sensor pin INPUT
pinMode(LED, OUTPUT); // LED Pin Output
void loop()
int sensorStatus = digitalRead(IRSensor); // Set the GPIO as Input
if (sensorStatus == 1) // Check if the pin high or not
// if the pin is high turn off the onboard Led
digitalWrite(LED, LOW); // LED LOW
[Link]("Motion Ended!"); // print Motion Detected! on the serial monitor window
else
//else turn on the onboard LED
digitalWrite(LED, HIGH); // LED High
[Link]("Motion Detected!"); // print Motion Ended! on the serial monitor window
b) Home automation
In a smart home environment, a study lamp is controlled using touchless air gestures instead of
physical switches. An IR obstacle sensor is placed near the lamp to detect hand movements. The
lamp operates in three different modes based on how long a person keeps their hand in front of the
sensor. If the hand is detected for approximately 3 second, the lamp should enter Low Mode, where
the LED blinks slowly. If the hand is detected for approximately 6 seconds, the lamp should enter
Medium Mode, where the LED remains continuously ON for 10 seconds. If the hand is detected
for approximately 4 seconds or more, the lamp should enter Party Mode, where the LED blinks
rapidly. Very short or accidental hand movements should not trigger any mode. Using only
Arduino, one IR sensor, one LED, and basic delay-based timing, write a program to implement
this gesture-controlled lamp system.
EXPERIMENT 3-soil moisture sensor and Temperature sensor interfacing
a) soil moisture sensor Interfacing
#define ledPin 6
#define sensorPin A0
void setup() {
[Link](9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
void loop() {
[Link]("Analog output: ");
[Link](readSensor());
delay(500);
}
int readSensor() {
int sensorValue = analogRead(sensorPin); // Read the analog value from sensor
int outputValue = map(sensorValue, 0, 1023, 255, 0); // map the 10-bit data to 8-bit data
analogWrite(ledPin, outputValue); // generate PWM signal
return outputValue; // Return analog moisture value
b)Temperature sensor Interfacing
Install DHT Library
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTTYPE DHT11 // DHT 11
#define DHTPIN 2
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
void setup() {
[Link](9600);
[Link](); //creating instance of DHT library
sensor_t sensor;
//delayMS = sensor.min_delay / 1000;
void loop()
{
sensors_event_t event; //creating an event to read data for temp & Humidity
[Link]().getEvent(&event);
[Link](F("Temperature: "));
[Link]([Link]);
[Link](F("°C"));
[Link]().getEvent(&event);
[Link](F("Humidity: "));
[Link](event.relative_humidity);
[Link](F("%"));
delay(1000);
c)In a food storage warehouse, grains such as rice and wheat must be stored under proper
temperature and moisture conditions to prevent spoilage and fungal growth. High moisture
or high temperature can damage stored grains and lead to financial loss. You are provided
with an Arduino, a soil moisture sensor (used here to simulate grain moisture level), a
temperature sensor, and LEDs. Design a monitoring system that indicates storage condition
status. If moisture level is high, a red LED should turn ON. If temperature exceeds a safe
limit (e.g., 35°C), a yellow LED should turn ON. If both temperature and moisture are within
safe limits, a green LED should turn ON. Write an Arduino program to implement this
system.
EXPERIMENT 4: UR Sensor and Servo motor Interfacing
a) UR Sensor
const int trigPin = 4;
const int echoPin = 5;
float duration;
float distance;
void setup() {
// put your setup code here, to run once:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
[Link](9600);
void loop() {
// put your main code here, to run repeatedly:
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
[Link]("Distance: ");
[Link](distance);
[Link](" cm");
delay(1000); }
b)servo motor
Design and implement an Arduino program that displays a menu on the Serial Monitor with the
following options:
1. Sweep Mode – The servo motor should continuously rotate from 0° to 180° and back to 0°
repeatedly.
2. Fixed Position Mode – The servo motor should move to 90° and remain in that position.
3. Step Mode – The servo motor should move in steps of 45° every 2 seconds (0°, 45°, 90°,
135°, 180°, then repeat).
Program::
#include <Servo.h>
Servo servoMotor;
int mode = 0;
int angle = 0;
void setup() {
[Link](5);
[Link](9600);
[Link]("Select Mode:");
[Link]("1 - Sweep");
[Link]("2 - Fixed 90 Degree");
[Link]("3 - Step Movement");
void loop() {
// Check if user entered something
if ([Link]() > 0) {
mode = [Link]();
[Link]("Mode Selected: ");
[Link](mode);
}
// MODE 1: Sweep
if (mode == 1) {
for (int i = 0; i <= 180; i++) {
[Link](i);
delay(15);
for (int i = 180; i >= 0; i--) {
[Link](i);
delay(15);
// MODE 2: Fixed 90 Degree
else if (mode == 2) {
[Link](90);
// MODE 3: Step Movement
else if (mode == 3) {
[Link](angle);
delay(2000);
angle += 45;
if (angle > 180) {
angle = 0;
c) In a smart highway system, toll gates operate automatically when a vehicle approaches. To
reduce manual work and waiting time, an automatic toll gate system is to be designed using an
ultrasonic sensor and a servo motor. When a vehicle comes within 15 cm of the toll gate, the gate
should automatically open by rotating the servo motor to 90°. The gate should remain open for 5
seconds to allow the vehicle to pass, and then automatically close by returning the servo motor to
0°. If no vehicle is detected within the specified distance, the gate should remain closed. Design
and implement the Arduino program to simulate this automatic toll gate system.