Interfacing Analog Sensors using Built-in ADC

 


COMSATS UNIVERSITY ISLAMABAD

 

MICROPROCESSOR SYSTEMS AND INTERFACING

                        LAB REPORT 7

SUBMITTED TO:

SIR KHIYAM IFTIKHAR

SUBMITTED BY:

JUNAID AHMAD

IBRAR AHMAD

JARRAR MALIK

REGISTRATION NO:

CIIT/FA19-BEE-089/ISB

CIIT/FA19-BEE-083/ISB

CIIT/FA19-BEE-087/ISB

 

DATE:

09-11-2021

Interfacing Analog Sensors using Built-in ADC

 

Objectives:

              Understand the function of ADC in microcontroller.

              Learn different mode of operation of ADC

              Interfacing of LM35 temperature sensor with Atmega328p.

Software Tools:

              Microchip Studio/AVR Studio

              Arduino

              Proteus ISIS

              AVR DUDESS

 

Hardware Tools:

 

Name

Value

Quantity

Arduino Nano

-

1

Breadboard

-

1

LM35

-

1

Table 7.1: List of Components

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.

 

 

 

PRE-LAB TASK:

 

Investigate and describe 5 analogue sensors that are commonly used in embedded systems. Explain what physical quantity they measure and provide the mapping function for the output voltage individually. Please note, all 5 sensors should sense different physical phenomenon.

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.

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.

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.

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.

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. 

 

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.

AVR CODE:

#include <inttypes.h>

#include <stdlib.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

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

#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)

 

 

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;

            }

}

// Function Initializes the ADC for 10-Bit Single Conversion mode..

unsigned char ADC_Initialize()

{

            ADMUX|=((1<<REFS0)|(1<<ADLAR));

            ADCSRA|=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);

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)

{

            return (float)(ADC_value)*(float)5/1024;

}

unsigned char VinToTemp(float Vin)

{

            unsigned char temp;

            temp=Vin/0.01;

            return temp;

}

 

 

 

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;

 

 

}

 

 

 

AVR SIMULATION:


PROTEUS -SIMULATION:


HARD-WARE:

 


Critical Analysis / Conclusion:

 

In this lab experiment we learnt to build an Analog to Digital convertor of the Atmega328p Arduino uno board. We also used LM35 temperature sensor for ADC conversion. 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.

 

 

إرسال تعليق

Post a Comment (0)

أحدث أقدم