Reading GPIO inputs
In the previous example, we controlled an LED as an output device. In this example, we will read the state of a button in a loop and print it in the terminal, every time we detect a change.
Start by creating a file named hello-button.py
and add the following python code inside it.
hello-button.py
To run the program, go to the terminal and write:
Now, if you modify the state of Pin 9, you will see the program respond to the new state and print the output in the terminal.
To stop the program press Ctrl+C.
Congratulations! We now know how to read from the GPIO inputs.
Reading from a while (1) loop is generally not a good programming practice and is used here only for demonstration purposes.
Last updated