Analog To Digital & Digital To Analog Conversion: Nasif M
Analog To Digital & Digital To Analog Conversion: Nasif M
Analog To Digital & Digital To Analog Conversion: Nasif M
&
Digital to analog
Conversion
Nasif M.
1
How to interface sensor with Microcontroller
• A sensor is an object whose purpose is to detect events or changes
in its environment, and then provide a corresponding output.
• Many sensors in the real world are simple resistive devices. A
photocell is a variable resistor, which produces a resistance
proportional to the amount of light it senses. Other devices like flex
sensors, force-sensitive resistors, and thermistors, are also variable
resistors.
2
Types of Sensors
• Analog sensor
• Digital sensor
3
Analog Sensors
There are different types of sensors that produce continuous
analog output signal and these sensors are considered as analog
sensors. Practical examples of various types of analog sensors are
as follows: accelerometers, pressure sensors, light sensors, sound
sensors, temperature sensors, and so on.
5
Digital Sensors
Electronic sensors or electrochemical sensors in which data
conversion and data transmission takes place digitally are called as
digital sensors. These digital sensors are replacing analog sensors
as they are capable of overcoming the drawbacks of analog
sensors. The digital sensor consists of majorly three components:
senor, cable, and transmitter. In digital sensors, the signal
measured is directly converted into digital signal output inside the
digital sensor itself then this digital signal is transmitted through
cable digitally.
6
Ultrasonic sensor
7
Digital Sensors
Pros
• Digital signal produced by the digital sensor could be
transmitted long distance.
Cons
• Usually complex sensor design.
8
LDR
• Light dependent resistor (LDR) is used to detect change in light intensity or
as a light sensor. LDR is basically a variable resistor.
• When intensity of light decreases, LDR offer high resistance. Hence there is
a inverse relationship between intensity of light and resistance of LDR.
• The LDR is a resistor, and its resistance varies according to the amount of
light falling on its surface.
9
LDR
LDR
10
Variable Resistor R2 value change
Micro-controller
LDR Micro-controller
14
Pulse-code modulation (PCM)
The most widely used technique for digitizing information signals for
electronic data transmission is pulse-code modulation (PCM). PCM signals
are serial digital data.
16
A. Sampling
• Sample is a single measurement of amplitude.
• The larger sampling rate, the better accuracy of conversion.
1010__________________________________________
1001__________________________________________
1000__________________________________________
0111_________________________________________
0110__________________________________________
0101 __________________________________________
0100 __________________________________________
0011 __________________________________________
Measurement
Sound wave sample
of amplitude
17
B. Quantization (1)
The quantizing of an analog signal is done by discretizing the signal with a
number of quantization levels. Quantization is representing the sampled
values of the amplitude by a finite set of levels, which means converting a
continuous-amplitude sample into a discrete-time signal.
18
B. Quantization (2)
• Both sampling and quantization result in the loss of information. The quality
of a Quantizer output depends upon the number of quantization levels used.
• The difference between an input value and its quantized value is called
a Quantization Error.
19
2-bit resolution with
four levels
of quantization
20
3-bit resolution with
eight levels
of quantization
21
C. Encoding
• After the quantization process, the corresponding level is represented
using binary numbers.
Encoded result: 10, 11, 11, 10,
01, 00, 00, 01
22
Analog pins
of Arduino
board
23
Arduino interfacing with LDR
24
Analog to digital conversion
• Microcontrollers are capable of detecting binary signals: is the button
pressed or not? These are digital signals. When a microcontroller is powered
from five volts, it understands zero volts (0V) as a binary 0 and a five volts
(5V) as a binary 1.
• What if the signal is 2.72V? Is that a zero or a one? We often need to
measure signals that vary; these are called analog signals.
• An Analog to Digital Converter (ADC) is a very useful feature that converts an
analog voltage on a pin to a digital number.
• All microcontrollers have a analog to digital converter(ADC) built into them
that allows us to convert these voltages into values that we can use in a
program.
25
Analog to digital conversion
• ADCs can vary greatly between microcontroller.
• The ADC on the Arduino is a 10-bit ADC meaning it has the ability to detect
1,024 (210) discrete analog levels. Some microcontrollers have 8-bit ADCs
(28 = 256 discrete levels) and some have 16-bit ADCs (216 = 65,535 discrete
levels).
• The ADC reports a ratio-metric value. This means that the ADC assumes 5V
is 1023 and anything less than 5V will be a ratio between 5V and 1023.
26
Relating ADC Value to Voltage
27
Question
The 10-bit ADC of the Arduino on a 5V system. If the analog voltage is
2.12V then what will be the ADC report as a value?
28
Relating ADC Value to Voltage
Micro-controller LED
30
Digital to analog
• Pulse Width Modulation, or PWM, is a technique for getting digital to
analog conversion.
• Digital control is used to create a square wave, a signal switched
between on and off. This on-off pattern (duty cycle) 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.
• Arduino digital to analog resolution is 8 bit. (0-255)
31
Duty Cycle
• A duty cycle is the fraction of one period in which a signal or system is
active. Duty cycle is commonly expressed as a percentage or a ratio. A
period is the time it takes for a signal to complete an on-and-off cycle.
32
Digital to analog 33
Digital to analog 34
PWM
supporting
digital pins
are: 3, 5, 6, 9,
10, 11
35
In the micro-controller, 12 bit digital to analog (DAC) resolution is set.
During the digital to analog conversion programming, you have called
“analogWrite (1024)” instruction. Calculate the duty cycle. Draw the
duty cycle diagram. Consider system voltage is 5 V.
36
• Arduino analog to digital resolution is 10 bit. (0-1023)
37
Problem
R1 = 5.6 ohm
V= 5V
V(out) = 2.78 V
Calculate the value of R2=?
38
Reference
Voltage Divider Network
The Voltage Division Rule, allows us to use the effects of resistance
proportionality to calculate the potential difference across each resistance
regardless of the current flowing through the series circuit. A typical
“voltage divider circuit” is shown below.
39