Lec4 Analog Sensors
Lec4 Analog Sensors
CSE 3105
Computer Interfacing & Embedded System
An analog signal is a voltage, current, or physical quantity that continuously and infinitely varies in accordance with some time-
varying parameter
A digital signal is a signal that represents data as a sequence of discrete values; at any given time it can only take on one of a
finite number of values.
Converting Analog Signal to Digital
Analog signals are signals that have a continuous sequence with continuous
values. These types of signals can come from sound, light, temperature and
motion.
The higher the resolution, the more steps that are available for
representing each value.
fs = 1/T
Where,
fs = Sample Rate/Frequency
T = Period of the sample or the time it takes before sampling again
fNyquist = 2fMax
Where,
For example, if the signal that you input into the digital system has
a max frequency of 100 kHz, then the sampling rate on your ADC needs
to be equal or greater than 200 kS/s. This will allow for a
successful reconstruction of the original signal.
Converting Analog Signal to Digital
Sample ADC Resolution Formula:
Where,
N =
Where,
n = Bit Size
Converting Analog Signal to Digital
Potentiometers are
symmetrical, so it
doesn’t matter which
side you connect the
5V
rail and ground to.
You connect the
middle
pin to analog input
0 on your Arduino.
Reading a Potentiometer
As you turn the potentiometer,
you’re varying the voltage that
you are feeding into analog
input between 0V and 5V. You
can confirm this with a
multimeter in voltage
measurement mode by hooking it
up as shown and reading the
display as you turn the
potentiometer’s knob. The red
(positive) probe should be
connected to the middle pin,
and the black (negative) probe
should be connected to
whichever side is connected to
ground.
Serial Library
[Link]
Reading a Potentiometer
Before you use the potentiometer to control another
piece of hardware, use the Arduino’s serial
communication functionality to print out the
potentiometer’s ADC
value on your computer as it changes.
void setup() {
pinMode (POT, INPUT);
Serial.begin(9600);
}
void loop() {
val = analogRead(POT);
Serial.println(val);
delay(500);
}
[Link]
TMP36 Temperature Sensor
The TMP36 temperature sensor easily correlates
temperature readings in Celsius with voltage output
levels.
- Temperature range
-50°C to 125°C
- Voltage output range
0V to 1.75V
TMP36 Temperature Sensor
The TMP36 temperature sensor easily correlates
temperature readings in Celsius with voltage output
levels.
- Temperature range
-50°C to 125°C
- Voltage output range
0V to 1.75V
TMP36 Temperature Sensor
- Temperature range
-50°C to 125°C
- Voltage output range
0V to 1.75V
TMP36 Temperature Sensor
- Temperature range
-50°C to 125°C
- Voltage output range
0V to 1.75V
V = mT + c
V = 0.01T + 0.5
100V = T + 50
T = 100V - 50
TMP36 Temperature Sensor
T = 100V - 50
1023 / 5 = 143 / x
=> x = (143 * 5) / 1023
=> x = 0.7 V
=> x = 700 mV
TMP36 Temperature Sensor
Suppose the ADC output is 143. What is the room
temperature?
Assume that the ADC is 10 bits. Reference voltage is 5v.
1023 / 5 = 143 / x
=> x = (143 * 5) / 1023
=> x = 0.7 V
=> x = 700 mV
10T = mV - 500
=> 10T = 700 - 500
=> 10T = 200
=> T = 20
TMP36 Temperature Sensor
Suppose the ADC output is 143. What is the room
temperature?
Assume that the ADC is 10 bits. Reference voltage is 5v.
1023 / 5 = 143 / x
=> x = (143 * 5) / 1023
=> x = 0.7 V
=> x = 700 mV
void setup() {
pinMode (TEMP, INPUT);
pinMode (BLED, OUTPUT); // Set Blue LED as Output
pinMode (GLED, OUTPUT); // Set Green LED as Output
pinMode (RLED, OUTPUT); // Set Red LED as Output
}
TMP36 Temperature Sensor
void loop() {
val = analogRead(TEMP);
if (val < LOWER_BOUND) { // LED is Blue
digitalWrite(RLED, HIGH);
digitalWrite(GLED, HIGH);
digitalWrite(BLED, LOW);
}
else if (val > UPPER_BOUND) { // LED is Red
digitalWrite(RLED, LOW);
digitalWrite(GLED, HIGH);
digitalWrite(BLED, HIGH);
}
else { // LED is Green
digitalWrite(RLED, HIGH);
digitalWrite(GLED, LOW);
digitalWrite(BLED, HIGH);
}
}
Using Variable Resistors to Make Your Own Analog Sensors
toLow and toHigh are the values you want to map the
brightness values to. Because analogWrite() expects a value
between 0 and 255, you use those values. However, you want
a darker room to map to a brighter LED. Therefore, when the
input from the ADC is a low value, you want the output to
the LED’s PWM pin to be a high value, and vice versa.
Using Analog Inputs to Control Analog Outputs
If you pass the output from the map function into the
constrain function, you can set the min to 0 and the max to
255, ensuring that any numbers above or below those values
are constrained to either 0 or 255.
Automatic Night Light
// Automatic Night Light
const int WLED=PA0; // White LED Anode on pin PA0 (PWM)
const int LIGHT=PA1; // Light Sensor on Analog Pin PA1
const int MIN_LIGHT=200; // Minimum Expected light value
const int MAX_LIGHT=900; // Maximum Expected Light value
int val = 0; // Variable to hold the analog reading
void setup() {
pinMode(WLED, OUTPUT); // Set White LED pin as output
}
void loop() {
val = analogRead(LIGHT); // Read the light sensor
val = map(val, MIN_LIGHT, MAX_LIGHT, 255, 0); // Map the light reading
val = constrain(val, 0, 255); // Constrain light value
analogWrite(WLED, val); // Control the White LED
}
Thank
You!