Reading analog sensors with the Raspberry Pi

Adafruit just posted an awesome tutorial on reading analog sensors with the Raspberry Pi. It’s a great walkthrough that can be applied to your next Raspi project as well as any project where you just need one more analog input.

Earlier, the folks over at Adafruit posted a tutorial on using a MCP3008 ADC with the Raspi to directly read analog values using a Raspi. Sometimes, though, you don’t need eight analog inputs and a 12-bit ADC to get a project off the ground. Adafruit’s tutorial for reading analog values without an ADC relies on a single 1μF ceramic capacitor attached between a digital input and ground. By pulling the sensor line high for a millisecond or two, the capacitor charges at different rates depending on the value of the analog sensor.

Yes, it’s just an RC timing circuit but seeing as how the Raspi doesn’t have an analog input, we figure this tutorial could help out a few people.


  1. Hack Man says:

    it’s just an RC timing circuit

    Then why title it Reading analog sensors with the Raspberry Pi?

  2. truthspew says:

    Someday I’ll get my Raspi – someday! I ordered it, paid for it. Some day they will ship it!

  3. SuperNurd says:

    Just go with the ADC, much easier to code around!

  4. M says:

    “By pulling the sensor line high for a millisecond or two, the capacitor charges at different rates depending on the value of the analog sensor.”

    Aren’t you supposed to pull the sensor line LOW–to drain the capacitor, then reading the charge time?

  5. ben says:

    Isn’t it how older joystick ports worked on older PCs ?

    • NewCommentor1283 says:

      yes, thats what i was going to say.

      analog joystick ports on desktop PC’s before USB f***** everything up was just a simple RC circuit and thats why often (VERY VERY OLD) fast computers needed to have the CPU slowed down in order to work with VERY old software that used the joystick…

      (and expected the CPU to slow down its attempts to read the joy,[EDIT: the entire software?] later on, CPUs were so fast there was no more assumed delay)

      and hence the turbo button.

      which should have been called “slowdown” and had the logic inverted for the LED ect…

      • NewCommentor1283 says:

        and because component values have a stronger effect on RC cuircits, explains why calibration every day is important… room temperature 25c -OR- 45c has big difference

      • Bill Stewart says:

        The other reason for slowing down the CPU when playing games (e.g. slowing a 286 down to 4.77 MHz) was because the games often had assumptions about how fast they were, so if the CPU was too fast the Space Invaders would come down and kick your ass before you could shoot back at most of them.

  6. problemchild68 says:

    Yes it is ant it’s a PITA!
    Better off with one of those I2C ADCs that Microchip are doing at around a dollar less CPU better accuracy and speed repeatability etc etc

  7. jc says:

    Bitbanging SPI in userspace in python again, are we? For gods sake that ADC has a kernel driver already, all you need to do is write a tiny platform code glue to make it ‘mysteriously appear’.

  8. paranoid says:

    formerly known as the Apple II paddle controller circuit..

  9. Ian says:

    Might be good to at least add a comparator circuit to make the threshold consistent and avoid scary metastable regions in the digital inputs, but I guess it will probably work okay either way.

  10. i love that shit. In the past i used the arduino…now i am happy :-)

Leave a Reply

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

You are commenting using your 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


Get every new post delivered to your Inbox.

Join 96,711 other followers