khaled a. al-utaibi [email protected]. digital vs analog signals converting an analog signal to...

16
Analog Inputs Khaled A. Al-Utaibi [email protected]

Upload: julianna-oconnor

Post on 17-Dec-2015

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Khaled A. Al-Utaibi alutaibi@uoh.edu.sa.  Digital Vs Analog Signals  Converting an Analog Signal to a Digital One  Reading Analog Sensors with the

Analog InputsKhaled A. [email protected]

Page 2: Khaled A. Al-Utaibi alutaibi@uoh.edu.sa.  Digital Vs Analog Signals  Converting an Analog Signal to a Digital One  Reading Analog Sensors with the

Digital Vs Analog Signals Converting an Analog Signal to a Digital One Reading Analog Sensors with the Arduino The TMP36 Temperature Sensor Interfacing The TMP36 Sensor Reading The TMP36 Sensor Temperature Sensor Example

Agenda

Page 3: Khaled A. Al-Utaibi alutaibi@uoh.edu.sa.  Digital Vs Analog Signals  Converting an Analog Signal to a Digital One  Reading Analog Sensors with the

Digital electrical signals have just two discrete levels: HIGH (5V) and LOW (0V)

In Arduino, we used the following functions to deal with digital signals:− digitalWrite(pin, HIGH) to set a pin HIGH,−digitalWrite(pin, LOW) to set a pin LOW, and−digitalRead(pin) to determine whether a digital pin had a

voltage applied to it (HIGH) or not (LOW).

Digital Vs Analog Signals

Figure 1: Analog Vs digital signals

Page 4: Khaled A. Al-Utaibi alutaibi@uoh.edu.sa.  Digital Vs Analog Signals  Converting an Analog Signal to a Digital One  Reading Analog Sensors with the

Analog electrical signals have continuous range of values These signals can vary with an indefinite number of steps

between HIGH and LOW values. In Arduino, HIGH is closer to 5V and LOW is closer to 0V. In Arduino, we can use the function analogRead(pin) to

read analog signals This function will return a number between 0 and 1023 in

proportion to the voltage applied to the analog pin.

Digital Vs Analog Signals

Figure 1: Analog Vs digital signals

Page 5: Khaled A. Al-Utaibi alutaibi@uoh.edu.sa.  Digital Vs Analog Signals  Converting an Analog Signal to a Digital One  Reading Analog Sensors with the

It is common to convert analog signals into digital signals in order to allow for efficient transmission and processing of these signals.

To convert an Analog signal into a digital one, some loss of accuracy is inevitable since digital systems can only represent a finite discrete set of values.

The process of conversion is known as Digitization or Quantization.

Analog-to-Digital Converters (ADC) are used to produce a digitized signals.

Digital-to-analog Converters (DAC) are used to regenerate analog signals from their digitized signals.

Converting an Analog Signal to a Digital One

Page 6: Khaled A. Al-Utaibi alutaibi@uoh.edu.sa.  Digital Vs Analog Signals  Converting an Analog Signal to a Digital One  Reading Analog Sensors with the

We can use the Arduino ADC pins to convert analog voltage values into number representations that we can work with.

The accuracy of an ADC is determined by the resolution.

In the case of the Arduino Uno, there is a 10-bit ADC for doing your analog conversions.

“10-bit” means that the ADC can subdivide (or quantize) an analog signal into 210 =1024 different values.

Hence, the Arduino can assign a value from 0 to 1023 for any analog value that you give it.

Converting an Analog Signal to a Digital One

Page 7: Khaled A. Al-Utaibi alutaibi@uoh.edu.sa.  Digital Vs Analog Signals  Converting an Analog Signal to a Digital One  Reading Analog Sensors with the

The default reference voltage of the Arduino ADC is 5V.

The reference voltage determines the maximum voltage that you are expecting, and, therefore, the value that will be mapped to 1023.

So, with a 5V reference voltage, putting−0V on an ADC pin returns a value of 0, −2.5V returns a value of 512 (half of 1023), and −5V returns a value of 1023.

Converting an Analog Signal to a Digital One

Page 8: Khaled A. Al-Utaibi alutaibi@uoh.edu.sa.  Digital Vs Analog Signals  Converting an Analog Signal to a Digital One  Reading Analog Sensors with the

To better understand what’s happening here, consider what a 3-bit ADC would do, as shown in Figure 2.−A 3-bit ADC has 3 bits of resolution. −Because 23=8, there are 8 total logic levels, from 0 to 7. −Therefore, any analog value that is passed to a 3-bit ADC will

have to be assigned a value from 0 to 7. −Looking at Figure 2, you can see that voltage levels are

converted to discrete digital values that can be used by the microcontroller.

−The higher the resolution, the more steps that are available for representing each value.

−In the case of the Arduino Uno, there are 1024 steps rather than the 8 shown here.

Converting an Analog Signal to a Digital One

Page 9: Khaled A. Al-Utaibi alutaibi@uoh.edu.sa.  Digital Vs Analog Signals  Converting an Analog Signal to a Digital One  Reading Analog Sensors with the

Figure 2: 3-bit analog quantization

Page 10: Khaled A. Al-Utaibi alutaibi@uoh.edu.sa.  Digital Vs Analog Signals  Converting an Analog Signal to a Digital One  Reading Analog Sensors with the

Different Arduinos have different numbers of analog input pins, but you read them all the same way, using the analogRead() command.

The analogRead() function reads the value from the specified analog pin. −Syntax:

analogRead(pin)−Parameters:

pin: the number of the analog pin you want to read (int) .−Return:

int (0 to 1023)−Note:

If the analog input pin is not connected to anything, the value returned by analogRead() will fluctuate based on a number of factors (e.g. the values of the other analog inputs, how close your hand is to the board, etc.).

Reading Analog Sensors with the Arduino

Page 11: Khaled A. Al-Utaibi alutaibi@uoh.edu.sa.  Digital Vs Analog Signals  Converting an Analog Signal to a Digital One  Reading Analog Sensors with the

The TMP36 temperature sensor (Figure 3) has the following characteristics:−Provides a voltage range from -40oC to

+125oC.−Provide typical accuracies of ±2°C.−Provides a voltage output that is linearly

proportional to the Celsius (centigrade) temperature.

−Provides a 750mV output at 25°C.−Provides a 500mV output at 0oC.−Has an output scale factor of 10mV/°C

(every 10mV corresponds to 1oC).

The TMP36 Temperature Sensor

Figure 3: TMP36

Page 12: Khaled A. Al-Utaibi alutaibi@uoh.edu.sa.  Digital Vs Analog Signals  Converting an Analog Signal to a Digital One  Reading Analog Sensors with the

The TMP36 temperature sensor can be easily interfaced to the Arduino bard as shown in the following Figure 4.

Interfacing The TMP36 Sensor

Figure 4: Interfacing the TMP36 to the Arduino board.

Page 13: Khaled A. Al-Utaibi alutaibi@uoh.edu.sa.  Digital Vs Analog Signals  Converting an Analog Signal to a Digital One  Reading Analog Sensors with the

The Arduino uses ADC to read the output of the TMP36 sensor as shown Figure 5.

We can determine the temperature as follows:−(1) Convert the integer value generated by the ADC to voltage (in

mV) to determine the input voltage Vin: VIN = DACOUT x (VREF x 1000)/1024 VREF = 5V = 5x1000 mV = 5000 mV Every division of the 1024 DAC output represents 5000/1024=4.88mV

−(2) Convert input voltage VIN to the corresponding temperature (in oC): TIN = (VIN – 500)/10 We subtract 500 from VIN because the TMP36 provides 50mV output

at 0oC. We divide by 10 because every 10mV corresponds to 1oC.

Reading The TMP36 Sensor

Page 14: Khaled A. Al-Utaibi alutaibi@uoh.edu.sa.  Digital Vs Analog Signals  Converting an Analog Signal to a Digital One  Reading Analog Sensors with the

Figure 5: Reading the TMP36 sensor.

Page 15: Khaled A. Al-Utaibi alutaibi@uoh.edu.sa.  Digital Vs Analog Signals  Converting an Analog Signal to a Digital One  Reading Analog Sensors with the

Interface the TMP36 sensor to the Arduino board as in Figure 4.

Then, write a program to read the input voltage from the TMP36 and displays the corresponding temperature on on the Arduino environment’s built-in serial monitor.

Temperature Sensor Example

Page 16: Khaled A. Al-Utaibi alutaibi@uoh.edu.sa.  Digital Vs Analog Signals  Converting an Analog Signal to a Digital One  Reading Analog Sensors with the

int DACout; // DAC outputint A0 = 0; // Analog input A0double Vref = 5.0; // reference voltage of the DACdouble Vin; // input voltage from the TMP36double Tin; // corresponding input temperature

public void setup(){// initialize and start the serial portSerial.begin(9600);

}

public void loop(){// read the DAC output corresponding to analog input A0DACout = analogRead(A0);// compute the input voltage received from TMP36Vin = DACout * (Vref * 1000.0)/1024.0;// compute the corresponding input temperatureTin = (Vin - 500.0) / 10.0;// display the temperature on the serial portSerial.println(Tin);

}