0% found this document useful (0 votes)
30 views

IoT Lab Mid Suggestion

The document provides code snippets for several IoT lab experiments involving distance measurement with ultrasonic sensor, temperature measurement with DHT sensor, LCD display, 4x4 keypad, and LED blinking with buzzer. The main functions and architectures are defined to implement these experiments.

Uploaded by

shorif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

IoT Lab Mid Suggestion

The document provides code snippets for several IoT lab experiments involving distance measurement with ultrasonic sensor, temperature measurement with DHT sensor, LCD display, 4x4 keypad, and LED blinking with buzzer. The main functions and architectures are defined to implement these experiments.

Uploaded by

shorif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

IoT Lab mid suggestion

2 Set -20 marks code main function or architecture

<=============DISTANCE Measurement==========>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Define the pins for the ultrasonic sensor


const int trigPin = 7;
const int echoPin = 6;
const int buzzerPin = 11;

// Define the I2C address of the LCD


const int lcdAddress = 0x3F; // Adjust this address according to your LCD

// Initialize the LCD library with the I2C address


//LiquidCrystal_I2C lcd(lcdAddress, 16, 2);
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the numbers for your specific LCD size

void setup() {
// Initialize Serial communication for debugging (optional)
Serial.begin(9600);

// Initialize the LCD display


lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);

// Set trigPin as an OUTPUT


pinMode(trigPin, OUTPUT);

// Set echoPin as an INPUT


pinMode(echoPin, INPUT);
pinMode(buzzerPin,OUTPUT);
}

void loop() {
// Clear the trigger pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Send a 10us pulse to trigger the ultrasonic sensor
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Measure the duration of the echo pulse


long duration = pulseIn(echoPin, HIGH);

// Calculate the distance in centimeters


int distance = (duration * 0.0343) / 2;

// Print the distance to the Serial monitor (optional for debugging)


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

if (distance<10){
digitalWrite(buzzerPin, HIGH);
}else{
digitalWrite(buzzerPin, LOW);
}

lcd.print(distance);
lcd.print(" cm");
delay(500); // Delay to update the LCD display every 0.5 seconds
lcd.clear();
Architecture:
<=====================Temperature Measurement ===================>

#include <DHT.h>

#define DHTPIN D2 // Define the pin where your DHT11 is connected


#define DHTTYPE DHT11 // Define the type of DHT sensor you are using

DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
dht.begin();
}

void loop() {
delay(2000); // Wait for 2 seconds between measurements

float temperature = dht.readTemperature(); // Read temperature in Celsius


float humidity = dht.readHumidity(); // Read humidity in percentage

if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
} else {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
}
}
Architecture:
<================LCD DISPLAY===============>

#include <Wire.h> // Include the Wire library for I2C communication


#include <LiquidCrystal_I2C.h> // Include the LCD library

// Initialize the LCD with the I2C address (0x27 for a common address)
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the numbers for your specific LCD size

void setup() {
// Initialize the LCD
lcd.init();

// Turn on the backlight (optional)


lcd.backlight();

// Print a message to the LCD


lcd.setCursor(0, 0); // Set cursor to the first row, first column
lcd.print("Manik's wow..!");
}

void loop() {
// Nothing to do here in this simple example
}

<========================LCD display============>
#include <Arduino.h>
#include <LiquidCrystal_I2C.h>

// Define the LCD I2C address


const int LCD_I2C_ADDRESS = 0x27;
// Create an instance of the LiquidCrystal_I2C class
LiquidCrystal_I2C lcd(LCD_I2C_ADDRESS, 16, 2);

void setup() {
// Initialize the LCD display
lcd.init();

// Clear the LCD display


lcd.clear();

// Display the message "Hello, world!" on the LCD display


lcd.print("Hello, world!");
}

void loop() {
// Delay for 100 milliseconds
delay(100);
}
Architecture:

<========================= 4x4 keypad==========================>


#include <Arduino.h>

// Define the keypad pins


const int KEYPAD_ROWS[4] = {2, 3, 4, 5};
const int KEYPAD_COLUMNS[4] = {6, 7, 8, 9};

// Define the keypad keys


char KEYPAD_KEYS[16] = {
'1', '2', '3', 'A',
'4', '5', '6', 'B',
'7', '8', '9', 'C',
'*', '0', '#', 'D'
};

void setup() {
// Set the keypad row pins as output
for (int i = 0; i < 4; i++) {
pinMode(KEYPAD_ROWS[i], OUTPUT);
}

// Set the keypad column pins as input


for (int i = 0; i < 4; i++) {
pinMode(KEYPAD_COLUMNS[i], INPUT);
}

// Initialize the serial monitor


Serial.begin(9600);
}

void loop() {
// Read the keypad
char key = readKeypad();

// If a key is pressed, print it to the serial monitor


if (key != '\0') {
Serial.println(key);
}

// Delay for 100 milliseconds


delay(100);
}

// Function to read the keypad


char readKeypad() {
// Set all keypad row pins to LOW
for (int i = 0; i < 4; i++) {
digitalWrite(KEYPAD_ROWS[i], LOW);
}

// Scan each keypad column


for (int i = 0; i < 4; i++) {
// Set the current keypad row pin to HIGH
digitalWrite(KEYPAD_ROWS[i], HIGH);
// Check if any keypad column pin is HIGH
for (int j = 0; j < 4; j++) {
if (digitalRead(KEYPAD_COLUMNS[j]) == HIGH) {
// A key is pressed
return KEYPAD_KEYS[i * 4 + j];
}
}

// Set the current keypad row pin to LOW


digitalWrite(KEYPAD_ROWS[i], LOW);
}

// No key is pressed
return '\0';
}
Architecture:

<=======================LED Blinking with Buzzer==============>

int LED =13;


int BUZZER =12;
void setup() {
pinMode(LED, OUTPUT);
pinMode(BUZZER, OUTPUT);
}

// the loop function runs over and over again forever


void loop() {
digitalWrite(LED, HIGH);
delay(5000);

for(int i=1; i<=5; i++){


digitalWrite(BUZZER, HIGH);
delay(1000);
digitalWrite(BUZZER, LOW);
delay(1000);
}

digitalWrite(LED, LOW);
delay(5000);
}

Architecture:

You might also like