Professional Documents
Culture Documents
IoT LAB #2
IoT LAB #2
Internet of Things
Lab # 2
Sensors Interfacing
Last date of Submission: 31-03-2023
Learning Objectives:
1. To understand the working of sensors interfacing with Arduino.
2. Getting familiar with the pulse width modulation techniques
3. Getting familiar with different sensors, applications and coding
Analog Input PINS: The main function of analog pins is to read analog sensors; analog pins
also have all the functionality of general-purpose input/output (GPIO) pins (same as digital
pins 0 - 13).
PIN Mapping: The analog pins can be used identically to digital pins, using aliases AO (for
analog input 0), A1, etc. For example, the code would look like this to set analog pin 0 to an
output, and to set it HIGH:
pinMode(A0, OUTPUT);
digitalWrite(A0, HIGH);
Pullup Resistors: The analog pins also have pullup resistors, which work identically to pullup
resistors on digital pins. They are enabled by issuing a command such as:
While the pin is an input, be aware however that turning on a pullup will affect the values
reported by analgoRead().
AnalogWrite():
This command writes an analog value to a pin. After a call to analogWrite(), pin will generate
a steady square wave of the specified duty cycle until the next call to analogWrite(). On
Arduino Uno pins 5 and 6 have a frequency of approximately 980 Hz. In most Arduino boards
this function works on pins 3, 5, 6, 9, 10, and 11.
AnalogRead():
This command reads the value from the specified analog pin. It will map input voltages
between 0 and 5 volts into integer values between 0 and 1023.
Syntax: analogRead(pin)
PWM stands for Pulse Width Modulation. Pulse Width Modulation, or PWM, is a technique
for getting analog results with digital means. Arduino uses this powerful PWM technique for
controlling analog circuits with its digital outputs. Digital control uses to be only turned on
(full 5v) or off (0v) in the binary format, and this on/off pattern can generate a square wave
signal. For example, if you want a LED to be half bright, you can either reduce the current
across the LED into half or use this more flexible PWM technique by sending 50% duty cycle
square wave signal to the LED.
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 Hz2. 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.
o Dimming an LED
o Providing an analog output; if the digital output is filtered, it will provide an analog
voltage between 0% and 100%.
o Generating audio signals.
o Providing variable speed control for motors.
o Generating a modulated signal, for example, to drive an infrared LED for remote
control.
Sample Program
Read the description and methodology mentioned above. Try to understand it and run the
code. Write the output in the box below.
Applications:
LDRs applications can be found in many consumer items such as camera light meters, clock
radios, alarm devices, night lights, outdoor clocks, solar street lamps and solar road studs.
Fig. LDR
Output:
If the sensor value is in the range of 900, relays become low and if less than 900 relay
becomes high.
Sample Program
The DHT11 is a relatively cheap sensor for measuring temperature and humidity. The DHT11
sensor includes a resistive-type humidity measurement component, an NTC temperature
measurement component and a high-performance 8-bit microcontroller inside and provides
calibrated digital signal output.
Pin Configuration: