Lab 03
Lab 03
CODE:
#include <avr/io.h>
int main()
{
DDRD = 0xFF;
DDRC = 0x00;
ADCSRA = 0x87;
ADMUX = 0x60;
while(1)
{
ADCSRA |= (1 << ADSC);
while(ADCSRA & (1 << ADIF) == 0);
PORTD = ADCH;
}
return 0;
}
TINKERCAD SIMULATION:
• INPUT = 1V:
Shayan Sheikh EE-21246
• INPUT = 2V:
• INPUT = 3V:
• INPUT = 4V:
Shayan Sheikh EE-21246
• INPUT = 5V:
VERIFICATION:
We can verify the proper functioning of the ADC by first converting the binary output into
decimal form and then calculating the percentage of the number, which therein will eventually be
multiplied by 5 (maximum voltage the potentiometer can allow) to give the analog voltage.
• For 1V Input:
𝐵𝑖𝑛𝑎𝑟𝑦 𝑂𝑢𝑡𝑝𝑢𝑡 = 00110011
𝐷𝑒𝑐𝑖𝑚𝑎𝑙 𝑂𝑢𝑡𝑝𝑢𝑡 = 51
𝐶𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑖𝑛𝑔 𝑡ℎ𝑒 𝑃𝑒𝑟𝑐𝑒𝑛𝑡𝑎𝑔𝑒:
51
∗ 5 = 1𝑉
255
• For 2V Input:
𝐵𝑖𝑛𝑎𝑟𝑦 𝑂𝑢𝑡𝑝𝑢𝑡 = 01100110
𝐷𝑒𝑐𝑖𝑚𝑎𝑙 𝑂𝑢𝑡𝑝𝑢𝑡 = 102
𝐶𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑖𝑛𝑔 𝑡ℎ𝑒 𝑃𝑒𝑟𝑐𝑒𝑛𝑡𝑎𝑔𝑒:
102
∗ 5 = 2𝑉
255
• For 3V Input:
𝐵𝑖𝑛𝑎𝑟𝑦 𝑂𝑢𝑡𝑝𝑢𝑡 = 10011001
𝐷𝑒𝑐𝑖𝑚𝑎𝑙 𝑂𝑢𝑡𝑝𝑢𝑡 = 153
𝐶𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑖𝑛𝑔 𝑡ℎ𝑒 𝑃𝑒𝑟𝑐𝑒𝑛𝑡𝑎𝑔𝑒:
Shayan Sheikh EE-21246
153
∗ 5 = 3𝑉
255
• For 4V Input:
𝐵𝑖𝑛𝑎𝑟𝑦 𝑂𝑢𝑡𝑝𝑢𝑡 = 11001100
𝐷𝑒𝑐𝑖𝑚𝑎𝑙 𝑂𝑢𝑡𝑝𝑢𝑡 = 204
𝐶𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑖𝑛𝑔 𝑡ℎ𝑒 𝑃𝑒𝑟𝑐𝑒𝑛𝑡𝑎𝑔𝑒:
204
∗ 5 = 4𝑉
255
• For 5V Input:
𝐵𝑖𝑛𝑎𝑟𝑦 𝑂𝑢𝑡𝑝𝑢𝑡 = 11111111
𝐷𝑒𝑐𝑖𝑚𝑎𝑙 𝑂𝑢𝑡𝑝𝑢𝑡 = 255
𝐶𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑖𝑛𝑔 𝑡ℎ𝑒 𝑃𝑒𝑟𝑐𝑒𝑛𝑡𝑎𝑔𝑒:
255
∗ 5 = 5𝑉
255
2. Now, remove the LEDs and provide the 8-bit digital output to an external DAC circuit i.e., convert
the digital output to analog for verification. You can use a simple R-2R circuit of Figure 8 or use
DAC0808 IC. For DAC0808 IC, refer to its datasheet for more information.
TINKERCAD SIMULATION:
• INPUT = 1V:
Shayan Sheikh EE-21246
• INPUT = 2V:
• INPUT = 3V:
Shayan Sheikh EE-21246
• INPUT = 4V:
• INPUT = 5V:
3. Measure the analog input given through potentiometer and the analog output reproduced
by the DAC. Compare both and verify the ADC and DAC.
• INPUT = 1V:
Analog Input = 1V
Analog Output = 1.02V
• INPUT = 2V:
Analog Input = 2V
Analog Output = 1.97V
Shayan Sheikh EE-21246
• INPUT = 3V:
Analog Input = 3V
Analog Output = 2.84V
• INPUT = 4V:
Analog Input = 4V
Analog Output = 3.77V
• INPUT = 5V:
Analog Input = 5V
Analog Output = 4.87V
Task: 02: To control the status of an LED based on the value of input analog
voltage.
Modify the previous task or, example to read 10-bit ADC value of voltage across
potentiometer instead of 8-bit result. Map the obtained 10-bit result with voltage
level using the step-size. Connect an LED to indicate the voltage level. Use some
conditions to build the following logic for controlling the LED status.
- If voltage is above 2.5V, the LED turns ON.
- If voltage is below 2.5V, the LED red turns OFF.
CODE:
#include <avr/io.h>
int main(void) {
DDRD |= 0x01;
DDRC &= 0xFE;
ADCSRA |= 10000111;
ADMUX |= (1 << REFS0);
int digital_output;
float voltage;
while (1) {
ADCSRA |= (1 << ADSC);
return 0;
}
TINKERCAD SIMULATION:
• INPUT VOLTAGE = 1.6V, LED DOES NOT GLOW
LINKS:
• https://www.tinkercad.com/things/kbhtg1qD3Ba-
lab3task1?sharecode=aepvMZmLBtY-gLxHaAm8DY7G5YVlSRAT5TuN6waSOA0
(Task: 01)
• https://www.tinkercad.com/things/g1G5TmT8n4k-
lab3task2?sharecode=iDZ1Gr0B9nHHfwSWwHEdiV6jWCTZ1x-ecGe25LMXs4Q
(Task: 02)