i-Zapp
Posts: 3
Joined: Thu Jan 16, 2020 3:52 am

virgin post: need help with external pull up resistor

Thu Jan 16, 2020 4:09 am

Mech Engineer here, dabbling with a "simple" electronics project... could use some EE help!...

i'm trying to sense a fairly high impedance input on the GPIO of my Zero, and struggling with the configuration.
i've gotten the Python code working perfectly with a simple button switch.

However the project ultimately will utilize a water leak sensor, which switches from open (infinite resistance) to about 3megaOhm (still very high) when wet. Presume i need a similarly large external pullup resistor, and thus I need to turn off the internal one.

when i adjust my code to the following, the CPU goes to 100%, the Pi gets brutally slow, and the IO doesnt work as expected (and very flakey). left with no choice but to reboot the Pi.
What am i doing wrong, please!!?

Code: Select all

from gpiozero import Button
switch1=Button(5,pull_up=None,active_state=False)


by removing the config info after the pin assignment ("5"), it's fine. but as above it doesnt behave. (or active_state=True)

thanks in advance

njh
Posts: 55
Joined: Wed Aug 09, 2017 2:07 pm

Re: virgin post: need help with external pull up resistor

Thu Jan 16, 2020 8:24 pm

What (if anything) is connected to the GPIO when it misbehaves, and does it get better when you (dis)connect something or press the physical button to ground it? Also what does the software do when it detects a 'button press'?

I can only guess the problem is due to spurious triggering of a floating / high-impedence input.

3Mohm is too high source impedence for reliable operation. According to this page (not Zero-specific but likely to be similar), input leakage current can be up to 5 uA.

emma1997
Posts: 507
Joined: Sun Nov 08, 2015 7:00 pm
Location: New England (not that old one)

Re: virgin post: need help with external pull up resistor

Thu Jan 16, 2020 9:08 pm

I had a similar requirement to detect pure water which has a very low EC. Almost an insulator. Couple inches of bare wire with 10meg external pullup worked fine even though it's technically out of spec. Same pu for a GL5516 LDR to sense very low light level (however takes half hour to change after lights off!).

For mission critical or commercial applications I'd add an FET or external gate with specs allowing for resistance that high. However my measurements show actual Pi input is at least two orders of magnitude better than spec so for hobby use just 10m pu. High humidity and the PCB was more influential.

You do have to disable the Pi internal pullup which can be very problematic with some libraries and OS versions. If noise or other interference is suspected a small cap to ground fixes that.

i-Zapp
Posts: 3
Joined: Thu Jan 16, 2020 3:52 am

Re: virgin post: need help with external pull up resistor

Thu Jan 16, 2020 9:49 pm

njh wrote:
Thu Jan 16, 2020 8:24 pm
What (if anything) is connected to the GPIO when it misbehaves, and does it get better when you (dis)connect something or press the physical button to ground it? Also what does the software do when it detects a 'button press'?

I can only guess the problem is due to spurious triggering of a floating / high-impedence input.

3Mohm is too high source impedence for reliable operation. According to this page (not Zero-specific but likely to be similar), input leakage current can be up to 5 uA.
it's a very simple breadboard setup with LEDs and other buttons to simulate additional inputs, and it works flawlessly unless i configure my code as i shown above for the floating input (so i can have an external pull up). it immediately begins to flake out and i have no choice but to kill the process and reboot the Pi.

I can accept spurious triggering, but not a lock up. seems the software cannot handle that floating input configuration.

as far as the high impedance triggering, seems its par for the course re water sensors. and the store bought unit (pumpalarm brand, and no longer supported) triggers just fine on these moisture sensitive inputs.

again, not a EE here, so please talk to me like i'm a 6th grader lol
:P

njh
Posts: 55
Joined: Wed Aug 09, 2017 2:07 pm

Re: virgin post: need help with external pull up resistor

Thu Jan 16, 2020 10:25 pm

i-Zapp wrote:
Thu Jan 16, 2020 9:49 pm
it's a very simple breadboard setup with LEDs and other buttons to simulate additional inputs, and it works flawlessly unless i configure my code as i shown above for the floating input (so i can have an external pull up). it immediately begins to flake out and i have no choice but to kill the process and reboot the Pi.
So is an external pull-up resistor actually connected to the GPIO? If not try connecting one of the intended value, to 3.3v. And a capacitor (say, 1nF) to ground. Do things improve?

The lockup is not normal, but if the GPIO was connected only to a wire (antenna) I can imagine it might be unhappy.

pcmanbob
Posts: 7959
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: virgin post: need help with external pull up resistor

Fri Jan 17, 2020 4:11 pm

You are never going to get a reliable pull up/down using the sensor directly as 6M ohms is just to high a value.

So this is one way your could use to read the sensor value, using a Wheatstone bridge and an op-amp.

Image

Now this circuit needs some set up before you connect the output to gpio 5 ,

So build the circuit connect the 5V and ground to the pi only and power your pi, now using a digital voltmeter measure the voltage on the output while the water sensor is fully saturated wet, you need to adjust POT1 until you get a reading of 1.71 - 1.72V DC.

now power your pi off , connect output to gpio5 insert SD card and power on

now you need to run this test program

Code: Select all

import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(5, GPIO.IN)
t= time.time()
while True:
    if GPIO.input(5) == 1:
        print ("Wet")
        print(time.strftime("%H:%M:%S"))
        
        while GPIO.input(5) == 1:
            w = time.time()
            time.sleep(0.1)
        print ("Time Wet ", round(w - t, 1), " seconds")
        t = w
        print ("-------------")
    if GPIO.input(5) == 0:    
        print ("Dry")
        print(time.strftime("%H:%M:%S"))
        
        while GPIO.input(5) == 0:
            d = time.time()   
            time.sleep(0.1)
        print ("time Dry ", round(d - t, 1), " seconds")
        t = d
        print ("-------------")
        

now obviously I don't have sensor so I just used a 6M ohm resistance that I could add another 7m ohms to to get the different values, which means I get a nice clean change over from wet to dry , you might find with the sensor there is a point were the sensor goes from wet to dry randomly while the sensor changes state, IE gets dried out or saturated wet.

only by experimentation with the actual sensor will know what actually happens.

You can replace RE1 with a variable resistor of say 1m ohm in series a 5M ohm resistor , that would allow you to match the wet value of the sensor exactly.

example of program output.

Code: Select all

[email protected]:~ $ python3 test2.py
Dry
15:48:28
time Dry  4.4  seconds
-------------
Wet
15:48:33
Time Wet  6.4  seconds
-------------
Dry
15:48:39
time Dry  452.0  seconds
-------------
Wet
15:56:11
Time Wet  936.0  seconds
-------------
Dry
16:11:47

EDIT .. to correct diagram....
Last edited by pcmanbob on Fri Jan 17, 2020 6:38 pm, edited 1 time in total.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

i-Zapp
Posts: 3
Joined: Thu Jan 16, 2020 3:52 am

Re: virgin post: need help with external pull up resistor

Fri Jan 17, 2020 4:19 pm

wow! i cant wait to get home and try it! thanks azillion @pcmanbob!

njh
Posts: 55
Joined: Wed Aug 09, 2017 2:07 pm

Re: virgin post: need help with external pull up resistor

Fri Jan 17, 2020 5:04 pm

[Edited] With apologies to @pcmanbob I would tweak that circuit before connecting it to the Pi.

A "traditional" 741 won't necessarily work properly with a single 5V supply. Instead you may need a low-voltage op-amp / comparator part (sorry I don't know any part numbers to recommend without looking them up).

That potentiometer might not prevent an over-voltage from reaching the Raspberry Pi! It may be preferable to have a resistor voltage divider between the output and Gnd.

Also the 100uF capacitor seems to be in the wrong place. It may be preferable to have a small capacitor between each comparator input and Gnd. By all means put a larger capacitor across the 5V supply.
Last edited by njh on Fri Jan 17, 2020 5:37 pm, edited 1 time in total.

pcmanbob
Posts: 7959
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: virgin post: need help with external pull up resistor

Fri Jan 17, 2020 5:36 pm

Well I have a working example of this circuit connected to my piB+ that's how I got the results I posted.

The max output possible on the output of the LM741 is 5v so the potential divider set as suggested will only deliver 1.72V with a 3.8V volt output from the LM741, so even if it went up to 5v you should still only see about 2.73V. ( I tested it )

The capacitor is there because when powered by my bench power supply you get a nice smooth output , once you power it from the 5v pin of the pi gpio header you get a 50hz 1.36v ripple on the output , the capacitor smooths this to just 408mV so the pi will see a stable input.

and yes I an using an official raspberry pi PSU.


EDIT....
putting a capacitor on each input stopped the circuit from working and I tried adding a capacitor across the supply it made no difference.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

njh
Posts: 55
Joined: Wed Aug 09, 2017 2:07 pm

Re: virgin post: need help with external pull up resistor

Fri Jan 17, 2020 5:52 pm

pcmanbob wrote:
Fri Jan 17, 2020 5:36 pm
Well I have a working example of this circuit connected to my piB+ that's how I got the results I posted.

The max output possible on the output of the LM741 is 5v so the potential divider set as suggested will only deliver 1.72V with a 3.8V volt output from the LM741, so even if it went up to 5v you should still only see about 2.73V. ( I tested it )

The capacitor is there because when powered by my bench power supply you get a nice smooth output , once you power it from the 5v pin of the pi gpio header you get a 50hz 1.36v ripple on the output , the capacitor smooths this to just 408mV so the pi will see a stable input.

and yes I an using an official raspberry pi PSU.

EDIT....
putting a capacitor on each input stopped the circuit from working and I tried adding a capacitor across the supply it made no difference.
Interesting. Your diagram is mis-drawn as it shows the potentiometer dividing between the max 5V output and the 5V rail. An obvious silly, but if the OP lacks EE training, it seemed worth pointing out before they blew up their Pi.
The capacitor shouldn't be needed. I wonder if the instability is related to the fact the LM741 is a bit under-voltage?

pcmanbob
Posts: 7959
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: virgin post: need help with external pull up resistor

Fri Jan 17, 2020 6:40 pm

njh wrote:
Fri Jan 17, 2020 5:52 pm


Interesting. Your diagram is mis-drawn as it shows the potentiometer dividing between the max 5V output and the 5V rail. An obvious silly, but if the OP lacks EE training, it seemed worth pointing out before they blew up their Pi.
The capacitor shouldn't be needed. I wonder if the instability is related to the fact the LM741 is a bit under-voltage?
well spotted , circuit is actually connected correctly.

Have updated diagram.

may be but its strange that its 50Hz
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

JohnsUPS
Posts: 143
Joined: Fri Jul 06, 2018 2:13 am
Location: USA

Re: virgin post: need help with external pull up resistor

Sat Jan 18, 2020 12:36 am

It appears as though you're using the 741 op amp as a comparator. Instead, I'd opt for an LM393. It probably has a higher input impedance, and is designed to work with single or dual supplies.
Also, adding hysteresis, if needed/desired, would be easier.

drgeoff
Posts: 10220
Joined: Wed Jan 25, 2012 6:39 pm

Re: virgin post: need help with external pull up resistor

Sat Jan 18, 2020 9:07 am

JohnsUPS wrote:
Sat Jan 18, 2020 12:36 am
It appears as though you're using the 741 op amp as a comparator. Instead, I'd opt for an LM393. It probably has a higher input impedance, and is designed to work with single or dual supplies.
Also, adding hysteresis, if needed/desired, would be easier.
+1

A 741 has bipolar input ransistors which need significant input bias current compared to the current in the sensing side of the bridge. A FET alternative such as TL071, LF351 etc might give more stability wrt temperature etc.

pcmanbob
Posts: 7959
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: virgin post: need help with external pull up resistor

Sat Jan 18, 2020 10:35 am

Yes using alternative op-amps might be better but it was a case of what I had in the bits box to see if it was even possible.

I still don't know if it will actually work with the sensor in question , awaiting response from OP.

Gone are the days were you could nip to the local electronics parts shop , if that was not the case I might have tried a different op-amp.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Automation, sensing and robotics”