skibum
Posts: 16
Joined: Fri Jan 27, 2017 1:45 pm

Pi Zero W gpio pins fluctuating

Thu Nov 28, 2019 10:51 pm

I was having issues getting a rotatry encoder working and no matter what I tried, nothing worked.

So, I resorted to a simple loop around reading the status of the pin. To my surprise, it was just randomly switching between high and low. So, I picked another pin. Same thing. Tried another. Same thing.

So I unplugged everything, so just power now and still the pins are fluctuating :-

Code: Select all

Port 16 is 0/LOW/False
Port 16 is 0/LOW/False
Port 16 is 0/LOW/False
Port 16 is 0/LOW/False
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 1/HIGH/True
Port 16 is 0/LOW/False
Port 16 is 0/LOW/False
Port 16 is 0/LOW/False
Port 16 is 0/LOW/False
Port 16 is 0/LOW/False
and on a different pin :-

Code: Select all

Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
I swapped SD card to see if the problem was somehow with raspbian, but it stayed with the Pi Zero W.

So, have I killed my Pi Zero W's somehow? Although as long as I'm not using the pins it seems fine.

Another Pi Zero W works perfectly as well. So it seemingly is just this one.

User avatar
davidcoton
Posts: 5513
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: Pi Zero W gpio pins fluctuating

Thu Nov 28, 2019 10:59 pm

Post your test code, maybe you are doing something wrong there?
Also, a picture of your wiring to the encoder, but the test program needs to work first.
Location: 345th cell on the right of the 210th row of L2 cache

skibum
Posts: 16
Joined: Fri Jan 27, 2017 1:45 pm

Re: Pi Zero W gpio pins fluctuating

Thu Nov 28, 2019 11:09 pm

The encoder also works connected to another Pi.

The test code is very simple which generated the output. Another pi zero it always displayed 'low' which I expected with nothing plugged in. The 'problem' pi zero with nothing connected to it, gave the earlier output. So its not the wiring as there isn't any.

Code: Select all

import RPi.GPIO as GPIO
from time import sleep     # this lets us have a time delay (see line 15)
GPIO.setmode(GPIO.BCM)     # set up BCM GPIO numbering
GPIO.setup(24, GPIO.IN)    # set GPIO25 as input (button)

try:
    while True:            # this will carry on until you hit CTRL+C
        if GPIO.input(24): # if port 25 == 1
            print "Port 24 is 1/HIGH/True"
        else:
            print "Port 24 is 0/LOW/False"
        sleep(0.1)         # wait 0.1 seconds

finally:                   # this block will run no matter how the try block exits
    GPIO.cleanup()         # clean up after yourself

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

Re: Pi Zero W gpio pins fluctuating

Thu Nov 28, 2019 11:24 pm

If you have nothing connected to the gpio that's being used as an input, then the pin is said to be floating.

If a pin is floating then it can randomly change from high to low all on its own.

As you have not set a pull up or pull down in software you will need to do it using external resistors connected to your encoder output.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
Burngate
Posts: 6374
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Pi Zero W gpio pins fluctuating

Fri Nov 29, 2019 10:10 am

I'm not sure that not setting a pull-up or pull-down would result in the GPIO floating - on power-up, the first 9 GPIOs have pull-up set, with the rest having pull-down, so unless anything else has changed that the GPIO should always have a pull of some sort.
skibum wrote:
Thu Nov 28, 2019 10:51 pm
Another Pi Zero W works perfectly as well. So it seemingly is just this one.
If only this Pi gives this result, then it's something you did to this Pi; probably something to do with the wiring of your encoder (though quite what, I can't imagine!)

Is it just the pins you used for your encoder, or do all the pins do this - there are lots to choose from.

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

Re: Pi Zero W gpio pins fluctuating

Fri Nov 29, 2019 10:37 am

Burngate two points ,

OP has not stated which OS is being used , yes buster sets pull ups/downs but I don't think previous versions of raspbian did.

Second he stated the pins did it with or without the rotary encoder attached, but has not stated if they were using external resistors for pull up/down , gpio pins fluctuating from high to low when set to input is a common sign of a floating pin.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
Burngate
Posts: 6374
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Pi Zero W gpio pins fluctuating

Fri Nov 29, 2019 11:11 am

Sure, ... but AIUI, those pulls are set before any OS is booted, so unless the OS changes things or some software is run to do so, those pulls are still active.

And from the way he wrote it, I understand another pristine Pi (that hasn't been connected to his rotary encoder) doesn't exhibit this problem.

If his second Pi also showed the problem without being connected to the encoder, or it didn't show the problem even though it had been connected to it, then I'm trying to shoot fish in an empty barrel.

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

Re: Pi Zero W gpio pins fluctuating

Fri Nov 29, 2019 11:19 am

I think with out knowing how the rotary encoder was connected, so we can judge if any damage might have been done to the gpio, we are operating in the dark some what,

but this seems to be the norm as users rarely give use the full facts until we have asked for them several times.

Edit.............

Just run the program posted by the OP on a pi2 using raspbian stretch lite.

the results with the gpio pin not connected to anything

Port 24 is 0/LOW/False
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False

same as the OP

with an external resistor ( 1K ) connected to gpio pin and ground

Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 0/LOW/False


As expected.

so using a pi2 and stretch I guess there are no pull ups/downs pre-set on boot or by software.....

So we need to know which which OS OP is using.

edit 2.......

Tested same code on pi4 and got these results

Port 24 is 0/LOW/False
Port 24 is 0/LOW/False
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True
Port 24 is 1/HIGH/True

so still some fluctuation on the gpio pin....

Edit3

running buster on the pi2 produces the same results as when using stretch so it looks to be hardware dependent as to whether the gpio are pull up/down by default
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
Burngate
Posts: 6374
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Pi Zero W gpio pins fluctuating

Fri Nov 29, 2019 11:53 am

Interesting. So the fish seem to be in a different barrel.

You've proved that the OS, be-it Stretch on a Pi 2 or Buster on a Pi 4, does change the pulls. Why it should do so is a mystery to me, but then many things fall into that class.

I spend most of my time on RISC OS, which almost certainly does things differently, and anyway I either set the pulls I want as a matter of course, and/or provide external pulls.

It might be interesting to expressly set a pull-up in the line

Code: Select all

GPIO.setup(24, GPIO.IN)
and see what happens - but Tesco's is calling

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

Re: Pi Zero W gpio pins fluctuating

Fri Nov 29, 2019 12:37 pm

Burngate wrote:
Fri Nov 29, 2019 11:53 am
It might be interesting to expressly set a pull-up in the line

Code: Select all

GPIO.setup(24, GPIO.IN)
and see what happens - but Tesco's is calling
So I ran the tests again using software set pull down and pull up.

So on pi2 with stretch

software pull down resulted in all " Port 24 is 0/LOW/False "

and software pull up resulted in all " Port 24 is 1/HIGH/True "

which is exactly what I expected,

the same tests run using buster produce the same results again as expected.

the pi4 running buster

produces exactly the same results .


I don't have a pi zero free at the moment my only one is install in the garage roof so not easy to access to change SD cards so cant test on a zero.

hope you enjoyed your trip to Tesco's....
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
Burngate
Posts: 6374
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Pi Zero W gpio pins fluctuating

Fri Nov 29, 2019 7:07 pm

Driving to Tesco, (actually being driven by my daughter), I had a thought (that worries her - she thinks I'm maybe having a funny turn. I tell her smoke only comes out of my ears when I'm thinking, but she won't believe me)

Does Python 2 or 3 disable any pulls unless told not to?
But another Pi zero apparently works okay.

I'm flummoxed.

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

Re: Pi Zero W gpio pins fluctuating

Fri Nov 29, 2019 7:20 pm

Burngate wrote:
Fri Nov 29, 2019 7:07 pm
Driving to Tesco, (actually being driven by my daughter), I had a thought (that worries her - she thinks I'm maybe having a funny turn. I tell her smoke only comes out of my ears when I'm thinking, but she won't believe me)
:lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol:
Does Python 2 or 3 disable any pulls unless told not to?
But another Pi zero apparently works okay.

I'm flummoxed.
good question if I get time tomorrow I will investigate.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 5922
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: Pi Zero W gpio pins fluctuating

Fri Nov 29, 2019 10:27 pm

I'm not sure what the "default" situation w.r.t. to pullups is (or whether it's changed with various O.S releases) and I'm not a Python programmer so can't comment if that makes a difference or not but since Mar. 2018 the boot-time GPIO pin configuration can be changed by commands in config.txt see @PhilE's Announcement: https://www.raspberrypi.org/forums/view ... 7&t=208748
Trev.
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, 2xB2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, and a A+) but Buster on the P4B's & P400. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

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

Re: Pi Zero W gpio pins fluctuating

Sat Nov 30, 2019 11:29 am

Burngate wrote:
Fri Nov 29, 2019 7:07 pm
Does Python 2 or 3 disable any pulls unless told not to?
Tested using pi2 running buster lite

Ran the same code with the software pull down once then edited code to remove software pull down and ran again.

As expected with software pull down gpio pin remained low, even if I toughed the gpio pins with my finger.
but when the code was run again without the software pull down , the gpio pin remained low until I touched it with my finger , it then started floating between high/low.

tested on both python 2 & 3

so if you remove the software pull down then python removes the pull down on the pi when the program is run, even if it was set previously.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
joan
Posts: 15122
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pi Zero W gpio pins fluctuating

Sat Nov 30, 2019 11:44 am

The default pulls are set by the GPIO "firmware" at power-up.

User avatar
joan
Posts: 15122
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pi Zero W gpio pins fluctuating

Sat Nov 30, 2019 11:49 am

GPIO.cleanup.
"At the end any program, it is good practice to clean up any resources you might have used. This is no different with RPi.GPIO. By returning all channels you have used back to inputs with no pull up/down, you can avoid accidental damage to your RPi by shorting out the pins. Note that this will only clean up GPIO channels that your script has used. Note that GPIO.cleanup() also clears the pin numbering system in use."

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

Re: Pi Zero W gpio pins fluctuating

Sat Nov 30, 2019 12:00 pm

Hi Joan

if you read all the tread , you will see that the OP was suffering with gpio pin 24 on a pi zero fluctuating between high and low with no pull up/down set.

I said this was normal for pi models , up until the pi 4 came in to being.

my test proved that when using a pi2 even with buster if you did not set a pull up/down in software or use an external resistor, the gpio pin in question floated.

So as far as I can see only the pi4 has default pull up/down set on gpio.

I think Burngate was just querying if a previously set pull up/down could affect the pin at a later date if no clean up was used , which I proved it would not.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
joan
Posts: 15122
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pi Zero W gpio pins fluctuating

Sat Nov 30, 2019 12:26 pm

Most GPIO have pulls applied at power-up as defined on page 102 of the Broadcom peripheral spec. Those pulls apply to all models other than the Pi4B. I don't know if the pull defaults have changed for the Pi4B. The floating values seen by the OP could be explained by the GPIO.cleanup routine clearing the pulls on the GPIO being used.

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

Re: Pi Zero W gpio pins fluctuating

Sat Nov 30, 2019 12:50 pm

Well when I tested it on my pi2 using both buster and stretch gpio 24 had no default pull up/down set as can be seen by the fact that the gpio 24 pin is floating with nothing connected to it and set to input.

results in my post 29 Nov 2019, 11:19

As far as I was aware you always needed to explicitly set a pull up/down in software or using external resistors to do it, and that is what I have always found and expected.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
davidcoton
Posts: 5513
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: Pi Zero W gpio pins fluctuating

Sat Nov 30, 2019 8:41 pm

Is your evidence not also consistent with the theory that any default pull set at boot is cleared by the Python GPIO startup?
Location: 345th cell on the right of the 210th row of L2 cache

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

Re: Pi Zero W gpio pins fluctuating

Sat Nov 30, 2019 11:35 pm

davidcoton wrote:
Sat Nov 30, 2019 8:41 pm
Is your evidence not also consistent with the theory that any default pull set at boot is cleared by the Python GPIO startup?
That's not what I was trying to prove, the original problem posted by the OP was that on a pi zero with no pull up or down set in software and with nothing connected to gpio 24 set as an input , he was seeing the input randomly change from high to low, I said this is because there was not pull up/down set on pin so it was floating .

from the very beginning of the pi you have always had to explicitly set pull up/down in software or in hardware , things only changed with the arrival of buster and the pi4.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
davidcoton
Posts: 5513
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: Pi Zero W gpio pins fluctuating

Sun Dec 01, 2019 9:38 am

skibum wrote: So, I resorted to a simple loop around reading the status of the pin. To my surprise, it was just randomly switching between high and low.
See also the code posted, as requested.

The OP's testing was done with the Python program. It is my contention that, WHATEVER THE STARTUP DEFAULT, the Python RPi.GPIO startup with no pull specified leaves the input floating. Set a pull, and this problem will go away. The problem with the encoder can then be investigated with the revised test program.
Location: 345th cell on the right of the 210th row of L2 cache

User avatar
Burngate
Posts: 6374
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Pi Zero W gpio pins fluctuating

Sun Dec 01, 2019 9:57 am

Of note is his original statement:
skibum wrote:
Thu Nov 28, 2019 10:51 pm
Another Pi Zero W works perfectly as well. So it seemingly is just this one.
I think we've proved that, for whatever reason, this one is not faulty, but we've not got to the bottom of why the second one is acting differently.

As for how he discovered the difference, we seem to be agreed that his encoder requires pull-ups/downs, but need more info on his wiring

User avatar
Milliways
Posts: 587
Joined: Fri Apr 25, 2014 12:18 am
Location: Sydney, Australia

Re: Pi Zero W gpio pins fluctuating

Tue Dec 03, 2019 5:16 am

The state of pullup on boot is well documented, and can be changed with Device Tree.

The state on exit of some programs may be undefined, but speculating like a bunch of ancient greek philosophers discussing the number of teeth a horse has is unproductive.

I prefer to follow the scientific method; it is simple to measure the voltage on an input pin - simply load with ~100kΩ resistor.

A simple circuit (2 220kΩ resistors in series from 3.3V to Gnd) allows the voltage to be measured and determine the state of pullup.
With no pullup this will be ~1.5V
With a pullup this will be ~2.7V
With a pulldown this will be ~0.5V
(these make allowance for the typical input impedance of a digital multimeter)

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13395
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Pi Zero W gpio pins fluctuating

Tue Dec 03, 2019 7:47 pm

IMHO the internal software controlled pull-ups (pull-downs) are far too weak (about 50 Kilo Ohm) for any use other than to pull-up (down) a completely unconnected GPIO.

The moment you attach a few centimeters of wire to such a GPIO in an electromagnetic "noisy" environment, the wire can act like an antenna and noise can overrule the soft pullup.

In any situation such as this you -should- add external (stronger than 4K7) pull resistors to either GND or 3V3.

Only the GPIO's regularly used for I2C have regular strength (1K6) external pullup's .

Return to “Troubleshooting”