machine quotidienne


Low-cost ADC using only Digital I/O

Start here Rules Tips Robots Blogs Challenges Forums Where to buy stuff Recent posts The Pulse About
Low-cost ADC using only Digital I/O

By TeleFox
@ Wed, 2009-12-23 23:56
Add a cheap ADC converter to your next microcontroller project using a few basic parts.

Ever find yourself in need of some extra ADC capabilities? Maybe you have a micro with no ADC built in, maybe you’ve used up all your ADC pins already, or maybe the integrated ADC doesn’t provide a high enough resolution. This is where a custom-built Capacitor ADC can become very useful.
Now, there are ADC ICs that you can buy which will handle all the ADC stuff externally and just feed the digital results to your micro – there isn’t anything wrong with these. If you can afford the cost and the PCB space then you can save yourself some time and effort by grabbing one of these. In my case however I had a small micro with no ADC, and if I added a big ADC IC to my circuit then what would be the point of having a small micro in the first place?

BTW, although I developed this particular method myself, I’m certainly not the first to use RC timing for ADC applications =)
If you are interested in some slightly more advanced stuff, I strongly recommend you check out the Microchip Tips n Tricks for 8-pin micros, which has many useful techniques that are applicable to small micros. As usual, Wikipedia also has a lot of good info on various ADC types.

So here’s the most basic example – 2 digital I/O pins, one resistive sensor, and one capacitor. I know 2 pins for one sensor isn’t very good in terms of pin efficiency, but it scales favourably with more sensors, as the number of pins = number of sensors + 1.
The method for reading the sensor is as follows:
• Set D I/O 1 & 2 as outputs, with D I/O 1 -> LOW and D I/O 2 -> HIGH.
• Set D I/O 1 as an input.
• Start a timer.
• When D I/O 1 changes from reading LOW to HIGH, stop the timer and record the value.

The time it takes for D I/O 1 to go from LOW to HIGH is a function of the resistance of the sensor R. It’s not a nice linear relationship, but for many applications this is far from important, especially if you’re just using a few thresholds to analyse the sensor. If you were using this method with an LDR in place of R, where the resistance drops as more light hits the sensor, then you’d see smaller timer values as more light fell on the LDR.

You can extend this approach to virtually an infinite number of sensors, just by connecting them all to the same capacitor and reading them sequentially. When you’re reading one sensor just follow the method above, and set all the other sensor D I/O pins to inputs – that way they won’t interfere with the sensor you’re reading.

One final note – I’ve just shown the basic technique for reading resistive sensors here, but you can do the same thing with semiconductor sensors (reverse-biased LEDs, photodiodes, phototransistors, etc) just by substituting R for the sensor. In some cases you may want to limit the sensor current with a fixed series resistor too.
Other types of sensors (such as RF antenna) can also be used with this technique, but you have to be able to use an I/O pin to ‘disable’ the sensor when you want to read the others. Often a few cleverly placed diodes or transistors will do the trick.

Login or register to post comments
By amando96
@ Sat, 2009-12-26 02:17
very good

will come in handy for sure
Login or register to post comments
By OddBot
@ Sat, 2009-12-26 06:46
A nice simple aproach to

A nice simple aproach to ADC. I like it :D
Login or register to post comments
By Weirdo
@ Sat, 2009-12-26 18:05
AVRs have built in pull-up

AVRs have built in pull-up resistors what are useable even when pin is analog input. So in theory it is possible to build DAC only with one pin. Somebody would test it and tell if it is possible
Login or register to post comments
By fritsl
@ Sat, 2009-12-26 20:49
Thanks for sharing :)

Thanks for sharing :)
Login or register to post comments
By Dannyv
@ Sun, 2009-12-27 16:37
Dutch website

Below a link to a dutch site. It go’s over making it in picbasic.. 2 methodes are described. the first one, with the pot instruction is only using a 100n capacitor and a 25K potentiometer. Im gonna try that soon…

For people who cant read dutch, try google translate..

Login or register to post comments
By droidbuilder
@ Wed, 2009-12-30 19:52
Congratulations! You’re in Make Blog!

Telefox I was just catching up on news and blogs when I happened to see this article mentioned on the Make Online blog Always nice to see one of us mentioned in a major online blog… Way to go!
Login or register to post comments
By TeleFox
@ Wed, 2009-12-30 23:32
Haha, while I’m definitely

Haha, while I’m definitely happy to see this little trick getting out there, the best part of this is the URL that Make has used for the article: “…/use_digital_io_pins_to_measure_anal.html”

Login or register to post comments
By patrickmccabe
@ Thu, 2009-12-31 01:14
congrats and LOL i laughed

congrats and LOL i laughed for a bit on that. :P
Login or register to post comments
Comment viewing options

Select your preferred way to display the comments and click “Save settings” to activate your changes.
User login
Username: *

Password: *

Create new account
Request new password
Log in using OpenID
Most Collected
Forum Posts
Recently Collected
Recent blog posts
Guitar Effects Pedal Power Supply
Motor Controller Board
Finally back online
A new sexy mobile phone…
“Moooo” Rare Swiss Cow Project has begun
Swarmbots.. hmm, cheap and small ones.
My uploaded files
Woohoo, more bot work done
Laser Distancing Probe using WiiMote – Brought to you by Rare Swiss Cow Productions
C328R camera and Arduino
Quick links
LMR on Facebook
LMR Scrapbook
RSS feeds
ALL LMR ARE BELONG TO US! Let’s make robots!