ADC on AVR using on chip comparator

Not all AVR microcontrollers come with built in ADC. But there is a way of building one using on chip comparator and timer counter. The comparator compares voltages on +v (Ain0) and -v (Ain1) inputs. If voltage in Ain0 pin is greater than in Ain1 then comparator ACO is set to ‘1’ otherwise it is ‘0’. Bellow you see AVR comparator circuit. The working of this ADC is as follows. First PB0 is set to ‘0’ in order to discharge capacitor. Then PB0 is programmed as input with no pull-ups and Timer is started to count. The capacitor starts charging to 5V through 10k resistor. When voltage on capacitor becomes greater than in input Vin, the comparator state switches to ‘1’. Program detects this and stops the timer counter and accumulated count is proportional to the input voltage Vin. So if Vin is greater, then timer counts more, and if Vin is less then timer counts less. Just one thing! The voltage on capacitor rises not linearly but exponentially. But if we restrict the input to the range of like 2.5V then this voltage raise can be considered as linear. In a example RC time constant is 1ms and capacitor…

Continue reading