ADC For Raspi Without Using An ADC

Schematic of ACD for a raspi

With all the amazing and wonderful things a Raspberry Pi can do, it is sorely lacking a dedicated ADC chip. Sure, you can wire up an ADC via SPI or even I2C with a little work, but still. It would be nice to have access to an Analog to Digital converter without having to go through the trouble. Fortunately, [Hussam] has figured out a way to do just this.

Using a comparator, two resistors, a single capacitor and a few lines of code, [Hussam] managed to get an active ADC working on his Raspberry Pi. He’s using the PWM1 and a passive RC filter to make a DAC. He then uses the comparator along with a ‘ successive approximation algorithm’ to complete the ADC.

[Hussam] mentions that the hack is not new, and this technique has been used before for microcrotrollers that lack a built-in ADC. But we are still impressed with his attention to detail in describing how to do this on a Raspi. Be sure to check out the link for full details, code, and an awesome description on how his algorithm works.

22 thoughts on “ADC For Raspi Without Using An ADC

    1. Yep, pretty much. I’m also pretty certain this is how the analog game controllers on the PC were done, as well as how it was done for the TRS-80 Color Computer joystick controllers.

          1. I stand corrected!

            PCs did use 555’s (well, the quad 558); I always though they used much simpler circuits to keep costs down. Others like Apple, Atari & Commodore did the same.

      1. Nah, they were even simpler.

        They were just charging a capacitor, and timing how long it took to discharge.

        The capacitor & pot inside the joystick were part of a one-shot multivibrator circuit, the rate it ran at was dependent on the pot value. So you’d reset the circuit, time how long it took for the pulse to happen, and that’s your position.

        Not the best idea, but it was cheap!

    2. Actually much worse than any ADC chips that you can buy. Most of the modern *low speed* ADC chips are SAR (mostly switched cap now) , sigma-delta, dual slope and all of them works better than this.

      – low conversion speed as you have to be a few time constants of the PWM filtering RC (1K, 10nF) for the filter output to settle down.
      – low resolution – dependent on how finely you can modify the PWM duty cycle and still have high enough PWM frequency so that the RC filter can provide a smooth DC value for the comparator. This last part is important.

      Now this could have been much improved upon in sigma-delta or single/dual slope around same level of hardware complexity. (actually less parts)

      1. One way to address the low PWM frequency is by scrambling the PWM. For instance, if you have a 128-sample PWM cycle and you want a 25% duty cycle, there is no reason you have to make the first 32 samples high and the remaining 96 low. As long as any 32 of the 128 are high, you will hit the 25% duty cycle. An easy way to accomplish that is by bit-reversing the count with a lookup table. I’ve done this, and it jacks up by a considerable amount the frequency the RC filter has to handle.

        1. A lot of the low end microcontrollers doesn’t have ADC, but they come with an analog comparator that can be used along with firmware for Analog to Digital conversion. Since the RPi doesn’t have one, you can use the external comparator.

          Microchip AN700 “Make a Delta-Sigma Converter Using a Microcontroller’s Analog Comparator Module”.
          http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en011642

          If you are crazy/lazy/desperate enough, a digital I/O pin with schmitt trigger type of input can be used instead of the comparator. Unfortunately, regular digital I/O pin without schmitt triggers doesn’t like the area between VIL & VIH which cause cross conduction which is not too good for the input circuits in the chip. I don’t know if the RPi inputs have schmitt triggers.

          Also see Microchip AN863 “A Comparator Based Slope ADC”, TI’s SLAA129 ” Implementing an Ultra-low-power Thermostat with Slope A/D Conversion”
          This one deals with essentially (thermistors) resistance.measurement, but the same idea can be used to sample external voltage (require some thinking which is lacking these days).

    1. I doubt it, but from the linked article:

      The Analog Comparator that I opted to use is the LM311. The main reason for choosing it is because I had one lying around. One minor issue with this comparator is that it needs a 5V supply on its VCC pin. Luckily the LM311 has an open-collector output and hence we can still set the output level to 3.3v with a 10K pull-up resistor.

  1. How is this less trouble than adding an ADC? You’re still adding external circuitry, and it’s not even going to work that well (may be acceptable based on individual requirements)…

    I don’t doubt the usefulness of this circuit, but don’t try to sell it as something it’s not…

    1. Maybe not less trouble, but LM311 is less than twenty cents in any kind of quantity. This could be the best bang for your buck.

      “don’t try to sell it as something it’s not” What is it not? It’s an ADC! Maybe it’s not a very good ADC, but so what? Even a pure digital input is actually a 1-bit ADC.

      The straw-man takes another beating in the hackaday forums! He just doesn’t stand a chance!!

      1. My complaint is with hackadays branding of this as simpler than adding an SPI or I2C ADC, which it is not. I take this directly from the statement “Sure, you can wire up an ADC via SPI or even I2C with a little work, but still. It would be nice to have access to an Analog to Digital converter without having to go through the trouble.” This statement implies that the addition of this circuit is simpler than the addition of an SPI or I2C ADC.

        As far as $$ goes, I agree that this may be cheaper.

        You should improve your reading comprehension and context analysis skills before trying to rip apart others comments…

Leave a Reply to TonyCancel reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

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