Python GPIO - Input Pin Push Button Example & Compatibility With Raspberry Pi 5

preview_player
Показать описание
This is a continuation of my guide on using the Raspberry Pi GPIO pins in Python. In a previous video I demonstrated how to use the output pins with a transistor circuit to power LEDs. Today's example shows how to use the input pins with a simple push button example. I also go over the Raspberry Pi 5 and its compatibility with existing libraries. Stay tuned for my next video where I'll show how to interface with sensors including an ultrasonic distance sensor and an IMU.

Purchase a Raspberry Pi on Amazon

Purchase at MicroCenter (Best Deal)

These are affiliate links where I earn a small commission for purchases at no extra cost to you.
This is the easiest way to help the channel, thank you!

Donations

Chapters
00:00 Intro
02:09 Raspberry Pi 5 compatibility
04:35 Voltage divider circuit for the push button
07:08 Explaining the code
09:46 Running the code and other potential uses
12:57 Outro
Рекомендации по теме
Комментарии
Автор

I didn't notice the "libraries" typo until after I uploaded the video. I fixed the thumbnail but can't fix it in the video. 🙃

Update: If using the official Raspberry Pi OS then you'll need to install the lgpio library as a system package, not with PIP. I've updated the written guide to include this.

Also something important I forgot to mention is the concept of a "pull up resistor" and a "pull down resistor". In this push button example R4 is acting as a "pull down resistor", meaning it pulls the input down low to ground to guarantee that the pin reads low if the button isn't being pressed. Then when the button is pressed, the voltage divider creates roughly 3 volts on the pin which pulls up the input pin so that it registers as high.

Alternatively, we could have tied the input pin to 3.3V with a "pull up resistor", then wire the button so that it forces the pin down to ground when it's pressed. Then in the code, we would check to see if the pin is low to turn on the LED, instead of checking if the pin is high. This method is the exact opposite of what was done in the video, and both are valid ways to achieve the same results. It's a similar concept as NPN vs PNP transistors which I explained in my Electronics 101 video on transistors.

The input pins also have internal pull up and pull down resistors that can be activated in software, which means R4 isn't necessary. But it's usually better to hard wire your own pull up/down resistor instead of relying on the internal one because it's easy to make a mistake in the code and you might forget.

Hopefully this makes sense, feel free to ask any questions.

PhazerTech
Автор

i am using raspberry pi 5 64 bit os and still not able to acces the gpio bin. even not able to detect a push button

anujpai
Автор

i am getting a error " RuntimeError: Cannot determine SOC peripheral base address" what should I do? please help me

tarunjeetsingh
welcome to shbcf.ru