Analog-To-Digital-Conversion-Laboratory-Activity-2
Analog-To-Digital-Conversion-Laboratory-Activity-2
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.
III. DISCUSSION
Analog-to-Digital Conversion
Page | 1
CPE 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
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.
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.
Page | 3
CPE 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
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.
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.
//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);
void loop() {
// read the sensor:
sensorValue = analogRead(sensorPin);
•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
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.
void setup()
{
pinMode(ledPin, OUTPUT); //set the pin as output
}
void loop()
{
val = analogRead(analogPin); //read the input pin
Page | 6
CPE 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
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
The function also handles negative numbers well, so that this example
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
Page | 8
CPE 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
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);
}
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);
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;
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);
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
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);
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);
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:
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.
Page | 13
CPE 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
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
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.
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
Assessed by:
___________________________ Date: ________________
DANIEL GRACIAS V. ESQUEJO
Comments:
Page | 15