Pico multiple sensor input

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.

Any suggestions please:

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?

2 Likes

Hi Toni,

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)
1 Like

Thanks Jeff

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)

Is ‘button[b].IN’ the correct option to use?

Thanks Jack,

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.

I’ll send my full code later today.

1 Like

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.

Code as follows:

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)

Hi Toni,

Instead of button[b].IN instead of button[b].value(). The .value() method is used to read the state of a pin.

Make this change and let us know how it goes.

Thanks Jack,

I have made the change to .value==1 and the buttons do still not turn on the led.

The print messages with the button pushed are continuous loops as follows for each loop:

nb = 4
button[nb].value not pressed <bound_method>
nb= 5
button[nb].value not pressed <bound_method>

etc, etc

In other words if using a 1 value for the test it still jumps to the else part of the loop no matter which button is pushed.

When a 0 is used for test value the exact same result occurs.

Is the <bound_method> statement significant?

This line

button[b] = Pin(buttonPin[l],Pin.IN,Pin.PULL_UP)

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).

2 Likes

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.

1 Like

Thanks Jeff,

Sorry you were correct about the index issue.

Using:

    if Pin.value[(b)]==1:
        print("button[b] pressed ",button[b].value())
        led[b].value(1)

as I thought you were suggesting brings up the error message for the ‘if’ line as:

Type error: ‘function’ object isn’t subscriptable

Did I misunderstand you?

The brackets need adjusting here

  if button[b].value() == 1:

Great, almost there

I really misunderstood the pin object constructors!!

it runs OK but with buttons not pressed all leds light up, then when a button is pressed, that one goes out. I think I can sort that out.

Thanks for your patience and yes I will read the appropriate MycroPython Pin library document again with a bit more background!

1 Like