You are on page 1of 16

COMSATS UNIVERSITY

MICROPROCESSOR SYSTEMS & INTERFACING


EEE-342

Lab # 07: Interfacing Analog Sensors using Analog to Digital


Converter

Name

Registration Number

Instructor’s Name DR. ASMA RAMAY

Lab # 07: Interfacing Analog Sensors using Analog to Digital


Converter
OBJECTIVES:
 Understand the function of ADC in microcontroller.
 Learn different mode of operation of ADC
 Interfacing of LM35 temperature sensor with Atmega328p.

TOOLS:

SOFTWARE TOOLS:
 Atmel Studio/AVR Studio
 Arduino
 Proteus ISIS
 AVR DUDESS

HARDWARE TOOLS:

PRE-LAB
Analog to digital converters are the most widely used devices for data acquisition. Most of the
physical quantities are analog e.g. temperature, humidity, pressure, light, etc. Therefore, we need
an analog to digital converter to convert the data into digital data in order to communicate with
the digital processors like microcontrollers and microprocessors.

7.1 Internal ADC of ATmega328p microcontroller


The ADC in Atmega328p converts an analog input voltage to a 10-bit digital value through
successive approximation. The minimum value represents GND and the maximum value
represents the voltage on the AREF pin minus 1 LSB. Optionally, AVCC or an internal 1.1V
reference voltage may be connected to the AREF pin by writing to the REFSn bits in the
ADMUX Register.
The ADC system of Atmega328P has following features
 10 bit ADC
 ±2 LSB absolute accuracy
 13 ADC clock cycle conversion rate
 8 multiplexed single-ended input channels
 Selectable right or left result justification
 0 to Vcc ADC input voltage range

7.2 ADC Register Description

1. ADC Multiplexer Selection Register – ADMUX

Bit 5 – ADLAR: ADC Left Adjust Result


The ADLAR bit affects the presentation of the ADC conversion result in the ADC Data Register.
Write one to ADLAR to left adjust the result. Otherwise, the result is right adjusted.

Bits 4:0 – MUX3:0: Analog Channel Selection Bits


The value of these bits selects which analog input is connected to the ADC. Table 8.3shows
these bit settings for selecting various single ended input channels:

2. ADC Control and Status Register A – ADCSRA


Bit 7 – ADEN: ADC Enable
Writing this bit to one enables the ADC. By writing it to zero, the ADC is turned off.
Bit 6 – ADSC: ADC Start Conversion
In Single Conversion mode, write this bit to one to start each conversion. ADSC will read as one
as long as a conversion is in progress. When the conversion is complete, it returns to zero.
Writing zero to this bit has no effect.
Bit 5 – ADATE: ADC Auto Trigger Enable
When this bit is written to one, Auto Triggering of the ADC is enabled (This mode is not
covered in this lab).
Bit 4 – ADIF: ADC Interrupt Flag
This bit is set when an ADC conversion completes and the Data Registers are updated. The ADC
Conversion Complete Interrupt is executed if the ADIE bit and the I-bit in SREG are set. ADIF
is cleared by hardware when executing the corresponding interrupt handling vector.
Alternatively, ADIF is cleared by writing a logical one to the flag.
Bit 3 – ADIE: ADC Interrupt Enable
When this bit is written to one and the I-bit in SREG is set, the ADC Conversion Complete
Interrupt is activated.
Bits 2:0 – ADPS2:0: ADC Prescaler Select Bits
These bits determine the division factor between the XTAL frequency and the input clock to the
ADC.

3. The ADC Data Register –ADCL and ADCH


These two registers contain 10 bits of the conversion result. The result is stored in two different
ways depending on the setting of ADLAR bit as shown below:
4. Digital Input Disable Register 0-DIDR0
When the respective bits are written to logic one, the digital input buffer on the corresponding
ADC pin is disabled. The corresponding PIN Register bit will always read as zero when this bit
is set. When an analog signal is applied to the ADC7...0 pin and the digital input from this pin is
not needed, this bit should be written logic one to reduce power consumption in the digital input
buffer.

7.3 Configuring the ADC


Following are the steps required to configure the ADC of AVR microcontrollers.
1. Disable digital I/O by writing ones in DIDR0.
2. Select a reference voltage and adjust your result to left/right using ADMUX register.
3. Select a pre-scalar for ADC clock on ADCSRA register. By default, the successive
approximation circuitry requires an input clock frequency between 50 kHz and 200 kHz to get
maximum resolution
4. Enable the ADC by setting the ADC Enable bit, ADEN in ADCSRA. Voltage reference and
input channel selections will not go into effect until ADEN is set. The ADC does not consume
power when ADEN is cleared.
5. A '0' must be written to the Power Reduction ADC bit in the Power Reduction Register
(PRR.PRADC) before the ADC can be used.
6. ADC result is stored in ADCH and ADCL. By default, the result is presented right adjusted.
ADCL must be read first, then ADCH, to ensure that the content of the Data Registers belongs to
the same conversion. The ADC has its own interrupt which can be triggered when a conversion
completes.

7. A single conversion is started by writing a '1' to the ADC Start Conversion bit in the ADC
Control and Status Register A (ADCSRA.ADSC). ADCS will stay high as long as the
conversion is in progress, and will be cleared by hardware when the conversion is completed. If a
different data channel is selected while a conversion is in progress, the ADC will finish the
current conversion before performing the channel change.
8. Auto Triggering is enabled by setting the ADC Auto Trigger Enable bit (ADCSRA.ADATE).
The trigger source is selected by setting the ADC Trigger Select bits in the ADC Control and
Status Register B (ADCSRB.ADTS).
7.3.1 ADC Conversion Result
After the conversion is complete (ADCSRA.ADIF is set), the conversion result can be found in
the ADC Result Registers (ADCL, ADCH). For single ended conversion, the result is:

Where VIN is the voltage on the selected input pin, and VREF the selected voltage reference
7.4 Modes of operation:
The ADC has two fundamental operation modes:
1. Single conversion mode
2. Free running mode

7.4.1 Single Conversion Mode:


In Single Conversion mode, you must initiate each conversion. When it is done, the result is
placed in the ADC Data register pair and no new conversion is started.
7.4.2 Free Running Mode:
It starts the conversion only once, and then, the ADC automatically will start the following
conversion as soon as the previous one is finished. Its conversion is continuous. Once initialized
it takes 13 ADC cycles for single conversion. In this mode ADC data register must be read
before new value is written.
7.5 LM35:
The LM35 is an integrated circuit sensor that can be used to measure temperature with an
electrical output proportional to the temperature (in degree Celsius).

PRE-LAB TASK:
EXPLAIN THE WORKING OF FIVE ANALOG SENSORS. EXPLAIN WHAT
PHYSICAL QUANTITY DO THEY MEASURE AND PROVIDE THE MAPPING
FUNCTION FOR THE OUTPUT VOLTAGE.
ANALOG SENSORS:

There are different types of sensors that produce continuous analog output signal and these
sensors are considered as analog sensors. This continuous output signal produced by the analog
sensors is proportional to the measure-and.

ACCELEROMETERS:
Analog sensors that detect changes in position, velocity, orientation, shock, vibration, and tilt by
sensing motion are called as accelerometers. These analog accelerometers are again classified
into different types based on the variety of configurations and sensitivities.
These accelerometers are available as analog and digital sensors, based on the output signal.
Analog accelerometer produces a constant variable voltage based on the amount of acceleration
applied to the accelerometer.

LIGHT SENSORS:
Analog sensors that are used for detecting the amount of light striking the sensors are called as
light sensors. These analog light sensors are again classified into various types such as photo-
resistor, Cadmium Sulfide (CdS), and, photocell. Light dependent resistor (LDR) can be used
as analog light sensor which can be used to switch on and off loads automatically based on
the day light incident on the LDR. The resistance of the LDR increases with decrease in light and
decreases with increase in light.

SOUND SENSORS:
Analog sensors that are used to sense sound level are called as sound sensors. These analog
sound sensors translate the amplitude of the acoustic volume of the sound into an electrical
voltage for sensing sound level. This process requires some circuitry, and
utilizes microcontroller along with a microphone for creating an analog output signal.
PRESSURE SENSOR:
The analog sensors that are used to measure the amount of pressure applied to a sensor are called
as analog pressure sensors. Pressure sensor will produce an analog output signal that is
proportional to the amount of applied pressure. These pressure sensors are used for different
types of applications such as piezoelectric plates or piezoelectric sensors that are used for the
generation of electric charge. These piezoelectric sensors are one type of pressure sensors that
can produce an analog output voltage signal proportional to the pressure applied to the
piezoelectric sensor.
ANALOG TEMPERATURE SENSOR:

Temperature sensors are widely available as both digital and analog sensors. Typically used
analog temperature sensors are thermistors. There are different types of thermistors that are used
for different applications. Thermistor is a thermally sensitive resistor that is used for detecting
changes in temperature. If the temperature increases, then the electrical resistance
of thermistor increases. Similarly, if temperature decreases, then the resistance decreases. It is
used in various temperature sensor applications.
Practical example of analog temperature sensor is thermistor based temperature control system.
This project is used to maintain constant temperature in an enclosed area. A block diagram of
temperature control system consists of lamp (that represents a cooler), temperature sensor
or thermistor, relay.
If the temperature exceeds certain value, then the lamp switches on automatically indicating a
cooler for bringing back temperature to normal value. Operational along with a negative
temperature coefficient thermistor is used for activating relay-in case if the temperature exceeds
certain range. As this process of activating relay for switching on cooler (in this system shown as
lamp) can be done automatically, hence there is no need to monitor temperature in
person. Thermistor is most frequently used analog temperature sensor, because of its low cost
advantage. Whenever changes occur in temperature, then the input parameters to the op-amp will
be altered. Thus, op-amp delivers an output that energizes relay and the load turns on or off
accordingly.
If we use a digital temperature sensor instead of analog temperature sensor, then the accuracy of
temperature control system can be improved.

MAPPING FUNCTION FOR THE OUTPUT VOLTAGE:

LM35 Transfer Function


The accuracy specifications of the LM35 are given with respect to a simple linear transfer
function:
VOUT = 10 mv/°C × T

Where;
VOUT is the LM35 output voltage
T is the temperature in °C

IN-LAB TASKS:

TASK-1: USE LM35 TO SENSE THE ROOM TEMPERATURE. CONVERT THIS DATA INTO
DIGITAL USING ATMEGA328P ADC AND DISPLAY TEMPERATURE VALUE ON VIRTUAL
TERMINAL.
COMPLETE THE CODE AND SIMULATE ON PROTEUS.

CODE:
#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>

#define F_CPU 16000000UL

#include <util/delay.h>
#include <string.h>
#include <math.h>

/****************** Definitions for UART *********************/


#include "debug_prints.c"
#define BAUD0 9600 // Baud Rate for UART
#define MYUBRR (F_CPU/8/BAUD0-1)

/*********************************************************************
************/
#define ADC_#include <inttypes.h>
#define ADC_CHANNEL0 0
#define ADC_CHANNEL1 1
#define ADC_CHANNEL2 2

#define ADC_VREF 5 // Ref voltage for ADC is 5 Volts


#define ADC_RES 10 // Resoulution of ADC in bits
#define ADC_QLEVELS 1024 // Quantization levels for the ADC

unsigned char ADC_Initialize();


unsigned int ADC_Read(unsigned char channel); // Reads the result of a single conversion from the
ADC

float ADC_Convert(unsigned int);


unsigned char VinToTemp(float Vin);
unsigned char read_temp_sensor(unsigned char ADC_channel);

#define TEMP_SENSOR_CHANNEL ADC_CHANNEL0


/*****************************************************************/

int main(void)
{
ADC_Initialize();
DIDR0=0xFF; //Disable digital I/O
DDRD = 0xFF;
UART0_init(MYUBRR);
printSerialStrln("Lab 8: ");
unsigned char temprature;

while(1)
{ printSerialStr("Temperature is ");
temprature = read_temp_sensor(TEMP_SENSOR_CHANNEL);
printSerialInt(temprature);
printSerialStr("\r \n");

PORTD = temprature;
_delay_us(1000000);
}

}
// Function Initializes the ADC for 10-Bit Single Conversion mode..
unsigned char ADC_Initialize()

{ //left adjust
ADMUX |=(1<<ADLAR);
// right adjust result. Vref = AVCC = 5V
/*** Write Code for this ***/
ADMUX|= (1<<REFS0); //Here select the prescaler for 16 MHz System Clock
ADCSRA |=(1<<ADPS1);
ADCSRA |=(1<<ADPS2); // Prescaler = 128
ADCSRA |=(1<<ADEN);// Clock for ADC = 125 KHz (should be between 50K to 200K)

return 0;
}
/* Function reads the result of a single conversion from the ADC
channel given as an argument*/
unsigned int ADC_Read(unsigned char channel)
{
unsigned char ADC_lo;
unsigned char ADC_hi;
unsigned int result;
ADMUX &= ~(0x07); // clear previous selction of channel
ADMUX |= channel; // Select the new channel
// Delay needed for the stabilization of the ADC input voltage
_delay_us(10);
//wait for ADC to finish any ongoing opeartion
while((ADCSRA & (1<<ADSC)) != 0);
ADCSRA |= (1 << ADSC); //start conversion
while((ADCSRA & (1<<ADIF)) == 0);
ADCSRA |= (1<<ADIF); // clear the flag by writing 1 to it
//result = (ADCH<<8)|(ADCL & 0xC0); // Left adjust result
ADC_lo = ADCL;
ADC_hi = ADCH;
result = (ADC_hi<<2)|(ADC_lo >> 6); // Right adjust result
return result;
}

/* This function takes an uint16 as input from the ADC.


This uint16 is an unsgined integer result of the ADC
encoded result. The function then converts this result
to floating point Voltage using the ADC_RES (resolution)
and ADC_REF (reference voltage) defined earlier*/
float ADC_Convert(unsigned int ADC_value)
{
float Vin;

Vin = ADC_VREF * ((float)ADC_value/ADC_QLEVELS);


return Vin;
}
/** This function takes the floating point Voltage value as input and
converts it to
corresponding Temprature in Celsius for an LM35 in Basic
Configuration.
The function returns an 8-Bit integer value of the Temprature */

unsigned char VinToTemp(float Vin)


{
unsigned char temprature=0;

float VoltsPerCentigrade = 0.01; // 10 mv per centigrade

temprature = (unsigned char) floor(Vin/VoltsPerCentigrade);

return temprature; // Temprature value returned


}
/** This function reads the Value of Temprature Sensor (LM35) from
pre-defined ADC Channel and returns the result in Degree Celsius
as an 8-Bit unsigned int */

unsigned char read_temp_sensor(unsigned char ADC_channel)


{
unsigned int ADC_value = ADC_Read(ADC_channel); // Read the sensor Connected at ADC_channel
float Vin = ADC_Convert(ADC_value); // Get the value in floating point

unsigned char temp_celsius = VinToTemp (Vin); // Convert to temprature and return

return temp_celsius;
}

SERIAL MONITOR OF ARDUINO

PROTUES SIMULATION
HARWARE IMPLEMENTATION

TASK-2: INTERFACE ANY ANALOG SENSOR ASSIGNED BY YOUR LAB


INSTRUCTOR, FIND MAPPING FUNCTION FOR ITS OUTPUT AND IMPLEMENT
ADC FOR THE SENSOR USING ATMEGA328P.

CODE:
#include <avr\io.h>
#include <avr\interrupt.h>
#include <avr\signal.h>
#include <util\delay.h>
void conversion(void);
int main(void) {
DDRC=0xFF;
ADMUX=0x60;
ADCSRA=0x83;
while(1) {
conversion();}}
void conversion()
{
ADCSRA|=1<<6;
_delay_ms(0.1);
PORTC=ADCH;
}

PROTUES SIMULTATION

CRITICAL ANALYSIS / CONCLUSION


The objectives of this lab were to build an Analog to Digital convertor of the Atmega328p
Arduino Nano board. The LM35 temperature sensor was used for ADC conversion. Not every
pin on a can perform analog to digital conversions on the Arduino board. The pins having an ‘A’
in front of their label (A0 through A5) indicate that these pins can read analog voltages. The
ADC in Atmega328p converts an analog input voltage to a 10-bit digital value through
successive approximation. The minimum value represents GND and the maximum value
represents the voltage on the AREF pin minus 1 LSB.
ADC has two fundamental operation modes:
i. Single conversion mode:
This is the simplest ADC mode. In single conversion mode we need to initiate
each conversion, after conversion result is shared in ADC data register pair.
ii. Free running mode:
In free running mode we need to initiate conversion only once then ADC
automatically starts following conversion. This mode saves additional CPU
load and heavy software development.

You might also like