I need a pico to respond to a number of individual push buttons sensors .
The buttons are created and coded as ‘button[b]’ sensors using a ‘b’ loop function.
To read these sensors I have tried the following options:
if button(b)==1:
if button[b]==1:
if button[(b)]==1:
if button([b])==1:
if button[b].IN==1:
if button[b].IN==1:
if button[b].IN==1:
if button[b].IN==1:
if button[b],value==1:
if button[b].value==1:
if button[b].value==1:
if button[b].value==1:
None of these seem to respond to the physical button action although I have tested the buttons individually with the classic ‘led[b].value(1)’ code and with a multimeter.
There are two important bits missing from the code fragment you have posted.
How are you setting the ‘b’ value when you do the button testing? Typically, it would be a loop where b runs from 0 up to the number of buttons (less 1) that you created. That way there is no need to guess what the button numbering is.
How are you testing whether or not the button press was recorded? Typically you would print a debug message or flash a LED or something when the test expression is true.
The problem is likely in either of these two areas, although it could also be in the button creation area code.
If you show the full code you are using someone may be able to spot the problem.
Are the second and third options a typo? Which of the four expressions in the first option doesn’t give you a compile-time error?
It would be great if you could share the rest of your code and a bit more of an overview of what you’re setting out to achieve. With a bigger picture, we can be much better help.
If you haven’t seen it already, we have a Raspberry Pi Pico Workshop for free on our site. Chapters 2.3 to 2.5 in particular will show you how to wire and use a button with your Pico.
We’ve used the following as a demonstration.
from machine import Pin
import time
# set up pin 15 as an input with a pull-down resistor
button = Pin(15, Pin.IN, Pin.PULL_DOWN)
# set up pin 16 connected to the LED as an output
led = Pin(16, Pin.OUT)
while True:
# if the button is pressed, then turn on the LED
if button.value() == 1:
led.value(1)
# if its not pressed, then turn the LED off
else:
led.value(0)
The ‘b’ values are set, as you say, by running a loop, both in the initial button creation as well as in the code execution.
The testing of the button operation is by the activation of a led corresponding to that button. This does not happen irrespective of the test being with the button search being set to 0 or 1.
The options are not a typo. My research found various possible ways of nominating the button being tested so I have tried all of them.
The two which gave a ‘0’ response were ‘button[b].IN==’, and ‘button[(b)].IN==’.
A simple print command for all of the others options gives a responses of either ‘non-callable’, ‘bound method’, or a list of that button’s definitions.
The relevant piece of code is:
while(True):
for b in range(7):
if button[b].IN==1
led[b].value(1)
Yes I have seen the video etc but that is all set for a single button operation which works perfectly but my problem has arisen where multiple buttons are involved.
The reason that I asked about the typo is that you have four identical statement is in the second option and 3 identical in the third option. So it wasn’t clear what you were trying to test.
The change from the single button example shown above to multiple buttons will depend on how you have created the references to the multiple buttons. The code you have posted suggests a list, but it’s possible that assumption is the cause of the problem.
from machine import Pin
ledPin=[9,10,11,12,13,14,15]
led=[0,1,2,3,4,5,6]
currentLedState=[0,0,0,1,0,0,0]
buttonPin=[2,3,4,5,6,7,8]
button=[0,1,2,3,4,5,6]
currentButtonState=[0,0,0,1,0,0,0]
number_elements=7
for b in range(number_elements):
led[b] = Pin(ledPin[b],Pin.OUT)
button[b] = Pin(buttonPin[l],Pin.IN,Pin.PULL_UP)
while(True):
for b in range(number_elements):
print("b =",b)
if button[b].IN==1:
print("button[b].IN pressed ",button[b].IN)
led[b].value(1)
else:
print("button[b].IN not pressed ",button[b].IN)
led[b].value(0)
can’t be right. I’m sure that the index is intended to be b.
The ‘button’ variable is type Pin. The syntax for returning the value of a pin is
> Pin.value([x])
which in your case would be
> print("button[b] not pressed ", button[b].value()
Is that the code you are using? (This is NOT tested!)
The message you are seeing indicates that you are printing the button object: that would be the ‘bound method’ part of the print. Then, you are printing the button list number, not the button object value (assuming you are pressing the buttons connected to pins 6 and 7).
It may be worth downscaling and starting with 2 buttons to test. Can you shoot through your current code and some images of your circuit so we have something to use as reference?
Here is some untested (but mostly copy paste) code that integrates 2 buttons.
from machine import Pin
import time
# set up pin 15 and 17 as inputs with a pull-down resistor
button1 = Pin(15, Pin.IN, Pin.PULL_DOWN)
button2 = Pin(17, Pin.IN, Pin.PULL_DOWN)
# set up pin 16 and 18 connected to the LEDs as an output
led1 = Pin(16, Pin.OUT)
led2 = Pin(18, Pin.OUT)
while True:
# if the first button is pressed, then turn on the first LED
if button1.value() == 1:
led1.value(1)
# if its not pressed, then turn the first LED off
else:
led1.value(0)
# if the second button is pressed, then turn on the second LED
if button2.value() == 1:
led2.value(1)
# if its not pressed, then turn the second LED off
else:
led2.value(0)
If this one works we can start tackling putting button and LED assignments into a matrix if that’s required for this project.