ADC speed problem?

I've searched the forum (in excess of 500 hits for ADC*) and can't find an obvious answer to my problem which is that I'm getting some very strange results (consistently low) when reading the ADC value from a buffered potentiometer:
js_test_cct.jpg

The code is as simple as I can make it to just test what values I'm getting from the system where a lever is controlling the pot, but not giving its full range, so typically on a +5V supply the range of input voltages is from 0.66V to 3.33V. The op-amp is a low power single supply device with rail-rail input and output, though neither gets near the supplies.

What's happening is that I'm not getting anything like the values I'd expect for a given pot position and hence voltage in. For instance, if I position the lever to get as near to 2.50V (50% x supply) as I can, I'd obviously expect a value of about 126/127 to be read back. What I'm actually getting varies from 17 to 86, but never as high as 126. Both the supply voltage and output from the buffer to the 08M2 are monitored with (cheap) DVMs. The 08M2 is a new device straight from the tube of spares I have, but I haven't tried swapping it yet.

This is the code:

Code:
	; Vers. 01, test for joystick in values.
	; Last updated 13/11/2017
	; 
	#picaxe 08M2
	; 
init: ;
	;
	symbol raw_jystk = b2   	; byte variable for joystick input
	;
	INPUT  C.4
	;
	; C.4 is an analogue voltage from the joystick indicating
	; its position between full reverse (?V) and full forward (?V).
	; 
	ADCCONFIG %00000000
	;
	; ---------------------------------------------------------------------
	;
main:
	;
	READADC C.4, raw_jystk   ; Read the value from the joystick pot.
	PAUSE 500
	DEBUG
	; 
	GOTO main
	; 
	end
It occurred to me, before I put the PAUSE command in, that I might be trying to read the ADC too quickly (despite the DEBUG), so I put in the PAUSE and indeed did get different values, but the value read via debug varies with delay value, e.g.
delay 50, read 17;
delay 500, read 42;
delay 5000, read 84.
So far as I can see from another thread, the ADC conversion takes less than 100µs, so it doesn't sound as though that's what the problem is. I've also tried swapping the position of the PAUSE & DEBUG commands around and that doesn't change anything either.

Any help/suggestions would be gratefully received please?

P.S. for the record, this is continued work on the throttle I described originally in http://www.picaxeforum.co.uk/showthread.php?27782-DAC-o-p-capacitor-query for which I had so many really helpful suggestions. Slow progress but getting there!
 
Last edited:

techElder

Well-known member
Not much time to evaluate, but my first suggestion is to get rid of DEBUG and sprinkle some SERTXD statements in there where you want to view the outputs.
 

hippy

Technical Support
Staff member
Everything looks okay. I would also recommend removing the DEBUG and using SERTXD but if you are happier with DEBUG then that's not a problem. You cannot read ADC too quickly so a PUASE isn't really needed but does not harm.

On the software I cannot see any problem with your INPUT and ADCCONFIG but they are not absolutely necessary so I would remove those just in case they are causing a problem. Go for the simplest code you can have -

Code:
#Picaxe 08M2
#Terminal 4800
Do
  ReadADc C.4, b0
  SerTxd( "ADC = ", #b0, CR, LF )
  Pause 1000 
Loop
Replace SERTXD with DEBUG if you prefer.

On the hardware side it could be that something is not wired quite right. You could unplug the actual op-amp chip and use a wire jumper to connect the pot input to the op-amp to what would be the op-amp output. That will then be reading the pot directly unbuffered, which should work. It won't be the full range but shouldn't be jumping about.

If it is all over the place that might suggest the pot ends are not correctly wired to 0V or 5V. Perhaps take a spare pot, wire that across 0V and 5V and see if its wiper gives as expected.

It could be that the pot itself is a bit noisy or the wiper jumps from the tracks.
 

hippy

Technical Support
Staff member
Your circuit digram says it's an AXE092 board - On that there is a high-side LDR and 10K divider feeding C.4 via a DIL switch. Have you ensured you have your op-amp driving the PICAXE pin directly and that the DIL switch is open circuit, or have removed the LDR ?

http://www.picaxe.com/docs/axe092.pdf
 
Thanks for the helpful suggestions. The AXE092 board is being used only because it's a very useful prototyping board. It's unpopulated apart from the components shown. The value read back with debug is absolutely stable, apart from minor bit variations (as is the DVM reading of the input voltage). The only variation seen is when I change the PAUSE command length.

Because the pot is 60k, connecting it directly to the input probably wouldn't work because the input needs to see less than 20k source impedance.

I'll certainly try the SERTXD option instead but I can't see why it would make a difference. Removing the INIT options wouldn't hurt either, but again, I doubt it will help.

I'll post an update after further tests.

Thanks again.
 
Modified the code to:
Code:
#picaxe 08M2
	
main:
	;
	READADC C.4, b2   ; Read the value from the joystick pot.
	PAUSE 1000
	SERTXD (#b2,13,10)
	; 
	GOTO main
	; 
	end
and discovered how the terminal pane worked - very useful, thanks for that suggestion. It doesn't change anything much though. :-(

Here's a few lines of data returned from the 08M2 with a PAUSE of 1000 (1 second):
107
83
64
56
53
51
52
51
51
52
51
50
51
51
50
51
51
50
51
51
50
50
51
This is from power-up, so it seems to take three or four readings to settle down and then settles on 50 or 51 where it should be giving 126/127 as I'm giving it exactly 50% of the supply voltage.

I think maybe I should indeed, as suggested, take the op-amp & pot out of the circuit altogether and just put a pair of 4k7 resistors in there across the supply and see what I get then. At least that should indicate whether the problem lies with the PICAXE or the pot/buffer.
 

Jeremy Harris

Senior Member
Have you tried actually measuring the voltage at the ADC pin?

Maybe it's showing the actual voltage at that pin, rather than what you think the voltage should be, and that the problem is upstream of the Picaxe.
 

hippy

Technical Support
Staff member
Because the pot is 60k, connecting it directly to the input probably wouldn't work because the input needs to see less than 20k source impedance.
In my experience that's never actually been a major problem. Perhaps in some applications but not so much when reading a pot.

A low(ish) source resistance is required so the internal ADC sampling capacitor quickly charges up to reflect the voltage applied. Once sampled the source is disconnected and the cap voltage is then converted to digital. If the resistance is higher the cap charges up slower, doesn't rise to the actual voltage by the time conversion starts..

If that proves to be the case one can either compensate for that in software or issue multiple READADC on the same pin. Each READADC kicks the capacitor closer up to what it should be, the final READADC will reflect that.

The odd thing is that the longer the delay between READADC the higher the voltage being read. That doesn't make a lot of sense unless there were some capacitor somewhere which is being 'shorted' through the ADC reading and is climbing between readings; the further apart between the readings the higher it's climbing.

I thought the capacitor was disconnected from the input between READADC but perhaps not.

Ignoring what values you expect to be getting, what values are you getting when the pot is roughly at 0%, 25% and 75% and 100% ?

Are you using an AXE027 or some other download cable ? Using an RS232 cable can affect ADC readings.
 
Okay, so the circuit is now:
js_test_cct2.jpg
It's difficult to get it any simpler.
The DVM confirms that exactly half the supply voltage is being fed to pin 3 (C.4) and with the same software as in the previous example, I'm getting much the same from the serial port:
77
63
54
51
51
51
52
51
52
But then I thought, maybe I should check the voltage at the actual pin on the 08M2.

DOH!

Nothing there, but 2.5V on pin 4 (not an ADC input). Another quick bit of resoldering and, bingo ....
128
128
128
128
128
128
128
128
128
128
128
128
I thought I'd so carefully checked what was connected to what, but obviously I hadn't.

What had really confused me was that the value being read back was changing as I moved the lever, going up and down as I moved the lever each direction, so it didn't look as though I'd connected anything to the wrong pin. I can only think that maybe there was some influence from the changing voltages on the adjacent pin.

So, apologies to all if I've wasted your time. I'm delighted to have learnt though how SERTXD and the terminal pane works now, so it has been a useful learning experience for me.

Thanks again all.
 

AllyCat

Senior Member
Hi,

Because the pot is 60k, connecting it directly to the input probably wouldn't work because the input needs to see less than 20k source impedance.
A 60k pot wired as a potential (supply voltage) divider has a maximum source impedance (at the centre of its track) of only 15k. Effectively, the power supply rail has a very low impedance to ground (otherwise it couldn't deliver the hundreds of mA or Amps that might be required) so the "source" impedance is two 30k resistors in parallel, or 15k. Microcochip "recommend" a maximum of 10k, but in all normal conditions 15k will be satisfactory, particularly if only using READADC (instead of READADC10) and/or put a small capacitor across the ADC input pin to ground.

IMHO the Op Amp buffer is completely unnecessary and this thread a good example of the merits of the Keep It SimpleS approach. ;)

FWIW you can sometimes use the same principle to emulate a "Log" Potentiometer (strictly a misnomer as the output response is normally exponential) for an audio volume control (and other applications related to human senses). For example, connect 10k between the wiper and lower end of the 60k pot. Now at the centre of track, the top resistance will still be 30k, but the lower is now 10k in parallel with 30k, or 7k5, so the output voltage would be only 7.5 / 37.5 or 20% of Vdd. But at the top and bottom of the track the output must still be 100% and 0%.

Cheers, Alan.
 
IMHO the Op Amp buffer is completely unnecessary and this thread a good example of the merits of the Keep It SimpleS approach. ;)
I absolutely agree about KISS and am grateful for the reasoned suggestion.

However, as I have already got the buffer circuitry in place:
HA142039rcs.JPG
I probably won't now take it off.

Also, it's presently driving about 18" of cable which might be more prone to interference without the low impedance buffer? I'm not sure at present how close the PICAXE will be to the pot/buffer so to be on the safe side I'll leave it in as there's minimal risk of failure of the few extra components involved.

And I'd like to reiterate what a pleasure it always is being a part of this forum. There are other places on the internet where, if one made a mistake as I did, some people might be downright rude, but here everyone is polite and more than helpful.

Thanks again.
 

hippy

Technical Support
Staff member
As this appears to be part of a safety critical application you should take multiple ADC readings so you don't misinterpret any noise picked-up on the signal line. You may also want to smooth out any variations, probably even prevent any unexpected large throttle increases. You should add a pull-down resistor at the PICAXE end to keep a zero input should the cable become detached.
 

hippy

Technical Support
Staff member
However, as I have already got the buffer circuitry in place: I probably won't now take it off.

I'll leave it in as there's minimal risk of failure of the few extra components involved.
If you do a failure mode analysis you will discover that there are far more possibilities of failure with the op-amp in place than there would be without it. Having the op-amp in place does not seem to add anything other than increasing the potential for problems. In that respect it may be wiser to remove it than keep it.
 
As this appears to be part of a safety critical application you should take multiple ADC readings so you don't misinterpret any noise picked-up on the signal line. You may also want to smooth out any variations, probably even prevent any unexpected large throttle increases. You should add a pull-down resistor at the PICAXE end to keep a zero input should the cable become detached.
Very helpful suggestions, thanks.

The throttle control I'm currently working on is the second of two, essentially similar, units but with different hardware for the actual throttle itself. The first one (to be used at the back of the boat) is more or less complete and uses a custom made APEM joystick. In theory, I could have connected it directly to the motor's control box as it has microswitches which actuate as it's moved from the centre (off) position to set either the forward direction, or reverse. The control electronics it connects to only require a 0 - 5V signal (from a pot or other source) defining throttle 0 - 100% and a 72V level switched to either forward or reverse inputs.

However, because it requires quite a light touch to move it from the centre-off position, it might be all too easy for someone brushing past to accidentally suddenly put the boat in full forward (or reverse). So I have three (illuminated) pushbuttons for Forward, Stop & Reverse confirmation and when the 20M2 detects that the control stick has been moved away from the centre stopped position, flashes the relevant Forward or Reverse pushbutton for confirmation that forward or reverse mode is intended, and only when the relevant button is pushed does it actually switch the relevant signal through to the control box.

The Stop pushbutton also acts as an emergency stop at any time which is potentially quicker than moving the joystick. I haven't yet programmed an emergency reverse option, which might be more beneficial than just stopping the motor.

The second throttle control for use at the front of the boat is designed to look more in keeping with the age of the the boat (1926):
DAV9626-BR.jpg
but is less easy to configure than the APEM joystick.

Thanks though to many really helpful suggestions from this forum back in 2014, I have a relatively simple mechanical arrangement whereby the lever controls the 60k sealed pot. It's not full range but as the DAC output only has 32 steps anyway, the resolution of the ADC in 8-bit mode is fine and just happens to be almost exactly twice the required resolution. Since getting it working (on the right pin), I have some basic maths working nicely to convert the 0.66V level to full reverse, 2.06V (+ a deadband) to stop, and 3.33V to full forward, and of course linear levels in between.

So that's it in a nutshell. I shall definitely look at software smoothing of the ADC signal, though I haven't seen any sign of problems so far. But of course pots do wear with age and become more noisy so it is a very sensible and helpful suggestion, thanks.
 
Top