21BCP406 LAB7 (1)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

21BCP406 G12 TEJAS MISTRY

Part A
th
Class B Tech CSE 4 Year Sub: Internet of Things
Lab
Aim: LCD will be used with Arduino / ESP32 with various sensors.
Prerequisite: Basics of programming, microcontrollers and basic electronics
Outcome:
1. Study and work of LCD.
2. Connecting microcontroller board with LCD.
3. Display of sensor data over the 16X2 LCD.
Theory:
1. Study and work of LCD.
2. Connection of 16X2 LCD with Arduino and ESP 32 microcontroller boards.
3. Display the values sensed by various sensors, such as ultrasonic sensors,
photosensitive resistance, and potentiometers.
4. Use a DHT sensor to display the temperature and humidity of the atmosphere.

Part B (Write for an individual) 1.

Steps:
#include <Wire.h>
#include<LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); void setup() {


// put your setup code here, to run once:
lcd.init(); lcd.backlight();
}

void loop() {
// put your main code here, to run repeatedly: lcd.setCursor(1, 0);
lcd.print("Hello World"); delay(3000);
lcd.clear();

lcd.setCursor(2, 0); lcd.print("This is a LCD");


lcd.setCursor(2, 1); lcd.print("Screen Test"); delay(3000);
lcd.clear();
}
21BCP406 G12 TEJAS MISTRY

Output:

Observation & Learning:


• Successfully interfaced an LCD with an Arduino using the I2C protocol.
• Displayed messages like "Hello World" and "This is a LCD Screen Test." • Learned to
control the LCD's backlight and clear the screen between messages.
• The delay function worked well to ensure proper message visibility.
• Gained understanding of using I2C for efficient communication and display
management.
Conclusion:
The experiment demonstrated how to use an I2C LCD with an Arduino to display messages. It
showed that the I2C protocol allows for easy communication and requires fewer connections.
This is useful in projects where efficient display management is required with minimal wiring.
21BCP406 G12 TEJAS MISTRY

Part B (Write for an individual)


2.
21BCP406 G12 TEJAS MISTRY
21BCP406 G12 TEJAS MISTRY

Steps:
#include <Wire.h>
#include<LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

const int trigPin = 3;


const int echoPin = 2;

long duration;
int distance;

void setup() {
// put your setup code here, to run once:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

lcd.init();
lcd.backlight();

Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH); delayMicroseconds(10);


digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = duration * 0.0343/2;

lcd.clear(); lcd.setCursor(0,
0); lcd.print("Distance: ");
lcd.print(distance);
lcd.print(" cm");

lcd.setCursor(0, 1); lcd.print("Duration:


"); lcd.print(duration);
lcd.print(" us");
21BCP406 G12 TEJAS MISTRY

delay(3000);
}

Output:

Observation & Learning:


• The 16x2 I2C LCD successfully displayed both distance and duration values measured
by the ultrasonic sensor.
• The ultrasonic sensor measured distance accurately using the trig and echo pins.
• The LCD showed dynamic output, updating the distance and duration values every 3
seconds.
• The connection with the ESP32 and Arduino boards worked efficiently, using minimal
pins through I2C communication.
• The project demonstrated the ability to integrate sensors with display modules for
realtime data output.
Conclusion:
This experiment successfully connected a 16x2 I2C LCD with Arduino and ESP32 boards to
display real-time sensor data from an ultrasonic sensor. The use of the I2C communication
protocol simplified the wiring while effectively managing the display of distance and duration
values. This setup is highly useful for projects requiring real-time data monitoring and display,
such as distance measurement systems or smart devices with sensor inputs
21BCP406 G12 TEJAS MISTRY

Part B (Write for an individual)


3.
Steps:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

#define POT_PIN 34

void setup() { lcd.init();


lcd.backlight();

lcd.setCursor(0, 0); lcd.print("Potentiometer");


lcd.setCursor(0,
1);
lcd.print("Value:");
delay(2000);
lcd.clear();
}

void loop() { int potValue =


analogRead(POT_PIN);

int potPercent = map(potValue, 0, 4095, 0, 100);

lcd.setCursor(0, 0); lcd.print("Pot


Value: ");
lcd.print(potValue);

lcd.setCursor(0, 1);
lcd.print("Percent: "); lcd.print(potPercent);
lcd.print("%");

delay(500);
}
21BCP406 G12 TEJAS MISTRY

Output:

Observation & Learning:


• The 16x2 I2C LCD successfully displayed the analog value sensed by the
potentiometer and converted it to a percentage.
• The potentiometer readings were captured and mapped to a percentage value between 0
and 100.
• The I2C LCD showed the real-time values on the screen, updating every 500
milliseconds.
• The integration with the ESP32 microcontroller was efficient, and the sensor values
were displayed accurately with minimal delay.
• The experiment demonstrated how different types of sensors can be connected to a
microcontroller and their values visualized on an LCD.
Conclusion:
This experiment effectively demonstrated how to interface a potentiometer with an ESP32
and display its real-time values on a 16x2 I2C LCD. By using the I2C protocol, the wiring
was simplified, and the sensor data was presented in a user-friendly format. The ability to
display sensor data, such as potentiometer readings, makes this approach valuable for a wide
range of applications, including monitoring and control systems where real-time feedback is
crucial.
21BCP406 G12 TEJAS MISTRY

Part B (Write for an individual)


4.
21BCP406 G12 TEJAS MISTRY

Steps:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define DHTPIN 2
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.backlight();

dht.begin();

lcd.setCursor(0, 0);
lcd.print("Temp & Humidity");
delay(2000); lcd.clear();
}

void loop() {
// put your main code here, to run repeatedly:
float humidity = dht.readHumidity(); float temperature
= dht.readTemperature();

if(isnan(humidity) || isnan(temperature)){
lcd.setCursor(0, 0); lcd.print("Sensor
Error!");
Serial.println("Failed to read from DHT Sensor!"); return;
}

lcd.setCursor(0, 0); lcd.print("Temp:


");
lcd.print(temperature);

lcd.setCursor(0, 1); lcd.print("Humidity:


");
lcd.print(humidity);

delay(2000);
}
21BCP406 G12 TEJAS MISTRY

Output:

Observation & Learning:


• The DHT sensor successfully measured and displayed both temperature and humidity
values on the 16x2 I2C LCD.
• The LCD showed the initial message "Temp & Humidity" for 2 seconds before clearing
the screen to display real-time readings.
• The sensor readings were updated every 2 seconds, providing current atmospheric
conditions.
• The code included error handling, displaying "Sensor Error!" on the LCD if the sensor
failed to read data.
• The integration of the DHT sensor with the Arduino and I2C LCD demonstrated
effective data acquisition and visualization.
Conclusion:
This experiment effectively illustrated how to interface a DHT sensor with an Arduino to
display temperature and humidity readings on a 16x2 I2C LCD. The setup provided real-time
monitoring of atmospheric conditions while employing basic error handling to manage sensor
failures. This approach is beneficial for environmental monitoring projects, smart home
systems, and weather stations, where tracking temperature and humidity is essential for
making informed decisions.

You might also like