0% found this document useful (0 votes)
5 views

Lab 12 ADC

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab 12 ADC

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

International Islamic University Islamabad

Faculty of Engineering and Technology


Department of Electrical Engineering

MICROPROCESSORS AND MICROCONTROLLER LAB

Lab 12 : ADC Programming with Arduino

Name:

Reg. No:

Date of
Experiment:

OBE Rubrics Evaluation

a) PSYCHOMOTOR (To be judged in the field/lab during experiment)


Sr. Level 1 Level 2 Level 3 Level 4 Level 5 Marks
Criteria
No. (0%) (25%) (50%) (75%) (100%) Obtained
0 1.25 2.5 3.75 5
Practical
With several With few With some Without
Implementation/
1 critical errors, errors, errors, errors,
Arrangement of Absent
incomplete incomplete complete complete
Equipment
and not neat and not neat but not neat and neat
Use of 0 0.5 1 1.5 2
Equipment or
2 Limited Some Considerable
Simulation/ Absent Competence
competence competence competence
Programming Tool

(b) COGNITIVE (To be judged on the copy of experiment submitted)


Sr. Level 1 Level 2 Level 3 Level 4 Level 5 Marks
Criteria
No. (0%) (25%) (50%) (75%) (100%) Obtained
Algorithm Design 0 0.25 0.5 0.75 1
or Data Record, Complete
3 Complete with Complete
Analysis and Absent Incorrect with few
some errors and Accurate
Evaluation errors

(c) AFFECTIVE (To be judged in the field/lab during experiment)


Sr. Level 1 Level 2 Level 3 Level 4 Level 5 Marks
Criteria
No. (0%) (25%) (50%) (75%) (100%) Obtained
Level of 0 0.5 1 1.5 2
Participation &
Good Encouraging
4 Attitude to Achieve Rare sensible Some sensible
Absent sensible sensible
Individual/Group interaction interaction
interaction interaction
Goals

5 Total Marks Obtained (Out of 10):

Lab No. 12: ADC Programming with Arduino Page 76


Objectives:
 To program and use the ADC feature of ATmega328
 Show 10 bit value of ADC on Serial Port.
Introduction:
ADC is used to convert the analog voltages into digital value. ADC is widely used in
data acquisition so most of the modern microcontrollers have on-chip ADC peripheral.
Arduino UNO has on-chip ADC of 10-bit resolution. It has 6 analog input channels. As
the ADC is 10-bit, so the converted digital output is stored in two 8-bit registers ADCL and
ADCH. Reference voltages for ADC can be connected to AVCC (Analog Vcc), internal
1.1V reference or external AREF pin. Minimum 0V and maximum Vcc can be converted
to a digital value. In ADC, Successive approximation method is used to convert analog
voltage into digital value. This circuitry requires a clock frequency between 50 kHz to 100
kHz.
Important Registers Associated with ADC:
Following registers are associated with the ADC of AVR:
ADCL Has 8 LSBs of converted digital result
ADCH Has 2 MSBs of converted digital result
ADMUX For left / right adjusted result, reference voltage and channel
selection
ADCSRA ADC control and status register

Single ended result can be found from following formula:


𝑉𝑖𝑛 × 1024
𝐴𝐷𝐶 =
𝑉𝑟𝑒𝑓
Where Vin is the voltage on the selected input channel, Vref the selected voltage
reference and ADC is the 10-bit converted digital decimal value.

ADMUX Register:
Bit # 7 6 5 4 3 2 1 0
Bit Name REFS1 REFS0 ADLAR MUX4 MUX3 MUX2 MUX1 MUX0

REF1 REF1 Voltage Reference Selection


0 0 AREF Pin Set Externally
0 1 AVCC Pin Same as VCC
1 0 (Reserved)
1 1 Internal 1.1V Fixed Regardless of VCC value

ADLAR = 0  Right Adjust the Result


ADCH ADCL
0 0 0 0 0 0 ADC9 ADC8 ADC7 ADC6 ADC5 ADC4 ADC3 ADC2 ADC1 ADC0

ADLAR = 1  Left Adjust the Result


ADCH ADCL
ADC9 ADC8 ADC7 ADC6 ADC5 ADC4 ADC3 ADC2 ADC1 ADC0 0 0 0 0 0 0

Lab No. 12: ADC Programming with Arduino Page 77


Analog Channel Selection Bits

MUX3… Input MUX3…0 Input


0
0000 ADC0 1000 ADC8(1)
0001 ADC1 1001 (Reserved)
0010 ADC2 1010 (Reserved)
0011 ADC3 1011 (Reserved)
0100 ADC4 1100 (Reserved)
0101 ADC5 1101 (Reserved)
0110 ADC6 1110 1.1 V
(VBG)
0111 ADC7 1111 0 V (GND)

Note: 1. For Temperature Sensor.


ADCSRA Register:

Bit # 7 6 5 4 3 2 1 0
Bit Name ADEN ADSC ADATE ADIF ADIE ADPS2 ADPS1 ADPS0

ADEN (Bit 7) ADC Enable:


1 = ADC is enabled 0 = ADC is disabled
ADSC (Bit 6) ADC Start Conversion:
Write this bit to 1 to start each conversion.
ADATE (Bit 5) ADC Auto Trigger Enable:
Auto Triggering of the ADC is enabled when this bit is set to 1.
ADIF (Bit 4) ADC Interrupt Flag: This bit is set when an ADC conversion
completes and the Data Registers are updated.
ADIE (Bit 3) ADC Interrupt Enable: Writing this bit to 1 enables the ADC
Conversion Complete Interrupt.
ADPS2:0 (Bits 2:0) ADC Prescaler Select Bits: These bits determine the
division factor between the XTAL frequency and the input clock to
the ADC

ADPS2 ADPS1 ADPS0 Division Factor


0 0 0 2
0 0 1 2
0 1 0 4
0 1 1 8
1 0 0 16

Lab No. 12: ADC Programming with Arduino Page 78


1 0 1 32
1 1 0 64
1 1 1 128

Schematic:

Sketch:

#define STEP_SIZE 5/1024


int ADC_Read(byte An) {
DDRC = 0x00; // make Port C an input for ADC input
ADCSRA = 0x87; // Enable ADC and select CLK/128
ADMUX = 0x40 | An; // 5V Vref, Select ADCn, right-justified
ADCSRA|=(1<<ADSC); // start conversion
while(( ADCSRA & (1<< ADIF ))==0); // wait for conversion to finish
ADCSRA |= (1<<ADIF); // Clear ADIF Flag
return ADC; // return ADC Value
}

void setup() {
Serial.begin(9600); // use 9600 bits per second
}

void loop() {
int A0 = ADC_Read(0); // Read Channel 0
Serial.print("ADC = ");
Serial.print(A0); // Send ADC Value Serially
Serial.print(" Vin = ");
Serial.println(float(ADC) * STEP_SIZE); // Show input Volts
delay(1000);
}

Lab No. 12: ADC Programming with Arduino Page 79


Lab Task:
An LM35 temperature sensor is connected to ADC A0 Pin. Write a Program to read
analog value of LM35 convert it to Centigrade and Send it to serial port. Use 1.1V Vref
and CLK/128 Prescalar.

Lab No. 12: ADC Programming with Arduino Page 80


Lab 12 Task Solution:

Lab No. 12: ADC Programming with Arduino Page 81

You might also like