Internet of Things Lab Manual
ISBN 9788119221745

Highlights

Notes

  

Chapter 3: Analog Values

Analog Signal: An analog signal is any continuous signal representing some other quantity, i.e., analogous to another quantity.

In the lower-right part of the Arduino board, you’ll see six pins marked “Analog In”; these are special pins that can tell us not only whether there is a voltage applied to them, but if so, also its value. By using the analogRead() function, we can read the voltage applied to one of the pins. This function returns a number between 0 and 1023, which represents voltages between 0 and 5 volts.

For example, if there is a voltage of 2 V applied to pin number 0, analogRead(0) returns 409.

Potentiomete r:

A potentiometer is a three-terminal resistor with a sliding or rotating contact that forms an adjustable voltage divider. If only two terminals are used, one end and the wiper, it acts asa variable resistor or rheostat.

The measuring instrument called a potentiometer is essentially a voltage divider used for measuring electric potential (voltage); the component is an implementation of the same principle, hence its name.

Potentiometers are commonly used to control electrical devices such as volume controls on audio equipment. Potentiometers operated by a mechanism can be used as position

transducers, for example, in a joystick. Potentiometers are rarely used to directly control significant power (more than a watt), since the power dissipated in the potentiometer would be comparable to the power in the controlled load.

PWM (Pulse Width Modulation): A technique the Arduino uses to write analog values.

A number of the digital pins on the right side of the Arduino can be used with PWM: pins 3, 5, 6, 9, 10 and 11. As you can see, each one of the pins is marked by the ~ symbol on the Arduino.

Program 3.1: To interface LED’s, potentiometer with Arduino and write a program to turn on or offmore of the LEDs by turning the potentiometer knob.

Components: Arduino Uno, 250 kΩ Potentiometer, 100 Ω Resistor, Red LED.

Circuit Diagram:

Code:

int sensorValue = 0;

void setup(){pinMode(A0, INPUT);pinMode(9, OUTPUT);Serial.begin(9600);

}

void loop()

{

sensorValue = analogRead(A0);if (sensorValue >= 500)digitalWrite(9,HIGH);

else

digitalWrite(9,LOW);

delay(2); // Wait for 2 millisecond(s)

}

Output:

Program 3.2: To interface LCD, push button, potentiometer with Arduino and write a program to display the no. of times (count) the push button is pressed on LCD and display message on LCD when push button is pressed.

Components: Arduino Uno, LCD 16 x 2, 100 Ω Potentiometer, 2pcs. 1 kΩ Resistor, Pushbutton

Circuit Diagram:

Code:

#include <LiquidCrystal.h>int button1 = 12;

int count = 0;

LiquidCrystal lcd(2,3,4,5,6,7); void setup()

{

lcd.begin(16,2);

}

void loop()

{

int buttonStatus1 = digitalRead(button1);if(buttonStatus1 == HIGH) {

count = count+1;lcd.setCursor(0,1);lcd.print(“Welcome”);

}

else {lcd.clear();}

lcd.setCursor(0,0);lcd.print(“Count = ”);lcd.print(count);delay(125);

}

Output:

Exercise 3.1: To interface LED’s, potentiometer with Arduino and write a program to implement allAnalog signal functions.

Program 3.3: To interface LED, Photo resistor (LDR) with Arduino and write a program to increaseand decrease the brightness of the LED based on the amount of light present.

Components: Photoresistor, Arduino Uno, Red LED, 1 kΩ Resistor, 100 Ω Resistor.

Theory:

A photoresistor (also known as a light-dependent resistor, LDR, or photo-conductive cell) is a passive component that decreases resistance with respect to receiving luminosity (light) on the component’s sensitive surface. The resistance of a photoresistor decreases with increase in incident light intensity; in other words, it exhibits photoconductivity. A photoresistor can be applied in light-sensitive detector circuits and light-activated and dark-activated switching circuits acting as a resistance semiconductor. In the dark, a photoresistor can have a resistance as high as several megaohms (MΩ), while in the light, a photoresistor can have a resistance as low as a few hundred ohms. If incident light on a photoresistor exceeds a certain frequency, photons absorbed by the semiconductor give bound electrons enough energy to jump into the conduction band. The resulting free electrons (and their hole partners) conduct electricity, thereby lowering resistance. The resistance range and sensitivity of a photoresistor can substantially differ among dissimilar devices. Moreover, unique photoresistors may react substantially differently to photons within certain wavelength bands.

A photoelectric device can be either intrinsic or extrinsic. An intrinsic semiconductor has its own charge carriers and is not an efficient semiconductor, for example, silicon. In intrinsic devices, the only available electrons are in the valence band, and hence the photon must have enough energy to excite the electron across the entire bandgap. Extrinsic devices have impurities, also called dopants, added whose ground state energy is closer to the conduction band; since the electrons do not have as far to jump, lower energy photons (that is, longer wavelengths and lower frequencies) are sufficient to trigger the device. If a sample of silicon has some of its atoms replaced by phosphorus atoms (impurities), there will be extra electrons available for conduction. This is an example of an extrinsic semiconductor.

Circuit Diagram:

Code:

int ldrPin = A0;int ledPin = 13;

int sensorValue = 0;

void setup()

{

pinMode(ledPin, OUTPUT);Serial.begin(9600);

}

void loop()

{

sensorValue = analogRead(ldrPin);Serial.println(sensorValue);

if(sensorValue <= 500)digitalWrite(ledPin, HIGH);

else

digitalWrite(ledPin, LOW);

}

Output:

Program 3.4: To interface LED’s with Arduino and write a program to show the fading effect onLED’s.

Component: Arduino Uno, 0.2 kΩ Resistor, Red LED

Theory:

In order to fade your LED off and on, gradually increase your PWM value from 0 (all the way off) to 255 (all the way on), and then back to 0 once again to complete the cycle. In the sketch below, the PWM value is set using a variable called brightness. Each time through the loop, it increases by the value of the variable fadeAmount.

If brightness is at either extreme of its value (either 0 or 255), then fadeAmount is changed to its negative. In other words, if fadeAmount is 5, then it is set to -5. If it’s -5, then it’s set to 5. The next time through the loop, this change causes brightness to change direction as well.

analogWrite() can change the PWM value very fast, so the delay at the end of the sketch controls the speed of the fade. Try changing the value of the delay and see how it changes the fading effect.

Circuit Diagram:

Code:

int led = 9;

int bright = 255;int fed = 5;

void setup()

{

pinMode(9, OUTPUT);

}

void loop()

{

analogWrite(led,bright);bright = bright + fed;delay(30);

if(bright <= 0){

for (int i=0;i<=255;i=i+fed)

{

bright = i;analogWrite(led,bright);delay(30);

}

}

else if(bright >= 255){

for (int i=255;i >=0;i=i-fed)

{

bright = i;analogWrite(led,bright);delay(30);

}

}}

Output:

Program 3.4: To interface LM35 sensor with Arduino and write a program to display temperaturedata on serial monitor.

Component: Arduino Uno, Temperature Sensor [TMP36]

Theory:

TMP36 is a temperature sensor chip which generates an analog voltage at the output which is linearly proportional to the Celsius temperature. Then convert this voltage into temperature based on a 10 mV/°C scale factor. It has a shutdown capability which limits the output current to less than 0.5 µA. It provides a supply current of up to 50 µA.

This sensor provides a highly precise temperature in centigrade. Most importantly, it produces output in dc voltage that we can measure easily with the help of any bare metal microcontrollers such as Arduino Uno, STM32F4, PIC16F877A. On top of that, Celsius’s temperature and an output voltage change linearly which makes it easy to compensate temperature/Voltage variations. Having a linear relationship is helpful. Because we will not require any external calibration circuit. Furthermore, it offers a very low output impedance. In short, it is very easy to interface this sensor with ADCs or microcontrollers having built-in ADCs.

Circuit Diagram:

Code:

void setup()

{

Serial.begin(9600);

}

void loop()

{

int sensorValue = analogRead(A0);float volt = (5.0 / 1024) * sensorValue;delay(2000);

float tempC = (volt - 0.5) * 100;Serial.print(“Temperate in Celsius”);Serial.print(tempC);

float tempF = (tempC * 9/5) + 32;Serial.print(“\nTemperate in Fahrenheit ”);Serial.println(tempF);

}

Output:

Exercise 3.2: To interface LM35 sensor with Arduino and write a program to display temperaturedata on LCD.

Program 3.5: To interface PIR sensor with Arduino and write a program to turn on and off LEDdepending on motion detection.

Component: Arduino Uno, PIR Sensor, Red LED, 100 Ω Resistor

Theory:

PIR sensors are more complicated than many of the other sensors explained in these tutorials (like photocells, FSRs and tilt switches) because there are multiple variables that affect the sensors input and output. To begin explaining how a basic sensor works, we’ll use this rather nice diagram

The PIR sensor itself has two slots in it, each slot is made of a special material that is sensitive to IR. The lens used here is not really doing much and so we see that the two slots can ‘see’ out past some distance (basically the sensitivity of the sensor). When the sensor is idle, both slots detect the same amount of IR, the ambient amount radiated from the room or walls or outdoors. When a warm body like a human or animal passes by, it first intercepts one half of the PIR sensor, which causes a positive differential change between the two halves.

When the warm body leaves the sensing area, the reverse happens, whereby the sensor

generates a negative differential change. These change pulses are what is detected.

Circuit Diagram:

Code:

void setup()

{

pinMode(13, OUTPUT);pinMode(2, INPUT);

}

void loop()

{

int sensorMotion = digitalRead(2);if (sensorMotion == HIGH)digitalWrite(13, HIGH);

else

digitalWrite(13, LOW);

}

Output:

Program 3.6: To interface Ultrasonic sensor with Arduino and write a program to display objectdistance(in inch/cm) in serial monitor depending on sound detection.

Component: Arduino Uno, Ultrasonic Distance Sensor

Theory:

An ultrasonic sensor is an instrument that measures the distance to an object using ultrasonic sound waves.

An ultrasonic sensor uses a transducer to send and receive ultrasonic pulses that relay back information about an object’s proximity.

High-frequency sound waves reflect from boundaries to produce distinct echo patterns

Ultrasonic sensors work by sending out a sound wave at a frequency above the range of human hearing. The transducer of the sensor acts as a microphone to receive and send the ultrasonic sound. Our ultrasonic sensors, like many others, use a single transducer to send a pulse and to receive the echo. The sensor determines the distance to a target by measuring time lapses between the sending and receiving of the ultrasonic pulse.

Circuit Diagram:

Code:

int trig = 12;int echo = 11;int travelTime;int distance;

int distanceInch;

void setup()

{

pinMode(trig, OUTPUT);pinMode(echo, INPUT);Serial.begin(9600);

}

void loop()

{

digitalWrite(trig, LOW);delayMicroseconds(10);digitalWrite(trig, HIGH);delay(10);digitalWrite(trig, LOW);

travelTime = pulseIn(echo,HIGH);

delay(20);

distance = (travelTime / 2) * 0.0343;distanceInch = (travelTime / 2)* 0.013464;Serial.print(“Distance in CM ”);Serial.println(distance);

Serial.print(“Distance in INCHES ”);Serial.println(distanceInch);

}

Output:

Click on Serial Monitor.