Author Topic: Cheap and nasty A/D conversion?  (Read 2726 times)

0 Members and 1 Guest are viewing this topic.

Offline brucehoultTopic starter

  • Super Contributor
  • ***
  • Posts: 4028
  • Country: nz
Cheap and nasty A/D conversion?
« on: February 10, 2017, 12:46:45 pm »
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 shifter http://www.ti.com/lit/ds/symlink/txs0108e.pdf Schematics are at
https://dev.sifive.com/hifive1/hifive1-schematics/

Am I crazy?
 

Online wraper

  • Supporter
  • ****
  • Posts: 16849
  • Country: lv
Re: Cheap and nasty A/D conversion?
« Reply #1 on: February 10, 2017, 01:12:21 pm »
Threshhold voltage will likely change with temperature. Ceramic capacitor is unstable. Capacitance will depend on temperature and applied voltage.
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12855
Re: Cheap and nasty A/D conversion?
« Reply #2 on: February 10, 2017, 01:27:56 pm »
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.

« Last Edit: February 10, 2017, 01:29:34 pm by Ian.M »
 

Offline langwadt

  • Super Contributor
  • ***
  • Posts: 4414
  • Country: dk
Re: Cheap and nasty A/D conversion?
« Reply #3 on: February 10, 2017, 01:35:09 pm »
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
 

Offline brucehoultTopic starter

  • Super Contributor
  • ***
  • Posts: 4028
  • Country: nz
Re: Cheap and nasty A/D conversion?
« Reply #4 on: February 10, 2017, 01:41:30 pm »
Threshhold voltage will likely change with temperature. Ceramic capacitor is unstable. Capacitance will depend on temperature and applied voltage.

Don'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.
 

Offline brucehoultTopic starter

  • Super Contributor
  • ***
  • Posts: 4028
  • Country: nz
Re: Cheap and nasty A/D conversion?
« Reply #5 on: February 10, 2017, 01:43:15 pm »
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.

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

Offline brucehoultTopic starter

  • Super Contributor
  • ***
  • Posts: 4028
  • Country: nz
Re: Cheap and nasty A/D conversion?
« Reply #6 on: February 10, 2017, 01:51:00 pm »
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

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

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12855
Re: Cheap and nasty A/D conversion?
« Reply #7 on: February 10, 2017, 01:53:33 pm »
The original IBM Game Control Adapter used an XR-558CP quad 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?
 

Offline phil

  • Regular Contributor
  • *
  • Posts: 67
  • Country: de
Re: Cheap and nasty A/D conversion?
« Reply #8 on: February 10, 2017, 01:59:14 pm »
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.
 

Offline brucehoultTopic starter

  • Super Contributor
  • ***
  • Posts: 4028
  • Country: nz
Re: Cheap and nasty A/D conversion?
« Reply #9 on: February 10, 2017, 02:02:57 pm »
The original IBM Game Control Adapter used an XR-558CP quad 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?

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

Offline danadak

  • Super Contributor
  • ***
  • Posts: 1875
  • Country: us
  • Reactor Operator SSN-583, Retired EE
« Last Edit: February 10, 2017, 02:17:55 pm by danadak »
Love Cypress PSOC, ATTiny, Bit Slice, OpAmps, Oscilloscopes, and Analog Gurus like Pease, Miller, Widlar, Dobkin, obsessed with being an engineer
 
The following users thanked this post: brucehoult

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9889
  • Country: us
Re: Cheap and nasty A/D conversion?
« Reply #11 on: February 10, 2017, 03:03:45 pm »
 

Offline brucehoultTopic starter

  • Super Contributor
  • ***
  • Posts: 4028
  • Country: nz
 

Offline David Hess

  • Super Contributor
  • ***
  • Posts: 16607
  • Country: us
  • DavidH
Re: Cheap and nasty A/D conversion?
« Reply #13 on: February 10, 2017, 08:08:49 pm »
What you are suggesting will work fine for low accuracy measurements.
 

Offline brucehoultTopic starter

  • Super Contributor
  • ***
  • Posts: 4028
  • Country: nz
Re: Cheap and nasty A/D conversion?
« Reply #14 on: February 11, 2017, 01:55:06 am »
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 this https://twitter.com/DavidGrubb18/status/829711362016342017)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf