0% found this document useful (0 votes)
6 views7 pages

All

The document contains multiple Arduino sketches for various projects, including blinking an LED, measuring distance with an ultrasonic sensor, and using a PIR sensor for motion detection. It also includes a sketch for reading temperature and humidity from a DHT sensor and sending that data to the ThingSpeak cloud platform. Additionally, it outlines the installation process for necessary libraries and provides setup instructions for each project.

Uploaded by

creativetalks111
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)
6 views7 pages

All

The document contains multiple Arduino sketches for various projects, including blinking an LED, measuring distance with an ultrasonic sensor, and using a PIR sensor for motion detection. It also includes a sketch for reading temperature and humidity from a DHT sensor and sending that data to the ThingSpeak cloud platform. Additionally, it outlines the installation process for necessary libraries and provides setup instructions for each project.

Uploaded by

creativetalks111
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

2. Write an Arduino sketch to blink an LED Light for a particular interval of time.

int ledpin=13;
int buzzpin=4;
void setup()
{
pinMode(ledpin, OUTPUT);
pinMode(buzzpin, OUTPUT);
}

void loop()
{digitalWrite(buzzpin, HIGH);
digitalWrite(ledpin, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(ledpin, LOW);
digitalWrite(buzzpin, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
}

3. Write an Arduino sketch to measure the distance(in cms) of a certain object.

int trigpin=2;
int echopin=3;
long duration;
float distance;
void setup()
{
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
[Link](9600);
}
void loop()
{digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
duration = pulseIn(echopin, HIGH,30000);
[Link](duration);
distance =( duration * 0.034) / 2;
[Link]("Distance: ");
[Link](distance);
[Link](" cm");
delay(500);
}

4. Write an Arduino sketch to


i. Blink an LED and a buzzer if the distance measured is less than a threshold
value

const int trigPin = 7;


const int echoPin = 6;
const int ledPin = 9;
const int buzzerPin = 10;
const int threshold = 5; // distance in cm
void setup() {
[Link](9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
float distance = duration * 0.034 / 2;
[Link]("Distance: ");
[Link](distance);
[Link](" cm");
if (distance > 0 && distance < threshold) {
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
delay(200);
} else {
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
}
}

[Link]. Illustrate the working of PIR Sensor with an example.

const int pirPin = 7;


const int ledPin = 9;
void setup() {
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
[Link](9600);
}
void loop() {
int motion = digitalRead(pirPin);
if (motion == HIGH) {
digitalWrite(ledPin, HIGH);
[Link]("Motion Detected");
} else {
digitalWrite(ledPin, LOW);
[Link]("No Motion");
}

delay(500);
}
[Link]. Write an Arduino sketch to Illustrate the IR and DHT Sensor
void setup() {
pinMode(7,INPUT);
[Link](9600);
pinMode(13,OUTPUT);
}
void loop() {
[Link]("IRSensorip ");
[Link](digitalRead(7));
if(digitalRead(7)==0)
{
digitalWrite(13,HIGH);
}
else{
digitalWrite(13,LOW);
}
}
[Link])DTH

Install CORRECT Libraries (Manual Method)

1 Download DHT library

[Link]
Click Code → Download ZIP

2 Download Adafruit Sensor

[Link]
Click Code → Download ZIP

Install ZIP Libraries

1. Open Arduino IDE


2. Go to Sketch → Include Library → Add .ZIP Library
3. Select:
o [Link]
4. Repeat for:
o Adafruit_Sensor-[Link]

#include <DHT.h>

#define DHTPIN 7
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
[Link](9600);
[Link]("DHT11 test!");
[Link]();
}

void loop() {
delay(2000); // VERY IMPORTANT

float h = [Link]();
float t = [Link]();
float f = [Link](true);

if (isnan(h) || isnan(t) || isnan(f)) {


[Link]("Failed to read from DHT sensor!");
return;
}

float hi = [Link](f, h);

[Link]("Humidity: ");
[Link](h);
[Link](" %\t");

[Link]("Temperature: ");
[Link](t);
[Link](" °C ");
[Link](f);
[Link](" °F\t");

[Link]("Heat Index: ");


[Link](hi);
[Link](" °F");
}

5. Write a Program to send the humidity and temperature data to Cloud(ThingSpeak).

Create ThingSpeak Channel

1. Go to [Link]
2. Sign in → New Channel
3. Enable:
o Field 1 → Temperature
o Field 2 → Humidity

In Arduino IDE:

DHT sensor library (Adafruit)

Adafruit Unified Sensor

ESP8266 board package

Thingspeak

#include <ESP8266WiFi.h>
#include <DHT.h>
#include <ThingSpeak.h>

#define DHTPIN D4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

const char* ssid = "swaroopa";


const char* password = "88888888";

WiFiClient client;

unsigned long myChannelNumber = 3248292; // Your Channel ID


const char* myWriteAPIKey = "WRITE_API_KEY";

void setup() {
[Link](9600);
[Link]();

[Link](ssid, password);
[Link]("Connecting to WiFi");

while ([Link]() != WL_CONNECTED) {


delay(500);
[Link](".");
}

[Link]("\nWiFi connected");
[Link](client);
}

void loop() {
float humidity = [Link]();
float temperature = [Link]();

if (isnan(humidity) || isnan(temperature)) {
[Link]("Failed to read from DHT sensor");
return;
}

[Link]("Temperature: ");
[Link](temperature);
[Link](" °C ");

[Link]("Humidity: ");
[Link](humidity);
[Link](" %");

[Link](1, temperature);
[Link](2, humidity);

[Link](myChannelNumber, myWriteAPIKey);

delay(20000); // ThingSpeak minimum update time


}

You might also like