The full Raspberry Pi Workshop in step-by-step format can be found here http://coreelec.io/piworkshop GPIO or General Purpose Input and Output is the interface that allows your Raspberry Pi to connect to the world.
Now that we’re a little bit familiar with python programming we’re ready to actually connect to the real world. We’re going to use our Pi to control a simple button input LED output circuit which you can find in the reference material for this section. Here I’m using 470 OHM resistors 2 LED’s - one green, one red, the color doesn’t really matter, a button switch and some male to female jumper wires as well as just some general breadboarding wire.
Always remember to power off your Pi when you prototype circuits - one brief short circuit could break your Pi, double check your connections before you power it on. The intention for this circuit is I want to program it such that I can vary the brightness of this LED and I’m just going to blink this one on and off. So here we have the red LED running at near full brightness and when I press and hold the button the green LED will start to blink and the red LED dims and brightens. OK so to achieve that I’m going to create new file from Python and I’m going to immediately save that in the Chapter 2 directory and I’m going to call it 2.2 LED blink and dot py - yep, there it is. OK, so lets get cracking.
The first thing I need to do is the imports so i need to import - and this is a specific package for the Raspberry Pi to control the GPIO. So r Pi dot GPIO and we’re going to use a trick here and say as GPIO. That’s essentially taking in all the functionality of rPi.GPIO and labelling it GPIO. Next we need to import time and this gives us some delays that we can use to actually create that blinking effect. It’s always a good idea at the start of your script to define your PIN definitions - these are label the PIN numbers that have been chosen for your project. For this project the LED that was being pwm’d - that was PIN 18 so the pwmPIN is PIN 18. PWM is that effect that allows you to control the brightness of the LED. The LED that was blinking - I’m just going to call that ledPIN, that is on PIN 23 and the button was on 17. So that means that if you change the circuit down the track but still want to have the original functionality in the code all you have to do is change the definitions for the PINs and everything should run just fine. I also need a constant - a constant duty cycle - that 75% is like saying - with pwm the duty cycle is a number between 0% and 100% that describes how much power is going to be delivered out of that PIN. So 100 is going to be as bright as the LED can be and 0 will be completely off with 50% being of course about half as bright as it can be. So I’m going to make a variable called duty and give that a value of 75%.
Now we need to set up the GPIO - so we do that using the GPIO object that we labelled before and we first need to set the mode. This is essentially setting the GPIO numbering to those that were defined by the manufacturer of the chip. That is to use these numbers we need to use the bcm or broadcom numbering system. Now we need to actually tell these PINS what we want them to do, whether we want them to be inputs, outputs, outputs with pwm so this is where we define that. So we can say GPIO setup and we’re going to set up the LED pin first - so lets set up that GPIO.OUT now we can copy that line and paste it for the pwm pin because it is still going to be an output. While we’re doing that I may as well save myself the typing and paste again but change pwm pin to button pin. Now we need to set this to GPIO IN because we’re using this buttons as an input. When we use buttons as inputs without any other supporting hardware - we can use buttons as inputs by activating what’s called an internal pull up resistor. If you don’t know what that is I’ve included a link to a tutorial in the resources. But for now, suffice to say that if you are going to use a button input to your Raspberry Pi 3 this is going to be how you are going to set it up. As long as you are using the same schematic. So thats pull up down equals GPIO dot pull up down up. So all of that is just to activate the internal pull up resistor which will allow us to use our button.
So, next we need to actually initialise the pwm channel - we’ve set up the pwm pin as an output but we haven’t actually attached it with any pwm properties yet. So we’re going to create an object called pwm and invoke GPIO dot pwm to attach it to pwm functionality. We’re going to choose the pwm pin and this next argument is just a number - that is the frequency that we want pwm to run at. Sorry, I didn’t quite cover it but every time we invoke something that has these brackets, what goes in between the brackets is essentially the input. So if GPIO.setmode is a function or a method then we’re giving it some information with what we’re putting into these brackets. So we can see that we can parse in multiple pieces of information or arguments so we could parse in any number as long as the function is set up to receive that number. So here we’re parsing 1,2,3 arguments and here we’re parsing in 2 . This one with 200 being the frequency in hertz that we want the pwm channel to run at.7.19