The Pico/W In C: GPIO Input
Written by Harry Fairhead   
Monday, 08 July 2024
Article Index
The Pico/W In C: GPIO Input
Basic Input Functions
Press Or Hold
How Fast Can We Measure?

Press Or Hold

You can carry on elaborating on how to respond to a button. For example, most users have grown accustomed to the idea that holding a button down for a longer time than a press makes something different happen. To distinguish between a press and a hold all you need to do is time the difference between line down and line up:

#include "pico/stdlib.h"
int main()
gpio_set_function(21, GPIO_FUNC_SIO); gpio_set_dir(21, false); gpio_pull_down(21);
gpio_set_function(22, GPIO_FUNC_SIO); gpio_set_dir(22, true); gpio_put(22, 0);
uint64_t t;
while (true) { while (gpio_get(21) == 0){}; t = time_us_64(); sleep_ms(1); while (gpio_get(21) == 1){}; t = (uint64_t)(time_us_64() - t); if (t < 2000*1000) { gpio_put(22, 1); sleep_ms(1000); gpio_put(22, 0); }
else {
for (int i = 0; i < 10; i++) { gpio_put(22, 1); sleep_ms(100); gpio_put(22, 0); sleep_ms(100); } } } }

In this case holding the button for 2s registers a “held” – the LED flashes 10 times and anything less is a “push” – the LED flashes just once. Notice the 1ms debounce pause between the test for no-press and press.

One of the problems with all of these sample programs is that they wait for the button to be pressed or held and this makes it difficult for them to do anything else. You have to include whatever else your program needs to do within the loop that waits for the button – the polling loop. You can do this in an ad hoc way, but the best approach is to implement a finite state machine, see later.

Serial Data

In the next program we need to gather some data and the simplest way of doing this is to use a serial connection between the Pico and the host machine. This topic is covered in detail in Chapter 16. If you have any problems making this work, read the section on setting up the UART. This is the short version and it should “just work”.

The Pico has a default UART connected to pins 1 GP0 TX and pin 2 GP1 RX. You simply need to connect the RX/TX pins to the corresponding TX/RX pins on the host machine. In the case of a machine that doesn’t have a serial port that can work at 3.3V you need an adapter – usually a USB to serial adapter. If you are using a Raspberry Pi 4 as the development system you can simply connect the Pico’s GPIO lines to the Pi’s:

Raspberry Pi

Raspberry Pi Pico

GND (Pin 14)

GND (Pin 3)

GPIO15 (UART_RX0, Pin 10)

GP0 (UART0_TX, Pin 1)

GPIO14 (UART_TX0, Pin 8)

GP1 (UART0_RX, Pin 2)

If you have already connected the Pico and the Pi together via the SWD interface you don’t need to connect the GND as there is already a ground connection. If you are only interested in transmitting data from the Pico you only need to connect Pin 1 and Pin 2.

The hardware setup is simple but you also need to use the:

sudo raspi-config

to set up the serial port on the Pi. Select Interfaces and then disable the Linux shell and enable the serial port. The system will reboot. After this you can use serial0 to communicate with devices connected to the serial port.

To make the connection use:

sudo minicom -b 115200  -D /dev/serial0

Notice that you can do this in a terminal window in VS Code which is the best way to work.

To make sure that the serial interface works try this simple program:

#include <stdio.h>
#include "pico/stdlib.h"

int main() { stdio_init_all();
while (true) { printf("Hello Serial World\n"); stdio_flush (); sleep_ms(1000); } }

Last Updated ( Wednesday, 10 July 2024 )