Readadc confusion.

tt92

Member
I have an application in which I want to recognise that a voltage has been applied to pin 1 of Picaxe 08M. Most times it recognises the voltage, but sometimes it doesn't. The voltage that I want to recognise can be between 2.0V and 2.5V



Here is the coding
.....................................
testPIR:
readadc 1, avalue
if avalue < lowval then testPIR
..............................................

I am using 127 as the value for lowval.

Should it be lower?
 

BradenBurton

New Member
Hi,
perhaps try something like this (Note not tested, this is psudo code)
Code:
testPIR:
READADC10 1, avalue
if avalue >= 409 and avalue <= 512 then
  gosub FoundIt
endif
goto testPIR

FoundIt:
  do something here
  return
Assuming the PICAXE has an exact 5V power source and you are reading into a word, see ylp88 below for a byte value.
 
Last edited:

ylp88

Senior Member
As noted above, READADC is referenced to your supply, thus the 2.5V upper limit you are trying to measure will have a digital value of half your word length when using a 5V supply.

For a byte variable (i.e. 8 bits ~ 256), you should set your threshold at (Vin/Vdd)*(2^n) = (2.5V/5V)*(2^8 bits) = 128. You need to adjust this accordingly with the number of bits if you use READADC10, for example.

I'll let you extend this to determine the threshold for your 2V lower limit.

ylp88
 
Last edited:

BradenBurton

New Member
Calculating adc

Just out of interest,
this is how I calculate the value for adc's

Vpicaxe = 5V power
1024 / 5 = 204.8
multiply by voltage to get ADC 2.5 * 204.8 = 512

if Vpicaxe = 4.8V then
1024 / 4.8 * 2.5 = 533

If that is of any use to anyone :)
 

BeanieBots

Moderator
Presumably, when your 2.0v ~ 2.5v is not connected, the voltage is 0v.
Is it an incomming signal (always connected) or a voltage which can be diconnected?
The reason I ask is because if the input is disconnected, the ADC input could float to any value if it is not pulled down.

However, I'm quite sure your problem is for the reasons explained above.
(note, the values given above assume ReadADC10 and not ReadADC)

On a PICAXE powered with 5v, testing for a value of 127 is very close to testing for a voltage of 2.5v.
If your input is either 0v or just over 2v, then you should test for about 1v.
Change your value to about 50 to test for 1v using ReadADC.
 

tt92

Member
Perhaps I had better be a bit clearer at the expense of being wordy.
I have several PIR receivers. each of which senses warm-body movement and turns on six white LEDs for twenty seconds to illuminate stairs or doorways or so on whenever someone comes by.
I replace one of those six LEDs with a connection to the Picaxe. When the PIR is triggered the voltage presented to the Picaxe is between 2.0V and 2.5V.
This voltage is supplied by the PIR, and I believe? that it is independent of the voltage at which the Picaxe is being powered. The cable from the PIR is connected directly to the Picaxe without any resistor.
 

Jeremy Leach

Senior Member
I think you need to sample some ADC values as the PIR activates and de-activates, just to see what sort of readings you are getting. Then design the code around these results.

We don't know the circuit in the PIR, and it's 'possible' that the line to the picaxe ADC is floating when the PIR LEDs are off, which won't help.
 

BeanieBots

Moderator
Always pays to give plenty of info including the purpose when posting.
You are close to the project, we only know what you post and have to guess the rest.
So, in breif, you testing to see if an LED is on by testing the voltage across it.
Your method should be OK but your trip point is too high.
Try the value of 50 that I suggested earlier.

Also, depending on how the PIR unit drives the LED, it is possible that there might be a very small leakage current going through the LED. This MIGHT be enough to present a small voltage to the PICAXE. An off LED is very similar to an open circuit up to the voltage at which it starts to conduct. For increased reliability, I would suggest fitting a 10k pull-down resistor on your PICAXE ADC input. That will ensure that the 0v is closer to 0v and can't float up.
 

Dippy

Moderator
Post a circuit diagram.
As I always say - it can replace a hundred words and remove ambiguities.

If you want a detailed ansser back then you have to put 'detail' in to the Forum.

You say you have a connection to PICAXE, but are the 'ground' (0V) lines commoned?

So, a drawing of how it is wired would be useful please.
 

hippy

Ex-Staff (retired)
As per BeanieBots in post in post #7, the READADC activation level you are looking for is around 2.5V for a 5V supply and what the PIR is putting out is close to that and can be lower.

The best way to determine the trip point is to find the mid point between the highest level the PIR puts out when not activated and lowest level put out when activated. So if highest when not activated was 0.5V, lowest when activated is 2.0V, mid point is (2.0-0.5)/2+0.5 = 1.25.

A suitable 'lowval' would be 1.25/Vpsu*255 = 63.75 for 5V supply, round up, so I'd use 64.
 

tt92

Member
I think I have the answer.
The first readadc in the program is there to tell the program which of two paths it is to follow: if the Picaxe is switched on with a voltage already applied to pin 1, the program is meant to run intermittently; if the Picaxe is switched on with no voltage applied to pin 1, the program is meant to run continuously. This worked consistently at my desk but not in the bush. I have now put in a two second pause after turning on the Picaxe and before the first readadc instruction in case the Picaxe needed a little warm-up time in the pre-dawn cold of winter and now it operates reliably.
I guess that is why it made no difference what value I tried for the readadc activation because the Picaxe simply wasn't operating correctly.
Thank you all for your advice. I'll try various values now to see what does and does not trigger.
 

BeanieBots

Moderator
I'll not dispute your solution but I will dispute your reasoning.
The PICAXE requires NO warmup time whatsoever, even on the coldest wettest of days!
You might see a variation of a few counts on the ADC value between -10C and 60C temperatures but the PICAXE will be functioning fully correctly the instant it is switched on. (it takes 76mS to sort itself out before running the first line of your code).

Your other hardware may not work in the cold though.
The PIR sensor needs to temperature stabilise for a start.
 

hippy

Ex-Staff (retired)
testPIR:
readadc 1, avalue
if avalue < lowval then testPIR
That code waits until the voltage read from the PIR is greater or equal to 'lowval', only continuing when it is. It doesn't really provide "two paths", more a 'gate' which holds the PICAXE where it is until the condition is met.

The PICAXE will be up-and-running as soon as it is turned on, but the PIR probably requires some 'warm-up time'.

I guess that is why it made no difference what value I tried for the readadc activation because the Picaxe simply wasn't operating correctly.
More likely, the PICAXE was operating correctly but the program wasn't robust enough to work in the cicumstances it was used in.
 

tt92

Member
The application is to trigger a camera shutter.
In one mode the shutter is triggered continuously until the camera battery runs out, whether the PIR is attached or not.
In the other mode the shutter is triggered continuously for one minute every time the PIR is triggered.

In practice, when I want to use the PIR, I set up the camera and the PIR, turn on the PIR and check that its LEDs are on, and then turn on the Picaxe.
The first readadc in CheckPIR should recognise that the the PIR is on and fall through into the testPIR loop.
If the first readadc does not recognise that the PIR is on, the program jumps to NoPIR and shoots continuously.
The program often missed the fact that the PIR was turned on and jumped to NoPIR.
Since I have added the pause 2000 (marked with the asterisks) the first readadc always recognises the signal from the PIR. So far.
I don't know why.If it works, and doesn't let the smoke escape, I am content.




This is the relevant bit of code.






'Programme
init:
input 1
setfreq m8 'set Picaxe frequency to 8MHz
start:
pwmout 2,52,105 '38kHz for 8MHz clock speed
low 4 'IR LED off
'===============================
'If the program is started with the PIR turned on and
'triggered, the program will loop, looking for a
'response from the PIR.
CheckPIR:
pause 2000 '****************************
readadc 1, avalue
if avalue < lowval then NoPIR 'No signaldetected from PIR.
'Commence continuous shooting.

'===============================
PIR:
'===============================
'Do not shoot unless triggered by a signal from the PIR
testPIR:
readadc 1, avalue
if avalue < lowval then testPIR
'================================
'Shoot at maximum rate for ~ one minute
for i = 1 to 300
gosub pulse_stream
pause 200 ' ' ~0.1 seconds between shots
next i
goto testPIR
'================================
'Normal continuous shooting
NoPIR:
gosub pulse_stream

pause 200 '~0.1 seconds between shots
goto NoPIR
 

tt92

Member
On 18-06, BeanieBots suggested adding a 10K pull-down resistor to the readadc pin.
There is no room inside the box for this as it stands. The Picaxe readadc is connected to the PIR by two wires:signal and earth. Is there any reason why I couldn't put this resistor at the other end of these wires, the end attached to the PIR? The wires are about four feet long.

Something else to add to the sum of human knowledge.
Because I had them already , I have used 3.5mm co-axial mono connectors for all my connections. Bad move. The contact between these plugs and sockets is dodgy: a light tap at the point where they are connected is enough to interrupt the earth/shield contact. The signal part of the plug is gripped by a little spring inside the socket, and probably doesn't ever lose contact, but the outer earth/shield plug merely slides into a cylindrical hole. It looks and feels as if it is a snug fit but can, as I said, momentarily lose continuity if moved even slightly. In my scheme, such an interrupted connection is equivalent to a second trigger. This could well be the cause of my problem.
On the weekend I will replace all these connectors with RCA connectors.
 

BeanieBots

Moderator
The purpose of the suggested resistor is to prevent the ADC pin from floating when there is no connection.
Hence, it MUST be on the PICAXE side of any break in the cable.
No room to fit one:eek: A surface mount R is no larger than a solder joint!
 

tt92

Member
The purpose of the suggested resistor is to prevent the ADC pin from floating when there is no connection.
Hence, it MUST be on the PICAXE side of any break in the cable.
No room to fit one:eek: A surface mount R is no larger than a solder joint!
Thanks for that.
"no room" was a sort of shorthand for saying "the circuit was tested, found to work, and is now fixed into a small space. If I remove it enough to be able to attach a resistor, I will almost certainly damage it. There are eight of these little devices."
 
Top