From the course: Raspberry Pi: GPIO

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Program the LED and switch with Python

Program the LED and switch with Python

From the course: Raspberry Pi: GPIO

Start my 1-month free trial

Program the LED and switch with Python

- [Instructor] Without software, the Raspberry Pi is an inert lump of plastic and metal. So let's write some very simple software to watch the switch and change the LED accordingly. I'm writing this software using the Thonny editor that is provided with Raspbien, and I'll write the code using Python. First I'll use the gpiozero module to connect to the LED in switch. And I need the pause command from the signal module. The gpiozero library allows me to create an LED object, I'll call it myLED. LED26 indicates that I want to use GPIO 26 to control the LED. Then I'll do the same for the switch, I'll create an object called mySwitch and assign it to a button on GPIO2. When those objects are created, I can test the LED, I'll use myLED.blink. Before you can run this code, add a pause at the bottom of the code. This keeps the code from terminating before the LED has a chance to blink. When I run this, I'll need to save it,…

Contents