0% found this document useful (0 votes)
10 views17 pages

Arduino Humidity Sensor Guide

Uploaded by

neerajashish9
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)
10 views17 pages

Arduino Humidity Sensor Guide

Uploaded by

neerajashish9
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

8) Humidity Sensor

It measures the moisture level in the air and is commonly used in various
applications such as weather stations, HVAC systems and environmental
monitoring. DHT 11 / DHT 22 sensors are used as thermistor and capacitive
humidity sensor. The capacitive sensor detects the moisture level, while the
thermistor measures temperature. The sensor sends a digital signal to the
Arduino, providing both temperature and humidity data.

Code
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
[Link](9600);
[Link]();
}
void loop()
{
delay(2000);
float h = [Link]();
float t = [Link]();
if (isnan(h) || isnan(t))
{
[Link]("Failed to read from DHT sensor!");
return;
}

[Link] ("Humidity: ");


[Link] (h);
[Link]("%. \t");
[Link] ("Temperature: ");
[Link] (t);
[Link](" °C");
}

Result:
The values will be shown and update every two seconds and we will get new
readings periodically in serial monitor. Example: Humidity: 45.00 %.
Temperature: 30.00 °C

Applications:
i) Aquariums: Helps monitor humidity in enclosed spaces, ensuring a
healthy environment for aquatic life.
ii) Industrial Application: Monitors humidity in manufacturing processes,
especially in industries like food processing, pharmaceuticals and textiles
where moisture control is vital.
iii) HVAC Systems: Integrated into heating, ventilation and air conditioning
systems to control humidity levels, enhancing comfort and energy efficiency.
iv) Health and Wellness: Used in applications like saunas or spas to
maintain comfortable humidity level for users.
v) Greenhouse and Agriculture: Helps in maintaining optimal growing
conditions by monitoring humidity levels which is crucial for plant health and
crop yield.
4) Seven Segment Display
It is a common electronic component, used for displaying decimal numbers and
some letters. It consists of a seven individual segments (LEDs) arranged a
figure-eight shape, along with an optional eighth segment for a decimal point.
Each segment can be turned on or off individually by controlling the current
flowing through it.

Code
#define A 2
#define B 3
#define C 4 // Pin definitions for the segments.
#define D 5
#define E 6
#define F 7
#define G 8 // segment patterns for digits 0-9
const int digitPatterns[10][7] =
{
{1,1,1,1,1,1,0}, //0
{0,1,1,0,0,0,0}, //1
{1,1,0,1,1,0,1}, //2
{1,1,1,1,0,0,1}, //3
{0,1,1,0,0,1,1}, //4
{1,0,1,1,0,1,1}, //5
{1,0,1,1,1,1,1} //6
{1,1,1,0,0,0,0} //7
{1,1,1,1,1,1,1} //8
{1,1,1,1,0,1,1} //9
};
void setup ()
{
pinMode (A, OUTPUT);
pinMode (B, OUTPUT);
pinMode (C, OUTPUT);
pinMode (D, OUTPUT);
pinMode (E, OUTPUT);
pinMode (F, OUTPUT);
pinMode (G, OUTPUT);
}
void loop ()
{
for(int digit = 0; digit < 10; digit ++)
{
displayDigit (digit); // Display current digit
delay (1000); // wait for 1 second
}
}
void displayDigit (int digit) // set segments based on digit pattern
{
digitalWrite (A, digitPatterns[digit][0]);
digitalWrite (B, digitPatterns[digit][1]);
digitalWrite (C, digitPatterns[digit][2]);
digitalWrite (D, digitPatterns[digit][3]);
digitalWrite (E, digitPatterns[digit][4]);
digitalWrite (F, digitPatterns[digit][5]);
digitalWrite (G, digitPatterns[digit][6]);
}

Result:
The seven segment display continuously count from 0 to 9, pausing for one
second between each digit.

Applications:
i) Digital Clocks: Used to display time in hours and minutes.
ii) Electronic Meters: Indicate measurements such as voltage, current and
resistance.
iii) Automotive displays: Used in dashboards to show speed, fuel levels
and other important metrics.
iv) Calculators: Show result of arithmetic calculations.
v) Industrial Equipment: Provide numerical readouts in machinery and
control panels.
5) LCD (Liquid Crystal Display)
It is a flat panel display technology, that uses liquid crystals to produce images.
The liquid crystals change their orientation when an electric field is applied.
This change affects how light passes through them, allowing control over the
visibility of specific areas on the display.

Code
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
[Link](16, 2);
[Link]("hello, world!"); // Print a message to the LCD.
}
void loop() {
[Link](0, 1);
[Link](millis() / 1000); // print the number of seconds since reset:
}

Result:
The LCD will display the message "hello, world!" on the screen.

Applications:
i) Prototyping: Ideal for creating and testing new ideas.
ii) Education: Commonly used in schools and workshops to teach
electronics and programming.
iii) Robotics: Building autonomous or remote-controlled robots.
iv) Data logging: Collecting and storing data from sensors for analysis.
v) IoT applications: Connecting devices to the internet for remote
monitoring and control.
3) Buzzer
It is an electronic device that converts electrical energy into sound, commonly
used in alarms, notifications and various electronic projects. By using digital
pins and programming logic, we can create simple alerts or complex melodies
making buzzers versatile component in various projects.
It is of two types:
i) Active Buzzer: Contains a built in oscillator, which
means it produces sound when powered.
ii) Passive Buzzer: Does not have an internal oscillator
and requires a specific signal to produce sound.

Code:
#define BUZZER_PIN 8 // Define pin for the buzzer
void setup()
{
pinMode (BUZZER_PIN, OUTPUT); //set buzzer pin as
an output
}
void loop()
{
digitalWrite (BUZZER_PIN, HIGH); //Turn the buzzer on
delay (1000); //wait for 1 second
digitalWrite (BUZZER_PIN, LOW); //Turn the buzzer off
delay (1000); //Turn the buzzer off for 1 second.
}

Result:
The buzzer will turn on, producing a beep for 1 second. It will then turn off for
1 second resulting in a pause and this cycle will repeat indefinitely.
Applications:
i) Alarms: Used in security systems and smoke detectors to alert users.
ii) Notifications: Indicate events or alerts in devices like timers and
microwave ovens.
iii) Medical Devices: Used in medical equipment for alerts and
notifications e.g., patient monitors.
iv) Robotics: Serve as sound outputs in robots for signaling and
communication
v) Musical Instrument: Employed in electronic musical instruments
for sound generation.
6) Ultrasonic Sensor
It is a device that uses ultrasonic waves to measure distance. It emits a sound
wave at a frequency higher than the audible range and measures the time it
takes for the wave to bounce back after hitting an object. The Arduino sends a
high signal to the Trig pin. This triggers the sensor to emit a short ultrasonic
pulse.

Code
const int trigpin = 9; //Trigger lin
const int echopin = 10; // Echo Pin.
void setup ()
{
[Link](9600); // Start serial communication
pinMode (trigPin, OUTPUT); // set trigger pin as output
pinMode (echoPin, INPUT); // set echo pin as input
}
void loop{
long duration, distance;
digitalWrite(trigPin, LOW); // clear the trigger
delayMicroseconds (2);
digitalWrite(trigPin, HIGH); // Set the trigger to high for
delayMicroseconds (10); 10 microseconds
digitalWrite(trigPin, LOW); // Read the echo Pin.
duration=pulseIn (echoPin, HIGH); //Calculate distance in cm
distance = (duration * 0.0343)/2; //Print the distance to the serial Monitor
[Link]("Distance: ");
[Link](distance);
[Link](" cm");
delay(1000); //wait for 1 second before the next measurement.

Result
The serial monitor will show the measured distance to the nearest object in
centimeters. Example: Distance: 25 cm, Distance: 30 cm.
Applications:
i) Embedded Systems: Common in microcontroller projects for displaying
data, status messages or menus.
ii) Measurement Instruments: Used in multimeters, thermometers and
voltmeters to display readings.
iii) Gaming Consoles: Display game scores, menus and statistics in
handheld or arcade games.
iv) Home Appliances: Found in microwaves, washing machines and
refrigerators to show settings and statuses.
v) Smartphone and Tablets: Often used in early models for user
interfaces and menus.
2) LED:
LED is an Arduino Uno component that combines basic electronics concept
with programming logic. By controlling digital output pins and managing
current flow, we can make the LED light up, blink or create more complex
lightening patterns, forming the basics for many interactive projects. It provides
insight into programming, circuit connection and control of electronic
components. It builds foundation skills for more advanced projects.

Code:
void setup ()
{
pinMode (13, OUTPUT); //set pin 13 as an output
}
void loop ()
{
digitalWrite(13, HIGH); //Turn the LED on
delay(1000); //wait for 1 second
digitalWrite(13, LOW); //Turn the LED off
delay(1000); //wait for 1 second
}

Result:
Then The LED will blink on for 1 second and then turn off for one second,
creating a visible blinking effect.

Applications:
i) Status indicators: Used to show the operational status of a device. e.g
power on/off.
ii) Signal Lights: Can be used in projects to signal various states e.g., alerts,
notifications etc.
iii) Communication: Used in communication protocols to indicate data
transmission e.g. blinking during data transfer.
iv) Prototyping: Essential in developing and testing circuits in various
electronic projects.
v) Light Patterns: Create light displays or patterns for decorations or art
installations.
7) DC Servo Motor:
It is a type a motor that provides precise control of angular position, velocity
and acceleration. The DC motor is powered by the input signal, causing it to
rotate. The direction and speed of the rotation depend on the voltage applied.
The control system sends a command signal to the servo motor indicating the
desired position or speed.

Code
#include <Servo.h>
Servo myServo; //create a servo object
void setup()
{
[Link](9); //attach the servo to pin 9.
}
void loop()
{
[Link](0); //Move the servo to 0 degrees
delay(1000); //wait for 1 second.
[Link](90);
delay(1000);
[Link](180);
delay(1000);
[Link](10);
delay(1000);
}

Result:
The servo motor moves to 0 degree first and held the position for 1 second and
then moves to 90 and then to 180 and again back to 0 degree, repeating
continuously, holding each position for 1 second.

Applications:
i) CNC Machines: Essential in computer Numerical Control (CNC) machines
for accurate tool positioning and movement.
ii) Drones and UAVs: Control flight surfaces and gimbals for stabilization
and maneuvering.
iii) Robotics: Used for controlling joint movements in robotic arm and legs,
allowing for precise positioning and manipulation.
iv) 3D printers: Used to control the movement of print heads and build
platform for layer by layer printing.
v) Educational Projects: Frequently used in STEM education to teach
students about mechanics, electronics and programming.
1) Arduino:
It is an open source electronics platform based on easy-to-use hardware and
software. It consists of a microcontroller, a development environment and a
community that shares projects and ideas. The Arduino Uno is one of the most
popular and widely used boards in the Arduino family. Its key features are:
i) Microcontroller:
Based on the Atmega 328 P, it is having a 16 MHz clock speed and 32kB of flash
memory for storing programs.
ii) Digital and Analog I/O:
The Uno has 14 digital input/output pins (6 of which can be used as PWM
outputs) and 6 analog input pins, allowing for a variety of sensor and actuator
connections.
iii) USB interface:
It connects to the computer via USB for programming and power. The board
can also be powered through an external power supply.
iv) Compatibility:
Supports a wide range of shields (add-on boards) and libraries, making it easy
to expand its functionality.
v) LED and reset Button:
Comes with a built-in LED on pin 13 for testing and a reset button for restarting
the board.

You might also like