From the course: Raspberry Pi Essential Training
Unlock this course with a free trial
Join today to access over 24,900 courses taught by industry experts.
Respond to a button press in Python
From the course: Raspberry Pi Essential Training
Respond to a button press in Python
- [Instructor] The GPIO header lets you read values from components. I'll show you how to determine whether a button, which is perhaps the simplest input component, has been pressed. You'll need to have your button wired up to pin 3. That's GPIO 2. The video Respond to a Button Press explains how you do that, and remember to power down your Raspberry Pi before you connect anything to that GPIO header. If Thonny's not running already, launch it now. If needed, click the New button to create a new program. I'll be using the same GPIO Zero library I used in the previous video. So this time let's import its Button submodule. Next, call the button constructor with the number of the logical GPIO pin you're using, which is 2, and assign it to a variable named Button. Let's set up a simple function called hi. Its job is to print out Hello, World. Next, assign that function to the button.when-pressed property. When used in that way, the Hi function is referred to as a handler because it…