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?

How Fast Can We Measure?

Buttons are one form of input, but often we want to read data from a GPIO line driven by an electronic device and decode the data. This usually means measuring the width of the pulses and this raises the question of how fast can we accept input?

The simplest way to find out how quickly we can take a measurement is to perform a pulse width measurement. Applying a square wave to GP22 we can measure the time that the pulse is high using:

#include <stdio.h>
#include "pico/stdlib.h"
int main()
{ stdio_init_all(); gpio_set_function(22, GPIO_FUNC_SIO); gpio_set_dir(22, false); uint64_t t;
while (true) { while (gpio_get(22) == 1) {}; while (gpio_get(22) == 0) {}; t = time_us_64(); while (gpio_get(22) == 1) {}; t = (uint64_t)(time_us_64() - t); printf("%llu\n", t); sleep_ms(1000); }
}

This might look a little strange at first. The inner while loops are responsible for getting us to the right point in the waveform. First we loop until the line goes low, then we loop until it goes high again and finally measure how long before it goes low. You might think that we simply have to wait for it to go high and then measure how long till it goes low, but this misses the possibility that the signal might be part way through a high period when we first measure it. This can be measured down to around 1 microsecond with an accuracy of 0.5 µs with the accuracy poor at the low end of the range.

Notice that in either case if you try measuring pulse widths much shorter than the lower limit that works, you will get results that look like longer pulses are being applied. The reason is simply that the Pico will miss the first transition to zero but will detect a second or third or later transition. This is the digital equivalent of the aliasing effect found in the Fourier Transform or general signal processing. 

In chapter but not in this extract

  • The Finite State Machine
  • FSM Button
  • FSM Hold Button
  • FSM Ring Counter

Summary

  • Input is hard because things happen at any time, irrespective of what your program might be doing.

  • You can call gpio_get at any time to discover the state of a GPIO line – the problem is when and how often to call it.

  • You can chose between external or internal pull-up/pull-down resistors.

  • Mechanical input devices such as buttons have to be debounced.

  • The power of software is that it can enhance a simple device. A simple button is either pushed or released, but you can use this to generate a third “held” state.

  • Using a polling loop you can handle inputs as short as a few tens of microseconds.

  • Most IoT programs are best written as a polling loop.

  • The Finite State Machine (FSM) is one way of organizing a complex polling loop so that inputs are tested and outputs are set once for each time through the loop.

  • Ideally the states of a FSM should not be simple statements of the inputs of outputs that determine the state, but for simple systems this can be difficult to achieve.

  • It can be difficult to work out what constitutes the events of a FSM. Ideally they should be localized in time so that they indicate the moment that something happens.

Programming the Raspberry Pi Pico In C
Third Edition

By Harry Fairhead

PicoC3E360

Buy from Amazon.

Contents

  • Preface
  • Chapter 1 The Raspberry Pi Pico – Before We Begin
  • Chapter 2 Getting Started
  • Chapter 3 Using GPIO Lines
  • Chapter 4 Some Electronics
       Extract: Erratum E9 Pull Down Problems ****NEW!
  • Chapter 5 Simple Input
        Extract:   GPIO Input ***
  • Chapter 6 Advanced Input – Events and Interrupts
  • Chapter 7 Pulse Width Modulation
        Extract: Basic PWM ***
  • Chapter 8 Controlling Motors And Servos
  • Chapter 9 Getting Started With The SPI Bus
  • Chapter 10 A-To-D and The SPI Bus
  • Chapter 11 Using The I2C Bus
  • Chapter 12 Using The PIO
        Extract: A 1-Wire PIO Program  ***
  • Chapter 13 The DHT22 Sensor Implementing A Custom Protocol
  • Chapter 14 The 1‑Wire Bus And The DS1820
  • Chapter 15 The Serial Port
  • Chapter 16 Using the Pico W
       Extract: Simple Web Client ***
       Extract:A Better Connect  ***
  • Chapter 17 The Pico/W In C: Direct To Hardware *
  • Chapter 18 Multicore and FreeRTOS

Extra: Adding WiFi To The Pico 2

*** Extracts from Edition 2 not yet updated.

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Stack Overflow Reveals Trends In AI Adoption
06/08/2025

While developers are using AI tools they are by no means satisfied with them. While LLMs are widely used, fewer than half of developers are making use of AI agents. And Vibe Coding is ruled out b [ ... ]



Google's Gen AI Skillup
29/07/2025

Gen AI Skillup is an educational initiative by Google to enable anyone to learn how to use Generative AI.


More News

pico book

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Wednesday, 10 July 2024 )