Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
273 views24 pages

Iot Lab Manual

Rajarajeshwari College of Engineering aims to empower students through technology and innovation, producing socially responsible professionals. The Computer Science and Engineering department focuses on extensive technical education, research, and collaboration with industries to address global challenges. The curriculum includes practical lab experiments and objectives to ensure graduates excel in their fields, adapt to evolving technologies, and uphold ethical standards.

Uploaded by

richardwill11
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)
273 views24 pages

Iot Lab Manual

Rajarajeshwari College of Engineering aims to empower students through technology and innovation, producing socially responsible professionals. The Computer Science and Engineering department focuses on extensive technical education, research, and collaboration with industries to address global challenges. The curriculum includes practical lab experiments and objectives to ensure graduates excel in their fields, adapt to evolving technologies, and uphold ethical standards.

Uploaded by

richardwill11
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/ 24

RAJARAJESWARI COLLEGE OF

ENGINEERING

INSTITUTE VISION

To empower young minds through technology, research, and innovation, to produce technically competent and
socially responsible professionals in higher education.

INSTITUTE MISSION

M1: To deliver excellence in education through innovative teaching, impactful research, and continuous skill
development, preparing students to meet global challenges with technical expertise and ethical responsibility.

M2: To foster a transformative learning environment that integrates technology, research, and practical
experience, empowering students to become skilled professionals and socially conscious leaders.

M3: To cultivate a culture of lifelong learning and professional excellence by encouraging creativity, research,
and community engagement, equipping students with the skills to thrive in a dynamic world.

M4: To provide a holistic educational experience that combines advanced technology, hands-on research, and
community-focused learning, shaping students into competent, ethical professionals who contribute positively
to society.
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING

VISION

To produce competent professionals who drive research, foster innovation, and develop technologies that
address global challenges and inspire future generations.

MISSION

M1: Provide extensive technical education to produce trained professionals and entrepreneurs.

M2: Establish a knowledge environment for progressive research by imparting industrial-based skill
development courses on the growth of a modernized environment.

M3: Create and develop innovative skills by collaborating with Industries to generate solutions for societal
impact with moral standards.

M4: To instill lifelong learning, adaptability, and resilience, equipping students to thrive in fast-evolving fields
and continuously advance technology.
PROGRAMME EDUCATIONAL OBJECTIVES (PEOS)

PEO1: Enable graduates to excel in higher education, conduct impactful research, and succeed in careers or
entrepreneurship in Computer Science and Engineering.

PEO2: Ensure graduates develop the skills and mindset to adapt to evolving technologies and acquire new
knowledge continuously.

PEO3: Cultivate professionalism and ethics in graduates, enabling them to contribute to societal progress and
technological advancement.
PROGRAM SPECIFIC OUTCOMES (PSOS)

On successful completion of the program, our graduates will be able to


PSO1: Investigate complex problems across various domains, applying appropriate computational techniques
to systematic design solutions and evaluating their effectiveness.

PSO2: Apply software engineering principles to design and develop high-quality, innovative software systems,
utilizing contemporary and emerging information processing technologies.
PROGRAMME OUTCOMES (POS)
On successful completion of the program, our graduates will be able to
PO1: Engineering Knowledge: Apply knowledge of mathematics, natural science, computing,
engineering fundamentals and an engineering specialization as specified in WK1 to WK4,
respectively, to develop to complex engineering problems.

PO2: Problem Analysis: Identify, formulate, review research literature, and analyse complex
engineering problems reaching substantiated conclusions with consideration for sustainable
development. (WK1 to WK4).

PO3: Design/Development of Solutions: Design creative solutions for complex engineering problems
and design/develop systems/components/processes to meet identified needs with consideration for the
public health and safety, whole-life cost, net zero carbon, culture, society and environment as
required. (WK5).

PO4: Conduct Investigations of Complex Problems: Conduct investigations of complex engineering


problems using research-based knowledge, including design of experiments, modelling, analysis &
interpretation of data to provide valid conclusions. (WK8).
PO5: Engineering Tool Usage: Create, select, and apply appropriate techniques, resources, and
modern engineering & IT tools, including prediction and modelling, recognizing their limitations to
solve complex engineering problems. (WK2 and WK6)
PO6: The Engineer and The World: Analyze and evaluate societal and environmental aspects while
solving complex engineering problems for its impact on sustainability concerning economy, health,
safety, legal framework, culture and environment. (WK1, WK5, and WK7).

PO7: Ethics: Apply ethical principles and commit to professional ethics, human values, diversity and
inclusion; adhere to national & international laws. (WK9)

PO8: Individual and Collaborative Team work: Function effectively as an individual, and as a
member or leader in diverse/multi-disciplinary teams.

PO9: Communication: Communicate effectively and inclusively within the engineering community
and society at large, such as being able to comprehend and write effective reports and design
documentation, make effective presentations considering cultural, language, and learning differences

PO10: Project Management and Finance: Apply knowledge and understanding of engineering
management principles and economic decision-making and apply these to one’s own work, as a
member and leader in a team, and to manage projects and in multidisciplinary environments.

PO11: Life-Long Learning: Recognize the need for, and have the preparation and ability for i)
independent and life-long learning ii) adaptability to new and emerging technologies and iii) critical
thinking in the broadest context of technological change. (WK8)
SYLLABUS
Subject : Internet of Things Lab Code: Teaching
BCS701 (3:0:2:0)
Hours/Week
(L:T:P:S)
8-10 Lab slots CIE Marks 25
Total Hours of Pedagogy
Laboratory Experiments

1. Develop a program to blink 5 LEDs back and forth.


2. Develop a program to interface a relay with an Arduino board.
3. Develop a program to deploy an intrusion detection system using an
Ultrasonic and sound sensor.
4. Develop a program to control a DC motor with an Arduino board.
5. Develop a program to deploy a smart street light system using an LDR
sensor.
6. Develop a program to classify dry and wet waste with the moisture sensor.
7. Develop a program to read the PH value of various substances like milk,
lime, and water.
8. Develop a program to detect the gas leakage in the surrounding
environment.
9. Develop a program to demonstrate weather station readings using Arduino.
10. Develop a program to set up a UART protocol and pass a string through
the protocol.
11. Develop a program for a water level detection system using an Ultrasonic
sensor.
12. Develop a program to simulate interfacing with the keypad module to
record the keystrokes.
COURSE OBJECTIVES

Understand about the fundamentals of Internet of Things and its building blocks along with their
CO 1
characteristics.

CO 2 Understand the recent application domains of IoT in everyday life.


I NTERNET OF THINGS
BCS702

CO 3 Understand the protocols and standards designed for IoT and the current research on it.

CO4 Understand the other associated technologies like cloud and fog computing in the domain of IoT.

Improve their knowledge about the various cutting-edge technologies in the field IoT and machine learning
CO5
applications.

COURSE OUTCOMES

CO 1 Explain the evolution of IoT, IoT networking components, and addressing strategies in IoT. C
I NTERNET OF THINGS

CO 2 Analyze various sensing devices and actuator types.


BCS702

CO 3 Demonstrate the processing in IoT.

CO4 Apply different connectivity technologies.

CO5 Elaborate the need for Data Analytics and Security in IoT.

CO-PO/PSO Mapping

PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PSO1 PSO2
I NTERNET OF THINGS

CO 1 3 2 2 - 2 - - - - - - 2 1
BCS702

CO 2 3 2 2 - 2 - - - - - 2 2 1

CO 3 2 2 2 - 2 - - - - - 2 2 1

CO 4 3 3 2 - 2 - - - - - - 2 1

CO5 3 2 2 - 2 - - - - - - 2 1
Microcontroller: ESP32
Introduction of ESP32:
The ESP32 is a dual-core system with two Harvard Architecture Xtensa
LX6 CPUs. All embedded memory, external memory and peripherals are
located on the data bus and/or the instruction bus of these CPUs.
ESP32 can perform as a complete standalone system or as a slave device to a host
MCU, reducing communication stack overhead on the main application
processor. ESP32 can interface with other systems to provide Wi-Fi and Bluetooth
functionality through its SPI / SDIO or I2C / UART interfaces. The two CPUs are
named “PRO_CPU” and “APP_CPU” (for “protocol” and “application”), however,
for most purposes the two CPUs are interchangeable.
GPIO Pins:
The ESP32 peripherals include:
 18 Analog-to-Digital Converter (ADC) channels

 3 SPI interfaces

 3 UART interfaces

 2 I2C interfaces

 16 PWM output channels

 2 Digital-to-Analog Converters (DAC)

 2 I2S interfaces

 10 Capacitive sensing GPIOs

The ADC (analog to digital converter) and DAC (digital to analog converter)
features are assigned to specific static pins. However, you can decide which pins are
UART, I2C, SPI, PWM, etc. – you just need to assign them in the code. This is
possible due to the ESP32 chip’s multiplexing feature.
Microcontroller: Arduino UNO
Introduction of Arduino UNO:

The Arduino UNO is the best board to get started with electronics and coding. If this
is your first experience tinkering with the platform, the UNO is the most robust board you
can start playing with. The UNO is the most used and documented board of the whole
Arduino family.

Arduino UNO is a microcontroller board based on the ATmega328P. It has 14 digital


input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz ceramic
resonator, a USB connection, a power jack, an ICSP header and a reset button. It contains
everything needed to support the microcontroller; simply connect it to a computer with a
USB cable or power it with a AC-to-DC adapter or battery to getstarted. You can tinker with
your UNO withoutworrying too much about doing something wrong, worst case scenario you
can replace the chip for a few dollars and start over again.

GPIO Pin Outs:

Sensors
Sensors Description
DHT 22 :

The DHT11 is a commonly used Temperature and humidity sensor that comes
with a dedicated NTC to measure temperature and an 8-bit microcontroller
to output the values of temperature and humidity as serial data.

BMP180 Barometric Pressure and Altitude Sensor:

The BMP180 Breakout has been designed to be used in indoor/outdoor


navigation, weather forecasting, home automation, and even personal health
and wellness monitoring. And to measure barometric pressure, and
temperature readings all without taking up too much space.

TOUCH PAD:

A touch sensor is a type of sensor that captures and records physical touch or
proximity. Provides direct mode i.e. toggle mode by pad option.

Ultrasonic Sensor

An ultrasonic sensor is an instrument that measures the distance to an object using


ultrasonic sound waves. An ultrasonic sensor uses a transducer to send and receive
ultrasonic pulses that relay back information about an object's proximity.
LDR Sensor:

The LDR is a special type of resistor that works on the photoconductivity


principle means that resistance changes according to the intensity of light. Its
resistance decreases with an increase in the intensity of light.

IR Sensor:

IR sensor is a device that uses infrared technology to detect objects or changes


in the environment. IR sensors can detect a wide range of physical properties
such as temperature, motion, and proximity.
MQ135 Gas Sensor:

The MQ 135 sensor can be implemented to detect smoke, benzene, vapors, and
other hazardous gases. It can detect various harmful gases.

Active Buzzer:

Buzzer meaning electronic component that generates sound through the


transmission of electrical signals. Its primary function is to provide an audible
alert or notification and typically operates within a voltage range of 5V to 12V.

RGB LED:

SMD RGB LED technology allows the colour of light to be changed by using
three diodes: red, blue and green.

Push Button:

A push button switch is a mechanical device used to control an electrical circuit


in which the operator manually presses a button to actuate an internal switching
mechanism.

10K POT:

This adjustable or Variable resistor are PCB mountable and has 3 terminals. The
voltage between the terminal varies as the preset is rotated. The Variable
resistors are used for variation voltage as per the need in a circuit.

5V Realy:

The primary purpose of a relay is to protect the electrical system from too high
of a voltage or current, allowing the safe operation of any equipment it connects
to.
OLED :

An organic light-emitting diode (OLED), also known as organic


electroluminescent (organic EL) diode is a type of light-emitting diode (LED).
OLEDs enable emissive displays - which means that each pixel is controlled
individually and emits its own light (unlike LCDs in which the light comes from
a backlighting unit).

Soil Mositure:

A Soil Moisture Sensor is used to measure the water content in soil.


It's often used in gardening, agriculture, and environmental monitoring
projects.

4x4 Matrix Keypad:


A 4x4 matrix keypad is a 16-button input device arranged in 4 rows and 4
columns. Each button connects a row to a column when pressed. It’s commonly
used in embedded systems for entering numbers, passwords, or menu selections. ✅ 1.
Install
Arduino
IDE
PH Sensor:

A pH sensor measures how acidic or basic a solution is, usually in the range of
0 (acidic) to 14 (basic). Pure water has a neutral pH of ~7.

1. Open your browser and go to:


👉 https://www.arduino.cc/en/software
2. Download the Arduino IDE for your operating system (Windows, Mac, or Linux).
3. Run the downloaded file and follow the on-screen installation instructions.
4. After installation, open the Arduino IDE.

✅ 2. Install ESP32 Board Manager


1. Open the Arduino IDE.
2. Go to File → Preferences.
3. In the "Additional Board Manager URLs" field, paste this URL:
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
4. Click OK.

✅ 3. Add ESP32 Board in Board Manager


1. Go to Tools → Board → Boards Manager.
2. In the search box, type esp32.
3. Select "esp32 by Espressif Systems", and click Install.
4. Wait for installation to complete.

🖥️ Selecting Your Board for Projects


🔹 For Arduino Uno Projects
1. Go to Tools → Board → Arduino AVR Boards → Arduino Uno.

🔹 For ESP32 Projects (IoT Experiments)


1. Go to Tools → Board → ESP32 Arduino → DOIT ESP32 DEVKIT V1.

✅ 4. Select the Right COM Port


1. Connect your board to your PC using a USB cable.
2. Go to Tools → Port, and select the port labeled (Arduino Uno) or (ESP32).
o Example: COM3 (Arduino Uno) or COM5 (DOIT ESP32 DEVKIT V1)

Experiment 1: Develop a program to blink 5 LEDs back and forth.


Pin Connections:
LED ESP32 Pin Arduino UNO Pin
L1 GPIO 2 D5
L2 GPIO 4 D6
L3 GPIO 5 D7
L4 GPIO 18 D8
L5 GPIO 19 D9

Code:
// Define GPIO pins for the LEDs
const int ledPins[] = {2, 4, 5, 18, 19}; // Adjust pins if needed
const int numLeds = sizeof(ledPins) / sizeof(ledPins[0]);

void setup() {
// Set all LED pins as OUTPUT
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW); // Turn off all LEDs at start
}
}
void loop() {
// Blink LEDs from left to right
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], HIGH);
delay(1000);
digitalWrite(ledPins[i], LOW);
}
// Blink LEDs from right to left
for (int i = numLeds - 2; i > 0; i--) {
digitalWrite(ledPins[i], HIGH);
delay(1000);
digitalWrite(ledPins[i], LOW);
}
}

Experiment 2: Develop a program to interface a relay with Arduino board.


Pin Connections:
Relay ESP32 Pin Arduino UNO Pin
Relay 1/2 GPIO 15 D7

Code:
// Define the relay control pin
const int relayPin = 7; // You can use any digital pin

void setup() {
pinMode(relayPin, OUTPUT); // Set the relay pin as output
digitalWrite(relayPin, HIGH); // Turn relay off initially (assuming active LOW)
}

void loop() {
digitalWrite(relayPin, LOW); // Turn ON relay (connects the load)
delay(1000); // Wait for 2 seconds

digitalWrite(relayPin, HIGH); // Turn OFF relay (disconnects the load)


delay(1000); // Wait for 2 seconds
}

Experiment 3: Develop a program to deploy an intrusion detection system


using Ultrasonic and sound sensor.
Pin Connections:
Arduino UNO
Component ESP32 Pin
Pin
Ultrasonic TRIG GPIO 13 D9
ECHO GPIO 12 D10
Buzzer Buzzer pin GPIO 15 D7

Code:
// Pin definitions
const int trigPin = 9;
const int echoPin = 10;
const int buzzerPin = 7;

long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);

Serial.begin(9600);
}

void loop() {
// --- Ultrasonic sensor distance measurement ---
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2; // Convert to cm

// --- Intrusion Detection Condition ---


if (distance < 50 ) { // Threshold: 50 cm or sound detected
digitalWrite(buzzerPin, HIGH); // Alarm ON
Serial.println("Intrusion Detected!");
} else {
digitalWrite(buzzerPin, LOW); // Alarm OFF
}

// Print distance for debugging


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

delay(500);
}

Experiment 4: Develop a program to control a DC motor with Arduino board.


Pin Connections:
Relay ESP32 Pin Arduino UNO Pin
Relay 1/2 GPIO 15 D7
Push Button 1/2 GPIO 4 D2

Motor to Relay:

 One terminal of the motor connects to + of power supply.


 The other motor terminal connects to COM (Common) of the relay
 NO (Normally Open) of the relay goes to GND of power supply

Code :
const int relayPin = 7; // Relay control pin
const int buttonPin = 2; // Push button pin

void setup() {
pinMode(relayPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}

void loop() {
int buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
if (buttonState == 0) {
digitalWrite(relayPin,HIGH);
}
else {
digitalWrite(relayPin, LOW);
}
}
Experiment 5: Develop a program to deploy smart street light system using
LDR sensor.

Pin Connections:
Component ESP32 Pin Arduino UNO Pin
LED GPIO 25 D5
LDR GPIO 34 A0

Code:
const int ldrPin = A0; // LDR connected to analog pin A0
const int lightPin = 5; // LED or relay control pin
int lightThreshold = 500; // Adjust threshold based on environment

void setup() {
pinMode(ldrPin, INPUT);
pinMode(lightPin, OUTPUT);
Serial.begin(9600);
}

void loop() {
int ldrValue = analogRead(ldrPin);
Serial.print("LDR Value: ");
Serial.println(ldrValue);

if (ldrValue < lightThreshold) {


digitalWrite(lightPin, HIGH); // Turn ON light (dark environment)
} else {
digitalWrite(lightPin, LOW); // Turn OFF light (bright environment)
}

delay(500);
}
Experiment 6: Develop a program to classify dry and wet waste with the
moisture sensor.

Pin Connections:
ESP32
Component Arduino UNO Pin
Pin
Moisture Sensor
GPIO 32 A0
A0

Code for Arduino UNO:


#define MOISTURE_SENSOR_PIN 34 // Use A0 for Arduino Uno

void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("Moisture-Based Waste Classification Started...");
}

void loop() {
int moistureValue = analogRead(MOISTURE_SENSOR_PIN);
Serial.print("Moisture Sensor Value: ");
Serial.println(moistureValue);

if (moistureValue > 2000) {


Serial.println("Waste Type: DRY");
} else {
Serial.println("Waste Type: WET");
}

Serial.println("----------------------");
delay(2000); // Delay for readability
}
Experiment 7: Develop a program to read the PH value of a various substances
like milk, lime and water.

Pin Connections:
Component ESP32 Pin Arduino UNO Pin
P0 GPIO 34 A3
VCC 5V 5V
GND GND GND

Code:
#define PH_SENSOR_PIN 36 // GPIO36 (VP on ESP32)

void setup() {
Serial.begin(115200);
analogReadResolution(12); // 12-bit ADC (0–4095)
}

void loop() {
int adc_value = analogRead(PH_SENSOR_PIN); // Read raw ADCs
float voltage = (adc_value * 5.0) / 4095.0; // Convert ADC to voltage (0–3.3V)

// Assume pH sensor gives 0V = pH 0 and 3.0V = pH 14 (standard range)


float pH_value = (voltage / 3.0) * 14.0; // Linear mapping to pH

Serial.print("ADC: ");
Serial.print(adc_value);
Serial.print(" | Voltage: ");
Serial.print(voltage, 3);
Serial.print(" V | Estimated pH: ");
Serial.println(pH_value, 2);

delay(2000); // Read every 2 seconds


}
Experiment 8: Develop a program to detect the gas leakage in the surrounding
environment

Pin Connections:
Component ESP32 Pin Arduino UNO Pin
MQ135 A0 GPIO 15 A0
Buzzer GPIO 4 D13

Code:
const int gasSensorPin = A0; // Use GPIO34 or GPIO36 if using ESP32 (input-only is fine for analog read)
const int buzzerPin = 13; // Connect buzzer to digital pin

void setup() {
Serial.begin(115200);
pinMode(buzzerPin, OUTPUT);
}

void loop() {
int gasValue = analogRead(gasSensorPin);
Serial.print("Gas Sensor Value: ");
Serial.println(gasValue);

if (gasValue > 300) { // Threshold may vary depending on environment


digitalWrite(buzzerPin, HIGH);
Serial.println("Gas leakage detected!");
} else {
digitalWrite(buzzerPin, LOW);
}

delay(1000);
}
Experiment 9: Develop a program to demonstrate weather station readings
using Arduino
Libraries :
 Install the DHT library by Adafruit

 Install

Adafruit_BMP085 Library by Adafruit


Pin Connections:
Component ESP32 Pin Arduino UNO Pin
DHT22 O/P GPIO 4 D2
SDA GPIO 21 A4
BMP180
SCL GPIO 22 A5

Code:
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_BMP085.h> // For BMP180
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

Adafruit_BMP085 bmp;

void setup() {
Serial.begin(115200);
dht.begin();
if (!bmp.begin()) {
Serial.println("BMP180 not detected.");
while (1);
}
}
void loop() {
float temp = dht.readTemperature();
float hum = dht.readHumidity();
float pressure = bmp.readPressure() / 100.0;
Serial.print("Temp: "); Serial.print(temp); Serial.print(" °C, ");
Serial.print("Humidity: "); Serial.print(hum); Serial.print(" %, ");
Serial.print("Pressure: "); Serial.print(pressure); Serial.println(" hPa");
delay(2000);
}
Experiment 10: Develop a program to setup a UART protocol and pass a string
through the protocol.
Pin Connections:
Component ESP32 Pin Arduino UNO Pin
LED GPIO 2 D13
Code:
#define LED_PIN 2 // GPIO2 for ESP32, use 13 for Arduino Uno
String inputString = ""; // To store incoming serial data
bool stringComplete = false;

void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("UART Command Processor Initialized.");
inputString.reserve(200); // Optional: reserve memory for speed
}
void loop() {
// When a complete command is received
if (stringComplete) {
processCommand(inputString);
inputString = "";
stringComplete = false;
}
}
// Serial Event triggered by new data
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;

// Check for end of line


if (inChar == '\n') {
stringComplete = true;
}
}
}
void processCommand(String command) {
command.trim(); // Remove newline or extra whitespace

if (command.equalsIgnoreCase("LED ON")) {
digitalWrite(LED_PIN, HIGH);
Serial.println("LED is turned ON");
}
else if (command.equalsIgnoreCase("LED OFF")) {
digitalWrite(LED_PIN, LOW);
Serial.println("LED is turned OFF");
}
else if (command.equalsIgnoreCase("STATUS")) {
int state = digitalRead(LED_PIN);
if (state == HIGH) {
Serial.println("LED STATUS: ON");
} else {
Serial.println("LED STATUS: OFF");
}
}
else {
Serial.print("Unknown Command: ");
Serial.println(command);
}
}

Experiment 11: Develop a program to a water level detection system using


Ultrasonic sensor.

Pin Connections:
Arduino UNO
Component ESP32 Pin
Pin
Ultrasonic TRIG GPIO 5 D9
ECHO GPIO 18 D10

Code:
const int trigPin = 5;
const int echoPin = 18;

void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
long getDistance() {
digitalWrite(trigPin, LOW); delayMicroseconds(2);
digitalWrite(trigPin, HIGH); delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
long distance = duration * 0.034 / 2;
return distance;
}
void loop() {
long distance = getDistance();
Serial.print("Water Level Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(1000);
}
Experiment 12: Develop a program to simulate interfacing with the keypad
module to record the keystrokes.
Libraries :

Pin Connections:
Arduino
Component ESP32 Pin
UNO Pin
R1 GPIO 14 D2
R2 GPIO 27 D3
R3 GPIO 26 D4
Keypad R4 GPIO 25 D5
C1 GPIO 33 D6
C2 GPIO 32 D7
C3 GPIO 19 D8

Code:
#include <Keypad.h>

const byte ROWS = 4; // Now 4 rows (same as original 4x4)


const byte COLS = 3; // Now 3 columns

// Define the keymap for a 4x3 keypad


char keys[ROWS][COLS] = {
{'1','2','3'}, // Row 0
{'4','5','6'}, // Row 1
{'7','8','9'}, // Row 2
{'*','0','#'} // Row 3 (Common layout for 4x3)
};

// Connect these ESP32 GPIO pins to your keypad's ROW pins


// You will need 4 pins for rows
byte rowPins[ROWS] = {14, 27, 26, 25}; // Remains the same as original 4x4

// Connect these ESP32 GPIO pins to your keypad's COLUMN pins


// You will need 3 pins for columns
byte colPins[COLS] = {33, 32, 19}; // Removed one pin as there are only 3 columns (GPIO 18 removed)

// Create the Keypad object


Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup() {
Serial.begin(115200);
Serial.println("4x3 Keypad Ready");
}

void loop() {
char key = keypad.getKey(); // Check for a key press

if (key) { // If a key was pressed (key is not NO_KEY)


Serial.print("Key Pressed: ");
Serial.println(key);
}
}

You might also like