Basic button press with Pico and CircuitPython

November 26, 2024, 16:32

introser

Hey, Im new to programming Pis and never did it before. Bought myself a Pico 1 (https://www.amazon.de/dp/B08TZX8X9V?ref=ppx_yo2ov_dt_b_fed_asin_title ). Read some HowTo`s and guides and wanted to start. First thing, very easy. Put in a button and print out the state of the button (True/False). Just be easy peasy, but I cant get it working.... I have the Pico, connected it via USB to my PC. As an IDE I am using Thonny and I installed CircuitPython 9.XX on it., by clicking on the bottom right corner. Selected CircuitPython, selected Pico 1 and pressed install. So far, so good. Everything worked. First test was to turn on the onbuild LED. That worked. Easy Next step was the button. In my head (and in all tutorials), I just had to connect one side of the button to 3.3V OUT, the other side to any GP Pin. So I selected GP13. Then I ran the following code: (check the first pic, else the message is to long) easy code. It prints out the value of the button all the time and it should turn on the LED while pressing. But that does not work... If I press the button, nothing happen. The button.value is still False. Rechecked my wires, everything looks good. My first thought was a bad button. But no, I measured it, it works. I measured the outgoing voltage of the 3.3V Output (Pin 36) and it was 3.3V. Measured the resistance between the 3.3VOUT and the GP13. No connection, then I pressed the button and there was a connection. So my wires are fine. So it has to be either the code or some kind of setting. Since I pretty much copy+pasted the code from here (https://learn.adafruit.com/getting-started-with-raspberry-pi-pico-circuitpython/blinky-and-a-button ), I dont think it is the code. First I thought it is probably some kind of bad connection/wrong circuitPython version/wrong port etc, but since I can control the onboard LED, it has to be the code. Anyone can help?

introser

Here is the code:
import time
import board
import digitalio

button = digitalio.DigitalInOut(board.GP13)
button.switch_to_input(pull=digitalio.Pull.DOWN)

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
led.value = False

while True:
    print(button.value)
    if(button.value):
        led.value = True
    else:
        led.value = False
    time.sleep(0.5) 

thunder07337

If I see this correctly, the pin header is not soldered. Then it cannot work. Sometimes the contact is there, sometimes the contact is not there. The LED is permanently soldered, so it is normal for this to work. But you have to solder the pin header!

introser

Its on a breadboard. Isnt that exactly for that purpose? For testing/playing around, so you dont have to solder anything on? The pin heads are actually connected. If I measure from the "gold area" from 3.3VOUT to GP13, I dont have a connection unless I press the button. Then I have a connection. But my code does not response for that

thunder07337

The bread board is there to avoid having to solder components to the Pi. But simply inserting a pin into the holes is not a reliable connection. It may work, but there is no guarantee! Therefore you should solder the pin header permanently. And you can then plug it into the bread board or plug the cables directly into the Pi. Unfortunately, I can't help you with the code.

oops.se

The holes in the Raspberry Pi Pico is called "Through Hole". The other way to connect components to a PCB is called SMD (Surface Mounted). Both ways to connect components to the PCB relies on soldering. What is Through Hole? Through-hole, or through-hole soldering, is the method by which components are mounted to a PCB using physical leads. These leads are hooked through holes drilled into the circuit board, hence the name. The leads are then soldered to the opposite side of the board to keep the component in place.