0% found this document useful (0 votes)
15 views25 pages

Arduino Basics: Sensors, Actuators & IDE

B.Tech 5th Semester
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)
15 views25 pages

Arduino Basics: Sensors, Actuators & IDE

B.Tech 5th Semester
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

JIMS Engineering Management Technical Campus, Greater Noida

Affiliated to Guru Gobind Singh Indraprastha University, Delhi


Approved by AICTE, BCI and NCTE
ISO 9001:2015 Certified Institution

Date :

Experiment-1
AIM: Introduction to Arduino platform and programming and Introduction to various actuators
& its applications.

Objective: We will be able to:


i. Understand the arduino board and its environments
ii. Understand the various types of sensors and actuators.

Requirement : PC with internet, Arduino board, breadboard

Theory:

Arduino UNO is a microcontroller board based on the ATmega328P (datasheet). It has 14


digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16
MHz ceramic resonator (CSTCE16M0V53-R0), a USB connection, a power jack, an ICSP
header and a reset button. It contains everything needed to support the microcontroller;
simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or
battery to get started.

There are entry level


boards like the UNO,
LEONARDO, NANO etc;
boards with enhanced
feature like the MEGA,
PRO, ZERO etc; boards for
Internet of Things like the
YUN, TIAN etc; and
wearable boards like the
LILYPAD, GEMMA etc.

Arduino Pin Diagram

Arduino pins are multiplexed meaning that most of the pins are multi-function. Also the 6-pin header
at the bottom and also at the top right corner near the USB socket are multifunction.
Function PIN PIN Function

D19 PC5 / A5 /PCINT13 /SCL


A
D18 PC4 // A4/ PCINT12 / SDA
R
AREF

D GND

NC U D13 PB5 / PCINT5 / SCK

IOREF D12 PB4 / PCINT4 / CIPO


I
PCINT14 / PC6 1(RESET) D11~ PB3 / PCINT3 / COPI
N
+3V3 D10~ PB2 / PCINT2 / SS

+5V O D9~ PB1/ PCINT1

GND D8 PB0 / PCINT0

GND D7 PD7 / PCINT23

VIN D6~ PD6 / PCINT22


B
PCINT8/PC0 . A0 / D14 23 D5~ PD5 / PCINT21

PCINT9/ PC1 / A1 / D15 24 O D4 PD4 / PCINT20

PCINT10/PC2 / A2 / D16 25 A D3~ PD3 / PCINT19

PCINT11/PC3 / A3 / D17 26 D2 PD2 / PCINT18


R
PCINT 12/PC4 / A4 / D18 / SDA 27 D1 / TX PD1 / PCINT17
D
PCINT 13/PC5 / A5 / D19 / SCL 28 D0 /RX PD0 / PCINT16

Integrated Development Environment


Introduction: The Arduino Software (IDE) is easy-to-use and is based on the Processing
programming environment. The Arduino Integrated Development Environment (IDE) is a
cross-platform application (for Windows, macOS, Linux) that is written in functions from C
and C++. The open-source Arduino Software (IDE) makes it easy to write code and upload it
to the board. This software can be used with any Arduino board.
The Arduino Software (IDE) – contains:
 A text editor for writing code
 A message area
 A text consoles
 A toolbar with buttons for common functions and a series of menus. It
connects to the Arduino hardware to upload programs and communicate with
them.
Installation of Arduino Software (IDE)
Download arduino from: [Link] and proceed with the
installation following the onscreen menus and by allowing the driver installation
process.

Sensors and Actuators


Sensors Actuators
Definition Sensors capture information Actuators, on the other hand,
(temperature, pressure, force,etc.) create changes in the
from the surrounding environment. surrounding environment.
They are input devices They are output devices
Types Sensors are termed digital or analog Actuators can operate on
depending on the type of signal they Analogue. However, some
generate (analog digital), correct? actuators, like LEDs, can be
Are these sensors that can output operate with both analog and
both digital and analog signals? digital input signal. LEDs can be
operated via PWM or even using
an analog signal to light them up
at some intermediate
brightness.
Types of Sensors in Arduino
Here are some commonly used sensors you'll come across when working with Arduino:
 Temperature sensor: Measures the temperature and humidity of the environment.

 Light-Dependent Resistor (LDR): Measures/senses the intensity of light.


 Ultrasonic sensor: Measures the distance of an object from the sensor.
 Motion sensor: Generally detects motion by sensing changes in infrared
energy/radiation.
 Soil moisture sensor: Measures the moisture level of the soil.
 Water sensor: Measures water level/detects water, and so on.
 There are other types of sensors that you can use with Arduino, but we'll just focus on
two: the LDR (light-dependent resistor) and ultrasonic sensor.

Types of Actuators in Arduino


Actuators in Arduino are components that convert electrical signals into physical/ mechanical
motion. Here are some actuators:
 LED (Light Emitting Diode): Used as light/visual indicators.
 Buzzer: Used to produce sound.
 Relay modules: Used to control high voltage devices.
 LCD (Liquid Crystal Display): Used as visual display for text, images, sensor data, and so
on. We'll dedicate a separate chapter to displays.
 DC Motor, Stepper Motor and Servo motors : Used to control angular or rotational motion
(an example is the movement of a robotic arm).

Arduino Program structure


//header file
//macro definitions
//global variable declarations
void setup()
{
//place to configure arduino pins for i/o mode (INPUT or OUTPUT)
// and to initialize the hardware
}
void loop()
{
//write program to do something useful
/may include statements, call to inbuilt functions or user defined functions
}
//user defined function definitions
Summary:

In this experiment we have learnt about the arduino Uno platform, the pin diagram, types of
sensors and actuators which will be quite often used in the IoT experiment.

Viva Questions

1. What are different type of pins on an arduino board

2. What is difference between sensors and actuators

3. Name five sensors and five actuators


Date:

Experiment 2
AIM : Introduction with running a blinking LED and fading LED with PWM

a. Arduino IDE and Operators in IDE.


b. Frequently used Functions in Arduino IDE

Objective:

At the end of this experiment we will be able to:

i. Learn the arduino platform and the use of operators, variables and functions
ii. Develop a program to blink and fade the LED light

Requirements:

 PC/laptop with Window or Linux or MAC.


 Arduino IDE installed on a PC/Laptop
 Arduino Board
 LED-01, Resistor 220 or 470 Ohm
 Potentiometer 1 K or 10K Ohm

Introduction:

LED – The LED is a special type of diode and have similar electrical characteristics to a PN
junction diode. Hence the LED allows the flow of current in the forward direction and blocks
the current in the reverse direction. It emits light when in forward biased.

LED Types: LED are single or multi-color. Single color LED have two terminals/legs whereas multi-
color LED have more than 2 legs. For example figure below shows 3-leg LED that can display
combination of two colors whereas 4-leg LED emit color which is a combination of three colors RGB.

We can control the LED Brightness using the digitalWrite() and AnalogWrite() functions.
DigitalWrite() is very simple as it require to connecting the LED pin to any of the digital I/O pins of
the arduino.
Controlling LED brightness (Fading of LED) :

Operations like fading of LED is done using the PWM pins of the arduino. On an Arduino Uno, PWM
output is possible on digital I/O pins 3, 5, 6, 9, 10 and 11. On these pins the analogWrite function is
used to set the duty cycle of a PWM pulse train that operates at approximately 500 Hz . Thus, with a
frequency fc = 500 Hz, the period is τc = 1/fc ∼ 2 ms. As with conventional digital I/O, the pinMode
function must be called first to configure the digital
pin for output.

The statement analogWrite( PWM_out_pin,


PWM_out_level); sets the PWM_out_pin pin to the
value of the PWM duty cycle and hence the effective
voltage according to Equation given below. The
parameter that sets the duty cycle is an 8-bit unsigned integer, i.e.,
a value in the range 0 ≤ PWM_out_level ≤ 255. The digital output
voltage of an Arduino Uno is either 0 V or 5 V.

Thus, PWM_out_level = 255 × τo / τc = 255 × Veff / Vs Figure : Scaling relationships for PWM parameters

Remember that the second input to analogWrite is an unsigned 8-


bit value, so PWM_out_level should be a byte variable type. Since Vs = 5 V always, for routine
calculations, we can use the simple formula PWM_out_level = 255 / 5 × Vef

PWM out level is the 8-bit value used as the second parameter to the analogWrite() function.

Therefore,

to supply an effective voltage of 5 V use PWM_out_level = 255 / 5 × 5 = 255

to supply an effective voltage of 3 V use PWM_out_level = 255 / 5 × 3 = 153

to supply an effective voltage of 1 V use PWM_out_level = 255 / 5 × 1 = 51

Code Explanation
1. A global variable LED_pin of type int is defined in the global area. It’s scope is the entire
program. Declaring it at the top allows us to use the LED_pin for the pin number 4.
2. The set up function will execute only once. It uses two functions-
a. pinMode(LED_pin, OUTPUT); to instructs the arduino to use LED_pin i.e. pin 4 as
Output.
b. digitalWrite(LED_pin, HIGH); to instructs the arduino to keep the LED_pin i.e. pin 4
glowing.
3. Loop() function runs continuously in the loop executing all instructions inside its scope from
beginning till the end and repeats this process as long as the device is powered ON.
a. The first line declares a variable pause of type int an assigns it a value 1000. This
variable is passed in the delay function. 1000 here will correspond to 1 seconds delay.
b. The LED (connected on pin 4) which was initially ON will be put OFF by the
function digitalWrite(LED_pin, LOW); and then ON by digitalWrite(LED_pin,
HIGH);
4. The LED Blinks after every 1 seconds interval.
5. LED Brightness control Using a Potentiometer
Screenshot:

Fading / Brightness Control of LED

Connect the potentiometer center leg to analog pin A0 and the other two pins to 5V and GND,
Connect an led to digital pin number 9

Code Explanation:

i. Declare a variable LED_pin =9


ii. In setup() function configure pin 9 as output
iii. In loop() function
iv. Define a variable pause to contain delay value
v. Read potentiometer value in PotReadOnA0
vi. Send the value to pwm pin 9

Output

Upload the sketch and watch the LED connected on pin 9 to fade as potentiometer is rotated.

Summary

In this experiment we learnt two examples of controlling the LED. In the first example we used
simple digitalWrite(pin, LOW) and digitalWrite(pin, HIGH) with delay function to blink the LED. In the
second example we learnt how to use PWM signal to control the brightness of the LED using
analogWrite(pin, value) function.
Viva Questions

1. What are the digital i/o pins which are also used as analog output pin?

2. How do you use the PWM pins in arduino?

3. Explain the concept of PWM.


JIMS Engineering Management Technical Campus, Greater Noida
Affiliated to Guru Gobind Singh Indraprastha University, Delhi
Approved by AICTE, BCI and NCTE
ISO 9001:2015 Certified Institution

Date:

Experiment-3
AIM : Arduino IDE, Operators and Frequently Used Functions

Objective: At the end of this experiment students will be able to:


i. Understand the arduino IDE and be able to create new sketch, open
existing, save, compile and upload it to arduino board
ii. Understand various operators and other built in functions.

Requirement: Arduino Software (IDE)


Theory : Arduino IDE

Features

 Verify / Upload - compile and upload your code to your Arduino Board.
 Select Board & Port - detected Arduino boards automatically show up here, along with the
port number.
 Sketchbook - here you will find all of your sketches locally stored on your computer.
Additionally, you can sync with the Arduino Cloud, and also obtain your sketches from the
online environment.
 Boards Manager - browse through Arduino & third party packages that can be installed. For
example, using a MKR WiFi 1010 board requires the Arduino SAMD Boards package
installed.
 Library Manager - browse through thousands of Arduino libraries, made by Arduino & its
community.
 Debugger - test and debug programs in real time.
 Search - search for keywords in your code.
 Open Serial Monitor - opens the Serial Monitor tool, as a new tab in the console
Sketchbook
The Arduino Software (IDE) uses the concept of a sketchbook: a standard place to store your
programs (or sketches). The sketches in your sketchbook can be opened from the File >
Sketchbook menu or from the Open button on the toolbar. The first time you run the Arduino
software, it will automatically create a directory for your sketchbook. You can view or change the
location of the sketchbook location from with the Preferences dialog.
Beginning with version 1.0, files are saved with a .ino file extension. Previous versions use the .pde
extension. You may still open .pde named files in version 1.0 and later, the software will
automatically rename the extension to .ino.
Tabs, Multiple Files, and Compilation
Allows you to manage sketches with more than one file (each of which appears in its own tab). These
can be normal Arduino code files (no visible extension), C files (.c extension), C++ files (.cpp), or
header files (.h).
Operators and Frequently Used Functions

Operators Variables Functions


Arithmetic Operators Conversion Digital I/O
 = (assignment operator)  char(), byte(), int()  pinMode()
 + (addition)  word(), long(), float()  digitalWrite()
 - (subtraction) Variable Scope & Qualifiers  digitalRead()
 * (multiplication)  variable scope Analog I/O
 / (division)  static  analogReference()
 % (modulo)  volatile  analogRead()
Comparison Operators  const  analogWrite() - PWM
 = =, !=, <, >, <=, >= Utilities Time
 Boolean Operators  sizeof()  millis(), micros(), delay()
 && (and) Math
 || (or)  min(), max(), abs(),
 ! (not) constrain(), map(), pow()
 sqrt()
Advanced operators, variables, and functions

Operators Variables Functions


Pointer Access Operators Conversion Trigonometry
 * dereference operator  char(), byte(), int()  sin(), cos(), tan()
 & reference operator  word(), long(), float() Characters
Bitwise Operators Variable Scope & Qualifiers  isAlphaNumeric()
 & (bitwise and)  variable scope  isAlpha()
 | (bitwise or)  static  isAscii()
 ^ (bitwise xor)  volatile  isWhitespace()
 ~ (bitwise not)  const  isControl()
 << (bitshift left) Utilities  isDigit()
 >> (bitshift right)  sizeof()  isGraph()
Compound Operators  isLowerCase()
 ++ , -- ,  isPrintable()
 += , -=, *=, %=, /=, |=  isSpace()
  isUpperCase()
Summary

We have learnt how to open the arduino IDE and explore its various tabs to configure and create
sketch. Arduino use the operators, variables and the function in the similar way we use in C
programming language. However there are certain built-in functions which cater to the need of the
arduino platform and boards. We can select one of many different types board including arduino Uno,
nano, sense-BLE, ESP and so on.
Viva Questions

1. Name various operators used in arduino.

2. Name the digital and analog input output functions used in arduino

3. How the bitwise NOT operator is used in arduino.

4. Explain the concept of PWM in arduino analog output.

5. Develop a program to blink the LED


Date:
Experiment-4
AIM : Control Structure (if else, for and while) and the user defined functions for writing programs
Objective:
The students will be able to :
1. Learn the syntax for writing the user defined functions and the control structure in
arduino program development
2. Make use of the control structure in developing arduino based applications
Hardware and Software Requirements:
 Arduino IDE installed on PC/Laptop
 Arduino board
 8 LED single color
 8 resistors 220 Ohms
 Potentiometer 1K or 5K
Theory:
User defined functions make the code reusable. Arduino control structures allow you to adjust the
flow of your code and take action based on certain conditions. For example, you can use control
structures to skip some statements, repeat statements while a condition is true, or make your code
do different things depending on different circumstances
Many of the structures below will use comparison operators.
Comparison Operators: These operators are used to define the condition for decision making by
the control structure.
 x == y (x is equal to y)
 x != y (x is not equal to y)
 x < y (x is less than y)
 x > y (x is greater than y)
 x <= y (x is less than or equal to y)
 x >= y (x is greater than or equal to y)

Control Structures

As the code to be written in arduino IDE is a “C” program, you are all already familiar with the syntax
of the following structures. Here we will use these structures in the context of the hardware.

1. if() and if()-else, nested if()


2. for(start_Val; final_val; step_Val)
3. while(condition) {do…..}
4. do {……..} while(condition);
5. switch(case){……}

In the following examples we will study the application of all the above structures.

1. if() and if()-else, nested if()


a) if (condition)

void loop() {
analogWrite(LED_pin, brightness);
brightness = brightness + FadeAmount;
if (brightness <=0 || brightness >= 255)
FadeAmount = -FadeAmount
FadeAmount = FadeAmount; // if condition is false then this line will execute
delay(5);
}

b) if...else

void loop() {
analogWrite(LED_pin, brightness);
brightness = brightness + FadeAmount;
if (brightness <=0 || brightness >= 255)
FadeAmount = -FadeAmount;
else
FadeAmount = FadeAmount;
delay(5);
}

2. for loop : This structure is used to execute a line of code a number of times as specified in the
control condition of the for() loop. Here is an example.
a) A simple for() loop

int timer = 100; // The higher the number, the slower the timing.
void setup() { // use a for loop to initialize each pin as an output:
for (int thisPin = 2; thisPin < 8; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
for (int thisPin = 2; thisPin < 8; thisPin++)
{ // loop from the lowest pin to the highest:

digitalWrite(thisPin, HIGH); // turn the pin on:


delay(timer);
digitalWrite(thisPin, LOW); // turn the pin off:
}
}

b) Array

This example demonstrates the use of an array to hold pin numbers in order to iterate
over the pins in a sequence. We can choose the pins in the array to be in any random
order, the example uses 2 through 7 i/o pins for LED.
int timer = 100; // for delay time.

int ledPins[] = { 2, 7, 4, 6, 5, 3}; // for LEDs

int pinCount = 6; // no. of LEDs

void setup() {

// for loop to initialize each pin as an


output:

for (int thisPin = 0; thisPin < pinCount; thisPin++) {

pinMode(ledPins[thisPin], OUTPUT);

void loop() {

for (int thisPin = 0; thisPin < pinCount; thisPin++) {

digitalWrite(ledPins[thisPin], HIGH); // turn the pin on:

delay(timer);

digitalWrite(ledPins[thisPin], LOW); // turn the pin off:

3. switch case

Demonstrates the use of a switch statement. The switch statement allows you to choose from
among a set of discrete values of a variable. It's like a series of if [Link] this example we will
map the PWM value of the potentiometer

// get from your sensor:

const int sensorMin = 0; // sensor minimum, discovered through experiment


const int sensorMax = 600; // sensor maximum, discovered through experiment

void setup() {
// initialize serial communication:
[Link](9600);
}

void loop() {
int sensorReading = analogRead(A0); // read the sensor:
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
// The map function maps the sensor range to a range of four options (0,1,2,3):

// do something different depending on the range value:


switch (range) {
case 0:
[Link]("dark"); // your hand is on the sensor
break;
case 1:
[Link]("dim"); // your hand is close to the sensor
break;
case 2:
[Link]("medium"); // your hand is a few inches from the sensor
break;
case 3:
[Link]("bright"); // your hand is nowhere near the sensor
break;
}
delay(1); // delay in between reads for stability
}

4. while(condition)

Ref : [Link]

Use of Photoresistor for fading the light

const int sensePin = A0; // pin that the sensor is attached to

const int ledPin = 9; // for PWM signal


const int indicatorLedPin = 13; // built-in LED pin
const int buttonPin = 2; // pin that the button
is attached to
// These variables will change:
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
int senseValue = 0; // the sensor value
void setup() {
// set the LED pins as outputs and the switch pin
as input:
pinMode(indicatorLedPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
// while the button is pressed, take calibration readings:
while (digitalRead(buttonPin) == HIGH) {
calibrate();
}
//signal the end of the calibration period
digitalWrite(indicatorLedPin, LOW);
// read the sensor:
senseValue = analogRead(sensePin);
// apply the calibration to the sensor reading
senseValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
// in case the sensor value is outside the range seen during calibration
senseValue = constrain(sensorValue, 0, 255);
// fade the LED using the calibrated value:
analogWrite(ledPin, senseValue);
}
void calibrate() {
// turn on the indicator LED to indicate that calibration is happening:
digitalWrite(indicatorLedPin, HIGH);
// read the sensor:
sensorValue = analogRead(sensorPin);
// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}
Summary:

We have learnt how to write our own functions for well structuring of the programs. We also learnt
how to use different control structures in developing arduino programs.
ViVa questions

1. Name the various comparison operators used in arduino.

2. Write the format and syntax of the for, if, while control structures

3. WAP to blink and fade the LED using potentiometer

4. Name the special inbuilt functions used in above theory and write their syntax
and purpose.
Date:

Experiment-5
AIM : Interfacing button to arduino digital pin and using it to control actuators (LED).
Objective:
i. To understand the concept of PULL-UP and PULL_DOWN in interfacing of a switch
with arduino
ii. To read the position of the switch to control actuators (LED)
Requirement: Arduino IDE, Push Button, 5mm LED, 220 Ω resistor

Theory:
We generally need a resistance at the terminals of the push button to limit the current through the
board pin. This resistance can be an internal built into the board or an external resistance. The way
this resistance is connected is the internal pull-up or external pull-up or pull down resistance.

With a pull-up resistor and with the button un-pressed you make a logic state ON and with the
button pressed you make a logic OFF.

With a pull - down resistor and a pressed button you make an ON logic state and OFF logic state
when its unpressed.

In case of internal pull-up we use the


INPUT_PULLUP hence the internal resistance get
connected to the pin where this command is used.

Example of button interfacing without the pull up


resistor,

On compiling and uploading the sketch, you'll see


the LED flickering or less bright. Pressing the
button and you see now the LED turned normaly
on (fully bright). Turning off the button and the
LED its flickering again.

To prevent the unknown state a internal pull-up, external pull-up or external pull-down resistor
will ensure the state on the pin is low. Add a resistor of approx. 5 K to 10K Ω to the circuit as
shown below.
In the figure on left we initialize digital pin 2 as an input with the internal pull-up resistor enabled
using INPUT_PULLUP as 2nd argument to the pinMode () function.

When your button is not pressed, the internal pull-up resistor connects to 5 volts. This causes the
Arduino to report "1" or HIGH. When the button is pressed, the Arduino pin is pulled to ground,
causing the Arduino report a "0", or LOW.

Ex-1: Interface a switch using INTERNAL_PULLUP with the arduino board and WAP to
ON / OFF the LED as per the position of the switch. Use the internal LED connected to pin
13 of arduino

Connect a switch to pin 2 and GND in internal_pullup as shown in the figure(extreme left).
In this program we use initialize digital pin 2 as an input with the internal pull-up resistor
enabled using INPUT_PULLUP as 2nd argument to the pinMode () function

When your button is not pressed, the internal pull-up resistor connects to 5 volts. This causes the
Arduino to report "1" or HIGH. When the button is pressed, the Arduino pin is pulled to ground,
causing the Arduino report a "0", or LOW.

Code Explanation:

i. In the setup() function, initialize [Link]() function


ii. Initialize pin 2 as input and enable internal pull-up
iii. Initialize pin 13 in output mode

In the void loop() function

iv. Read the switch status in the sensorValue variable


v. Print this value on serial monitor
vi. Glow LED if sensorValue is HIGH or off if sensorValue is LOW

Arduino Sketch

void setup() {

[Link](9600); //start serial connection

//configure pin 2 as an input and enable the internal pull-up resistor

pinMode(2, INPUT_PULLUP);

pinMode(13, OUTPUT);

void loop() {

int sensorVal = digitalRead(2); //read the pushbutton value into a variable

[Link](sensorVal); //print out the value of the pushbutton

// Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
// HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when the button's
//pressed, and off when it's not:

if (sensorVal == HIGH) {

digitalWrite(13, LOW);

} else {

digitalWrite(13, HIGH);

Ex-3: Example of external pull-up resistor with the button

// constants won't change. They're used here to set pin numbers:


const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:


int buttonState = 0; // variable for reading the pushbutton status
void setup() {
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
[Link](9600);
}
void loop() {
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
if (buttonState == LOW) { //pressing the button will produce a LOW state 0V
digitalWrite(Led,HIGH); //the led with turn on
[Link](buttonState);
} else{
digitalWrite(Led,LOW); //the led with turn off
}
[Link](buttonState); //check in the serial monitor
}
Ex-3: Interface a switch and an LED with the arduino board and WAP to ON / OFF the
LED as per the position of the switch. Use of pull Down resistor
The configuration is shown in the figure on the extreme right side. When the switch is off it
grounds through a resistor giving a low state at the input pin. When the switch is pushed, it
connects to Vcc and the input state of the switch at the input pin is HIGH. This LOW and HIGH
state is used to ON or OFF the LED. Sketch
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
}
void loop() {
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
Summary:

In this lab we have learnt the use button with out pull up and with the use of internal, external
pull up and external pull down. We also saw how the problem of flickering of LED is
resolved using the pull up or pull down resistors.

Viva question:

1. Explain the function of the [Link]() in arduino.

2. Explain what happens if don’t use a pull-up resistor at the input of a button /
switch.

3. Write the button input state when button connected in (i). with internal pull up,
(ii). With external pull up, and (iii) with external pull down resistor.

4. Draw the figure to configure the button without pull up / pull down and with
the help of internal and external pull up and external pull down.
Date:

Experiment-6
Aim : Measure humidity and temperature
Objective: At the end of this experiment we will be able to:

i. Understand the working of DHT module


ii. Interface DHT11 with arduino and develop a program using DHT-11

Requirement: PC with Arduino IDR, Arduino Uno board, DHT-11 module and 03 jumper
wire.

Theory

DHT11 Sensor

The DHT-11 is a digital-output relative humidity and temperature sensor. It uses a capacitive
humidity sensor and a thermistor to measure the surrounding air. It is smaller and less
expensive but this sensor is less precise, less accurate and works in a smaller range of
temperature and humidity when compared with DHT22. DHT11 has a sampling rate of 1Hz,
which means it can provide new data once every second. Comes with 4 pin or 3 pin. Figure
below shows the pin names. However before interfacing just check the pin names on the PCP
of DHT-11 module.

Humidity
Sensor:

The humidity
sensing
component
has two electrodes
with a moisture-
holding
substrate (usually a salt or conductive plastic polymer) in between.

As the humidity rises, the substrate absorbs water vapour, resulting in the release of ions
and a decrease in the resistance between the two electrodes.

This change in resistance is proportional to the humidity, which can be measured to


estimate relative humidity.

NTC Temperature sensor


DHT11 also includes a NTC thermistor for measuring temperature. A thermistor is a type of
resistor whose resistance varies with temperature. It has a negative temperature coefficient,
its resistance varies as shown in figure above.

The sensor also includes an 8-bit SOIC-14 packaged IC. This IC measures and processes the
analog signal using stored calibration coefficients, converts the analog signal to digital, and
outputs a digital signal containing the temperature and humidity.

DHT11 can measure temperature from 0°C to 50°C with a ±2.0°C accuracy, and humidity from
20 to 80% with a 5% accuracy.

Another alternative to DHT11 is "DHT22/AM2302 Digital Temperature Humidity Sensor". A


bit more expensive, but it has a wider range and has better precision.

Interfacing DHT-11 with Arduino


Interfacing DHT-11 is very simple. Connect Vcc to +5V pin and GND to GND pin of arduino.
The data pin of the DHT-11 can be connected to any digital pin 0 to 13 (here connected to
digital I/O pin 8). This is shown in
figure below.

Code:
Program to measure humidity and
temperature and display on the
serial monitor

// Open Arduino IDE

//Select port and the arduino uno board

//Install DHT11 library

#include <DHT.h>

//Define Constants
#define DHTPIN 2 // data input pin where DHT 11 connected to

#define DHTTYPE DHT11 // DHT 11 (AM2302)

// Initialize DHT sensor for normal 16mhz Arduino

DHT dht(DHTPIN, DHTTYPE);

//Variables

int chk;

float hum; //Stores humidity value

float temp; //Stores temperature value

void setup()

[Link](9600);

[Link]();

void loop()

//Read data and store it to variables hum and temp

hum = [Link]();

temp= [Link]();

//Print temp and humidity values to serial monitor

[Link]("Humidity : "); [Link](hum);

[Link](" %, Temp: "); [Link](temp); [Link](" Celsius");

delay(2000); //Delay 2 sec.

Summary:
In this experiment we have learnt the basics of the DHT-11, its interfacing with arduino
board and the programming to measure the humidity and temperature and display the
reading on the serial monitor.

ViVa Questions:

1. What is the purpose of DHT-11 sensor module and explain its working.

2. What do you understand by NTC sensor

3. What is use of the DHT-11 library

You might also like