0% found this document useful (0 votes)
0 views6 pages

Arduino%20Programming_20BCE10885-1

The document provides Arduino programming examples for interfacing with various sensors including a temperature sensor, humidity sensor, and ultrasonic sensor. Each section includes code snippets that demonstrate how to read data from the sensors and display the results on a serial monitor or LCD. The examples illustrate the conversion of sensor readings into meaningful values such as temperature in Celsius and Fahrenheit, and humidity levels, while also controlling LED indicators based on distance measurements from the ultrasonic sensor.

Uploaded by

HAGE Maru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
0 views6 pages

Arduino%20Programming_20BCE10885-1

The document provides Arduino programming examples for interfacing with various sensors including a temperature sensor, humidity sensor, and ultrasonic sensor. Each section includes code snippets that demonstrate how to read data from the sensors and display the results on a serial monitor or LCD. The examples illustrate the conversion of sensor readings into meaningful values such as temperature in Celsius and Fahrenheit, and humidity levels, while also controlling LED indicators based on distance measurements from the ultrasonic sensor.

Uploaded by

HAGE Maru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

Fundamentals of Sensors and IoT

Arduino Programming
Mehak Chabra 20BCE10885
Question 1: Interfacing with temperature sensor
// Define the analog pin, the LM35's Vout pin is connected to
#define sensorPin A0

void setup() {
// Begin serial communication at 9600 baud rate
Serial.begin(9600);
}

void loop() {
// Get the voltage reading from the LM35
int reading = analogRead(sensorPin);

// Convert that reading into voltage


float voltage = reading * (5.0 / 1024.0);

// Convert the voltage into the temperature in Celsius


float temperatureC = voltage * 100;

// Print the temperature in Celsius


Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.print("\xC2\xB0"); // shows degree symbol
Serial.print("C | ");

// Print the temperature in Fahrenheit


float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF);
Serial.print("\xC2\xB0"); // shows degree symbol
Serial.println("F");

delay(1000); // wait a second between readings


}
Question 2: Interfacing with humidity sensor
#include <LiquidCrystal.h>

int tmppin = A1;


int tmp = 0;
int celcius = 0;

LiquidCrystal lcd_1(12, 11, 5, 4, 3, 2);

void setup()
{
Serial.begin(9600);
Serial.println("Serial Monitor Started");
lcd_1.begin(16, 2);
lcd_1.print ("Humidity:");

void loop()
{
tmp = analogRead(tmppin);
float celsius = 100 * 5.0 * tmp/ 1024;

Serial.println(celsius);

lcd_1.setCursor(6,0);
lcd_1.print(celsius);
lcd_1.setCursor(2, 1);

if (celsius <40) {
lcd_1.print ("LOW Humidity");
}
else{

lcd_1.print ("HIGH Humidity");


}
}

Question3: Interfacing with ultrasonic sensor


const int pingPin = 8;
const int LEDPin1 = 9;
const int LEDPin2 = 10;
const int LEDPin3 = 11;
const int LEDPin4 = 12;
const int LEDPin5 = 13;

void setup() {
Serial.begin(9600);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(6, OUTPUT);
}

void loop() {
long duration, inches, cm;
int status;

pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

status = map(cm,0,336,255,0);
Serial.println(status);
analogWrite(6, status);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
if (cm < 50){
digitalWrite(9, LOW);
delay(100);
digitalWrite(10, LOW);
delay(100);
digitalWrite(11, LOW);
delay(100);
digitalWrite(12, LOW);
delay(100);
digitalWrite(13, LOW);
delay(100);
}else if (cm >50 and cm < 100){
digitalWrite(9, HIGH);
delay(100);
digitalWrite(10, LOW);
delay(100);
digitalWrite(11, LOW);
delay(100);
digitalWrite(12, LOW);
delay(100);
digitalWrite(13, LOW);
delay(100);
} else if(cm > 100 and cm <150){
digitalWrite(9, HIGH);
delay(100);
digitalWrite(10, HIGH);
delay(100);
digitalWrite(11, LOW);
delay(100);
digitalWrite(12, LOW);
delay(100);
digitalWrite(13, LOW);
delay(100);
}else if(cm>150 and cm <200){
digitalWrite(9, HIGH);
delay(100);
digitalWrite(10, HIGH);
delay(100);
digitalWrite(11, HIGH);
delay(100);
digitalWrite(12, LOW);
delay(100);
digitalWrite(13, LOW);
delay(100);
} else if(cm>200 and cm <250){
digitalWrite(9, HIGH);
delay(100);
digitalWrite(10, HIGH);
delay(100);
digitalWrite(11, HIGH);
delay(100);
digitalWrite(12, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
} else if(cm>250){
digitalWrite(9, HIGH);
delay(100);
digitalWrite(10, HIGH);
delay(100);
digitalWrite(11, HIGH);
delay(100);
digitalWrite(12, HIGH);
delay(100);
digitalWrite(13, HIGH);
delay(100);
}
delay(100);
}

long microsecondsToInches(long microseconds) {


return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;}

You might also like