160 likes | 328 Views
Analog Inputs. Khaled A. Al- Utaibi alutaibi@uoh.edu.sa. Agenda. 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
E N D
Analog Inputs Khaled A. Al-Utaibi alutaibi@uoh.edu.sa
Agenda • 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
Digital Vs Analog Signals • 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). Figure 1: Analog Vs digital signals
Digital Vs Analog Signals • Analog electrical signals have continuous range of values • These signals can vary with an indefinite number of steps between HIGHand LOW values. • In Arduino, HIGH is closer to 5V and LOWis 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 1023in proportion to the voltage applied to the analog pin. Figure 1: Analog Vs digital signals
Converting an Analog Signal to a Digital One • 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 • 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 • The default reference voltage of the Arduino ADC is 5V. • The reference voltage determines the maximumvoltage that you are expecting, and, therefore, the value that will be mapped to 1023. • So, with a 5V reference voltage, putting • 0Von an ADC pin returns a value of 0, • 2.5Vreturns a value of 512 (half of 1023), and • 5V returns a value of 1023.
Converting an Analog Signal to a Digital One • 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.
Reading Analog Sensors with the Arduino • Different Arduinoshave 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.).
The TMP36 Temperature Sensor • 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 750mVoutput at 25°C. • Provides a 500mV output at 0oC. • Has an output scale factor of 10mV/°C (every 10mV corresponds to 1oC). Figure 3: TMP36
Interfacing The TMP36 Sensor • The TMP36 temperature sensor can be easily interfaced to the Arduino bard as shown in the following Figure 4. Figure 4: Interfacing the TMP36 to the Arduino board.
Reading The TMP36 Sensor • 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.
Temperature Sensor Example • 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.
intDACout; // DAC output int A0 = 0; // Analog input A0 doubleVref = 5.0; // reference voltage of the DAC double Vin; // input voltage from the TMP36 double Tin; // corresponding input temperature public void setup(){ // initialize and start the serial port Serial.begin(9600); } public void loop(){ // read the DAC output corresponding to analog input A0 DACout = analogRead(A0); // compute the input voltage received from TMP36 Vin = DACout * (Vref * 1000.0)/1024.0; // compute the corresponding input temperature Tin = (Vin - 500.0) / 10.0; // display the temperature on the serial port Serial.println(Tin); }