# 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 charges to 2.5V in about 720us. So this is the worst case of conversion if Vin is equal 2.5V.

There are techniques of removing the nonlinearity of RC charging. One of them is Transistor current source usage. In this circuit PNP transistor provides current about 80uA. Time to charge capacitor can be calculated as follows: T=(C*5)/I=6.25ms. So iv Vin would be 5V, then conversion time = 6.25ms.

So this takes some conversion time, but for processes like temperature monitoring is suitable.

Note that 200 Ohm resistor is used to limit capacitor discharging current to protect port from damage.

This site uses Akismet to reduce spam. Learn how your comment data is processed.