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.

Comments

  1. onebiozz says:

    basically recreating what’s found inside of an ADC chip!

    • cr0sh says:

      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.

      • Jan says:

        Actually the controllers on a PC used to use a 555 chip as a monostable flip-flop instead. But the idea is similar.

      • Tony says:

        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!

    • tekkieneet says:

      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)

      • Jomegat says:

        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.

      • arachnidster says:

        I’d love to see an article exploring other discrete ADC architectures!

        • tekkieneet says:

          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).

  2. Hello, can GPIO4 take 5V as an input?

    • Matouš says:

      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.

    • Lennart says:

      the 311 has some form of open collector output. There is a 10k pullup to 3.3v.

  3. bigbob says:

    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…

    • F says:

      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!!

      • bigbob says:

        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…

  4. iwasdbear says:

    I’m sure I have a Byte magazine from about 1980 with this circuit in it. I don’t think [Hussam] “figured out a way to do just this”

  5. chris says:

    Regarding the ciruit I think there are much simplier possibilities like the ones used by the parallax propeller:

    http://www.parallax.com/downloads/an008-sigma-delta-analog-digital-conversion

  6. ka1axy says:

    Protip: When drawing a schematic, no matter what your art director says, blue lines on black is never a good choice.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 93,791 other followers