Cheap and nasty A/D conversion? - Page 1
Forum section: electronics / beginners

author: brucehoult
author_rank: Super Contributor
timestamp: February 10, 2017, 12:46:45 pm
content: I have an Arduino-like board (HiFive1) that, like Raspberry Pis but unlike most Arduinos, doesn't have any analogue inputs. They are very handy for simple demos such as making a LED blink faster or slower, positioning an RC servo etc.Would the following simple-minded scheme work? I don't need high accuracy or linearity or anything like that. I have a very limited selection of parts lying around at present.Proposed parts:- one (1) digital IO pin, switched as an output most of the time, then input when making a reading- a 220 ohm resistor attached to the IO pin- a 10 nF ceramic capacitor from the 220 ohm resistor to ground.- a 10 k ohm resistor from the junction of the 220 ohm and 10 nF, to ...- a 10 k ohm potentiometer from the fixed 10 k to +3.3 V.The idea is for the IO pin to be driven low most of the time, keeping the capacitor discharged to within 1% - 2% of ground (or at least the IO pin output voltage). I figure the maximum current from a charged capacitor would be restricted to about 15 mA (and 0.15 mA - 0.3 mA via the pot), and the capacitor discharged with a time constant of about 2 us.When a reading is desired, the IO pin is switched to a (high impedance) input and sampled until digitalRead() returns 1 instead of 0. The capacitor will charge via between 10k and 20k ohms, depending on the pot setting, with a time constant of around 0.1 - 0.2 ms.If I understand it correctly, one time constant will get the input voltage to about 63%, two time constants to 86% etc. So it seems likely that the digital input will change from 0 to 1 sometime roughly around 1 time constant (certainly sometime between 0.25 (22%) and 2).I don't think I'd break anything (ok, maybe put 440 ohm on the IO pin, not 220), but the point on which I'm not clear is how consistent and reliable the switching point would be on something which after all is intended to have clear hi or lo inputs and fast switching between them.I certainly would not be surprised if different pins switched at different points, let alone different chips. But would one pin be reasonably consistent?The CPU itself is running at 256 MHz (at 1 cycle per instruction, except loads/stores. multiply (4 cycles), divide and mispredicted branches). There is a register incrementing at 32 MHz, so no problem to get accurate timings, if the switching point is consistent.The CPU doesn't connect directly to the IO pins, but via a TI TXS0108ERGYR level shifterhttp://www.ti.com/lit/ds/symlink/txs0108e.pdfSchematics are athttps://dev.sifive.com/hifive1/hifive1-schematics/Am I crazy?

author: wraper
author_rank: Unknown
timestamp: February 10, 2017, 01:12:21 pm
content: Threshhold voltage will likely change with temperature. Ceramic capacitor is unstable. Capacitance will depend on temperature and applied voltage.

author: Ian.M
author_rank: Super Contributor
timestamp: February 10, 2017, 01:27:56 pm
content: Yes you are crazy. Read the TXS0108ERGYR datasheet - its full of stuff like internal pullup resistors (probably polysilicon or weak MOSFETs) so highly temperature dependant + one-shot edge accelerators. You could fudge around it with a Schmitt trigger input buffer and a open drain gate or a MOSFET to discharge the cap, but that's 2 I/O pins and a lot of extra parts so you'd be better off using a 5V I2C ADC. Its more cost effective the more channels you need.

author: langwadt
author_rank: Super Contributor
timestamp: February 10, 2017, 01:35:09 pm
content: that was basically how the old pc game port worked, 100k pot from +5V to capacitor, timer measured how long it took to charge from 0 to a threshhold

author: brucehoult
author_rank: Super Contributor
timestamp: February 10, 2017, 01:41:30 pm
content: Quote from: wraper on February 10, 2017, 01:12:21 pmDon't even care. I just want to wiggle the pot and have the reading change, and have the reading not change by much over a period of seconds if I don't wiggle it.

author: brucehoult
author_rank: Super Contributor
timestamp: February 10, 2017, 01:43:15 pm
content: Quote from: Ian.M on February 10, 2017, 01:27:56 pmNo question at all that's better. Of course.The problem is it would take a week to get one of those, and I want to make a quick and dirty demo tomorrow.

author: brucehoult
author_rank: Super Contributor
timestamp: February 10, 2017, 01:51:00 pm
content: Quote from: langwadt on February 10, 2017, 01:35:09 pmYep. I have my Apple ][ schematic in front of me (original book). They used a 150k pot. And a transistor to discharge the capacitor. And a 555 (558 actually).https://www.dropbox.com/s/pjf44hdwdr0f2q6/Photo%2010-02-17%2C%204%2048%2030%20PM.jpg

author: Ian.M
author_rank: Super Contributor
timestamp: February 10, 2017, 01:53:33 pm
content: The original IBM Game Control Adapter used anXR-558CPquad timer that was designed to have RC networks on its timing pins.@brucehoult,Suck it and see, but don't be surprised if the TXS0108ERGYR oscillates and/or overheats!Have you got any 5V OPAMPs, Schmitt input 74xx00 series logic or even 555 timers hanging about, or a low pincount PIC or AVR you could program as an ADC slave?

author: phil
author_rank: Regular Contributor
timestamp: February 10, 2017, 01:59:14 pm
content: Do you have any comperators or opamps? If you do, you can use a PWM-DAC to generate different voltages and compare them with the input voltage.Not the fastest approach, but might be the most stable in your situation.

author: brucehoult
author_rank: Super Contributor
timestamp: February 10, 2017, 02:02:57 pm
content: Quote from: Ian.M on February 10, 2017, 01:53:33 pmSadly none of those here. Back in NZ, yes.I'll can probably get my hands on a PCF8591 next week, but that's next week :-)I mostly want to be reasonably sure I won't blow something up.

author: danadak
author_rank: Super Contributor
timestamp: February 10, 2017, 02:09:41 pm
content: This might help -http://www.ti.com/lit/an/snoa627/snoa627.pdfhttps://www.ecnmag.com/article/2012/03/rc-analog-digital-converters-simplify-design-temperature-humidity-co-instrumentationhttp://www.bytecraft.com/Low_Cost,_Low_Speed_A_D_conversion_for_Embedded_SystemsRegards, Dana.

author: rstofer
author_rank: Super Contributor
timestamp: February 10, 2017, 03:03:45 pm
content: I think the Parallax BS2 uses a similar processhttp://wwwcourses.sens.buffalo.edu/mae576/Spring2002/LABS/Lab2_2002.pdf

author: brucehoult
author_rank: Super Contributor
timestamp: February 10, 2017, 03:05:30 pm
content: Quote from: danadak on February 10, 2017, 02:09:41 pmCool. The 3rd one is abusing a digital input the same way I propose, but he's using PWM averaging instead of my RC timing. I'll try that.

author: David Hess
author_rank: Super Contributor
timestamp: February 10, 2017, 08:08:49 pm
content: What you are suggesting will work fine for low accuracy measurements.

author: brucehoult
author_rank: Super Contributor
timestamp: February 11, 2017, 01:55:06 am
content: I didn't try the A/D yet ... got distracted by trying for D/A just by calling good old digitalWrite(pin, bool) from software once every microsecond. I don't seem to have the filtering quite right, but it's much better with it than without it.(The "Challenge accepted" bit is about thishttps://twitter.com/DavidGrubb18/status/829711362016342017)