Explorer phat inputs not working

I am trying to get python to read when I press a button with my explorer phat.
the code I am using is as follows

import explorerhat as eh

while True:
print(state)

and I have wired up my button so one leg is going to ground and the other to input one

when I run the code 0’s fill up the screen but when I press the button the 0’s continue

Any help would be much appreciated

What you’re quite possibly missing is the fabled pull-up resistor, or some way of giving your input a “normal” state.

What I think is happening is this ( excuse my use of probably horribly incorrect use of the word charge! ):

• Input is 1 because it’s “floating” and happens to have just enough charge to read high
• You press the button and ground input, removing all charge
• When the button is released, there’s no path to return the input back to high so it stays low

You need a very weak ( 10k is a popular choice ) resistor which connects between your button and the input, and to +5v. This is called a “Pull Up” resistor. It does just that- very weakly pulls the line back up to 5v when it’s no longer grounded.

When your button is pressed, the pull up resistor is too weak to raise the line to high, current just flows through and straight to ground via the path of least resistance.

The Pi has built in pull ups that usually solve this problem for you, but since Explorer HAT has an input buffer and the pull ups are on the other side of that buffer, they don’t work in this case.

Here’s a random, but applicable diagram I borrowed from the internet:

Update: when I run the code instead of the zeroes we had before we get 1’s but when I press the buttons the 1’s still continue so it is the same as before but with 1’s instead of 0’s

Hi

What happens if you short the button out as it may be faulty. The circuit shown will keep the input high which is why you see 1 until the switch is closed. If the switch isn’t working then it will remain at 1.

Can you add a picture of the wiring to the switch? How many connections does it have?

Cheers

Paul

Thanks for the idea of a faulty button gave a new one a go and it worked perfectly

cheers for the help everyone!

William