0% found this document useful (0 votes)
15 views

Analog-To-Digital-Conversion-Laboratory-Activity-2

Uploaded by

Denver Sumbagan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Analog-To-Digital-Conversion-Laboratory-Activity-2

Uploaded by

Denver Sumbagan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

CPE 12 - MICROPROCESSOR SYSTEMS

Department of Computer Engineering


Instructor: Engr. Daniel Gracias V. Esquejo

LABORATORY ACTIVITY NO. 2


ANALOG – TO – DIGITAL CONVERSION

I. OBJECTIVES

At the end of the laboratory activity the student should be able to:

1. Understand how the Arduino converts analog input signals to digital signals.
2. Control LED and RGB LED outputs from an analog input.
3. Detect the ambient light intensity using a light-dependent resistor.
4. Incorporate temperature and humidity sensor
5. Use Pulse Width Modulation (PWM) in controlling LED and RGB LED outputs.
6. Calibrate analog input readings.
7. Map an input analog reading to a desired range of output values.

II. EQUIPMENT AND MATERIALS

• 1 pc Arduino Uno 328p


• 1 pc Breadboard
• 3 pcs 220 ohms Resistor (1/4 watts)
• 1 pc 10K Potentiometer
• 1 pc LDR
• 1 pc RGB
• 1 pc DHT11 Temperature and Humidity Sensor
• Desktop Computer

III. DISCUSSION

Analog-to-Digital Conversion

Connecting digital circuitry to sensor devices is simple


if the sensor devices are inherently digital themselves.
Switches, relays, and encoders are easily interfaced
with gate circuits due to the on/off nature of their signals.
However, when analog devices are involved, interfacing
becomes much more complex. Electronically
translating analog signals into digital (binary) quantities
is possible with the use of an analog- to-digital converter.

Analog-to-digital conversion is the process of converting a continuous quantity to a


discrete time digital representation. The electronic device used for this process is
an analog-to-digital converter (ADC). ADCs convert an input analog voltage or
current to a digital number proportional to the magnitude of the voltage or current.
ADCs are used virtually everywhere where an analog signal has to be processed,
stored, or transported in digital form. Fast video ADCs are used, for example, in TV
tuner cards. Slow on-chip 8, 10, 12, or 16 bit ADCs are common in microcontrollers.
Very fast ADCs are needed in digital oscilloscopes, and are crucial for new applications
like software defined radio.

Page | 1
CPE 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

Pulse Width Modulation (PWM)


Pulse Width Modulation, or PWM, is a technique for getting analog results with
digital means. Digital control is used to create a square wave, a signal switched
between on and off. This on-off pattern can simulate voltages in between full on (5
Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus
the time that the signal spends off. The duration of "on time" is called the pulse width.
To get varying analog values, you change, or modulate, that pulse width. If you repeat
this on-off pattern fast enough with an LED for example, the result is as if the signal is
a steady voltage between 0 and 5v controlling the brightness of the LED.

In Figure 1 below, the green lines represent a regular time period. This duration
or period is the inverse of the PWM frequency. In other words, with Arduino's
PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each.
A call to analogWrite() is on a scale of 0 - 255, such that analogWrite(255) requests a
100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the
time) for example.

Figure 1. Various PWM signals.

Light Dependent Resistor (LDR)

Photoresistors or light dependent resistors (LDRs) are very


useful especially in light/dark sensor circuits. As its name suggests,
it offers resistance in response to the ambient light. The resistance
decreases as the intensity of incident light increases, and vice Figure 1. An LDR
versa. In the absence of light, LDR exhibits a resistance of the order
of mega-ohms which decreases too few hundred ohms in the presence of light. It can
act as a sensor, since a varying voltage drop can be obtained in accordance with the
varying light.

Page | 2
CPE 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

Photoresistors come in many different types. The most common are the inexpensive
cadmium sulfide cells which could be found in many consumer items such
as camera light meters, street lights, clock radios, alarm devices, and
outdoor clocks.

RGB LED
An RGB LED is actually three LEDs in one bulb. The housing
contains separate red, blue and green LEDs which share a common
cathode, or negative terminal. The brightness of each color is determined
by its input voltage. By combining the three colors in different amounts,
you can turn the LED any color you want and lets you create all sorts Figure 2. RGB LED
of colors with just four pins from the Arduino. Usually, the output color
can be controlled by sending PWM signals to the leads of the RGB LED.
We have a color chart available on the internet for different colors, there are
different values of primary colors as a result of which we can visualize that color. For
example, if we want the white color shade, we have to use the maximum values of all
primary colors which are 255 in decimal, as a result of which we can get the white color.
There are values of some other colors given in the figure 3.

Figure 3 Color Shade Values

Page | 3
CPE 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

Temperature and Humidity Sensor


The DHT11 sensor is a digital temperature and humidity
sensor that can be used with an Arduino board. It consists of a
humidity sensor and a thermistor to measure the surrounding air's
relative humidity and temperature.
The sensor communicates with the Arduino board using a
single-wire serial interface, and it requires a digital Input/Output
(I/O) pin to read the data. The sensor can measure the
temperature in the range of 0 to 50°C with an accuracy of ±2°C,
and it can measure the relative humidity in the range of 20% to
80% with an accuracy of ±5%.

DHT11 sensors typically require an Figure 4. DHT11 Temperature and Humidity


external 10K pull-up resistor on the output sensor
pin for proper communication between the
sensor and the Arduino. However, because the module already includes a pull-up
resistor, you do not need to add one. The module also includes a decoupling capacitor
for filtering power supply noise.

To use the DHT11 sensor with an Arduino board, you need to connect the
sensor's data pin to one of the Arduino's digital I/O pins, and you also need to power
the sensor using the 5V pin and ground pin of the Arduino board. After connecting the
sensor, you can read the temperature and humidity data by using the appropriate
Arduino libraries and programming code.

Calibrating Analog Input


Sometimes analog readings don’t fit exactly the entire range of ADC output
values (0 – 1023) of the Arduino. This happens mostly on interfacing sensors in which
only a subset range of the possible input voltage range (0 – 5V) is utilized. Hence in
this case, rescaling of the ADC output would be necessary in order for the Arduino to
use a valid range of values which might be needed in some calculations. This could be
done by initial calibration of the sensor before starting the main program.

This example below demonstrates one technique for calibrating sensor input. The
Arduino takes sensor readings for five seconds during the startup, and tracks the
highest and lowest values it gets. These sensor readings during the first five seconds
of the sketch execution define the minimum and maximum of expected values for the
readings taken during the loop.

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


to const int ledPin = 9; // pin that the LED is attached
to

//The initialization below may seem backwards. Initially, you set the
//minimum high and read for anything lower than that, saving it as the
//new minimum. Likewise, you set the maximum low and read for
anything//higher as the new maximum.
int sensorValue = 0; // the sensor value
int sensorMin = 1023; // minimum sensor
value int sensorMax = 0; // maximum
sensor value

void setup() {
// turn on LED to signal the start of the calibration period:

Page | 4
CPE 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

pinMode(13, OUTPUT);
digitalWrite(13, HIGH);

// calibrate during the first five


seconds while (millis() < 5000) {
sensorValue = analogRead(sensorPin);

// record the maximum sensor


value if (sensorValue >
sensorMax) {
sensorMax = sensorValue;
}

// record the minimum sensor


value if (sensorValue <
sensorMin) {
sensorMin = sensorValue;
}
}

// signal the end of the calibration


period digitalWrite(13, LOW);
}

void loop() {
// read the sensor:
sensorValue = analogRead(sensorPin);

// apply the calibration to the sensor reading


sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

// fade the LED using the calibrated value:


analogWrite(ledPin, sensorValue);
}

Useful Arduino ADC Functions

•analogRead(pin)

Description
Reads the value from the specified analog pin. The Gizduino board contains a
6 channel 10-bit analog to digital converter. This means that it will map input
voltages
between 0 and 5 volts into integer values between 0 and 1023. This yields
a
resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per
unit.

Parameters
pin: the number of the analog input pin to read from. (A0-A5 or 0-5)

Returns
An integer value ranging from 0 to 1023

Example

Page | 5
CPE 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

// potentiometer wiper (middle terminal) connected to analog pin 3;


// outside leads to ground and +5V
int analogPin = 3;

int val = 0; //variable to store the value

read void setup()


{
Serial.begin(9600); //setup serial
}

void loop()
{
val = analogRead(analogPin); //read the input pin
Serial.println(val); //print value
}

• analogWrite(pin, value)

Description
Writes an analog value (PWM wave) to a pin. It can be used to light a LED at varying
brightness or driving a motor at various speeds. After a call to analogWrite(), the
pin will generate a steady square wave of the specified duty cycle until the next call
to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin). The
frequency of the PWM signal is approximately 490 Hz. You do not need to call
pinMode() to set the pin as an output before calling analogWrite(). This function has
nothing whatsoever to do with the analog pins or the analogRead() function.

Parameters
pin: the pin to write to (works only on pins 3, 5, 6, 9, 10 and 11 of the Gizduino)
value: the duty cycle: between 0 (0% duty cycle, always off) and 255 (100%
duty cycle, always on).

Returns
Nothing.

Example
The sketch below sets the output to the LED proportional to the value read from
the potentiometer.

int ledPin = 9; //LED connected to digital pin 9


int analogPin = 3; //potentiometer connected to analog pin 3 int
val = 0; //variable to store the read value

void setup()
{
pinMode(ledPin, OUTPUT); //set the pin as output
}

void loop()
{
val = analogRead(analogPin); //read the input pin

//analogRead values go from 0 to 1023, analogWrite values


from
//0 to 255
analogWrite(ledPin, val / 4);

Page | 6
CPE 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

• map(value, fromLow, fromHigh, toLow, toHigh)

Description
Re-maps a number from one range to another. That is, a value of fromLow would
get mapped to toLow, a value of fromHigh to toHigh, values in-between to values
in-between, etc.

Note that the "lower bounds" of either range may be larger or smaller than
the "upper bounds" so the map() function may be used to reverse a range of
numbers, for example

y = map(x, 1, 50, 50, 1);

The function also handles negative numbers well, so that this example

y = map(x, 1, 50, 50, -100);

is also valid and works well.

The map() function uses integer math so will not generate fractions, when the math
might indicate that it should do so. Fractional remainders are truncated, and are not
rounded or averaged.

Parameters
value: the number to map
fromLow: the lower bound of the value's current range
fromHigh: the upper bound of the value's current range
toLow: the lower bound of the value's target range
toHigh: the upper bound of the value's target range

Returns
The mapped value.

Example
/* Map an analog value to 8 bits (0 to 255) */
int ledPin = 9;
int potPin = A0;

void setup() {}

void loop()
{
int val = analogRead(potPin); //read analog input
val = map(val, 0, 1023, 0, 255); //map read analog input
analogWrite(ledPin, val); //output mapped value to a LED
}

Page | 7
CPE 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

IV. DIAGRAM

Figure 5(a). LED RGB connection with potentiometer Diagram

Figure 5(b). LED RGB PWM connection Diagram

Figure 5(c). LDR Connection Diagram

Page | 8
CPE 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

Figure 5(d). DHT11 Temperature and Humidity Sensor Connection Diagram


Figure 5. Breadboard Layout Wiring Diagram

V. PROCEDURES
Part I. Basic Analog I/O
A. Control blink delay of a LED using a potentiometer
1. Construct the breadboard connection in Figure 5(a).
2. Once connected, Encode the sketch below and upload it to the Arduino
board:

int sensorPin = A0; // select the input pin for the potentiometer
int ledPinred = 11; // select the pin for the LED
int ledPingreen = 10;
int ledPinblue = 9;
int sensorValue = 0; // variable to store the value coming from
the sensor

void setup() {
// declare the ledPin as an OUTPUT
pinMode(ledPinred, OUTPUT);
pinMode(ledPingreen, OUTPUT);
pinMode(ledPinblue, OUTPUT);
}

void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPinred, HIGH);
digitalWrite(ledPingreen, HIGH);
digitalWrite(ledPinblue, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPinred, LOW);
digitalWrite(ledPingreen, LOW);
digitalWrite(ledPinblue, LOW);
// stop the program for for <sensorValue> milliseconds:

Page | 9
CPE 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

delay(sensorValue);
}

3. Turn the potentiometer clockwise and counterclockwise. Observe the


behavior of the onboard LED in the Arduino and the RGB LED as you turn
the potentiometer.

B. Fade LED output using PWM


1. Construct the breadboard connection in Figure 5(b).
2. Once connected, Encode the sketch below and upload it to the Arduino
board:
int ledPinred = 11; // select the pin for the LED
int ledPingreen = 10;
int ledPinblue = 9;
int fadeVal = 0; //stores LED fade value

void setup()
{
// nothing happens in setup
}

void loop()
{
//fade in from min to max in increments of 5 points:
for(fadeVal = 0 ; fadeVal <= 255; fadeVal +=5)
{
//output a PWM signal (range from 0 to 255):
analogWrite(ledPinred, fadeVal);
analogWrite(ledPingreen, fadeVal);
analogWrite(ledPinblue, fadeVal);

//wait for 30 milliseconds to see the dimming effect


delay(50);
}

//fade out from max to min in increments of 5 points:


for(fadeVal = 255 ; fadeVal >= 0; fadeVal -=5)
{
//output a PWM signal (range from 0 to 255):
analogWrite(ledPinred, fadeVal);
analogWrite(ledPingreen, fadeVal);
analogWrite(ledPinblue, fadeVal);

//wait for 30 milliseconds to see the dimming effect


delay(50);
}
}
3. Observe the RGB LED that you connect to the Arduino.

C. Control LED brightness using PWM


1. Construct the breadboard connection in Figure 5(a).
2. Once connected, Encode the sketch below and upload it to the Arduino
board:
//Analog input pin that the pot is attached to
int analogInPin = A0;
//Analog output pin that the LED is attached to

Page | 10
CPE 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

int analogOutPinblue = 9;
int analogOutPingreen = 10;
int analogOutPin2red = 11;

int sensorValue = 0; //value read from the pot


int outputValue = 0; //value output to the PWM (analog out)

void setup()
{
Serial.begin(9600); //initialize serial communications at 9600bps
}

void loop()
{
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPinbkue, outputValue);
analogWrite(analogOutPingreen, outputValue);
analogWrite(analogOutPinred, outputValue);

// print the results to the serial monitor:


Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);

// wait 10 milliseconds before the next loop


// for the analog-to-digital converter to settle
// after the last reading:
delay(50);
}
3. Open the Serial Monitor and Turn the potentiometer clockwise and
counterclockwise. Observe the behavior of the RGB LED as you turn the
potentiometer.

D. Manipulating color shade using RGB


1. Construct the breadboard connection in Figure 5(b).
2. Once connected, Encode the sketch below and upload it to the Arduino
board:

int blueLED = 9;
int greenLED = 10;
int redLED = 11;

void setup() {
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(blueLED, OUTPUT);
}

void loop() {
RGB_output(255, 0, 0); //red color
delay(3000);
RGB_output(0, 255, 0); //lime color
delay(3000);
RGB_output(0, 0, 255); //blue color
delay(3000);

Page | 11
CPE 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

RGB_output(255, 255, 255); //white color


delay(1000);
RGB_output(128, 0, 0); //maroon color
delay(3000);
RGB_output(0, 128 , 0); //green color
delay(3000);
RGB_output(128, 128, 0); //olive
delay(3000);
RGB_output(255, 0, 255); //magenta color
delay(3000);
}

void RGB_output(int redLight, int greenLight, int blueLight)


{
analogWrite(redLED, redLight);
analogWrite(greenLED, greenLight);
analogWrite(blueLED, blueLight);
}

3. Observe the RGB LED that you connect to the Arduino.

E. RGB LED Module using Digital Method


1. Construct the breadboard connection in Figure 5(b).
2. Once connected, Encode the sketch below and upload it to the Arduino
board:

int blueLED = 9;
int greenLED = 10;
int redLED = 11;

void setup() {
pinMode(blueLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
}

void loop() {
digitalWrite(blueLED, HIGH);
delay(1000);
digitalWrite(blueLED, LOW);
digitalWrite(greenLED, HIGH);
delay(1000);
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
delay(1000);
digitalWrite(redLED, LOW);
digitalWrite(blueLED, LOW);
delay(1000);

3. Observe the RGB LED that you connect to the Arduino.

Part II. Advanced Analog I/O


A. Detect ambient light intensity using LDR
1. Construct the breadboard connection in Figure 5(c).
2. Once connected, Encode the sketch below and upload it to the Arduino
board:

Page | 12
CPE 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

//LDR pin
const int sensorPin = A0;
//min sensor reading, discovered through calibration
int senMin;
//max sensor reading, discovered through calibration
int senMax;
//current sensor reading
int senReading;

void setup()
{
//initialize serial communication:
Serial.begin(9600);

pinMode(13, OUTPUT); //use onboard LED


digitalWrite(13, HIGH); //signal start of calibration

while (millis() < 5000) //calibrate during the first five seconds
{
senReading = analogRead(sensorPin);
Serial.println("Calibrating sensor...");
if (senReading > senMax) //record the maximum sensor value
{
senMax = senReading;
}
if (senReading < senMin) //record the maximum sensor value
{
senMin = senReading;
}
}
digitalWrite(13, LOW); //signal the end of the calibration period
}

void loop()
{
senReading = analogRead(A0); //read the sensor:

//map the sensor range to a range of four options:


int range = map(senReading, senMin, senMax, 0, 3);

//do something different depending on the


//range value:
switch (range)
{
case 0:
Serial.println("dark");
break;
case 1:
Serial.println("dim");
break;
case 2:
Serial.println("medium");
break;
case 3:
Serial.println("bright");
break;
}
}

3. Open Serial Monitor. Try varying the light received by the LDR by shining
a flashlight on it. Observe the messages displayed in the Serial Monitor.

B. Temperature and Humidity Sensor

Page | 13
CPE 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

1. Construct the breadboard connection in Figure 5(d).


2. Once connected, Encode the sketch below and upload it to the Arduino
board:

#include <dht.h> //The DHT11 library


#define signalPin 5 //data or signal pin connected to
//pin 5 of arduino

dht DHT; //Creates a DHT object

void setup() {
Serial.begin(9600); //initialized serial communication
}

void loop() {
int readData = DHT.read11(signalPin); // function used to read
//the temperature and humidity data from the DHT11 sensor
//connected to the signalPin

float temp = DHT.temperature; // read temperature value


float hum = DHT.humidity; // read humidity value

Serial.print("Temperature = ");
Serial.print(temp);
Serial.print("°C | ");
Serial.print((temp*9.0)/5.0+32.0); // Convert Celsius to
// Fahrenheit
Serial.println("°F ");
Serial.print("Humidity = ");
Serial.print(hum);
Serial.println("% ");
Serial.println("");
delay(2000);
}
3. Open the Serial Monitor and try to place a hot object or cold object near
the sensor then observe the reading.

VI. PROGRAMMING ACTIVITIES.


Create the connections and the program of the following activities.

1. Create a program that changes the color of the RGB LED using the
detected ambient light intensity using a light-dependent resistor (LDR) and
output the ambient light (Bright, Dim and Dark) intensity in the Serial
Monitor.

2. Create a program that will monitor the temperature in 0C and 0F that the
reading will be displayed in the Serial monitor and also incorporate the RGB
LED to monitor the HOTNEST and COLDNEST based on the range below.
Temperature (T) Remarks
0> T <=20 Degree Centigrade COLD (GREEN LED)
T > 20 Degree Centigrade HOT (RED LED)

Page | 14
CPE 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo

ASSESSMENT RUBRICS (ACTIVITY)


Name: ______________________________________ Course and Year: _________
Due Date: _____________________ Date Checked: __________________________
LABORATORY ACTIVITY NO. 1 LABORATORY ACTIVITY NO. 1
( PROGRAMMING ACTIVITY 1) ( PROGRAMMING ACTIVITY 2)
CRITERIA POINTS CRITERIA POINTS
Functionality (35) Functionality (35)
Code Efficiency (35) Code Efficiency (35)
Breadboard Component Breadboard Component
Layout/Fritzing Design Layout/Fritzing Design
Connections (20) Connections (20)
Wiring Wiring
(Neatness, color coding) (5) (Neatness, color coding) (5)
Timeliness (5) Timeliness (5)
TOTAL EARNED POINTS TOTAL EARNED POINTS

Assessed by:
___________________________ Date: ________________
DANIEL GRACIAS V. ESQUEJO

Comments:

Page | 15

You might also like