Compare two voltages to trigger an interrupt

Thread Starter

gdallas

Joined Apr 25, 2012
74
Hi, Im designing a battery powered transmitter that detects the position of a valve. The valve will remain in the same position for the majority of the time, therefore I wish to sleep the device in this state, and only wake should the valve begin moving, i.e. change in analogue input voltage. Im using a PIC18F, but find my self scratching my head a little with regards how I detect this change in voltage to the ADC to trigger an interrupt and hence wake the device.
My logic has brought me to the practical solution where if I could constinously sample at a set rate and store the AI voltage and continuously compare it to a live AI voltage value. I wondered if a sample and hold IC would be good enough to facilitate this then use the PIC18F comparator to trigger an interrupt when a differential is detected. I had considered using the DAC to provide a dynamic Vref to the comparator, but its only 5-bit on this PIC and isn’t good enough for my application.

Any thoughts on my proposal, and or any better ideas??

Thanks
 
Last edited:

Thread Starter

gdallas

Joined Apr 25, 2012
74
Measure the analog voltage with the ADC and store it to a variable. Then look at the watchdog timer feature - runs in sleep mode.

http://ww1.microchip.com/downloads/en/DeviceDoc/31026a.pdf

Once the watchdog timer is set, then runs out and wakes the device again, measure again and do what is needed if the ADC moves.

HI GopherT, so if I understand your correctly. I could store the last AI value immediately before initiating a sleep to a variable, then I presume id need to keep sampling when in sleep mode (approx 210uA draw from the ADC) and should any new AI value deviate from the stored variable, then the WDT would not reset and hence be used as in interrupt to wake the PIC?
 

Thread Starter

gdallas

Joined Apr 25, 2012
74
Ian, Yes. lets say the valve moves to 50% open and contol philopshy detects a period if inactivitiy and puts the PIC to sleep. 50% would then become the new ref point for the comparator. and should the valve move to 60% or 40% then the comparator would detect this differnetial and use an interupt to wake the PIC. the pic will resume normal operation until the valve again stops moving plus a time delay of say 60s. if no further movements are detected inthis time the device is sleeping once more and what ever the last %open value is will assume the role of the new ref point for the comparator. make sense? I speak of this in %, but really its Volts im talking about.
 

GopherT

Joined Nov 23, 2012
8,009
HI GopherT, so if I understand your correctly. I could store the last AI value immediately before initiating a sleep to a variable, then I presume id need to keep sampling when in sleep mode (approx 210uA draw from the ADC) and should any new AI value deviate from the stored variable, then the WDT would not reset and hence be used as in interrupt to wake the PIC?
Not exactly. You could wake the PIC each time the WDT trips then make your measurement and compare with code to the previous measurement. You would need some threshold difference because an ADC on pic18 is high resolution and some noise may be present. In any case, the measurement can be made in a fraction of a secon (let the pic stabilize for about a hundredth of a second and measurement takes a few microseconds. Then sleep again if new value is not high enough to bother with -or- make your adjustment or light your LED if your analog value is high enough.

Then sleep again. Even checking one time every second and taking about 1/100th second to check means your battery lasts 100x longer than without sleep. Adjust as needed times as needed.

Also, make sure I/O pins keep your external devices off to maximize energy conservation to extend battery life. Including LEDs, motors, leaky electrolytic caps, ...
 

Thread Starter

gdallas

Joined Apr 25, 2012
74
Not exactly. You could wake the PIC each time the WDT trips then make your measurement and compare with code to the previous measurement. You would need some threshold difference because an ADC on pic18 is high resolution and some noise may be present. In any case, the measurement can be made in a fraction of a secon (let the pic stabilize for about a hundredth of a second and measurement takes a few microseconds. Then sleep again if new value is not high enough to bother with -or- make your adjustment or light your LED if your analog value is high enough.

Then sleep again. Even checking one time every second and taking about 1/100th second to check means your battery lasts 100x longer than without sleep. Adjust as needed times as needed.

Also, make sure I/O pins keep your external devices off to maximize energy conservation to extend battery life. Including LEDs, motors, leaky electrolytic caps, ...
ok got you thanks. Had another idea too please let me know your thoughts. say i used another standalone 8 bit DAC , and used this value as my ref to the comparator on the pic each time i sleep the device. Would the dac retain this value once the pic sleeps, obviously coms will be lost but wondering if the last valueis held. if it is then surely all I need is to configure a pic port as a comparator and check for voltage change between the DAC and the live value. I guess this would mean id need two comparators to trigger an interrupt should the live voltage swing low or high.

does that make sense, and if so seem like a reasonable approach? to me this would seem like a very low power mode as I dont believe Id need he ADC burning away during sleep either.
 

Thread Starter

gdallas

Joined Apr 25, 2012
74
If that be the case The DAC on the chip has 32 levels... That's 0.15v increments with a 5v rail.. Isn't this good enough?
no. Its a 3.6 rail. the valve would need to move greter than 3% in either direction before it wakes the pic which isnt acceptable. i think a standaline daq would suffice at 8 bit though
 

GopherT

Joined Nov 23, 2012
8,009
There are multiple ADC channels on the pic 18f and, yes, you can use one to monitor the voltage on a reference capacitor as an output pin charges (output high) or discharges (output low) a reference capacitor through a resistor to a set voltage. The comparitor can then be used to wake the device from sleep mode.

Make sure you switch the output control pin to a high impedence input tris-state once the voltage is set. You will have to find a low-leakage capacitor and do some experiments.

It is impossible to know which method is best for you because we don't know
- how often you expect the valve to move
- how soon you need to know that the valve moved
- how stable your voltages will be over the measured time period
- if noise is present, how do you want to handle hysteresis (software or hardware)
- what accuracy you expect if 3% is not good enough
...

In any case, it is not necessarily important to find the "best" solution, you just need a solution that is good enough and that you feel comfortable implementing because you understand how to do it and understand the limitations and possible errors.
 
Last edited:
Top