Raspberry Pi GPIO - Getting Started with gpiozero

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop we're learning to use the Raspberry Pi GPIO we'll see how the GPIO works and how to easily control devices attached to it using Python and the GPIO 0 library we're cooking up something special today so welcome to the workshop [Music] hello and welcome to the workshop and today we are going to be working with the Raspberry Pi GPIO the general purpose input output bus that's the 40 pin connector that you're going to see on all modern raspberry PI's as well as a number of other single board computers because this GPIO has been unofficially adapted as a standard among many single board computer manufacturers now here in the drôme bot workshop when we get sensors or motors or displays we tend to use an arduino to control them and there are a lot of good reasons for that arduino is are relatively inexpensive they're fairly easy to work with there are a variety of library supporting all sorts of i/o devices and of course they've got all those wonderful little io pins but sometimes the arduino doesn't have enough computational power after all the arduino really has about the computational power of some of the processors we are working with in the late 1970s and sometimes your project needs a bit more intelligence and that's when you can turn to something like a Raspberry Pi or other single board computer so before we start working with the GPIO I wanted to see how the Raspberry Pi and the Arduino stacked up against each other so let's compare the Raspberry Pi to the Arduino the Raspberry Pi is a microcomputer the Arduino is a microcontroller this is the most significant difference between the two platforms the Raspberry Pi uses an operating system on a micro SD card the Arduino uses a bootloader contained on the MCU tip the Raspberry Pi has USB video camera display and audio connections the Arduino has a USB connection the Raspberry Pi has i2c and SPI buses the Arduino also has i2c and SPI buses the Raspberry Pi has a number of digital i/o ports the Arduino has both digital i/o ports have analog input ports the Raspberry Pi runs on 5 volt USB power most arduino is run on 5 volt USB as well as a second power connector that it can accept 8 to 20 DC volts the Raspberry Pi is a commercially patented product the Arduino is an open-source design some models of the Raspberry Pi use up to a 1.5 gigahertz 64-bit quad-core CPU their arduino AVR based microcontrollers use a 16 megahertz 8-bit single core microcontroller unit raspberry PI's have from 512 megabyte to foreign gigabytes of RAM Arduino is have from 2 kilobytes to 8 kilobytes of static RAM the Raspberry Pi has 26 digital i/o ports they arduino x' have 14 to 54 digital i/o pins depending upon which model you're using the Raspberry Pi has no analog inputs the Arduino has 6 to 16 analog inputs again depending upon which model you're using some models of the Raspberry Pi have integrated Bluetooth and Wi-Fi the Arduino boards do not have bluetooth or Wi-Fi some raspberry PI's have an Ethernet connector the Arduino zs-- do not have a built-in Ethernet connector you can expand the functionality of your Raspberry Pi by using hats hardware attached on top boards the Arduino has a similar expansion capability using fields when you get either board you'll need a few other things to get it to work with a Raspberry Pi you'll need a micro SD card a USB or USB C power supply depending upon which board you've got a keyboard a mouse at HDMI monitor and the number of adapter cables as required for your own setup to run the Arduino you will need a USB cable and a computer so which the two will you be using for your next project so now that we've seen the difference between the two different platforms which one makes the most sense for your project well if your project requires some very precision timing or if you need to measure pulse widths to a very high degree of accuracy and they're relatively low frequency then the Arduino probably wins out microcontrollers are generally better at doing things like that than a microcomputer however if you need to add a lot of intelligence to your project or interface it to the Internet then something like a Raspberry Pi or another microcomputer would make a lot more sense sometimes the choice isn't that easy let's say for example you were designing a controller for your sprinkler system and you had a number of devices you needed to control well both of these devices the Arduino and the Raspberry Pi could be good choices for a design like that and if you wanted to add a few sensors for things like soil moisture or to find out if it was raining right now you could add that to both of the platforms as well but if you wanted to add something more advanced let's say the ability to connect the weather forecasts and find out if it's about to rain so that you didn't water your lawn an hour before a huge rainstorm then something like a raspberry pie makes more sense so if we decided to use a Raspberry Pi which pie should you use there are a variety of them to choose from so let's go and take a look at a few of those pies right now alright ladies and gentlemen welcome to the party that we're having with some of the raspberry PI's that live here in the drone bot workshop and since some of these boards haven't been out of their anti-static bags for a while I thought I'd give them each nametags so they could all introduce themselves to each other and mingle a little bit so let's get introduced to a few of these boards now this is the Raspberry Pi to be this board is a few years old about maybe four years old right now maybe close to five years essentially it's still a very valid Raspberry Pi it's got all of the standard connectors as a camera connector over here there's a display connector here this of course is the 40 pin oh that we're going to be working with today it's got a connector for HDMI over here this is a micro USB connector that you use for a power supply to power it now on this side we've got an Ethernet connection I believe this was only 100 megabit Ethernet and some USB 2 connections four of them now this board doesn't have any integrated Wi-Fi or Bluetooth on it and next to that will put your nametag back on so we know who you are this is the Raspberry Pi 3 B+ and it's a looks a lot like the other boards got the same connectors on it however this board also has bluetooth and Wi-Fi on it and so it's also got a faster Ethernet I believe it goes to 300 megabits it isn't full gigabit but it is faster than the 100 megabit Ethernet that the to be had on it then we move up these three and now these are the latest members of their ass berry pie family the pie for B and the only difference between them is the amount of RAM they have this one is 1 gigabyte this has 2 gigabytes and this has 4 otherwise they look the same they look a bit different than the other boards do in terms of connectors they got the same GPIO connection and the same audio connection the same camera and display connections but it uses a USB see for the power and it uses two micro HDMI outputs here because this can handle 2 4k video outputs these are 10 1080 these ones here and so it uses these micro HDMI connectors it also has on the USB to the port's or USB 3 now and this is a full Gigabit Ethernet and of course it also has Wi-Fi and as I said this one comes with three different amounts of memory now on the other end of the scale we have the Raspberry Pi 0 this is the amazing little five dollar computer quote-unquote and as you can see it's pretty basic it has a GPIO that we're going to be working with today but it doesn't come with a connector soldered on although there is one version called the zero w8 and wh literally means wireless and a header that does have the connector on it otherwise you can solder one on yourself or just solder to the individual pins that you need so it's kind of versatile as you can see it's very small it's got a mini HDMI connector so not to be confused with the micro connector this one is using it's a mini HDMI output it's got two micro USB jacks one of these is for power and the other one is a USB connection it's your only connection into the board and it's got a camera connection now it's got the 0 and the 0 W and the big difference is the 0 W cost twice as much this is a ten dollar computer but for the extra five bucks you get integrated Bluetooth and Wi-Fi so it's a more useful one and it is actually the model that we're going to be working with today and so there you have it some members of their aspect I family having a little get-together now any of the Raspberry Pi family members that we just looked at would be dis perfect for our experiments today what I'm going to be using is the PI 0w and the reason I chose it over the PI 0 was because of its integrated Wi-Fi and Bluetooth end of specifically the Wi-Fi now I won't be using this today but in few clear experiments I want to download additional libraries and it'll be a lot easier to do that on a machine that is connected to Wi-Fi than one that isn't but of course you don't just use a Raspberry Pi 0 or a 0 W you need a few accessories to go along with it in order to do the experiments and you certainly don't need everything I've got phone here so don't panic but I want to show you a few of the things that you will need in order to work with the Raspberry Pi 0 or 0 W now first of all let's take a look at the zero W over here as you can see it's a very tiny computer and this is the connector over here for the micro SD card and so you're going to need a micro SD card so I've got one over here that I can use you're gonna want to get a good quality micro SD card as well don't buy just a store brand or some by a name-brand microSD card that has very high specs on the side here you can see that we've got a connection for the HDMI but this probably doesn't mate with the HDMI cables you have so in order to resolve that I've got this little adapter and this little adapter just plugs into the HDMI and lets you put a full-sized HDMI into the device now there are two micro USB connectors over here this one is for power and this one is basically a micro USB connector now since you're going to have to connect a keyboard and a mouse you're going to need to find a way of doing that one way of doing that is with one of these little hubs and these are really neat these actually not only have three USB outputs full-sized USB but they also have an Ethernet connection so this would be good for the pi0 as well so basically you can add that onto your PI by plugging that in over here and now you've given it a few extra USB ports that you'll need and of course you're going to need a power supply now I've got a few supplies back over here this is a really good power supply for the Raspberry Pi 0 it's a 2.5 amp USB supply and that's about the minimum current you want for the other PI's you'll probably want a 3 or 3.5 amp if you can find it what I like about this supply it has an on/off switch over here and that's one thing that all of the PI's are lacking now I just brought this one out as well if you're working with a PI 4 and not one of these you're going to need a USBC supply and this is the official Raspberry Pi USB C power supply for that device now of course the PI zeros that you've seen over here are lacking one thing we're going to be working with the GPIO but right now there is no connector on there well you can solder a connector on I've done that with this Raspberry Pi 0w and this is the one we're going to be using today and then after that you can connect to this GPIO now there's several different ways you can do that one of the most calm ways is to use one of these devices and sometimes called a tea cobbler although I think that's a brand name it's basically a t-connector and it's got a 40 pin adapter over here and these pins over here can be inserted into a solderless breadboard you'll see that they've already got markings on this so you know what pin you're connecting to and then you just use this ribbon cable to connect to here and then connect the other end up to the GPIO on your Raspberry Pi and that makes it very easy to get at all the GPIO pins if you don't want to do anything so esoteric though these things are great these are just mailed to female DuPont connectors and you can rip them off like I've done to a couple over here and you can just use the female ends over here to connect to the specific pins of the GPIO that you're using which is pretty well what I'm going to be doing today because I'm not going to be using a lot of the pins today there's a few other things I have on the desk over here that would just make your life easier and your prototyping with this stuff these are little cards you can use to make what they call P hats with your small Hardware attached on top boards it'll attach to the top of your Raspberry Pi zero and you can use things like this in order to connect them so you can use this for prototyping and this is a little adapter that you can use to go onto the top of here still allow you to attach a hat and yet attack a cable as well so that you can use the T device over here so there's a lot of ways that you can prototype with the Raspberry Pi zero so now that we've taken a look at this let's start working with our GPIO so let's get familiar with the Raspberry Pi general purpose input output or GPIO bus the 40 pin GPIO bus is on every modern Raspberry Pi this is a Raspberry Pi zero where there is no connector soldered in other models have a connector here you'll notice pin 1 is identified because it is marked as a square as opposed to the circle and that all of the even pins of the bus are on one side and the on pins on the other side several of the pins are designated as GPIO pins these pins allow digital input and output to the Raspberry Pi and they operate on a 3.3 volt logic level two of the pins provide a five volt output there are also a number of ground pins on the GPIO bus there are a couple of pins that provide a 3.3 volt output and finally two pins for the IDE problems that are used on the Raspberry Pi hats which attach to the connector earlier models of the Raspberry Pi used a 26 pin GPIO and the first 26 pins of the modern 40 pin 1 are identical some of the DPI Oh pins have particular significance pins 3 & 5 are the i2c connections pins 8 & 10 provides serial i/o and pins 19 21 23 24 and 26 are the SPI connection GPIO pins can be referenced using multiple naming schemes the most common one is the Broadcom pin number scheme this is the actual pin number on the broad con hip and not the pin number on the GPIO the physical pin number is also sometimes used with many GPIO libraries you need to declare which numbering scheme you are using at the beginning of your program note that the logic pinned on the Raspberry Pi our 3.3 volt and not 5 volt these pins are not 5 volt tolerant and applying 5 volts of them could destroy your Raspberry Pi the power pins can supply the hats and other attached devices as long as they don't consume a lot of current the 5 volt pin can also be used to power the Raspberry Pi but this is not advised as an internal feasible link will be bypassed if you power the board this way now when we're working with the Raspberry Pi we're going to be coding in Python and we're going to need to use a library to work with our GPIO there are several choices of libraries or AP is that you can install into Python to work with the Raspberry Pi GPIO one of the original ones was called wiring PI and it was pretty well superseded by the RPI GPIO and you'll probably see more examples for the RPI GPIO than anything else there is also another one called Pi GPIO and that has an advantage in that the code and that is pretty well transportable to dist about any other single board computer as well I understand there's even a Windows implementation of that but the one that we're going to use today the library that we will be using is one called GPIO 0 this is a library that was written by and endorsed by the Raspberry Pi organization and it is a good library for beginners with the GPIO because it actually acts like a bit of a shell and it insulates you from a lot of the fine points that you need to work with on the GPIO for example when you're using an input it will automatically invoke the built-in pull-up resistors in the PI so that you don't have to do any extra coding for that so you'll find it really easy to work with and it comes with a very excellent set of examples as well and so we're going to follow through a few of those examples today when we work with our Raspberry Pi GPIO and so the best way to learn about working with the GPIO 0 library is to read the documentation and you'll find the length of that documentation in the article that accompanies this video now we're going to go through a few the basic recipes that they have and these are basically fist examples that use a simple led and push button switch and we'll go through a few of them and for every one of these examples they supply the Python code and for most of the examples they show you more than one way of accomplishing everything and so that's an interesting thing to look at as well my dirty to go through some of the other examples as well even the ones we're not going to be using they do things like eventually attack things like motion sensors light sensors distance sensors and even motors up to the Raspberry Pi and control it through this library and through the GPIO once you've done that you can go into some more advanced examples down over here as well as remote GPIO examples and migrating from the rpi GPIO library to this GPIO library so to learn about the GPIO 0 library this is probably the very best result and we're going to go through a few of these examples today and so now that we know what's library we're going to be using and we've got everything ready it's time to hook it all up so let's go and take a look at the wiring diagram in which will hook up both an LED and a push-button switch to our GPIO now for our GPIO experiments we are course are going to require a Raspberry Pi I'm going to be using a Raspberry Pi 0:w but any modern Raspberry Pi with a 40 pin GPIO will work note that the zero and the zero W do not have a header installed on them the one that I'm going to be using will have a header soldered on you'll also need to configure your Raspberry Pi with the latest version of raspbian and I'm going to assume that you've got a mouse a keyboard and a video monitor connected to the device in addition to the Raspberry Pi we're going to need a momentary contact push button to switch an LED I'm showing a red one but any color will work and a drop-in resistor for that led and I'm using two hundred and 20 ohms pin six on the connector is one of the many ground connections and we're going to connect that to the cathode of our LED pin 11 is also referred to as GPIO pin 17 we'll connect that to one side of the dropping resistor and the other design of the dropping resistor will connect to the anode of the led pin 3 we'll connect the one side of the push-button switch this is also known as GPIO pin 2 will connect the other side of the switch to the ground and this completes the wiring for our experiments so here we are at the raspbian desktop now if this is a brand new Raspberry Pi and you just installed raspbian the first time you log in you'll get walk through a series of steps that ask you questions about the type of keyboard you have and the locale that you're in in your language etc you'll need to answer those questions and other thing you might want to do is set up the Wi-Fi networking so that you have networking ability now we don't need that for any of our experiments but it might be handy for you to be able to open say a web browser to reference the GPIO 0 library documentation or perhaps the article accompanying this video now one thing you will need to set up from the Raspberry Pi is you'll need to enable the camera interface because we'll be using the Raspberry Pi camera and so go down to preferences and then go to Raspberry Pi configuration and then go to interfaces now they have a number of interfaces here and by default they're all going to be disabled you'll notice my camera which is the very first one here is enabled and I've clicked that go and click yours and enable it and then click OK now when you do that you'll need to reboot the Raspberry Pi before you can continue now what we are going to be doing today is we're going to be doing our GPIO experiments with the GPIO 0 library and I've got the documentation for that up over here now you could go through the examples and just copy the examples and paste them into your Thani IDE which is the IDE we'll be using to work with Python on the Raspberry Pi but what I've done is I've created a number of individual Python files these ones over here and these have each of the individual examples on them and I can just right-click them and open the Thani IDE now if you'd let the copy of all these individual files I have them available in a zip file and you'll find information about getting that on the article on the drone bot workshop comm website so once you get yourself set up we're ready to work with the Raspberry Pi GPIO now anyone who has ever worked with an Arduino is familiar with the blink sketch it is sort of the hollow world sketch of the arduino world in which it just blinks an LED on and off and they even use the LED attached to pin 13 so that you don't need to hook anything to the board because there's an internal LED attack of that pin well the first couple of programs that we're going to be writing today in order to work with the Raspberry Pi GPIO and the LED perform the exact same function as the blink sketch they'll flash the LED on and off I thought it would be a good idea to compare the programs we're going to be using in Python with the Arduino blink sketch this will be expected I think for those of you who don't work with Python or haven't worked with it very much because you can compare the coding structure between the C++ used in the Arduino and the Python that we're going to be using with the Raspberry Pi and I'll leave it to you to decide which is an easier way the flash in LED and here we have the classic Arduino blink sketch and of course this is probably the first sketch that you used when you first got an Arduino and many Arduino x' come with this sketch already running on it this is some sort of a quality control test now let's just take a look at how the sketch works we're using an LED called LED built-in and this constant refers to the LED that's already wired on to the Arduino board so you don't actually need any extra hardware to run this sketch now the built-in LED is also connected to pin 13 and you and connect an LED to pin 13 for this as well if you wish so in the setup we define the LED built-in as an output and then we go into the loop and on the loop we simply write the LED high which turns it on we delay for 1000 that's 1000 milliseconds which is a second and then we write it low which turns it off and we delay for another second and if we take a look at our LED I've already uploaded the sketch you can see that indeed it is flashing once a second now if we wanted to modify this we can modify the delay period so if I for example made the delay 2000 instead of 1000 the LED should stay off and on for twice as long so it will blink slower and I'll send it up to my Arduino and as you can see it had the effect that we thought that it would have now of course what we're doing is we're creating a square wave and we're sending it out to the LED and by manipulating these delay times and contained the duty cycle of the square wave so if we wanted to for example we could keep the LED and set that to 500 that will be on for 2 seconds and off for 1/2 second and there you go and so quite simple but I want you to bear to principles of this sketch in mind as we then go and look at the Python code we'll be using on the Raspberry Pi to accomplish the same thing so here is our Raspberry Pi and as you can see I've got it doing the same thing we had our Arduino doing it's blinking the LED now you can see my hookup over here very simple I've got my LED drop and resistor and push button on this small solderless breadboard this orange wire comes from the push-button it goes to physical pin number 3 which is GPIO 2 the yellow wire is from the dropping resistor that goes to the LED and that goes to pin number 11 which is GPIO 17 and then the black wire is just the ground connected to pins and my LED is flashing away let's see how that actually works now here's the Python code that we're looking at in the Thani IDE which is an IDE you can use for working with python on the Raspberry Pi there are other ideas as well but this is the one that it defaults to now now if you look at our Python code you can see a lot of similarities to the C++ code we looked at in the Arduino sketch now we do need some libraries over here which we didn't need in this sketch we'll start off from the GPIO zero library we're going to import a class called led which as its name implies is a class that has a number of functions that lets us work with LEDs we'll also from the time library import a class called sleep and sleep is very much like the delay function in the Arduino C++ code it just makes the code stop for a predetermined amount of time now I'll define an object to represent our LED we're going to call it red and we'll use the LED class and point at the GPIO pin number 17 which is where we have our LED connected and then this is a loop the wild true is always going to evaluate is true and so it's going to run the for statements below now remember in Python we don't use brackets instead we nest things by using indents so these four things will run right after each other and so we'll take our red LED object and turn it on then we will go to sleep for one second this parameter represents seconds then we'll turn it off and we'll go to sleep for a second and repeat and as you saw that makes the LED flash back and forth once on and once off and that's pretty cool the same thing that we did with the Arduino sketch we can do with this Python program if I wanted to make this LED flash slower I could change these values to two thus giving it two seconds you could also use fractions by the way so things like 0.5 of the second or valid values and we'll send that up to the LED now let's just it and started again and as you can see our LED is flashing a lot slower than it was before because now it's on for 2 seconds and off for 2 seconds and so basically we've used a similar piece of code to blink the LED there's another way we could do it as well that they show you in the GPIO zero examples it's the next example actually if you take a look at the code for this this is even simpler again we import the LED object and we now import from a signal library something called pause and will once again define red as being the object representing our LED notice there are no semicolons at the end of the statements in Python and then we'll just simply use the blink function of the LED class and say red blink and so we'll just tell it to go off and blink and there you see it blinking and it's back to blinking at once per second now there are parameters you can give this you can give it the on time and the off time let's make it blink faster this time let's give it an arm time a 0.5 and off time of 0.5 as well and there are additional parameters as well here you can do a fade in and a fade out to the LED as well and so let's stop our code and I'm using my keyboard right now to do that by the way with the f5 key and there you can see our LED is blinking a lot faster now because we've given it those parameters and so this is an even simpler way of blinking an LED using Python so as you've seen using the GPIO 0 library with Python makes it very simple to flash an LED because it has a class exclusively dedicated to working with LEDs in fact I think it makes it a bit easier than the coding on an Arduino to do the same thing but that's my own personal opinion now in addition to classes for the LED it also has a class for working with buttons which we can use with our push-button so let's go take a look at a few other coding examples now now before we move on to the push-button let's do a couple of other things with the LED involving pulse width modulation now PWM as you know is a way that we can vary the brightness of an LED or control the speed of a motor and you can do that with the Arduino on certain digital i/o pins you can do it with the Raspberry Pi as well and we're using a class called PWM led from the GPIO zero library to regulate the brightness of the LED using pulse width modulation we're also importing the sleep class again like we did in our original program we'll define our LED as being PWM LED on GPIO pin number 17 and then we go through a while true loop again we'll set the value parameter of the LED to zero which will turn it off we'll go to sleep for a second then we'll set the value to zero point five which brings it the half brightness sleep again for a second and then we'll go and set the value to one which turns the LED completely on and we'll keep it on again for one second and then repeat and so if we look at our LED we can see it's going to half brightness now full now off half full and off etc and of course we could adjust the number on the sleep parameter to keep it on those different stages for different amounts of time now there is another function that PWM LED has that is interesting that's similar to this and it is the pulse function so here is a program that uses the pulse function and LED pulse let's just go and send that up to our board I'm using my f5 key again and there's an LED pulse now as you can see it's got kind of a fade in and a fade out and pulse has several parameters that you can adjust to adjust the rate of this and the amount of time for the fade in and the fade out as well and that's all in the GPIO zero documentation to which you'll find the length of course in the article accompanying this video on the Durham bot workshop comm website ok so one final thing that we can do with the LED is start working with our push button now here we have the first piece of that we're going to use to work with the push button and we're going to import two classes from GPIO zero and this is how you import multiple classes you just separate them with a comma we're going to import the led class and the button class which allows us to work with push buttons and a game we're going to put a pause at the very end of the program if the program does come to an end we will pause it now we once again to find the LED is a LED object on GPIO pin 17 and button will be a button object this is our push button on GPIO pin number two which is physical pin number three and so when the button is pressed when we get a one pressed event we'll turn the LED to on and when the bright button is released we'll turn the LED off and that seems pretty basic and let's take a look at it and yes indeed when I press a button I can turn my LED on now of course this is not a particularly useful little program since we could simply do this without the Raspberry Pi and just give ourselves a bit of voltage and turn the LED on directly from the button but the point here naturally is to learn how to use the Python code we're getting an input from the button and sending it outputs the LED now the other way of doing this is with this example and with this example we're important the same two things the LED and the button classes we're going to use pause again at the end of the program we're defining the LED and the button is the same but we're simply saying LED source equals button and so this is a much faster way of accomplishing exactly the same thing so as you can see with the GPIO zero library you're coding in Python is actually pretty minimal now let's look at a few other things we can do with the button now this is a program that basically uses the wait for press event from the button so the button is actually going to sit here and not do anything this code won't do anything until the button is actually pressed let's send that up to her and there as you can see in the bottom here which is the shell I dis pressed my button and it said the button was pressed and now the program is over there's pressing the button further won't do anything for me let's take a look at another program that'll show us when buttons are pressed and released now here's another one we're going to import button and we're going to use the pause and we're going to define two functions one of them says say hello and we're going to print hello and the other one says say goodbye we're gonna print goodbye and everything we print is going to end up in the shell down here by the way now when the button is pressed button when pressed we take the say hello function so we should print out hello and when the button is released we should say goodbye and this should continue to work continuously so here I'll hold it down release it I've got hello and goodbye now I want to show you one other thing that's rather interesting and will illustrate one of the differences between running code on the raspberry pi then running it on the urge we know let's go and take a look at the very simple button example and basically when it's true if the button is pressed it says the button is pressed of the button is not pressed it says it isn't pressed now what I'm going to do over here is I'm going to run this code but I'm going to run this code in the terminal this will open up my terminal and button is not pressed button is pressed I'm holding it what I want you to notice though is that I'm also getting results in the shell for the previous program in other words both of these programs are running simultaneously and I think that's pretty cool that you can actually run two things at the same time of course you got to be very careful doing that but that just illustrates a bit of the power you have using the GPIO with the Raspberry Pi now as we saw earlier when we compared the Arduino and Raspberry Pi the Raspberry Pi has a number of other i/o interfaces of the Arduino does not have and one of them is a CSI connector for a small camera and for our last experiment we're going to connect the Raspberry Pi camera up to that CSI interface and use a couple of push buttons attached to the GPIO to control it for this experiment we'll need our Raspberry Pi again and we'll also need a Raspberry Pi compatible camera any type will suffice we'll need two momentary contact push-button switches which we'll call SW one and SW two we'll begin by connecting our camera to the camera connector on the Raspberry Pi make certain that you use the correct cable for this and note that the Raspberry Pi 0 and 0 W have a smaller camera connector than the other models do we'll begin our wiring by connecting pin 6 which is a ground to one side of each of the push-button switches well then connect pin 3 which is also GPIO pin 2 to the remaining side of SW 1 and finally we'll connect pin 5 which is GPIO pin 3 to the other side of SW 2 and this completes our wiring so here's our camera test setup I've got my two push buttons over here the red one on this side is SW one and one on this side is SW 2 SW one being connected to pin 3 which is GPIO 2 and SW 2 is connected to pin 5 which is GPIO 3 now up over here you can see I've got the camera suspended and it's facing down toward the Raspberry Pi so this way our Raspberry Pi can take a selfie now let's look at the code we'll need to achieve this this is the code that's been provided and we'll start off again by importing a number of classes from libraries for the button for the camera also we want date/time and a pause now we're going to define a couple of objects to represent the push-button we're only using one button in this case the red one and we're also define an object for the camera then we start by defining a function that we're gonna call capture and in capture we take the date/time of now which is the immediate date/time in ISO format and assign that to a variable called timestamp and then we use the camera object and we do a capture in other words take a picture and we're going to send that capture over here to the root of our PI directory and we're going to use the time stamp itself as the name of the file dot jpg so take a picture with the timestamp being the name of the file and then we go out of this function here and take a look at the actual program and so we're looking for a when pressed event from the button when that happens we'll execute the capture function and take our picture and then we'll start everything over again so let's go take a look right now in the root of the PI directory and we'll press the button and it appears that we have created a file and we'll just go in and open that file and there we have a selfie of the PI 0 and a little bit of my thumb in there as well so it does appear to work and it's still running every time that I press the button as you can see it'll take another picture so this is a great way to take a selfie of your Raspberry Pi now here's another program to control our camera this time we're going to be using both push buttons one button will provide a preview function and the other button will take the actual picture now we start off by importing the same libraries and classes from those libraries and then we define our two buttons with the define left button as being the one attached GPIO to and right button is the one attached GPIO 3 so left button in my case is the one indeed on the left which is the red push button and this is the one that's going to be providing the preview function the right button will take the picture and then we also define an object of course for the PI camera the function capture is identical to the one we used last time it'll take a picture and label it with the timestamp of the current time now here's where we define how the buttons work the left button is the preview 1 and so when it is pressed down we do a start preview and as long as that button is held down the preview will continue and then when the button is released it'll stop the preview the right button when it is pressed just executes the capture function as before so let's go into our directory again which have cleared out of files and I'm gonna hold down the preview button and as you can see I'm getting a preview and so I can move around and try to get the ultimate picture right now for my selfie and now I'm going to press the other button and we did indeed get a file which we can open and actually it's going to be a picture of what we just looked at and there you go and again my thumb got in the picture and you can also do them simultaneously I can hold the preview down move the camera around try not to get the foam in there and at the same time do a quick press of the button and make another picture at the same time so essentially with a few lines of Python code we filter cell as a camera which is pretty impressive and so today we've learned the basics of working with the Raspberry Pi GPIO and we've seen how libraries like GPIO 0 can make it very easy to work with things like push buttons and LEDs and in future videos I'm going to be showing you how we can do additional things at the GPIO there is a lot to it and we can hook up all of the same sensors and display devices that we've used with the Arduino we can control motors with it and of course there's the SPI and the i2c bus on there so you'll be seeing a number of additional videos about working with the GPIO in the near future now the best way to find out about those videos is to subscribe to the channel and when you subscribe please click on that Bell notification button and that way you'll be notified every time I create a new video now if you'd like to get the code that I used in today's video and some easy to use Python files instead of copying them off of the GPIO 0 website or if you need a bit more information about what we talked about you'll find an article that accompanies this video and the link to that article in the drill bot workshop comm website is below the video you'll also find the link below the video to join the newsletter the newsletter is my way of keeping in touch with you every two weeks I send the newsletter just to let you know what's going on here in the workshop and finally if you want to discuss today's video or if you'd like to make secure for new content for the drôme bought works off youtube channel and website then go over to the drone bot workshop forum you'll also find a link to that under the video and you can discuss the topic with fellow enthusiasts just like yourself so until we meet the next time please take good care of yourselves and I hope to see you again very soon here in the workshop good bye for now well I don't know how it happened but it appears we had a Gatecrasher at our party this guy came in masqueraded as a 3 b plus but it turns out he's only a 3b and this fellow over here will get rid of this guy this guy is really the 3b plus and as you can see he's got a shiny silver tip because he's faster he's also got a power over ethernet connector over here so this is his nametag and we'll have the contact security about that guy over there [Music]
Info
Channel: DroneBot Workshop
Views: 212,199
Rating: 4.9420991 out of 5
Keywords: Raspberry Pi GPIO, raspberry pi (computer), raspberry pi beginners tutorial, raspberry pi guide for beginners, gpio explained, python (programming language), pi vs arduino, raspberry pi vs arduino, general-purpose input/output, raspberry pi
Id: iL_oZGHLHvU
Channel Id: undefined
Length: 48min 39sec (2919 seconds)
Published: Sat Feb 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.