AiyaPK
Posts: 7
Joined: Fri Sep 06, 2019 5:23 pm

gpio button works on pi3 but not pi4... but LEDs on pi4 works?.....

Sun Sep 15, 2019 6:45 pm

trying to get this stupid simple circuit to output the readings I'm expecting.....

Code: Select all

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
buttonA=23
buttonB=24
GPIO.setup(buttonA,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(buttonB,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)

print (GPIO.input(buttonA))
print (GPIO.input(buttonB))
so when I run the code, both print statements give me 0

So my understanding is that setting up the button for GPIO23 wired to ground, I should be using PUD_UP to pull the voltage up to 3.3V. so by default I should get a reading of 1 when the button isn't pressed, and a reading of 0 when the button IS pressed.
Alternatively, GPIO24 wired to the 3.3v should be using PUD_DOWN, and give me a reading of 0 by default, and a reading of 1 when I press the button?...
So I would expect the reading from GPIO24 to be 0.... but how come I don't see 1 for the first print statement?

Just to go further, I actually had if statements coded out after that actually printed the status of the button, those didn't do anything and I'd constantly get a reading of 0 regardless.

Leading me to wonder if my gpio is even working for buttons. I say buttons specifically because I hooked up LED's to these and they seem to work fine.
Sketch1_bb.jpg
Sketch1_bb.jpg (106.6 KiB) Viewed 397 times
Last edited by AiyaPK on Mon Sep 16, 2019 2:49 pm, edited 1 time in total.

asavah
Posts: 364
Joined: Thu Aug 14, 2014 12:49 am

Re: can't get super simple gpio button tutorial to work

Sun Sep 15, 2019 7:01 pm

I don't think you need resistors for push buttons.

User avatar
DougieLawson
Posts: 36540
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: can't get super simple gpio button tutorial to work

Sun Sep 15, 2019 7:09 pm

Try this:

Code: Select all

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
buttonA=23
buttonB=24
GPIO.setup(buttonA,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(buttonB,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)

while True:
  print (GPIO.input(buttonA))
  print (GPIO.input(buttonB))
Having the resistors is OK, that protects things if the GPIO is set as output and the button gets pressed.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
neilgl
Posts: 1181
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: can't get super simple gpio button tutorial to work

Sun Sep 15, 2019 7:39 pm

It looks like your wiring is dodgy (or switches the wrong way round) as if you run your code without anything attached to any GPIO pin, you get:

Code: Select all

python simple.py
1
0
I am running that from a terminal, no IDLE, no THONNY, no GEANY
And @Dougie's idea is a good one to have a while loop looking at the buttons forever (maybe add a short delay in that loop ).

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

Re: can't get super simple gpio button tutorial to work

Sun Sep 15, 2019 7:56 pm

If you are using the small push button switches as shown, the legs on them are often to short to make a good contact with the breadboard terminals.

Try testing your code using a short Dupont cable in place of the switch.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

AiyaPK
Posts: 7
Joined: Fri Sep 06, 2019 5:23 pm

Re: can't get super simple gpio button tutorial to work

Sun Sep 15, 2019 10:33 pm

@dougie
Yes I should probably do a while True loop just for ease of reading. I figured without the loop I should've at least expected the correct response at least once. But I totally should use the loop anyway.

@neilgl
Could you clarify on the 'dodgy' aspect? I have zero experience with circuits and never took it in school (these days I really wish I did though. I'm also curious about the push buttons and how they might be placed wrong. I was under the impression that there's no particular orientation other than making sure the claws point up/down along the rows and that pressing the button connects all 4 legs together.

@pcmanbob
Ooooooo I always wondered about these small push buttons when used with breadboards... so in my circuit I guess I could in theory just short it with wires instead of buttons, thus emulating a 'button down' scenario just to see if I get the correct readings.... I will def try that!

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

Re: can't get super simple gpio button tutorial to work

Sun Sep 15, 2019 10:56 pm

AiyaPK wrote:
Sun Sep 15, 2019 10:33 pm
I'm also curious about the push buttons and how they might be placed wrong. I was under the impression that there's no particular orientation other than making sure the claws point up/down along the rows and that pressing the button connects all 4 legs together.
You are under the wrong impression.

Most of those switches have the 2 legs on one side connected together and the other two legs connected together. Pressing the button joins the two pairs together. When you don't know the correct orientation just use either of the two pairs of diagonally opposite legs and leave the other two diagonal legs not connected to anything.

Get yourself a multimeter. Even the $5 (or less) ones are adequate for testing and troubleshooting RPi circuits. Use a resistance range to identify the switch connections.

Or use a LED and series resistor plus the 3.3 volts from the RPi as a continuity tester.

As hinted above the short length of the legs and the distance that the conducting strips are below the surface of the breadboard can hinder getting a connection between them.

User avatar
neilgl
Posts: 1181
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: can't get super simple gpio button tutorial to work

Mon Sep 16, 2019 11:08 am

Yes what @drgeoff said about the small pushbuttons. And did it give 1,0 with nothing connected (yes)?
For "dodgy" (i.e. not working well) wiring, it could be poor (i.e. loose or damaged) connections inside the breadboard, or a connector on the dupont cable may be loose, or the conductor inside the insulated wire may be broken (that one got me some time ago - it looks ok but it isnt.)

AiyaPK
Posts: 7
Joined: Fri Sep 06, 2019 5:23 pm

Re: gpio button works on pi3 but not pi4... but LEDs on pi4 works?.....

Mon Sep 16, 2019 2:54 pm

ok so I went back and tested my buttons and wiring with the multimeter.
Continuity is there where I would expect it and the button seems to function normally.
So the twist is that my code and the buttons seem to work fine on my pi3B+....
but on the pi4 (which is what I've been using initially for these tests), the buttons don't work...
However, the LED's on those same GPIO pins on my pi4 work fine
I think at this point I've tried the buttons on at least 3-4 different GPIO pins, and it doesn't work. It's almost as if there's either something wrong with my pull_up_down coding, or my pi4 just isn't setup to work with buttons somehow?.......
(I have changed the subject of this thread to better reflect the problem)

User avatar
neilgl
Posts: 1181
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: gpio button works on pi3 but not pi4... but LEDs on pi4 works?.....

Mon Sep 16, 2019 3:27 pm

Ah, what does your code (simple.py) give on the pi3 with nothing attached and the pi4 with nothing attached? Start a terminal and run the script (no Thonny/IDLE...)
on my pi4 I get

Code: Select all

python simple.py
1
0
on my pi3B+ I get

Code: Select all

python simple.py
1
0

AiyaPK
Posts: 7
Joined: Fri Sep 06, 2019 5:23 pm

Re: gpio button works on pi3 but not pi4... but LEDs on pi4 works?.....

Mon Sep 16, 2019 5:53 pm

I got it!!!!
I have everything working now.
I came across this thread while looking into pi4 specific problems with the gpio:
https://www.raspberrypi.org/forums/view ... p?t=244375

All this time I had thought it was my bad wiring and so I didn't realize this was a pi4 specific issue that needed an update.

I ran:

Code: Select all

$ pip3 install --upgrade --pre rpi.gpio
and everything works as normal now!!
Thanks everyone for the help!!

User avatar
neilgl
Posts: 1181
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: gpio button works on pi3 but not pi4... but LEDs on pi4 works?.....

Mon Sep 16, 2019 6:38 pm

Ah, I had foolishly assumed you had done an update / upgrade on the pi4.

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

Re: gpio button works on pi3 but not pi4... but LEDs on pi4 works?.....

Mon Sep 16, 2019 6:47 pm

AiyaPK wrote:
Mon Sep 16, 2019 5:53 pm

I ran:

Code: Select all

$ pip3 install --upgrade --pre rpi.gpio
and everything works as normal now!!
Thanks everyone for the help!!
If this fixed your problem then it suggests you have never run an update on buster since flashing you SD card.

There have been a lot of updates for buster and the pi 4 , so I would suggest you do an update of the OS .

Code: Select all

sudo apt update
sudo apt full-upgrade
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

AiyaPK
Posts: 7
Joined: Fri Sep 06, 2019 5:23 pm

Re: gpio button works on pi3 but not pi4... but LEDs on pi4 works?.....

Mon Sep 16, 2019 8:09 pm

I too thought I had run it before since on that thread it mentioned that sudo apt update/upgrade should cover that...
I had run this multiple times for my pi3
I had only recently acquired this pi4 but in my excitement completely forgot to update and dove right into gpio tutorials haha
my bad my bad... thus earning me the beginners tag ;)

User avatar
neilgl
Posts: 1181
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: gpio button works on pi3 but not pi4... but LEDs on pi4 works?.....

Mon Sep 16, 2019 8:37 pm

Ok no problem.

Return to “Beginners”