Unleash The Raspberry Pi Through Physical Computing | Kevin Sidwar | Skillshare

Unleash The Raspberry Pi Through Physical Computing

Kevin Sidwar, Software Engineer + Hardware Enthusiast

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
25 Lessons (3h 10m)
    • 1. Welcome to Class

    • 2. Prepare the SD Card (Steps 1-3)

    • 3. Install Raspbian and Update (Steps 4-9)

    • 4. The GPIO Header

    • 5. Setup Basic Input/Output

    • 6. How to Hook up an LED (Step 1)

    • 7. Intro to Fritzing and LED Hookup (Step 2-3)

    • 8. Use Python to Control an LED (Step 4-5)

    • 9. Safety Reminder

    • 10. Theory and Switch Hookup (Steps 1-2)

    • 11. Switch Integration (Steps 3-5)

    • 12. Button Integration (Steps 6-7)

    • 13. Soldering Headers

    • 14. Intro to SPI (Step 1)

    • 15. Enable and Test SPI Protocol (Steps 2-6)

    • 16. Basic Setup and Test (Steps 1-4)

    • 17. Controlling the Display (Steps 5-11)

    • 18. I2C Basics (Step 1)

    • 19. Configure Pi for I2C (Steps 2-5)

    • 20. Datasheet Review and Hookup (Steps 1-2)

    • 21. Add Sensor Code to Python File (Steps 3-6)

    • 22. Datasheet Review and Hookup (Steps 1-2)

    • 23. Script and Test (Steps 3-4)

    • 24. Twitter Integration

    • 25. Conclusion

15 students are watching this class

About This Class

This class is for beginners interested in unlocking the hardware potential of their Raspberry Pi so that it can interact with the physical world.

This is the first class on Skillshare to cover the Raspberry Pi and contains just over 3 hours of video instruction.

During the course we'll take this pile of stuff:


And turn it into this:


The class will start with the very basics of setting up a Raspberry Pi straight out of the box. It will then cover 3 main capabilities provided by the Raspberry Pi for physical computing:

  • General Purpose Input/Output
  • I2C Protocol
  • SPI Protocol

Students will become very familiar with each of these via the class project which will be to create a very simple home monitoring device.

At the end of the class, students will have the basic knowledge and skills required to start creating their own Pi-based hardware projects.


The Class Will Cover the Following:

  • Setup: Unboxing and setting up the Raspberry Pi
  • PI GPIO Header: What it is and the functionality it provides
  • General I/O: How to configure and use a pin as an input and output
  • SPI Protocol: What it is and how to use it on the Pi
  • Sensors: How to hook them up and read them
  • I2C Protocol: What is is and how to use it on the Pi
  • To the Cloud: How to connect your creation to the web without writing a website
  • ...and much more

As needed, the class will discuss basic electronics principles but is not meant to provide deep coverage of that topic. I'll walk you through everything you need to know to stay safe and have fun. The class will use the Python programming language. Programming experience is not required but will definitely help.

The following is a shopping list of items for the class project if you wish to follow along:

  • Raspberry Pi (power supply, SD card, keyboard, monitor, internet connection via ethernet cable or wifi adapter)

  • Pi Case (optional but recommended) - Make sure you can still access the header pins.

  • Breadboard (large)

  • Soldering Iron and Solder (very little soldering, maybe borrow)

  • Several Feet of Hookup Wire

  • Pi Cobbler from AdaFruit (optional)

  • 330 resistor (1)

  • 1K resistor (3)

  • 10K resistor (2)

  • LED (any color)

  • Breadboard-Mountable Switch

  • Breadboard-Mountable Push Button

  • Break Away Male Header Pins (optional for solding to breakout boards)

  • Seven Segment Serial Display

  • TMP102 Breakout Board

  • PIR Motion Sensor from Parallax

To help with your shopping I've created a shopping list at Sparkfun which contains almost everything you will need. While the temp sensor and display are only sold by Sparkfun you can get the rest of the parts from any distributor like Mouser, Newark or Digikey. I love Sparkfun but recommend NOT buying breadboards from them as they are very difficult to break in.

Sparkfun Shopping List

I recommend getting the motion sensor either directly from Parallax or from Mouser.

If you're in Europe you can find a lot of this stuff at proto-pic which should save you some money on shipping.

If you have any questions about the list of the materials before making a purchase feel free to contact me at sidwarkd at hardlysoftware dot com.


1. Welcome to Class: - welcome to unleash the raspberry pi through physical computing. - First of all, - I want to tell you how much I truly appreciate you enrolling in this class. - If it any point during this class, - you don't understand something or need some help. - The best place to go is the community tab on the class page. - You're also welcome to email me or reach out on Twitter. - I'm happy to help. - So first of all, - what is physical computing Wikipedia describes. - It is building physical systems by the use of software and hardware that consents and - respond to the analog world. - In the last five years, - there's been an explosion in the hardware hacking space. - Some amazing low cost tools have been created that have allowed hobbyists to start creating - serious hardware projects. - That, - combined with the fact that writing a blob with the latest shiny software tool just isn't - cutting it anymore for the basement hackers out there. - There's a huge desire to write software that leaves the room of our PC and the Internet and - actually starts interacting with the physical world around this, - making your toaster tweet, - getting feedback from your car sensors locking your house from the Beach. - These are all examples of physical computing. - There are a lot of hardware tools available for physical computing, - but the raspberry pi has gained a lot of traction not only because of the price but also - because it can run software that many people already have experience with. - Unlike microcontrollers, - which predominantly require you to write in C or assembly the raspberry pilots, - you create hardware projects using things like python or even know J s. - So what exactly will we cover in this class? - We'll start by setting up and configuring the raspberry pi. - We'll cover in detail the basic digital input output capabilities it provides. - Next, - we'll introduce you to the purple capabilities exposed to be the Pies GP I Ho header and - cover each one in detail. - We'll do this by building a simple home monitoring device. - We'll wrap things up by connecting our device to the Internet so we can check the status - remotely. - We'll throw in some tips and tricks along the way and also cover some electron ICS - principles to help you in your own projects. - There are some things that won't be covered in this class, - however, - while we will cover some basic electron ICS knowledge along the way. - This class isn't meant to teach electron ICS in depth. - I wouldn't even consider it an intro to electron ICS, - as we will gloss over some topics who also be using python to write the code for our device - but won't be covering python in depth. - Most of the code, - however, - will be very simple and will not require deep knowledge of the python language. - Even so, - I've included some links for learning python in the resource section. - Going through some of those first will definitely help but isn't required. - It's not required that you follow the class project exactly, - but it would help to ensure that you are reaching the learning milestones. - List of materials needed to follow along can be found on the info tab for this class. - It can also be found in the resource section for this unit. - I tried to keep the Costas lowest possible. - The most important objective of this class is for you to have fun. - As we go through the various units. - I hope that your imagination will take over as you think of ideas to try and projects to - create. - I encourage you to act on those desires and share them with the class by creating your own - student project. - All right, - let's get started. 2. Prepare the SD Card (Steps 1-3): - Welcome back in this unit will go over setting up our raspberry pie with an operating - system and getting it ready to start a project. - As always. - If you have any questions, - feel free to submit them on the skill share class page, - where you can send me an email or try and find me on Twitter. - So before we can boot up our raspberry pi need to give it some sort of base operating - system. - Unlike a conventional computer, - the pie doesn't have a traditional hard drive. - It's hard drive is the SD card we put in it. - But just like a normal computer, - the pie needs something to boot up. - Traditionally, - that would be something like Windows OS 10 or a form of Lennox. - But even if the hardware were compatible, - the pie would have no hope of running something like Windows or OS 10. - But it turned down version of Lennox will do just fine, - and that's exactly what we're going to install. - The pilot doesn't use a normal CPU like an Intel I seven, - so we can't just throw a bun two or red at Lenox on it. - We need a special distribution of clinics. - Unfortunately, - for us, - there are plenty to choose from. - Lots of smart people have modified various Lennix distributions to make them work on the - raspberry pi. - An exploration of all the available distributions is beyond the scope of this class, - but you can read more about them on the downloads page at raspberry pi dot board. - For our purposes, - we're going to stick with the recommended starter distribution known as Raz B in. - So let's go ahead and walk through it. - We'll start by going to the raspberry pi dot org's homepage Well appear to downloads Now. - In the early days of the raspberry pi, - you had to follow special steps just to get Raz b in onto your SD card. - News has changed all that and made it extremely easy to get started. - As you can see here, - we simply have to for madness D card and copy of the unzip nubes image onto it. - Let's go ahead and download that well. - That's downloading. - You'll notice that they recommend that we four matter SD card using the SD card - associations formatting tool. - So let's go ahead and get that tool now. - The downloads air over here on the left hand side, - and we'll just choose the SD four matter for dot over windows. - As usual, - we have to go through a license agreement. - So we'll go ahead and accept that and the down little start. - I'll go ahead and fast for the download is it takes just a minute. - Okay, - once that finishes downloading, - just go ahead out to that location where just downloaded on windows will unblock it before - we extract it. - We'll just extracted here. - Okay, - great. - Now let's go ahead into that folder and we will launch the set up the Senate for this tools - . - Really easy. - We're basically just gonna go ahead through and accept off defaults, - so I'll sort of fast forward and let it play through a little faster so you can see what's - going on. - Start by going to next. - Just accept the defaults here and hit Install. - Okay, - which this finches up. - We'll go ahead and click finish, - and that's all there is to sitting up that tool. - And you can see we have a little bit left to go on our nubes download so we'll go ahead and - fast forward. - That hasn't finishes. - No way we watch this formatting tool. - You'll see here that we have a drive dropped down. - This will be the drive that you have us that represent your SD card. - In my case, - that's G. - Now you want to make sure you verify this because if you select the wrong Dr here, - you could end up formatting your local hard drive or another are drive that as things like - pictures and things like that. - So, - as you can see here, - my ruble disk is my G drive. - You can also see the size is correct will come into the options you could just leave. - The format type is quick, - but you'll want to turn the format size adjustment toe on and then click. - OK, - Once we've done that, - we are already to format this drive, - so we will go ahead and we'll click format. - Once RST card is formatted, - we're ready to put the image on toe. - So it's come back to this image that we downloaded. - Go ahead and unblock it like we did before, - and we will extract it here. - Oh, - this will only take a minute. - All we need We don't want to copy the folder. - We want to copy the contents of the folder. - So let's go ahead and select all of this will, - right Click and we'll do a copy and then we want to put that onto our SD cards will come - over here to this window. - If you remember, - SD card is the G Dr will open that and we'll just paste our files here. - Now it's a lot of files, - so this will take a second. - So we'll go ahead and use the magic of video to speed this up. - I only take a few seconds. - All right, - that's it. - This SD card is now ready to be placed into our pie so that we can boot it up for the first - time, - which will allow us to install raspy in. - So let's go ahead and do that next. 3. Install Raspbian and Update (Steps 4-9): - Okay, - so now that we have our card here with nubes on it, - we're ready to hook everything up. - So I've got the raspberry pi out here. - You can install it in a case if you want to. - Just make sure that you leave access to these header pins because we'll need those later, - and all we're gonna do is hook all of this stuff up. - So first thing we're gonna do is insert the SD card. - Make sure it's nice and snug will go ahead in hook up our monitor. - I'm gonna start with wired Internet, - So go ahead and look that up. - And the last thing that will need is a keyboard, - so we'll go ahead and that it the U. - S B port. - Now, - I would wait to connect your power toe last. - That's just the way I normally do it. - Um, - another thing that you wanna be aware of is you don't want to lay this on a surface that - can conduct electricity, - so don't put it on anything metal. - You can actually leave it without a case. - It's fine if you don't want to use one. - Just make sure that it's sitting on something sturdy so that won't fall off. - And that won't connect electricity. - So the last thing will do is will connect our power. - And now we are ready to boot up for the first time. - I'd like to make just a quick note on the video quality in this unit. - It's really bad. - As I'm trying to record directly from another screen, - I promised. - The whole course won't be like this. - As soon as we get our pie installed, - I'll start to ssh in and screen record so everything will look much nicer than the first - time that you boot up your pie. - Now that you've got Nubes on your SD card, - this is a screen that it's going to boot to. - It is going to give you the choice to install bunch of distributions. - We want to stick with Raz B in, - which is the one that's highlighted by Deep all. - So if you just push space bar on your keyboard, - it'll put an X on that, - saying that it's highlighted. - And then, - if you notice you might not be able to see on the screen in the top left, - there's it, - says Install, - and there's an I in parentheses, - so If we just push I on our keyboard, - it's gonna give you a warning that it's going Teoh race everything on the SD card and be - overridden, - and so then we'll just hit. - Enter to say continue, - and it will start the installation. - This is a great time to grab a snack or a drink, - as this will take a while. - While Raz B is installing you, - compress L to set your language will bring up a drop down. - You can select the one that you want. - You can also choose your keyboard layout by pressing nine. - If you do it now, - while it's installing, - you won't have to worry about it later. - Once the install finishes, - you'll get this message box that says OS is installed successfully. - Just it entered to say OK, - that'll trigger a reboot of your pie. - And after the pie loads everything up, - you'll see this blue screen with the white dialog box on it. - This gives you some options to configure your raspberry pi. - The only thing I would recommend doing here is making sure that ssh is enabled. - To do that will go down the Option eight Advanced options hit, - enter and scroll down to ssh and hit Enter should be enabled by default. - But just check it and make sure and hit. - Enter. - It'll say ssh server enabled Had entered for Okay. - Once you've done that, - if you hit the right arrow key, - it will take you down to the bottom and highlight the select. - If you do one more right arrow, - it will take you to finish and hit. - Enter. - It's off screen here, - but that will bring you up to your first prompt. - What you want to do from this command line here, - the very first test you want to do is an Internet connectivity test. - I recommend just doing ping space google dot com, - and you can see that I'm getting responses, - which means my Internet is up and running. - Just that control. - See, - to cancel out of that. - Now that your raspberry pi has raz being installed and is connected to the Internet, - we're ready to move on. - One thing I like to do right after getting Raz being installed is to configure it for ssh. - I just want to clarify that this is not required for the class. - If you want to continue using the keyboard and monitor. - That's just fine. - Ssh stands for secure show and will allow us to log into our pie from another machine on - our network. - In my case for my desktop computer, - this allows me to get rid of the monitor and keyboard connected to the pie. - The way I like to do this is to have my router reserve a static i p address for my - raspberry pi based on its Mac address. - Is there so many possible network configurations and router types? - I won't cover how to do this, - but there are plenty of resource is online. - If you do a little searching on Google, - all we really need for ssh is the I. - P address of rpai. - If you're using Lennix, - you can simply open a terminal window in type ssh, - followed by a space and then the i P address on windows. - I like to use putty. - So for my desktop, - I will launch putty and you'll see that I need to fill in some information the port in - connection type default. - Ssh. - So all I need to enter is the I. - P. - Address of my pie, - which I've configured in my router to be 10.0 dot 0.17. - Once I've entered that, - I'll come down here and click open, - and it will launch a terminal window from here. - I will just log in with the default credentials that come with our Raz B and distribution. - The user name is pi P. - I in the password is raspberry all lower case, - and that will bring me to a terminal problem, - just as if I were right in front of the pie. - Just as a test, - I'll do an LS command, - which lists the directory contents, - and you can see that it works just fine. - I really like using ssh because it allows me to be on my desktop computer, - where I can easily look things up on the Internet while I'm working on my projects. - Another thing I like to do at this point is to install a USB WiFi adapter so I can unhook - my Ethernet cable again. - This is not something that I'll cover and is definitely not required for the class. - But if you intend to use WiFi, - I'd suggest that you set it up. - Now. - Make sure any WiFi adapter you purchase specifically list that it's compatible with the - raspberry Pi as many will not work with our pie booted up and razz being installed almost - ready to start working on our class project. - The last step of setting up our pie is making sure Raz being is up to date. - We do that with two commands pseudo after get update and sudo apt get upgrade. - The pseudo part of these commands is something we will use frequently throughout this class - . - Sudo is a program on Lennox that allows you to run commands his route. - If you're a Windows user, - think of it as allowing you to run commands with administrator privileges. - So in this case will run apt, - get update and after get upgrade with administrator privileges, - you will need Internet access for these two. - Run. - We'll do the update first, - followed by the upgrade. - These commands do take a while to run, - so it's a good time for another snack run with our pie updated. - We're ready to move on in the next unit. - I'll introduce you to the GP Io header on the raspberry pi 4. The GPIO Header: - in this unit were going to introduce you to the general purpose input output header on the - raspberry pi. - We'll talk about the capabilities it provides and cover a few do's and dont's having - followed the raspberry pi for a while now, - I've seen a lot of cool projects, - but many of them use the raspberry pi only is a very cheap Lennix machine, - things like print servers and file servers, - Minecraft, - consuls and beginning programming environments. - Those things are really cool. - Don't get me wrong. - But for me, - the real power of the raspberry pi comes when we start connecting hardware to it. - We do that through the GP Io header through the header. - We can achieve quite a bit because it gives us access to things like the S P I and I two c - protocol as well as general input and output. - Don't worry about what all that means right now, - because we're gonna cover all of it during this class. - What's important to know is that these things were we used to connect to the outside world - . - So instead of thinking of the SP I protocol, - we can think of a display and for general input and output. - We can connect things like motion sensors, - buttons, - switches. - And then when we talk about the eye to see protocol, - we can connect things like sensors and other components, - the concepts we're gonna cover in this class or general enough that you can use them to - connect almost anything to your raspberry pi. - So the first thing we need to do is locate the header. - Well, - it's pretty hard to miss, - as you can see whether you have a model A or model be raspberry pi the headers in the same - place. - It's just in the upper right hand corner if you're looking at it from this perspective, - so let's take a closer look. - To be able to correctly identify the pins, - we need to know how they're numbered. - We start by locating Pin One Now. - From this image, - it may seem like the P one label at the top left of the header is identifying Pin one, - but that's only a coincidence. - P one is just the name of this header. - As of the second revision of the Raspberry Pi board there, - six headers on the pie labeled P one through P six. - Now just to the right of that, - P one label was a little white box, - which is the actual indicator that this is indeed pin one. - The numbering then continues, - similar to how you would read a book left to right top to bottom with pin to here, - followed by pins three and four. - And then, - if you count all the way down, - you'll get to the bottom where there's pins 25 26. - So what do all of these pins do they provide us with? - All of the functionality will need to connect our pride external hardware. - We have power in the form of both 3.3 and five volts. - That's important to know that there's some limitations to how we can use power from the - header and we'll talk about those as we get further along. - For now, - I just know that we can only pull a limited amount of power from those pins and we have the - ground pins to complete the powers. - Then we have pins for the various protocols will be using as our project progresses, - things like S, - P, - I and I to see one aspect of the header. - We won't be covering this class is the you are. - As you can see, - the pins for that are located here. - Finally, - we have quite a few pens available for the basic digital input output. - Now, - unless you have a photographic memory, - there's no way you're going to remember what each of the pins is just by looking at the - header. - I mean, - I guess you could memorize them, - but I like to take an easier approach. - And there are a few ways to simplify working with the header and remembering what functions - the pins provide here. - Just a few. - The simplest and cheapest ways to just print out a pin diagram for your board revision. - There are different revisions of the header pin map, - and in just a minute we'll show you how to figure out what yours is in the middle. - Here is an ingenious concept called the raspberry leaf. - It was created by Simon Monk and is meant to be printed out and then installed on your pie - . - Just like this, - this is a really nice way to mark your header. - If you'll be using jumper wires to make your connections, - it takes a little bit of effort to create because you need to pre perforate the leaf before - sliding it onto the header, - but makes it so you don't have to constantly refer to a print out and count pins. - Finally, - there's the high end solution known as the pie cobbler from Ate a fruit. - You simply connect a ribbon cable to your header, - and the cobbler board has all of the pens marks. - This makes it really easy to connect to a bread board, - and it's what I'll be using for our project just to keep things clean. - Now, - if you don't want to spring for the pie cobbler, - don't worry about it. - Just haven't pronoun handy and use jumper wires. - And that worked just great. - So now that we know that there's different versions of the header, - we need to figure out what version we have on our pie, - and that's a really simple thing to do. - All we have to do is Buddha Bar pie to a command prompt. - Here we're gonna type in cat C A T space, - and then we'll do forward slash Prock ford slash CPU info. - Now, - if you're not familiar with Lennox operating system Cat is just a built in utility that - will allow us to display the contents of a file. - And in this case, - we're gonna display the contents of the CPU info file inside the Proctor folder. - So we hit. - Enter here. - It's gonna tell us a bunch of information and the one that we care about right now is this - revision down near the bottom 000 e. - In my case. - So what we do is we take that revision and we come over to this page here on the Lennix dot - org's. - And there's a hardware history table, - and what we'll do is just look up our revision number in this table, - As you can see for mine tells me that this model be already knew that. - But the important column is the PCB revision. - Here it's version two point. - Oh, - we can also get some other information like this. - Particular one was manufactured by Sony. - Now that's important because most sites that give you information about the pin out of the - header will have both versions available. - For instance, - we have this simple table here. - There's both versions, - and even the raspberry leaf has both versions that you can print out and install in your - pie. - Now that you've identified your vision and chosen a way to identify your pins. - Go ahead and set that up. - Now you go ahead and pause the video and do that. - Whether you're just printing out a sheet, - installing a raspberry leaf or using the pie cobbler, - now's a good time to set that up. - It's also a good time to get your bread board out, - because we're gonna be using that shortly. - If you are using the pie cobbler, - go ahead and hook it up. - Now, - if you plan to use jumper wires to follow along, - that's okay to just make sure bread board is nearby because we're gonna start filling it up - . - So here's what my set up looks like so far. - Before we go any farther, - I want to cover a few items regarding header safety, - otherwise known as how to not ruin your raspberry pi first. - If you plan to use jumper wires to make your connections, - do not use alligator clips. - It may look OK, - but they're likely to big and can cause a short. - The power and ground pins are right next to each other, - so if you inadvertently short them together, - you'll break your raspberry pi. - Instead, - I recommend using isolated jumper wires. - As you can see, - these create a much cleaner connection, - and there's no risk of shorting pins together. - Another way you can accidentally short pins together is by touching the header while the - pie is powered on as a general rule, - never touch or try to make any connections on the header. - While the raspberry pi has turned on. - Electrostatic discharge can also be a problem in touching the header, - even when the pie has turned off. - So be sure to ground yourself before touching anything or making connections. - So let's review never make connections, - while the pious powered on it only takes a minute to turn it off, - make the connections and booted back up. - It's not worth ruining a pie to save a minute, - even though there's a five vote pin on the header. - The peripheral pins on the pyre, - not five vote tolerant. - That means that if you connect a five volt source toe one of the input output pins, - you can ruin your pie. - If for some reason you need to interface with the system that delivers five bolts, - you'll need to use some sort of level shifting technique. - If you're going to use jumper wires, - use isolated female jumper wires to make connections to the header. - Or use a ribbon cable like you saw in my set up with pie cobbler. - Make sure you always verify which board revision you are working with to make sure your - connections air correct. - Chances are you have border vision to, - but it's best not to risk it, - especially since it's easy to figure out. - As we showed along that same note, - you probably heard the phrase measure twice. - Cut once. - When you're dealing with making connections, - verify the pin twice and connect once. - This is especially important when connecting jumper wires directly to the header. - Always double check the pin you're connecting. - Finally, - never, - ever touched the header while the unit is powered on. - That goes for other parts of the pie as well. - While it's on hands off and while it's off, - be sure to ground yourself before touching anything. - Well, - that's enough of theory and scary warnings. - Now it's time to start working on our project. - In the next unit will introduce you to the general input and output capabilities of the pie - and talk about how we will use them to start piecing together our class project as always. - If you need help or have questions, - the best place to go is on the Skill share website for this class under the community tab, - you can also reach him via email or find me on Twitter. 5. Setup Basic Input/Output: - welcome to Unit four of the class. - This unit will be covering the set up of the basic digital input output that a raspberry pi - header provides us. - So let's remind ourselves of a few things before we get started. - When dealing with the Iot header, - we need to remember that 3.3 volts is considered a digital one or the high state. - It actually needs to be greater than 1.3 bolts, - but we'll just say 3.3 bolts and ground or less than or equal 2.8 volts, - is considered a digital zero. - Our GP I opens can be configured to be an input or output, - but they can only do one job at a time. - It can't be an input and an output at the same time. - And finally, - we need to remember that while there is a five volt pin on the header, - the input out pins are only 3.3 vote tolerant. - So if we connect five votes to those, - we're gonna end up ruining our pie. - Now it's set up our pies that we can have access to this functionality, - the pipe on script. - Okay, - so we have our pie booted up here and we're ready to start working with the input and - output capabilities provided by the header. - Now, - as I stated in the introduction to the course, - we're gonna be using Python to do all of the code that we're going to need to write for our - monitoring device. - Raspberry by default, - comes with python installed, - and you can see that if you from your command line here just you python dash dash version. - You see, - the python 2.7 dot three is installed. - But for what we're gonna do, - we're gonna need a little bit more than just the basic python. - We're gonna need the developer tools and we're also gonna need something called the rpai - dot g p a library. - And so let's go ahead and install those. - We'll just do a pseudo after get install, - which is just if you're not familiar with Lennox, - that's just saying that this is a standard command. - To install new packages, - you will need to be connected to the Internet for this store. - That's what Do sudo Apt get install hi Thon Dev and we'll let that go ahead and install it - says it's gonna take up 42.2 megabytes of additional dicks space. - Is that okay? - Yes, - This might take a minute, - so we'll go ahead and skip ahead. - Okay, - Now that that's installed, - let's go ahead and install the, - uh, - raspberry pi GPO Python module will do that. - Very similar to how we did the python Dev modules do sudo after get install. - It's python Bashar pied a g p i o. - And when this model is going to do for us is that it's gonna make it really easy to access - the general purpose input output pins on the raspberry pi. - And as you can see, - the latest version of the Rpai GPO library is already included with the RAZ B and - distribution. - So if you remember from before when we needed to find out our border vision, - we did this we did cat slash Prock slash CPU info and we use this revision number to go - look up in a table. - What border vision we have. - Well, - now that we have the GPO library, - let's go ahead and launch a python environment. - We do need to use pseudo here because of the way that the GP I module access is the low - level hardware. - So you need to make sure you run. - This is ah, - administrator here and from the prompt here. - We're gonna go ahead and import our pied a g p i o as g p A. - That's just says that we're gonna import that module and we're gonna reference it as GP io - . - So go ahead and do that. - They will do GP ioo dot rpai underscore revision and you could see we get a to here, - which is correct. - We found out before from my specific raspberry pi that my board revision was too. - So this is another easy way you can tell after you've installed libraries to get your - border vision instead of having to look it up in a table. - This also tells us that everything looks like it's installed properly, - So we are ready to start trying to access the input and output and seeing what we could do - with it. - As always, - if you need help or have questions, - the best place to go is on the Skill share website For this class under the community tab, - you can also reach him via email or find me on Twitter 6. How to Hook up an LED (Step 1): - Now that we have our GP i o module working on our pie, - it's time to start connecting things to it. - Now, - a few of the things we're gonna cover involve electron ICS principles. - If you don't understand them, - try to look something's up online to get help. - If that doesn't work, - feel free to submit questions to the skill share class page. - I'd be happy to answer those there. - So when we talked about our header on rpai and the GP I opens, - we talked about how they could be configured as either an input or output. - We're gonna tackle the output side first. - So if you remember from our header section you have a pin here. - Multiple pins, - actually that connect to ground will destroy one of them. - And we also have a pin appear that connects to 3.3 volts. - And we'll draw our I open like this. - No. - So the first thing to understand is when you configure a pin to be an output, - what happens when we toggle? - It's setting in our python scripts, - so there's two ways to set this. - We can either set this to a one or two a zero high or low. - And when we set an output pin when a pince configure doesn't output to a one or high, - it's gonna try and drive it to 3.3 volts. - If we send it to a zero or connected low, - it's gonna connect it ground. - And so what we want to do first is we want to see how we can connect an led to turn the led - on and off. - That's sort of the hello World project in hardware. - So let's see. - How we might do this we're gonna do is if we could do this like this, - we could come out of our Iot pin come down. - We need to go through resistor now, - the resisters to limit the current to our led. - That's the symbol for it. - And this is the symbol for our led, - and we'll connect it back down to our ground pin here. - So what's gonna happen is when we drive this pin high, - we set it to 3.3 volts. - Current flows from a high potential to low potential, - so it will try and flow from 3.3 volts down through here into ground. - The resistor limits the amount of current. - But when that current flows to our led, - that's what's going to turn it on. - So that's one way to get our led to turn on. - Now we can also hook this up another way. - Strong other. - I open here. - This time we're gonna draw the line out of our 3.3 volts, - go through the resistor through our led and come down to our pin. - So remember, - there's two states to the pin, - 3.3 volts and zero volts. - Now, - if we set this high or to 3.3 volts, - no current is gonna flow from 3.3 volts to 3.3 boats so are led Will not turn on. - However, - if we set this toe low or connected to ground, - that's when the current will flow. - So it's the exact opposite. - In this case, - if we set our, - um, - are pinned to be high, - that turns the led on. - We set our pinned to be high in this case, - it turns it off. - Just something to remember when we're writing our scripts for it. - Um, - we can hook this up either way. - The way I prefer to do it is this way and I don't want to get too much into the details of - how the raspberry pi handles this, - but something to know when we hook it up this way, - the Penis said to source the current. - In other words, - when it turns to 3.3 volts, - it's trying to provide the current to flow to ground to turn the led on. - In this case, - the pin is sinking the current, - the current actually being supplied from the 3.3 supply on the pie down through the - resistor and into the output pin. - Um, - it's called sinking current. - Now the raspberry pi can Onley source so much current based on the drivers that air in it - when I mean when I say drivers, - I don't mean software drivers. - I'm talking about hardware drivers that drive the current, - um, - for how these input output pins work. - So you can hook this up either way for our purposes, - we're gonna be using this one. - We're gonna be setting it up so that the 3.3 supplies is driving the current through our - led. - And so we just need to remember that in our Python script later, - zero is going to be on one is gonna be off. - So now that we've seen how this looks on our white board and how we might draw this up, - let's move to our bread board and see how we might connect this up to get it working. 7. Intro to Fritzing and LED Hookup (Step 2-3): - all right. - Now that we've seen how our circuit will look on our white board, - it's time to put it on to our bread board. - And I just want to take a minute to show you a tool that I like to use when I'm planning - out circuits for different projects that I work on. - And it's called Fritz Ing. - The website, - for it is just fritz in dot org's and I'll put a link to it in the show notes here. - But this is what it looks like here. - And what we're gonna do is we're gonna download. - This will come to the download page and you can see that I am running windows. - But you can download it for Windows, - Mac or Lennox. - So I'm just gonna download the windows version of it here. - Well, - that's downloading. - What this is is a good way to visualize on a bread board your circuit before you actually - put the components on your bread board. - It's great for more complicated circuits to visualize how you're gonna connect things also - , - to see how you're doing on real estate on your bread board, - things like that, - we're gonna use it in our project to show the different phases. - As we connect things up, - I'll do these all inference sing, - and I will show you what they look like in that before we put him on the bread board. - Now that's finished. - I'll just pull this window over here. - That's just in a Zippo here. - Soldiers to extract all once that's extracted will just come into the folder here, - and we'll just double click on it to launch it and let me just go ahead and slide this over - here are screen resize it here. - Okay, - so it comes up to this nice welcome screen, - but we're gonna come right over here. - This is where we're gonna work most of our times in this bread board. - You and you could see this looks just like a real bread board. - This is one of the things I really like about spritzing is it has a very real life. - Look to it. - You just use the scroll wheel on your mouse to zoom in and zoom out, - and then all you do is you grab parts over here, - there's different libraries and you can download additional libraries and you just drag him - over onto your bread board, - example resistor. - And down here you can change the values. - You know, - just the color bands. - Just how it would look in a real resistor. - You can add things like switches that will get into later and and buttons things like that - now fit in there. - They're all the right size. - And when you want to connect things up, - you can just come down here. - There is a wire part, - and we can just, - ah, - click from here. - So we have to drag them on here. - Tatchell wire. - From here to here, - you can even use the control A control click that will kind of give you this curve. - Look to the wire seeking bend, - Um, - and kind of make it, - you know, - look a little more rial, - which is nice. - The other thing it has Ah, - that will be doing next. - Things like this weaken dragon led on and connect that up will show you how to do that in a - minute. - And even if you come to the microcontrollers, - I believe is the name of section here we can even drag on a raspberry pi the mountain You - could see you could move things around. - You can If you get down in the corner here, - you can You can rotate him and moving like that. - Anyway, - I just want to give you a brief introduction. - This is a tool that will be using you don't have to. - I just like it. - Like I said to plan my layouts on my bread board, - see what space is gonna be taken up and how I want a route wires and things like that. - So just something to be aware of. - I'll be showing you the fritz seeing images as we go along as we connect different parts - and it's a great tool. - If you want to download it and play with it, - check it out. - Okay, - so here is what are led circuit? - Looks like when we hooked it up to our bread board. - Inference sing. - This is just like we drew it up on the white board. - We come out of the 3.3 volts header pin through a resistor. - In this case, - it's 330 OEMs into our led and then are led. - Connects back to an input output pin. - In our case, - it's gonna be pinned 12. - Now, - in this diagram, - it's labeled 18. - That is the BCM reference number, - and we're gonna talk about it a little bit. - The difference between the board pin number, - which is this, - is literally pin 12 if you remember how he counted it in Unit three. - But it's also referred to has been 18 to the broad common chip. - We'll talk about that a little bit later, - but for now, - this is how we have it set up. - A couple things to note. - If you are not familiar with how bread boards work, - it's really easy along the top and bottom. - The pins connect horizontally. - If we're looking at it like this and then in the middle, - the pens connect vertically. - They do not cross the middle of the bread board. - So the pens on this side on the top side here are not connected to the pins on the bottom - side, - and the other thing to remember is your led has to be hooked up the right way. - You can hook it up backwards, - which will burn it out, - so make sure you're paying close attention to how you hook that up. - So this is how I intend to lay it out on our actual bread board. - Let's see what that would look like. - And that's what it looks like when I have hooked it up to my actual bread board. - Now, - for those that aren't using the pie cobbler, - that's okay. - I have the Fritzi image here, - uh, - connecting it straight to the raspberry pi. - You're also welcome to do that. - It will work just fine. 8. Use Python to Control an LED (Step 4-5): - with our led connected to our bread board. - We're ready to test it To do that, - we're gonna come back to our terminal here and we're going to launch a python show. - We're going to do that with pseudo python. - We need the pseudo because of how a library works. - The rpai g p a library works. - And so we're going to import that library just like we did in the last section. - Import rpai dot gp io as g p i. - O. - And then the very first thing that we're gonna need to do when we're using this library is - we need to set the mode. - When that does, - is sets the mode of how we're going to reference the pins through other various functions - that will use and their two options. - There's GP ioo dot board and GP ioo dot bcm board means we're going to refer to the pins by - their physical number. - When looking at the header, - we went over that, - um, - in section three, - where we talked about the GPO header how the pins are numbered. - The BCM mode means that we're gonna need to refer to the pin numbers based on how they're - numbered. - on the Broadcom chip that the raspberry pi runs on. - And so you can do this either way. - However, - if we use the board were guaranteed, - that are python code will work between revisions because the library is smart enough to - know what revision were using. - So I'm gonna recommend that we use the board mode. - Okay, - Now that we have that set, - we need to set up our pin whether as an input or is an output. - In the case of the led, - we're gonna set it up as an output. - So for that, - all we have to do is GPO dot set up reference the pin number. - We're connecting to pin 12. - If you remember from our bread board, - and we're going to set this to gp ioo dot out that sets it up is an output. - Soon as we hit, - enter on this, - you'll notice that the led will turn on. - That's because the default state of the output zero if you remember from when we drew this - up on our white board, - when we set the output pin to zero, - that's what's going to turn out. - Led on. - That's what happens here. - So to turn it off. - You remember. - All we have to do is set it high. - So we do that by saying, - gp ioo dot output Reference the pin number again, - and one when we hit Enter here, - you'll see that it turns off and we could just play around with this. - We can turn the led, - um, - back on. - We can turn it back off. - That's all there is to it. - We have successfully connected in led. - Now, - when we're done using this library before we exit, - you want to do a GP Io dock cleanup that just cleans up how the pins were set up. - If we don't do this next time we try to run a script, - it's gonna tell us that the pain is already configured and give us a warning. - It's not a huge deal, - but it's just a good idea to clean up when we're done, - and that's it. - We've got our led working. - Now that we've talked about how toe configure pin is an output, - let's move on to the input functionality in connecting are switching button as always, - if you need help or have questions, - the best place to go is on the Skill share website For this class under the community tab, - you can also reach him via email or find me on Twitter 9. Safety Reminder: - before we go connecting things, - start bread board. - There's just a couple of things I want to mention the first I already mentioned in the - header unit, - and that's whenever you're making connections to your raspberry pi. - Always unplug it. - Some of them you can make safely, - but it's best not to risk it. - Just unplug it. - It takes two seconds. - Make your connections. - The pie boots up very quickly. - It's not worth risking burning out your pie. - The second thing along the same lines is depending on where you live. - You might build up a lot of static electricity when you're moving around. - Right now, - we're just putting things on a bread board that are out in the open. - You can touch resistor legs and other things, - so you need to be careful when you're getting in there and pushing the button and moving - the switch. - And before you do that, - it's best to discharge the static electricity that builds up on you. - Touch something that's grounded to get that spark out of you. - That way we won't ruin anything. - This stuff is really pretty safe. - People get scared that don't know Ah lot about electricity. - They think I'm an electrocute myself. - I'm gonna burn my house down. - We're dealing with low voltages and low currents. - I'm not saying don't be safe. - I'm just saying Don't worry too much. - You might get a shock. - You might burn out your pyre, - your components. - That's really probably all that will happen. - So I want you to have fun, - but be safe when you're doing it. 10. Theory and Switch Hookup (Steps 1-2): - All right, - so now we've covered how to hook up and input output pin to be an output to driving led. - Now let's talk about how we can configure it to be an input to read this state of things - like switches and buttons. - So when a penance configured as an output, - it's actually driving the state. - It's saying be 3.3 bolts or be tied to ground when it's configured as an input pin. - It's going to read this state of the pin and decide. - Are you closer to 3.3 bolts? - Are you closer to zero volts now? - It's not so simple. - Listing which one's closer. - There's certain limits that determine what's high and what's low. - But that's the general idea. - And so we want took a circuit up to this so that when we, - uh, - flip a switch, - it'll give us one state. - And when we put the switch back in its original position, - it will give us another state. - So let's see how that how we might do that. - So we're gonna come off of the 3.3 vote pin up here. - We're gonna come down, - and we're gonna just leave this open and we'll pretend that this is our switch like this. - Come down and well connected ground. - And so this point, - the switches open, - and then we're gonna put our Iot pin out there. - Now, - this might look fine to start with. - So in this state that we have here, - if we sample this pin, - it's going to read ground, - cause this is open up here. - If we close this switch, - it's going to connect us up to 3.3 volts. - That might seem like an easy way to get a different state when the switch is in one - position, - as opposed to the other position. - However, - this would be a terrible idea because as soon as we flip this switch, - we're gonna connect are 3.3 volts to ground, - and that's when you kill your pie. - So how can we make this circuit a little better? - Well, - we can't have a short circuit. - So what we can dio is we can just put a resistor in here. - So now when we flip our switch, - the 3.3 volts isn't gonna be tied straight to ground. - It's gonna go through resistor first, - which will prevent that short circuit situation and so will read still ground when the - switches disconnected, - even though the resisters there that'll still read ground. - And then when we flip our switch, - it's gonna read 3.3 volts. - So this is pretty good. - We could connect this up and this would work just fine. - However, - there is one thing that we might want to consider. - What if we accidentally configure this pin to be an output? - Well, - in that case, - it's gonna try again to drive the state to 3.3 volts or ground. - Now, - if it's set to 3.3 volts and the switches closed, - nothing's gonna happen. - No currents gonna flow from 3.3 volts to 3.3 bolts. - But what happens if it's configured as an output and we try to drive it to ground? - So we set it to a zero. - If we do that and we flipped the switch, - we're back to the state where 3.3 volts is gonna connect straight through the ground and - we're gonna have a short circuit. - So now we could just be really careful and not miss configure our pin, - but nobody's perfect. - And we might make a mistake and so we can prevent a problem here by again using a resistor - so we'll have a resistor here. - Now again. - When the switch is open, - we're still going to read ground. - And when we close the switch, - we're still going to read. - 3.3 volts will have to choose the values of these so we don't drop. - Um, - too much voltage here. - But this will work just fine to read the two different states 3.3 volts when the switch is - closed and ground when the switch is open. - Now that's all there is to it. - So that's how we would do a switch. - Now let's take just a second to talk about a button. - Well, - turns out are we have to do is take out that symbol and replace it with our symbol for a - button. - We'll use that as our button because really, - a button is nothing more than a momentary switch. - When you press it, - it closes the circuit. - When you let go, - it opens the circuit back up. - Just how when you flip a switch, - it closes a circuit. - When you flip it back the other way, - it opens it back up so there's really nothing we need to do differently for a button than - we already did for a switch. - So we'll actually use the exact same layout for our button that we're gonna install that - we're going to use for our switch. - Let's take a look at how I've mocked this up in print. - Sing Now. - I changed a few things since the last view of just the led being connected. - You'll notice that I've taken the 3.3 bolt and ground connections off of the cobbler and - attached him up across the top. - And if you remember from the previous explanation, - the pins on the top of the bread board connect horizontally. - So by connecting the black and green wires here, - I get 3.3 bolts in ground all the way across and Aiken Branch off of those wherever I need - now, - a convention that I'll be using here, - and it helps me to stay organized and keep my connections right is that I'll use green - wires to represent ground, - and I use black wires to represent 3.3 bolts. - For me, - it's easy to remember Green and ground both start with G, - and it just helps me keep myself organized. - So now let's take a look at this switch connection here. - Well, - the very first thing that we need to know is, - how do we connect our switch? - It's got three pins on it, - but what did those pins mean? - And a common theme that you'll see as we go through this is that the data sheet will tell - you everything that you need to know when working with things from switches and buttons all - the way up to our display in temperature sensor. - So let's take a look at a section of the data sheet for the switch. - We can see that the switches three pins labeled 12 and three with to being in the middle - that if we look here on the left, - we can see the little circuit drawing, - which shows that, - too, - is the common in connects toe one in three, - depending on which position the switches in. - So if we switch the pin to the left when looking at it at this view, - it connects pins one in two. - If we switch it to the right, - it'll connect pins two and three. - Now we only need two of the pens, - so we'll leave Pin three unused for now. - So with the switch in the position to the right pins, - one and two will be disconnected. - And when we switch it left pins, - one and two will be connected, - and that's all we'll need to hook it up to our input pin. - So if we look at this connection, - we can see that we have 3.3 bolts coming down to pin one and then pin, - too. - If you remember, - we talked about having to protections one to protect the input output pin if we - accidentally configured it as an output and to prevent the short circuit to ground. - Now the values that we chose for these resistors Air one K in 10-K see the show notes for a - link that has a good explanation of how to hook switches up toward input output pins and a - deeper explanation of how he came up with those resistor values. - Another conventional use here is yellow wires to indicate a connection to an input or - output pin. - In this case, - we've connected our switch to pin 16 so we'll need to remember that when we start writing - our scripts now that we've mocked this up. - Inference sing. - Let's see what it looks like when we actually hook it up on our bread board. - Here's what mine looks like before starting the next video, - go ahead and take a minute to hook yours up a swell so you can follow along in the next - lesson again. - If you're not using the pie cobbler, - here's what it would look like if you're connecting it straight to your pie. 11. Switch Integration (Steps 3-5): - with our switch hooked up to our bread board, - we're ready to start testing it. - So we'll launch a python shell just like we've done before using pseudo, - and we'll follow the same pattern like we did when we set up our pin is an output we need - to import the rpai that GPO library. - And remember, - the very first thing that we need to do is a set mode, - and we'll continue to use the GPO dot board mode. - And this time, - when we do our set up, - we're gonna choose pin 16 because that's what we've connected to honor Header. - And instead of doing gp ioo dot out, - we're gonna do GPO dot in because we're configuring this pin to be an input. - And all we need to do to test this state of Arpin is G p i o dot input and pass the pin - number again. - 16 and you see here we have zero, - and that's because our switches in the right position so pins one and two on our switch or - not connected. - Now, - if we come over and move our switch to the left and try it again, - you'll see that we get a one this time and we could just test that again. - I still get a one. - Let's go ahead and switch it back and we get a zero again. - So this is working perfect, - just as we expected. - When the switch is in one position, - we get a zero. - When we flip it, - we get a one. - So far in the class, - we've been doing all of our python testing from the show. - I think it's a good time now to move into a file so we can start running this and not have - to re type in everything every time. - So to do that, - let's just from our command line here. - Let's make a directory using the Make dir command. - I'll just call mine skill share again. - We'll be using the Nano program to edit our file, - and I'll just call it monitor dot pie. - So in here we're gonna do the same thing that we did from the show. - We're gonna import rpai dot GPO as g p I. - O. - And then, - of course, - we have to set our mode, - which is board that we've been using, - And so to test our move to a file, - let's just do something with our led. - So I'm gonna put a comment in here to set up the only deep in. - And if you remember, - just like we did in the show, - we'll do gpu dot set up 12 is what are led is connected to and will do. - GP ioo dot out Now, - if you remember before when we did this set up the default state is zero, - which in our case, - turns are led on. - But what if we don't want the led to turn on when we configure our pin? - Well, - there's a nice little argument that we can add on to the end here called Initial, - and if we say initial equals g p i o dot high. - When it sets up the pin, - it will make the initial state high, - which in our case, - will turn the led off. - Then we'll just do a simple test. - Here will do GP ioo dot output 12 and then set the pin low, - which will turn are led on. - And then we'll just wait for a second and try to turn the led back on just a simple flash - test to make sure that our script is working as we expect and so toe weight that second, - we're going to need to import another python module. - So we'll come up here and we'll do from time import sleep. - And all that does is gives us access to this sleep function in the time module so that we - can call it here right after we turn our led on. - So just to asleep one which will hold us up for one second, - and I will do GPO dot output 12 and said it to gp ioo dot high, - which will turn are led back off. - And of course, - when we're done, - we want to do the cleanup like we talked about not exit out of Nano. - All you want to do is hit control X, - and it'll ask you save modified buffer. - And if you say yes to this since we already gave it a name, - it will follow up with the question file name to write monitored up I If you just hit enter - here, - it will save our file. - And if we do a directory listing, - you'll see. - Now we have this monitor dot pie file. - I forgot to change directories into the Skill share directory before I created this. - So let's go ahead and do a move monitor dot pi into the skill share boulder, - and you'll see that it's missing now. - So let's CD into the skill shareholder, - and you can see our files right there. - Justus we needed to use pseudo to launch. - The Python show will also need to use pseudo when we run our scripts. - So to run that monitored up I fall that we created. - We'll just do pseudo python and then give it the file name monitor dot pie and we'll go - ahead and hit. - Enter here and perfect There's are led. - It gave us one flash. - That's all it was supposed to do. - So we've successfully created the base for our script that we're gonna right. - So all the code that we write in the future now other than doing maybe quick tests will be - in this monitored up I and we'll just build this file up until we get all the functionality - that we're looking for. - Now that we've shown that we can write our code in our Python file, - we don't want it to flash our led. - That's not the functionality that we're looking for. - So let's write something a little bit more meaningful, - so we'll go back into our file here. - Well, - just come down here and follow the same pattern that we did with the led. - Penn will put in a little comment that we're setting up the switch pin well. - The gp ioo dot set up. - Remember, - it's been 16 this time is what are switches connected to. - We're configuring it as an input. - So the gp ioo dot in and let's go ahead and get rid of this code that we wrote to flash the - led Ah, - quick tip in Nano is if you get on a line that you want to delete the entire line. - If you just hit control K, - that will cut our entire line out. - So we'll just do that. - Get rid of those lines. - One thing that I found very handy when working with the raspberry pi in Python scripts is - to wrap on my code in a try except block. - Basically, - what that does is everything you put under the try. - It will run normally, - and then when it reaches an exception, - it will go into your except handling. - And the reason I do this is because when we run these from this show, - they will just run forever. - And the way you cancel those is by hitting control, - See which exits out and brings you back to the shell. - Prompt. - Well, - when you do that, - it could be anywhere within your main try block. - And so we want to make sure that things get cleaned up. - Remember, - we need to call that gp ioo dot cleanup. - So the handle that I do in except keyboard interrupt what that'll do is it'll hit that - except block when we do control. - See? - So let's come in here to our try. - And what we want this to do is just run forever. - Like I said, - until we hit control c So we'll just wrap everything in a wild, - true loop and we'll just test our switch. - So we'll say if GPO dot input 16 is high, - so they have flipped the switch to the left. - Then we want to go ahead and do GPO dot output 12 is our led will set it too low, - so we'll turn it on. - So what we're gonna do is we're going to set this up so that when we flip our switch are - led turns on. - And then when we flip it back to the right, - the led will turn off. - So this will come in here and sample if it's high turn are led off and we'll do else. - So the switches in the low reading or to the right We just want to do GPO dot output 12 and - then said it high, - which turns it off. - And then down here in the except Block, - is where we want to do the cleanup. - So put that here. - Then we'll clean up our line down there and then finally will just want to exit gracefully - . - So we'll do assist dot exit and given a code of one now to be able to use this system exit - , - we need to go ahead and import the module for that. - So we'll come back up to the top here. - We're just doing import cysts that way it won't throw an error when the script runs. - All right, - that's it. - Let's just give this a quick look. - And you see that I have a typo there. - I didn't put the tea on inter evidence where I like to give Dakota quick once overlooked - before I close it and save it. - So let's fix that and we will save an exit. - Okay, - let's run this again using pseudo, - and we'll come over here and flip our switch and you can see it turns are led on Perfect. - Just like we wanted. - Now that switches working as we want it to, - Let's move on and see how we're gonna hook up our button. 12. Button Integration (Steps 6-7): - remembering back to the first video in this unit, - we explain how a button is nothing more than a momentary switch. - So this is how I have it constructed, - influencing. - Now It may look different because I have the resisters oriented a little differently, - but if you look at the connections, - it's all the same. - In the case of our switch, - our 10-K resistor goes through the green wire up to ground, - where, - in the case of our button, - I've just attached it straight to ground without using the wire. - And then are 3.3. - Bolt connection comes down to the other side of the button, - and the one K resistor goes out to the GPO wire, - which is again yellow. - You'll notice that my button straddles the middle of the bread board. - Oftentimes you'll do this if it would be a problem to connect pins in the same row - vertically. - In our case, - our button doesn't have that problem. - I just felt like installing it this way for real estate purposes, - and the reason I know that it's not a problem is by looking at the data sheet again. - If you look at the dimensional drawing here in the lower right hand corner. - We have the terminal arrangement. - We can see that pins that share a side of the button aren't connected to each other. - In fact, - the connection goes from pins 2 to 4 and 123 Now you could arrange this anyway so you could - set it up. - So pin one and pin four connected when you push the switch or pins two and three. - But it's important to note that pins one and two are connected together and pins three and - four connected together again. - This is where the data sheet is so critical it shows the internal connections. - The only other way that you'd be able to figure this out would be to hook up a meter and - keep pressing the button to try and figure out which pins were connected when the button - was pushed in when it wasn't pushed. - This is how we haven't laid out. - Let's see what it looks like in my set up. - And for those not using the pie cobbler, - this is what we have so far. - So go ahead and take a minute and hook your button up so that we can test it. - Okay, - So now that we have our button installed, - let's go ahead and write some code to test it. - So it's open apart, - monitor Paul again and we'll do just the same thing that we've done before. - We'll go ahead and we will put comment in here about setting up the button pin. - And we'll set that up just a lot like the switch been if your call from when we talked - about the theory on this ah button is nothing more than a momentary switch. - So we'll set it up as an input. - And now all we need to dio is we're not going to interact with the led A to this point with - our button. - What we're gonna dio is going to come down here in our loop this while that just - continuously runs and we're gonna do some logic around the buttons, - so we'll see if gp iota input 18. - I remember that's the pin number that are Button is connected to the pool's. - Did she pay a high? - And for now, - we're just going to print something to the screen just like that. - So now we've set up our button isn't input. - We've We're gonna check in on while loop. - And if we press it, - we're just gonna print something out to the console to make sure that are set up is working - correctly. - So let's go ahead and give this shop. - We're gonna go ahead and exit. - We want to save our changes. - Okay, - so let's go ahead and run this pseudo python monitoring up. - I I should just be running there and then let's come over here and touch her button and see - what happens. - Okay? - We got a ton of pronounce here of you pressed me. - So what is going on? - Let's go ahead and cancel this hour. - It's obviously isn't working the way that we wanted to. - Let's go back in to our file here. - Now, - this while live this while true, - right down here is gonna be executing so fast, - and it's gonna be testing that input so fast that just the little bit of time that you have - that held down the button how down? - Even though it's a fraction of a second, - that logic is being executed. - Testing If it's high and it is high for those, - you know, - whatever. - 102 100 milliseconds. - And so it's gonna print that just as many times as it can in that amount of time. - So an easy way we can fix this the simplest way this just come down here and will say sleep - for a second. - So after you've printed out the message, - just hang out for a second before you do anything else, - and that gives us enough time to release the button and see if that changes things. - Let's go ahead with that. - CLECs it out will save. - And now let's go ahead and run this. - All right, - let's come back over to our set up here and push a button and see what happens. - Okay, - pressed it a couple times there and we only got one print out, - so that's working pretty good. - It's good and close this again. - So now what might be an issue with this? - We come back in here if you notice when this sleep executes here. - It's holding up our entire program, - so it's not checking this switch. - That's not doing anything else. - It is literally just staying right there until the one second is up, - and then the program will continue to execute. - Now there are ways to get around this the GP I O module has some things for event detection - , - and I played around with this quite a bit, - where you can create callback functions where the hardware will detect a change and call a - function that you write any time there's a rising edge, - which is what we're doing. - When we press the button that we're going from zero volts to 3.3 volts, - it will detect that rising edge and call your function. - However, - when I was missing around with that, - it was very sensitive to static electricity, - to the point where if I moved around in my chair, - it would set off interrupts, - as in it noticed that there was a change on the pin. - Now we can eliminate that. - We could do some things to D bounce our switch, - and we could shield the pie to make sure it's not picking up anything like that. - But it was really interesting to see how sensitive that was when we set up the edge, - detect. - So to keep things simple for now, - this sleep, - even though it is holding up our program some it's not really gonna hurt us in this - application, - so we're not gonna get much more complicated in this. - I'll put a link down below, - too. - Some of the resource is on the event detection, - so you can look at that If you'd like Teoh, - it is an option. - But for now, - we're just gonna keep it simple and just use this sleep. - We've covered a ton during this unit. - We have are switching our button connected and working and integrated into our python - script. - And that's great. - If you've made it this far, - you're doing an awesome job. - Now we're gonna get into the two communication protocols SP I and I two c But before we do - that, - we need to take care of a little bit of sauntering and that's up next in unit seven. - As always, - if you need help or have questions, - the best place to go is on the skill share website For this class under the community tab, - you can also reach him via email or find me on Twitter 13. Soldering Headers: - before we move on to talk about SP I and I, - two c, - we need to get our boards ready to attach to our bread board. - As you can see here, - we've got the display on the left and temperature sensor on the right. - Now these air on what are called breakout boards, - which means the pins are broken out to these little holes that you can see. - But there's no way for us to connect it to our bread board. - So we need to fix that by sauntering on some headers or wires. - Now, - the best way to do this is to take some header pins like you see here and use a pair of - pliers to break them down into the sizes that will need on the display. - Will need one of seven to cover the ground pin to the select been and then, - as you can see on the temperature sensor, - we're just gonna need four on the top two on the bottom. - So this is what they look like broken out. - This is what the header would look like if we held it in place on the display module. - Now, - one of the tricks that I used to make this easy to sauder. - Is it? - Just use a bread board as a jig. - And so we'll put our header in with the long portion actually installed in the board. - So the short ends air sticking up in such a way that we can layer module on top. - And it's just easy to Sauder. - So this is what it would look like for the display. - Now, - the two pin header you see here is just to keep our board level during sauntering. - We're not actually gonna sort of that. - In fact, - that would be a disaster because, - as you can see on the board here, - that's the negative and the positive. - So we would essentially be shorting it together when we tried to power it up. - And this is what the full jig looks like with the display and the temperature sensor - headers plugged in. - So we're gonna do is set our modules or a breakout boards on top of those header pins like - you can see here to get ready for soldering. - I wanted to make a video to show you how to sauder this. - Unfortunately, - my camera wasn't good enough to get a real tight shot. - There's lots of tutorials on the Internet for how to Sauder. - And this is probably one of the simplest things to Sauder. - So don't worry about this. - You can do this, - not a problem. - So here's a close up of our temperature sensor sitting on the jig. - So what we're gonna do when we saw her, - as you can see in this picture, - is you want to have your sauder on one side and your iron, - your hot iron on the other, - and what you'll do is you'll touch the iron to the pin and the pad on the breakout board - and then bring the sauder in from the opposite side. - So this is a pin after I've sorted it, - and here's the entire temperature sensor sauntered, - and we're just gonna repeat and do the same thing on the display. - This is what it will look like when it's done. - So now we've got our to breakout boards, - Stoddard, - and we're ready to install them on her bread board for a project again. - Don't let the sauntering freak you out, - even if you've never saw her before. - This is something that you can handle, - I promise. - If you don't have hitters. - That's all right, - too. - You can use just why you could just saw her wires directly in here. - It's a little harder because you have to hold the wire and, - um, - started at the same time, - but not impossible. - In fact, - here's a module that I've used before where I've done just that. - I didn't have headers, - and so I used regular wires that I had stripped. - I said, - It's a little hard to do but doable. - If you don't have headers, - I recommend headers. - If you can get a hold of some. - All right, - that's it. - Now that our boards air sodded, - we're ready to move on to the next unit. 14. Intro to SPI (Step 1): - Okay, - so the first protocol that we're gonna cover when connecting to our pie is the S P I - protocol. - Now, - with any communication protocol, - the goal is to connect two things together. - So in our case, - we've got a raspberry pi. - We want to connect that to our display an SP isa protocol that we can use to do that. - So when we're connecting anything together, - the first thing we need to take care of is the power. - Now, - the pie, - if you remember, - has 3.3 volts and five volts, - and it also has a ground. - Now it's important to remember when you're working with S P I or any protocol for that - matter where you're connecting to devices together, - that you're consistent with your power. - So you don't want to be sending five volts down to a device that can only handle 3.3 bolts - . - That's how you're gonna start breaking Things now are display is able to handle 3.3 volts. - So we're okay to share the power from the pie to power. - Now we do have to worry about doesn't have enough power, - cannot supply enough current, - and we'll look at that a little bit later, - but for right now, - let's just assume we can share it so we'll connect the power there in the ground here. - Now, - even if you have a scenario where the display is gonna have its own power, - you have to connect the grounds together. - That way, - when the lines are talking the signal wines, - they are referencing the same thing as ground. - Because if this has a different idea of what ground is than this, - when you start sending data across it, - it could get very confusing. - The data is gonna get jumbled. - It probably isn't gonna work out really well for you, - so make sure you connect the grounds. - So the next thing we need is how do they communicate together? - Well, - an SP I It's known as the three wire or four wire protocol, - and that's because it uses generally three or four lines and we'll draw each of those here - . - Now, - an important concept in S P. - I is an idea of master and slave that always has to be a master device, - which in this case is gonna be a raspberry pi and a slave device. - All communication in this protocol is driven by the master. - And just as a side note, - the raspberry pi actually can't be a slave. - So you couldn't hook to raspberry PiS together. - One is a master and was a slave because the only mode this can operate in is a master role - in the SP I protocol. - It's our first line is what's called Master Out slave in and this is always referenced from - the perspective of the master. - So on the display, - it's also gonna be called Mos Side because it's still master out Slavin because the display - is the sleeve. - And that's where data goes from our master towards our slave. - The next one is master in slave out, - which, - as you can guess, - it's just data coming back this way. - No, - the state is sent only when a clock is running. - A clock is what keeps the two devices in sync to make sure that they're sampling and - reading the data at the same time. - So that's the next line. - This is our clock line, - and it's gonna look something like this. - And remember, - this is always driven by the masters so the master will supply this clock, - so the display knows what speed to run out, - and then the final line is what's called Chip Select. - Now you'll see this sometimes with a bar over it like this. - That just means that it's active low. - Typically, - we think of Ah zero as being zero bolter ground and then hire one being 3.3 volts or five - volts, - or what's known as the supply voltage. - In this case, - the chip select is the exact opposite. - When it's pulled low or its pulled the ground, - that means it's enabled or it's turned on. - So these are the four lines that we need to communicate with now. - One of the unique parts about S P I is that data is transferred at the same time, - meaning as the master is sending data down to the slave. - The slave is always sending data back Now, - it may not be actually putting data on this line, - but the masters going to read it. - So on every clock edge that the master is sending data down this line. - It's also sampling this line to see if data is coming back. - So SP is not really a good protocol to send a bunch of data and then wait for a slave to - process it and then the slave decide to send it back because, - remember, - it's always driven by the master. - So to get data back from the display, - the pie has to be sending data. - And so the way this will happen sometimes is if the master needs to read from the slave. - It'll just send dummy data down. - That's the same length is the data that it needs to read back. - So one of the cons of this is it's a little tricky sometimes to get communication going - both ways. - It's really meant to be driven by the master. - Now, - one of the pros of this is that we can share these lines so you can see we have the display - here. - We could also have another module, - maybe something like a sensor down here that can share all of these lines. - So it's gonna take the master out slave in the master and slave out in the clock all shared - with these lines here. - So when you hook it up on a bread board, - they can all share those three lines, - and then it's gonna have its own chip select right here. - That can come up and connect to the pie and so it can share these three lines just with the - display and only needs one additional line. - So for each additional device, - you only need one additional pin on your raspberry pi. - So it's important to remember one of the great things about SP I is. - It's a standard meaning as long as the display or the sensor or whatever other module you - want to connect is SP I compliant, - meaning it knows how toe work on this protocol, - you can connect it in the exact same way that we've connected display in the center here. - You don't have to figure out or right special code to create a communication protocol. - It's just that is it s p I is the standard and they know how to talk to each other Now that - we understand how to connect to devices that are going to communicate with S P I. - Let's talk about the different modes. - You hear people talk about SPE mode zero or one, - there's actually four mode 012 and three. - Let's talk about what that means. - So I remember when I said that the data is driven by oclock line in a clock line is gonna - look something like this when it's transmitting data. - And if we think of this is zero bolts and this is 3.3 volts. - In our case with the pie, - this is what's known as clock polarity zero or C pole zero. - Now, - just like the Chip select line can be active low, - the clock can be active low as well, - so it might also look like this going from 3.3 volts down to zero. - So again, - this is 3.3 volts. - This is zero. - This is what's known as clock polarity one. - So those are the two different ways that you can drive the clock line. - Now data is going to be put on that master out and master in line during certain windows of - time while this clock line is running. - And so if we think about this, - if we think of the windows where the data can come in where maybe it's zero here or this is - a one and this is a one. - What's known as the clock phase determines when the master and slave or going to sample the - data lines to determine whether it's a one or a zero and so we can either sample on the - leading clock issue of the clock starts here on the left. - If we sample on the leading edge, - this is known as clock phase or C p h Not C p A c p h a of zero and so you'll see the data - will be sent in such a way that whether the clock is active, - higher, - active, - low, - the phase of the data is gonna sample so that it's sampled right in the middle of the data - on the leading edge. - Now, - clock phase one is the exact opposite. - It's gonna sample on this falling edge. - So if he's sample right here, - you can see that's gonna sample right in between the windows. - But on clock phase one, - the windows are actually gonna shift the phase of its gonna shift. - So look, - just something like this. - Where again, - this is a zero. - This is a one. - And if we did one more, - this would be a one that would be clock phase one. - So we've got CPJ zero cph a one. - Now The combination of these two things are what determine the mode and so if we look at it - clock polarity, - Clark fees and then mode. - This is a simple table. - We've got four options you've got 0001 10 and 11 of these Corp respond to the different - modes. - So if clock polarity is zero again like this and the phases zero So the data looks like - this. - That's mode zero. - Now we just got on the table, - would one to and three Now, - by default, - the pie operates in mode zero, - and so does our display, - so we're not gonna need to do anything special. - But it's good to know, - because you may have a slave device that says I use mode to and so you need to calibrate - the master so that it speaks that same language as it were so that the clock is in sync in - the day doesn't sink. - If all of this still seems a little confusing, - don't worry about it. - For now, - we're gonna connect things up. - You'll see how it works. - There's lots of resource is online that you can learn more about this. - You don't have to have a deep understanding of the different modes of SP I when the - polarity and the phase are to be able to connect devices and talk over this protocol. - I just wanted to give you a heads up so you could have a general idea of what we're talking - about when we refer to things like SP I mode zero or clock polarity and things like that. - So now that we've talked about the theory, - let's get in there and hook something's up. 15. Enable and Test SPI Protocol (Steps 2-6): - So now that we've talked about the theory of SP, - I let's get it working on our pie. - So we're gonna come to L A next out organ and there's a page dedicated to SP I on the by. - It's got a lot of different information here. - We're not gonna cover it. - All right? - Now, - we're gonna come down to this link for the loop back test because what we want to do is see - if RSP I module is working. - It says this could be used to test SP. - I send and receive. - Put a wire between Master Out Slavin and Master and slave out. - Okay, - so we're gonna come over to our set up here, - and you can see this blue wire just right here is connecting our master out slaving in our - master and slave out on our pie cobbler There. - So we have that connected just like it says in the directions in the next part. - Says we need to download a file here. - So I've got my terminal here where I'm ssh into my raspberry pi and were literally just - going to copy exactly what they've written there before. - We do that, - though, - I want to make a little directory here that we can work out of. - So we'll do. - Make sure S p I Then we'll CD into that and we'll just do this command. - Now, - don't worry about what w get is. - It's just a utility that's gonna allow us to download a file in this case, - the SP I Dev underscore test dot c file. - So let's just go ahead and do that. - Okay, - So that's gonna download if we just do an ls will see that it's there. - The next part of this is a GCC line. - Now, - if you don't know much about compiling files, - that's okay, - that's all we're gonna do here. - We need to compile this source file. - So we're going to GCC desh Oh sp I underscore test in the file that we're gonna compile is - the one that we just downloaded. - So this is going to do is it's gonna create an output file called SP I Devin or score test - . - Let's go ahead and hit Enter. - That only takes a second. - If we list the directory again, - you'll see we now have this SP I Devonish court test and we're just gonna run that just - like it says in the instructions. - Okay, - Now it says can't open device. - No such file or directory. - So what's going on here is that by default SP I is not enabled on our raspberry pi raz B - and distribution. - And we can see that by looking at a special file. - We'll just use this cat command again. - We've used it before. - We're gonna look in e t c mon proved dot de raspy blacklist dot com We had intern that - we'll see It says blacklist SP I and I two c by default. - Many users don't need them and then it hasn't blacklisted. - Well, - we do need them, - so we need to turn that off. - So we're gonna go pseudo nano. - Don't forget the pseudo because we need elevated privileges to change this file, - and we're just gonna go to that same file, - okay? - And in here, - all we're gonna dio is we're gonna put a pound symbol in front of the S P I line, - which basically turns that into a comment. - We'll save our changes now, - After we do that, - we need to reboot for the changes to take effect, - so we'll just go ahead and do a pseudo reboot and we'll wait for that to come back up. - Okay? - Our pies booted back up. - And now we're ready to try our test again. - So let's change directories back into that s p I directory that we created. - And if you remember, - we compiled that file. - So we're just going to run it again dot slash sp i dev Test Dash d. - Okay, - now we get this SP I mode bits per word, - Max Speed. - The important part is this block of characters this f f f the 40 line that tells us that - data is making it out of the master output pin and onto the master input pin. - Now, - we've confirmed that the ESPN module is actually working on our raspberry pi. - That means we're ready to connect our display, - and that's what will take care of next 16. Basic Setup and Test (Steps 1-4): - remembering back when we talked about SP I there were some things that we needed to know to - be able to use the protocol, - things like the clock mode, - how fast we could run it and what wines that we were going to use. - And so, - before we can hook up our display, - we need to know those pieces of information. - So for that, - we're gonna turn to the data sheet. - Now, - remember, - we've said this before the data. - She is your friend for these projects. - It will tell you the answer to most of the questions that you have. - Some data sheets are better than others. - And so we're gonna go ahead and look at the seven segment display data sheet that sparked - point is put together as far as data sheets go. - This is right there up at the top. - They have done a really nice job of laying it out, - making things very clear and easy to understand. - And the information is very accessible. - Let's come down here to the data sheet links section. - We're gonna go into this interface specifications link. - As you can see, - it's gonna tell us about the U. - R S P I and I two c communication protocols. - As you can guess, - since we've been talking about the SP I protocol, - that's the protocol that we're gonna use to talk to our display. - So if we come to this section here, - we can find all the information that we need to talk to our display. - We can see that we're going to use the slave data in also known as the master Out slave in - the slaves select, - or sometimes we call that the Chip Select and the clock line noticed that we're not gonna - use the master in slave out, - which would be the SD open on the display because we don't need to get any information back - from the display. - Were simply gonna be sending commands down to it. - The other thing that's important here is down here in bold. - It says the maximum speed of the SP I clock input is 250 killer herds. - That's gonna be important when we start talking to it from the pie to make sure that we're - not sending too fast of a clock signal that the display can understand. - And then finally it lists that the display is configured toe work with SP I mode zero and - you'll recall what that means from when we talked about the theory of SP I The nice thing - about this is SP I mode zero is what the pie operates in by default, - so we won't need to do anything special there. - All right, - that's all we need to know about the SP I protocol. - But there's one other thing I want to look at. - So let's go back to this main page here and I want to go into hardware specifications. - Every module that you connect to your raspberry pi should have information about the - electrical characteristics. - So if we scroll down here, - we'll have things like the physical dimensions. - But what we're really looking for are these operating characteristics. - If you recall when we talked about the header, - we can only draw so much power from the 3.3 volts power supply and the raspberry pi. - We need to make sure that as we're connecting modules that we're not over doing it on power - . - If we are, - we need to find an external power source. - As you can see from the table here, - it gives the supply current for different voltages. - We're gonna be using the one right here in the middle of 3.3 volts so you can see that the - minimum current draw will be 3.8 million amps and the maximum will be 7.9. - So right around eight milligrams. - So if we take the worst case scenario, - our display can dropped eight mil amps of current from our raspberry pi 3.3 volts power - supply. - So we just need to make a note of that to make sure that as we're connecting things, - we're not pulling too much power. - Eight mil amps is well below the 50 mil amp. - Limit. - So we're okay to use our raspberry pi to power the display. - Now, - before we actually connect our display to the board, - let's use Fritz seem to see what it'll look like. - So the first thing library didn't have an exact match on the display that we're gonna be - using. - But this was close enough in had the right number of pens across the top. - Let's go over each of the connections. - First and foremost, - we have to have power, - says you can see here we have our ground in our 3.3 bolt connection. - The display can run from his low as 2.4 bolts all the way up to five volts. - So 3.3 puts us right there in the middle. - We're sticking door conventions, - as we have before green, - representing ground in black, - representing 3.3 bolts. - Aside from that, - all we need to do is make the connections for the SP I protocol. - We have the clock line, - the master out, - which is how we're gonna send data down to the display in the chip Select line. - Last. - This is what's gonna tell the display that the raspberry pi is ready to send data to it. - And that's all there is to hooking up RSP. - I display toe a raspberry pi at this time you want to pause the video and make the - connections on your own bread board? - This is what my bread board looks like after I've connected my display. - It's not that we haven't hooked up. - We can go ahead and boot rpai back up and see if we can get this thing to point out some - characters To do that, - we're gonna go back to the data sheet. - We're gonna go back to the main page here and we're gonna go to this basic usage section. - This will describe to us how we can display numbers. - So the first thing they do is they give us an example. - Display one to a B and the data she describes. - How weaken send actual binary values to print out zero through nine and a through F, - or we can use the asking code for it. - So, - as it says right here, - we can send 01 0 to 0 a zero B, - or we can send them as 31 32 41 42 which the asking codes for all of those so we have to do - is send that data down the wire to our display module, - and it should show up on Scream Well, - a neat way that we contest this. - If we come back, - T Olynyk stud, - or go over here, - we can see that there's a way to test the SP I interface through this show, - and that's just by echoing straight to the device. - Let's go ahead and try this. - You bring my show window back over here. - My prize booted back up as I was trying to film the display. - I noticed that with lights on it in the camera on, - I was getting a refresh problem that was easily fixed by turning off the lights. - So the next few shots of video that you're going to see our with the lights turned off. - It's hard to see everything else on the bread board, - but the most important part, - which is the display, - is nice and easy to see. - We're gonna type it just exactly how it says it here at Go Dash any backslash x one x last - exit to back slash x 03 Now the backslash exorcist says we're passing a Hexi decimal value - here and then we're gonna pipe this out to Deb SP I 0.0 when we hit. - Enter. - Let's see what we get. - Awesome. - Get 123 right there on the display. - Okay, - so now that we have 12 300 display, - how do we get rid of it? - Well, - if we go back to the data sheet here, - we scroll down a little bit more, - says clearing. - The display says the clear display command bite is hex 76. - So go ahead and bring our consul back here and we'll dio echo, - dash any x 76 and we'll pipe it out just like we did before we hit Enter and we get a clear - display. - So this shows that our display is working. - We can send commands to it. - We get it to print out digits. - We can clear it. - Everything's ready to go so that we can start integrating this into our python script - pretty good. 17. Controlling the Display (Steps 5-11): - we obviously don't want to use the Terminal two controller display, - and we do need to integrate it into our python script. - And to do that, - we're gonna need a python module. - There are several available, - but the one that we're going to use is this one that we confined on Get hub called Pie Dash - S P I Dev, - this few files that we're gonna need our this sp i dev module dot c and set up dot pie, - which is how will install the module for python to use. - We can get this a couple of way if you want to install, - get and pull the repository down. - You can do that, - but this class isn't meant to go over getting detail. - So instead of using that, - we're just going to use w get again. - Get my terminal here and to get these two files. - First of all, - let's make a directory form. - Call it Python Dash s P I little CD into that can to get these were just gonna do w get - https and then raw, - get hub dot com and then we follow the rest of the path as we see it up in the address bar - . - That's how we get the set up dot pie. - And if you just use your up arrow key, - get the path back, - we'll just take off, - set up it up pie in the U. - S. - P i. - Deb, - underscore Montreuil dot c and you'll notice it's not quite like the URL, - as we have to add the branch in there. - The master part of the link and I include these links in the show notes so that you can - just copy him. - Okay, - so let's do a directory listing and you can see that we have both of them there. - And now to install this for python to use are we have to do is to a pseudo python the name - of the Python file and then doing install. - This will install the module so that will have access to it from our pipe on script. - Now, - before we added into our script, - let's test it. - Using the python show again, - I needed to turn the lights off so that the display was nice and clear. - So let's go ahead and do Sudo Python to bring up our python show, - and then we need to import S P I dead. - That's the module that we just installed. - So let's go ahead and do that. - Then we'll set a variable sp i equal to SPF dev dot s p I Deb like that. - Then we're gonna do SP I open and then the arguments to this. - If you remember when we were using this show, - we're piping it out to SP I Deb 0.0 There's also a 0.1 which will use the other chip select - pin on a raspberry pi And so the arguments are just that. - So for 0.0 will do zero zero like this to open it up just like that Now to write data. - All we do is we say sp I done expert to or transfer to And then we can send the data here - as a array of bytes. - If you remember before we sent, - we can do Hexi decimal in python like this As we sent 01 zero to 03 I think that's right, - Syntex. - Okay, - Perfect. - We got 123 on their display so that's working notice. - We had to put the zero before the X is where as in the show, - we just did. - The exit is that's just syntax difference between Python and Shell. - So if you also recall the way to clear the display was sending 76. - Let's see if that's working. - All right. - Perfect. - Okay, - so that means that we have successfully downloaded the Python module and we can use it now - we just need to add it to our scripts. - So let's exit from here. - Actually, - before we do that, - I want to do an SP. - I close just to clean that up, - and now it will have an exit. - You'll notice When we did these transfer commands, - we got an array of bytes back. - So we sent 12 and three. - We got back a list in Python of 000 when we sent the 76 We got bags. - Eras. - Well, - remember in the SP I protocol, - you have to send a bite to get a bite. - So what the library is doing is when it sends those bites, - even though we're not connected to the display to get information back, - it's still trying to read the master in slave out pin on the pie and telling you I read - back in this case all zeros and that would be the case because we don't have our display - talking back to the pie. - But just so you know, - when you're using this library when you do the transfer, - that return value that you see underneath is what it's reading back from the slave device. - All right, - this is going really well. - Now let's see d back into our skill share folder and will go back into our file our monitor - , - Dr Pie, - and we need to add our SP I Deb module. - So let's right here at the top. - The first thing that we needed to do, - if you remember, - is import SP i, - Dev, - and now we need to open it up so we'll do that just here the top. - Now this isn't right now. - Our Python script is very script e. - It's it's not. - If anybody's watching that has some experience of programming, - it's not object oriented. - All we're doing is we're taking baby steps here to get all of the functionality that we - need. - We can always come back later and re factor this, - but the purpose of the class isn't too right. - Perfect pipe on. - It's too. - Show you how we can use python to interact with that raspberry pi. - So let's come down here to another comment right here will open things up like we did, - so C s p I equals SP I Deb, - uh, - sp I dead. - Okay. - And we'll open it up just like we did in the shell. - If you're called from the data sheet, - there is a max speed on our display of 250 kilohertz, - so we can set that here. - You noticed we didn't have a problem when we used it in the show, - but it's still good practice to set the max speed. - If we know it, - we could just say Max speed hurts equals 250,000. - Just to make sure that we're not going too fast for display. - Okay, - so that opens it up. - Now it's ready to transfer. - Before we do that. - I want to come up here and I want to write a couple of functions, - and the 1st 1 is gonna be for sending data so we'll just call this SP. - I sinned. - I apologize for that blue font, - and I know it's kind of hard to read And what we're gonna pass it is the bus that we're - using and then the data that we want to send and we have to send an array to it. - So we call this the transfer list, - just create a list there, - and now we might want to send different things to this function. - We may want to pass it Hexi decimal value, - like we did in the show. - Or we may want to just pass a normal text or even just a imager. - And so we want to be able to handle all those. - What we're gonna do here is we're just gonna check the type, - so bring it will say, - if type of data now, - this will return the type of the variable that's passed in data. - So it might be a string. - It might be an imager. - It might be a list, - so we can just check that and we'll see if that equals the built in python type of stir. - What we want to do is looped through the data. - So for each character in that string, - we want to do a transfer list. - Stop upend. - Add to that, - we don't want to just depend the character we want to pass the asking value of the - character. - And for that, - there's a built in function python called Ward. - So it's a ward of see is what will pen to the list. - Okay, - then we'll just do some LFC here. - I left the type of data equals a list. - So if we send in a list of bites, - all we're gonna do in that case is a pendant to transfer list. - And we can just do this. - We just do plus equals data and python that will upend the to list together. - Okay. - And the last one that I want to handle here is if we pass an integer, - this isn't well, - say transfer list. - Not part Sequels that attend the uniter just like that. - Finally, - our else case here that will drop into it. - It's none of those types. - We'll just do a print se must be stir int or list. - Okay. - And then the last thing that will want to do after we've gone through that loop is we want - to do our bus dot Okay? - Remember, - the bus will have that sp I object transferred to. - We'll just send it our transfer list. - Okay, - so we have our SP I sinned. - Let's go ahead and test this. - So you've recall when we have had our button here and before we're just printing out that - you pressed me. - Why don't we go ahead and use this instead of printing? - Why don't we go ahead and try an SP I send of? - We'll just do it like we did in the show. - One to three. - I was trying just like that. - So if we press their button announced that it printing to the screen, - we will send 123 So let's go ahead and exit, - and we'll save that. - No, - let's go ahead and run this. - And if you remember, - we need to do pseudo. - So we'll do. - Pseudo python monitored up. - I Okay, - now it's telling us our max speed hurts isn't a bad file, - descriptor. - So let's go ahead and come back in to here. - What will do for now is well, - just comment that out. - I don't have to go back to the documentation to see what we have wrong there. - Okay, - let's try to run this again. - We're getting a warning here. - That is because on our previous run, - it opened up the GPO pin for 12. - We don't need to worry about this. - We can actually turn these up, - but for now, - our scripts still is running. - So let's come over here and see what happens when we push a button. - Okay? - This is why we test. - As you can see, - we got an air here, - says SPS and takes exactly two arguments in our script accidents. - So he forgot who's to pass our bus to our functions. - Let's come down here and we need to send S p. - I hear. - Okay, - let's hope third time's a charm. - All right. - Perfect. - Push a button we get 123 All right. - We've successfully integrated the SP I Dev module into our script. - So weaken start to write a bunch of helper functions to display the things that will need - for this project. - First thing I want to do is to display the time. - So the default state of our monitor, - I think it would be nice to shore show the current time on the display. - So that's what we'll go ahead and do next instead of making you watch me type all of this - into our python script. - I've gone ahead and made the changes so we can display the time. - And I just want to go over them since come back into our file. - Okay, - so the first thing I did was I imported local time in stir f time from the time module here - at the top. - That's going to allow us to get our time and four minute for display. - One thing I'd like to mention also while we're here is I did some more research on the - setting the max speed and I've changed that here you can see in my transfer to function. - We were just passing the list before we can pass a second argument, - which is the max speed that we want to transmit at which again was 250 kilohertz. - So I've made that change there. - I'm not sure why setting the member is not working because that is a member of the S P. - I object, - but this will work just fine for us. - I've added this clear display function which takes a bus is the argument. - And then we just do a send that hex 76 that you've seen before. - And then our display time function here, - we're gonna do is call stir F time in the documentation for this is readily available - online from pipe on. - But the percent h percent m here says give us the hours as a two digit number in the - minutes is a two digit number from our local time. - We just call local time there and then we're gonna call clear display, - just in case anything showing them will send that time String down to RSP. - I send function, - come down here a little bit more We can go ahead and cut out this max speed hurts line put - in just to keep things clean. - The first thing I do is I set our ah variable clock mode to true. - And then I do display time so right away When you turn this on when you start running the - script, - the time should display then down here in our loop for the button. - I just added an if else so if we're in clock mode, - if we're already displaying the time and we push a button, - we want to set the clock mode to false and clear the display later on will add our - temperature when we get that module connected. - If we're not in clock mode and we push the buttons. - That's the else we're gonna want to set clock mode to True, - and then we'll display our time. - So let's go ahead and exit out of here. - Save this, - All right, - so let's go ahead and run this and we'll see over under Borden we hit. - Enter the time shows up, - and if we come over to it, - we push our button. - The time turns off, - push it again. - He comes back on. - Now there's one problem with this and you'll notice that the time is in military time and - 17 04 would be 504 It's not 504 p m. - Where I'm at right now. - It's actually still in the morning. - So what's going on here? - We're gonna go ahead. - I don't like the display to be in the state where the screens on when we're working so - we'll go ahead and push the button again. - To turn that off, - we'll come back and exit from our script. - So this brings up an important point about the raspberry pi. - It doesn't have a real time clock that keeps track of time when it's unplugged. - When it's unplugged, - it loses all state information. - It does. - However, - when you boot it up, - try and pull the correct time. - If you're connected to the Internet, - it will go out and try and pull the correct local time. - But it will only pull the correct local time if you have your time zone select, - which we haven't done yet. - And so let me show you how to set this up really quick. - We just come to our command line here and we do TZ select. - This is gonna take us through some problems. - The last justice select a continent in the Americas so due to enter, - then get him in the United States and my time zone is actually mountains. - I'll do 18 okay? - And it's gonna say, - Is everything correct? - This is the local time. - That's right. - It is about 10 06 in the morning here and we will say yes, - that's fine. - Now, - an important part here it says here is that TZ value again, - this time on standard outputs that you can use the user been TZ slight commanded shell - scripts America slash Denver and then up above it says TZ equals American Denver. - Okay, - so what we need to do is we need to add this to our profile so that when we start up, - it'll automatically set the correct local time. - So to do that, - we're going Teoh, - change directories back up in your room here. - We're going to nano dot profile and what? - We'll do it The very bottom here. - Back up here, - you can see this line right here that says TZ equals American Denver Export TZ. - We just want to add that to our profile. - So we'll come down and will say TZ equals and you need to have the single quotes here - America slash Denver semi colon export TZ. - And when we do that, - when we boot up, - it'll set the time zone to mountain time. - So we're gonna go ahead and exit here will save this, - and then we're going to do a pseudo reboot. - All right? - My pie is booted back up and just real quick, - let's launch a python show. - We'll do from time in court, - local time. - Just a test that this worked local time. - Now you can see the TM Our is 10 in the minutes is 22 10 22 and that's what time it is here - right now. - So now we have the correct local time, - so exit and we'll CD back into our folder that we've been using Run our monitor again. - We hit enter. - You'll see that we get the correct local time. - Now the very last thing I want to do with the display before we move on is you'll notice - the time is showing 10 22 correctly, - but normally on oclock, - you'll have a colon in the middle of the tent between the 10 and the 22 we don't have that - are display is capable of showing that, - but we're not sure how to do that yet. - We haven't done that yet, - so let's figure that out. - Where we gonna go? - You guessed it data sheet. - So it's exit out of our program here. - OK, - but come up to the data sheet. - We're still in the basic usage apart, - so let's go back to the home. - Well, - the serial 17 display data sheet and we'll go down to special commands. - You can see here it has decimal colon apostrophe control will come down here, - So if we look at this, - we need to determine for the colon bit to be set. - What's the value we need to send to our device? - One easy way to do that. - You can kind of cheat. - I'm gonna grab my calculator over here. - We're gonna change the view to programmer, - and we could just enter the binary form of what it would be. - So digit one will be a zero digit two will be a zero. - Did you? - Three will be a zero digit four b zero, - but then the colon needs to be a one. - So let's just starting with the cones do one and then 000 z room the four zeros - representing bit zero through three, - which are also digit 13 digit for And then if we just change this to decimal, - we can see that 16 or if we want to descended as hex would send it as hex 10 Now, - if you remember from RSP I send function, - we could send it either way. - For now. - Let's just send it as 16. - Pull this back aside. - Bring a terminal back up to do Nano. - I will come back in here is part of our displaying time. - I will send the time down, - but then we'll do NSP I send now. - I said we're gonna send it as an imager, - but we need to send the special control bite first. - So let's send it as a list of hex again. - So we'll send zero x 77 which is that command bite. - And then we need to send dear X 10 Okay, - so after we've sent the time down, - this should go ahead and turn our Colin on. - Let's exit out, - seem it and run it and see what we get when we hit Enter. - All right, - that looks much better now, - our displays correctly showing the time it's got the colon. - It looks nice and pretty, - and that's pretty good. - So that is all I really want to cover for S P I. - We've covered a lot of ground here. - We've gotten some good functionality out. - Now, - let's go ahead and move on to the I to see protocol and see how we can get our temperature - sensor hooked up 18. I2C Basics (Step 1): - All right, - we've got SP I working on our pie. - Now it's time to talk about the second communication protocol that we're going to use in - our project, - which is I to see some people call it. - I i c I squared. - See, - It stands for Inter integrated circuit. - And what we're gonna do is we're gonna use this protocol to talk to the temperature sensor - that will be using you're gonna see a lot of similarities to the SP I protocol. - There's also gonna be some differences, - but we're gonna follow a very similar pattern that we followed before. - And that is we're gonna start with power. - How are we gonna power a temperature sensor? - Well, - we need to refer to the data sheet to see how much current it draws to see if the pies able - to provide power to it. - And we'll look at that a little bit later. - But I can tell you that the temperature sensor is very low power, - so we'll be okay to use the 3.3 volts supply from the pie again. - So let's go ahead and make those connections. - And I just want to make a quick note. - Um, - just like an SP I We need to make sure that our voltages agree when we're doing I to see - meaning the pie wants to speak in 3.3 volts. - So we need to make sure temperature sensors okay, - doing that as well. - We never want a sensor that's going to use five volts talking toe rpai even though we do - have five votes on the raspberry pi. - The input output pins or not, - what's called five boat tolerant meaning. - If you put five volts on an input pin, - it's going to burn it out. - You may ruin your pie, - so just keep in mind, - because remember, - this doesn't have to be a temperature sensor. - We're using a communication protocol to talkto a module. - This module could be a temperature sensor. - It could be a gas sensor. - It could be some other module. - But when you're buying these, - just make sure, - especially if you're using the pie that they speak the same voltage language. - In this case, - 3.3 bolts are temperature sensor does. - So we're okay Now, - let's see the lines that we're gonna need to connect this unlike SP I that requires three - or four I to see only requires to. - That's in addition to the power lines, - of course. - And they are SDA and s CEO SDA is the data line and SCL is the clock line So this will look - very similar like it did in SP I it's gonna send the clock signal now unlike SP I Though - there's not separate lines for sent and received, - the same line is used and that's the SDA line. - Another similarity to SP I is that there's this concept of a master and a slave. - The pie again will be our master device. - The temperature sensor will be our slave device. - The master will always drive the clock line which syncs up master and slave Now one of the - main differences between SP I and I to see is that how the master contacts the slave If you - remember with SP I we had a concept of a chip select line so we needed an extra pin for - each additional sp I device. - We wanted to connect to our pie so the pie could let it know it's ready to talk to it in - the eye to see protocol. - There's a concept known as ad dressing every single module that you'll connect has an - address. - And what happens is the pie. - The first thing it does before it talks. - It says all the devices connected to the line. - I want to speak to the device with this address. - Right now, - all of the devices that are connected will compare that to their internal address and - decide. - Is that me? - Yes. - Okay, - I'm gonna start listening. - It's not me. - I don't need to worry about it. - And so most modules will come with some form of address configuration. - In the case of our temperature sensor, - there's an extra pin that we can connect up to 3.3 volts or connect down to ground to - adjust the address because it comes with a base address. - But what if we wanted to stick three or four of these temperature sensors on the same eye - to see data lines? - Well, - they don't need to have different addresses, - and so there's always a way to do that. - I should say most of the time there's a way to do that with modules that we connect. - And so, - just like an S p I we can take, - we can share these lines we can come off of the data line here, - we can come off of the clock line and we can connect another module down here and you can - do this multiple times connected another module year running into each other and again, - there's no additional connection to the pie for those. - They'll all have a unique address that the pie will say. - I need to talk to this address now and the correct one will start to listen and we'll - understand what the pious saying. - So, - um, - that's one of the pros. - It uses less lines than SP I One of the cons to it, - though, - if you compared to S p I is that it's a little bit slower. - The maximum speed you can talkto tonight to see devices 3.4 megabits per second. - Whereas SP I can go upwards of. - There's really no defined upper limit, - but you can get up in the 2050 megabit transfer rates, - so that's our right to see protocol. - That's how we'll connect towards temperature sensor. - Let's go ahead and see what this looks like when we connect it and figure out all the - details 19. Configure Pi for I2C (Steps 2-5): - Before we hook up our temperature sensor, - we need to configure pie so that we can access the I two C bus. - If you recall from the SP I unit, - we needed to modify a few files and download some tools before we could communicate over - that protocol. - I two C has similar requirements, - so let's go through those briefly. - Let's start by modifying the modules file to tell our pie to load up the EITC modules at - boot time. - And to do that, - we'll do Pseudo Nano. - The file is in the E. - T. - C folder. - It's called modules. - Like it says, - this file contains the names of kernel modules that should be loaded at boot time. - That's what we'll do is we'll come down here Well, - had I to see Dash BCM 2708 as well as I to see Dashed Dev, - we'll go ahead and exit and save the other file that we need to modify is that blacklist - file. - If you remember from the SP I module, - so we'll do a pseudo Nano also in the E. - T. - C folder, - it's in. - The mob proved that d sub folder. - If you remember the name It's raspy. - Die raspy dash blacklist dot com, - and we'll comment out the line that blacklist the EITC module. - Finally, - we'll need to install a few tools that will need in the next unit. - Since we're using, - Python will need to install the python Dash s M bus package. - Let's do that with a sudo apt get install Python Dash s m bus and once that's finished will - install the I two c tools by doing a pseudo I have to get install I two c tools you know, - since says the EITC tools already on the newest version. - That's because the sm the python dash S m bus installed it right. - Those are all the things that will need to have installed crust access or i two c bus on a - raspberry pi. - Now, - for all of these changes to take effect will need to reboot. - So let's go ahead and do that and will continue on in the next unit 20. Datasheet Review and Hookup (Steps 1-2): - as we've done with everything that we've connected so far, - we're going to take a moment to review the data sheet for our temperature sensor because - there are a few questions we still need to answer before we can hook it up and start - communicating with it. - The temperature sensor data sheet weighs in at a light 19 pages, - but that can still seem pretty daunting if you're a beginner. - An important thing to remember is that reading a data sheet is just like anything else. - The more you do it, - the better you get. - At first, - you might stumble through them, - looking for the one piece of information you need. - But as you do it, - more and more becomes second nature. - Instead of scrolling around the 19 pages of this particular data sheet, - I just want to highlight the important stuff and, - as usual, - will start with power. - I mentioned earlier that the temperature sensor is very low power, - so we will be fined a steel power for it from the pie. - But the data sheet is the place that will tell us for sure. - This is the top of the very first page of the sensor data sheet and we can see first of all - , - that the supply range for this sensor is 1.4 volts to 3.6 volts. - We're planning to pull from the 3.3 boat line on the pie, - so that is within the allowable range. - The second thing we see right here on the first page is the quiescent current range of one - micro am to 10 micrograms. - Let's get a little more detail by looking at the electrical characteristic section of the - data sheet. - We could see the same information here about the power supply operating range being 1.4 to - 3.6 bolds. - But we see a little more information about the quietus and current question. - Current is the amount of current that the sensor will draw from the power supply. - Went inactive, - were just sitting there, - connected and not actively doing anything. - Remember, - though, - that this device may be connected to the same lines as other sensors, - so you'll see different values for when the bus is inactive and active with different clock - speeds. - In any case, - the values Aaron, - the microgram range and if you recall previously, - we mentioned that the 3.3 bolts apply on the pie in source a max of 50 milligram, - so we're just fine. - One thing to note is that the sensor does draw more current in very short bursts when - actively driving the data lines as seen here. - But three mil amps and very short bursts is still very low. - So now that we know we're OK on power, - let's talk about how we're going to address our sensor. - Remember that each device has a unique address that the pie will need to know so it can - talk to it. - Here's the section of the data sheet that explains how we can configure are temperature - sensors address. - It says here we can connect up to four of this particular sensor on a single bus, - and we configure the address based on how we connect the A zero pin. - Let's just use this first address in the table by connecting our A zero Pinder ground again - . - We can use our calculator to figure out that this binary number is the Samos Hex 48 or 72 - in decimal format. - That will come in handy when we start writing code to talk to our sensor. - The last thing you need to understand before you hook up your sensor is how to actually - talk to it and get a temperature reading. - Well, - we know that we'll be using the act to see protocol. - But just as we needed to know what data descend over SP I tore display. - We also need to know the things we need to say to our sensor. - So it will give us a temperature reading because the same SDA line is used for send and - receive. - We need to understand how data is exchanged between the two devices. - Essentially, - the pie is the master needs to say Hey, - sensor with address 72 please give me a temporary thing. - Then it will release the data line and drive the clock line for the sensor to respond with - temperature data. - For that, - we need to read about the pointer register. - As it says here, - The pointer register of the device is used to address a given internal data register. - As we can see from this table therefore different registers that we can read and or right - to inside of our TMP 10 to censor the one with the current temperature reading is at - address 00 So instead of saying please give me a temporary ting, - what we're really saying is please give me the contents of your internal register at - address 00 If this seems confusing, - that's perfectly normal. - Sometimes this stuff has to sink in after reading it multiple times in the data sheet. - Once we start getting into the python code, - you'll see that a lot of this is abstracted away for us. - And I promise it won't be that difficult to understand now that we know what registered to - read to to get the temperature data, - we need to understand how to interpret that data. - So for that we need to take a quick look at the temperature register section of the data - sheet. - As we see here, - the temp data comes back in two bites, - and it says one least significant bit equals 10.6 to 5 degrees Celsius. - We'll come back to this table later when we're writing our python code so we can properly - translate the data into a meaningful temperature reading. - All right, - now that I've melted your brain with the data sheet, - let's get it actually hooking this thing up and playing with it. - Here's how I intend to connect the sensor on our bread board. - I've pulled power from the top, - just as I've done with everything else. - Remember, - Green is ground and blackest 3.3 volts. - I've also connected the SDA in SCL lines over to the corresponding pins on the pie cobbler - . - The breakout board for the temp sensor has the pins clearly labelled, - so it is very easy to know which pins go where. - Finally, - to set her address correctly. - I have the A zero pin connected to ground. - This will give us that address of 72 that we mentioned earlier. - This is what it would look like connected directly to your pie. - And here's what it looks like hooked up on my board. - Now that we're all hooked up, - let's see how we can start communicating with our sensor 21. Add Sensor Code to Python File (Steps 3-6): - with our temperature sensor hooked up, - we're ready to start playing around with it so we can start getting temperature readings - from it. - So with our pride booted back up and everything connected, - the first thing we want to dio is to make sure that we have everything hooked up correctly - . - And we can use one of the utilities that we downloaded and installed called I two c detect - . - So just like with the python shell and with some of the other things we need to run, - this is pseudo So we'll do pseudo I to see detect dash y and the number of the eye to - seaport that we want to test now. - For most of you, - that will be I to seaport one. - However, - some of the early raspberry pies that were delivered used I to seaport zero. - So if you have an older pie, - you may need to you zero is this last argument. - In my case, - it will be a one no hit enter And what this does is tries to find any devices with an - address on the I two c bus. - And as you can see right here, - we have our temperature sensor with hex address 48. - These were all hex addresses, - and that's the Samos 72 like we showed before. - So if you run this and you don't see a 48 there, - it means you don't have something hooked up correctly or didn't install one of the tools - that we did in the previous video. - If you do see the 48 that means the Pichon C and is ready to talk to your temperature - sensor. - So let's do that next, - as we've done before will launch a python show, - and all we have to do is import the SM bus module. - And then we'll just say I two c equals a Simba Stop S M bus and then pass it the EITC port - . - We want to communicate over in our case one. - Now, - before we try to read the temperature. - Let's review the data sheet really quick. - Here's the section on the temperature register now, - As it says here, - two bites must be read to obtain data and are described in table three and four. - So when we ask for the temperature from our sensor, - we need to read two bites back to get the full temperature value so back here in our show - we want to do I two c dot reid word data. - Pass it. - An address in this case will pass the hex 48 and the address of the internal register that - we were on a read. - Remember that zero? - Now, - the SM bus module has a few functions that we can use in this case we're using Read word - data. - Ah, - Word is two bites, - which is what we need. - There's also a function called read bite data, - which will show you in a minute. - But this will give us the full temperature register value back from our temperature sensor - . - So if we hit enter, - you'll see we get this war. - Ginger 24,595. - That's coming from our temperature sensor. - Now you can do this. - Just is easily. - We use the hex value for the address, - but you can also use the image of value if it was 72 so Well, - read it again. - You see, - we get the same value back. - They're also functions on the EITC variable that we have here for writing data back to our - sensor. - We're not gonna get into that. - We're going to stick with just reading the temperature for now. - So as I mentioned, - we can also read just a single byte of data. - So instead of reading both bites of the temperature information, - we can just do read bite data, - which will read a single bite back. - You'll see we get 19 there. - So remembering back from our data sheet, - we know that the temperature data that we get back needs to be multiplied by 0.625 to give - us a temperature reading. - So let's see what happens if we do that. - So we multiply by 0.6 to 5, - and I forgot to assign Art Temple value there. - So do temp equals I to see that read Work data 72 0 Okay, - let's try that again. - Now what is this? - We're getting 1537 degrees Celsius. - It's definitely not that hot where I'm sitting right now, - so something's wrong. - This is where reading the data sheet and understanding what we're working with really comes - in handy and can be a little tricky if you've never done it before. - So let's bring our data sheet back up Now. - There's a couple of things that are important to note here, - right here, - it says. - Note that bite one is the most significant byte, - followed by bite, - too. - So from Table three and Table four, - we see bite. - One is the most significant bite, - and bite to is the least significant byte. - And that's how the temperature sensor is going to send the data back to us. - However, - the standard for I to see is to send data least significant byte first. - And so are temperature sensors actually going to send that data back bite, - too, - as the first bite and bite one of the temperature data as the second bite. - We can see that if we come back here and we do a binary on the value that we read that 24 5 - 95 and you'll notice the least significant byte, - which is here on the right ends in 10011 But if we come back to our data sheet will see - that bite to at least significant byte should have four zeros at the end, - so our bites air swamped. - That's our first problem. - The second problem that we have is you'll notice that this temperature data is shifted. - So the first bit of the temperature data is actually in the fifth bit of that least - significant byte. - We know that we're reading data from our temperature sensor, - and we know that we need a massage it a little bit. - So I think we can be done in the shell for now and make these changes right in our monitor - , - not pie file. - So let's go ahead and do that instead of having you watch me type. - These changes into are monitored out pipe. - I'll have gone ahead and made them, - and we'll walk through them together. - Feel free to pause the video as we go and make the changes in your file if you're following - along at home. - So if we come back into our monitored up I pile here, - the very first thing I did was an import SM bus, - just like we did in our shell. - We need to import that module so that we can use our I two c bus. - The next thing here, - right at the top, - is that I defined a new function called get underscore temp and I pass and I two c bus is - an argument to it. - Now we remember from our data sheet that are bites were coming back in the wrong order were - actually the right standard order for I to see but the wrong order for how we want to read - them. - And they needed to be shifted a little bit. - So with the data sheet is a guide here the changes that I made if you don't understand this - the first time through it might help the watch it a few times so that it can really sink - into how I accomplished this. - So from the data sheet, - it helps if we look at the bite side by side. - This is how we'd like to have gotten the bites back from our temperature sensor. - However, - this is how we actually got them back. - So the first thing that we need to do is isolate each bite and to do that, - we'll use something called a bit mask. - If you're not familiar with binary operations, - that's okay. - I'll put a link down in the resource is so you can learn more about it. - But basically what we're going to dio is we're going to on Lee, - take the bits that we want for each bite the high bite in the low bite. - Then we're free to move them around as we please. - So for that I have two masks bite one mask, - which takes the bottom eight bits from our return temperature reading and bite to mask, - which will isolate the top eight bits. - So this is the state that we're trying to get to all of the temperature bits, - right? - Aligned with t zero being all the way it bit zero in our temperature value. - So to do that will do a binary and of our temperature reading with the bite one mask which - isolates that bite one. - And then what will need to do is you can see that the first bid in that is actually the - fourth temperature bit. - So we'll shift it up by four. - That's what this symbol right here does. - And then we'll do something very similar for bite to will and our temperature value with - our bite to mask toe, - isolate those bits and then we need to shift them down. - You'll see. - We need t three t two t one and t zero. - We have to shift those down 12 bits so that they're all the way down to the right. - Once we do that, - our bites are in the right position. - All we have to do is or them together again, - another binary operation, - and that will give us the actual value that we want. - And one last thing to remember from the data sheet is that that value needs to be - multiplied by 10.625 to give us the temperature reading in Celsius. - So I just added a print statement for now, - Celsius like this. - And then I do the conversion to Fahrenheit and print that out as well. - And for those paying close attention, - Yes. - I just realized that I have spelled Fahrenheit incorrectly. - So let me go ahead and fix that. - Okay, - so that's the function. - Forgetting our temperature. - Now, - I just came down here and followed the same pattern that we've done before. - Right after RSP I module set up, - I set up the I tease the eye to see module by doing an SM bus. - That s m bus one. - If you remember earlier from our button testing, - we found that when we push the button without having asleep, - we got a lot of you pressed me output. - That's because that wild loop is executing so fast. - Well, - we don't want to read our temperature hundreds of times every second. - And so I put in this last temperate ing value that has the current time. - When we start up and down here in the wild loop, - I added some code that says, - if the current time minus the last temp reading is greater than or equal to five. - And so what that does is basically says, - Has it been five or more seconds since our last temperate ing? - If so, - will say the last temp reading is right now and then we'll get the temp. - So if we exit out and we run this, - we should see a temperature reading coming in every five seconds. - So let's go ahead and hit. - Enter. - The 1st 1 will take five seconds, - and there we go. - We get 69 degrees here, - and it will keep giving us temperature every five seconds. - So this is perfect. - Are temperature sensors working? - It's giving us a value, - and we figured out how to interpret that data. - It was a little more complicated than we would have liked, - but remember Using the data sheet is your guide. - You can figure this stuff out now that we have this working instead of printing our - temperature out to the console. - Why don't we put it on our display? - Let me show you how to do that again. - I've made the changes so you don't have to watch me tight. - So let's walk through those now. - The first thing I did was in our get tempt function here. - As I commented out the print lines cause we don't need to print those out of the council - anymore. - And I've added a return. - In my case, - I want to return the fair in high value. - If you live in a part of the world where you prefer Celsius, - you can return your Celsius value here. - Either way works just fine. - The next thing I did was created display function just like we display our time. - We also have a function to display our temperature again. - We need to pass the bus because we'll need that to talk to RSP. - I send function and the temperature that we want to display Now we need to modify the value - of our temperature before we can send it down to our display. - We only have so many characters on our little display, - so we need to format this in a way so that it will all fit. - And to do that I've just used some python formatting functions here. - Let's just walk through it real quick. - The corn four says. - I want the max width of this to be four characters wide, - including the decimal point, - and I want one decimal point precision and the F says it's afloat, - and the second F actually appends the literal character F two r string When we're done and - what we're formatting is the rounded value of our temperature with one decimal precision, - our temperature sensor can give us readings like 69.125 degrees Fahrenheit. - We can't display all of that. - And so what all of this does is wraps that down into 69.0 and then we want to display the F - on our display so that we can indicate that it is fair and height that we're showing if - you're doing Celsius, - all you would change. - Here is the second F to B A C that will give us back of value like 69.0 f. - Well, - if you remember when we configured our display, - we can't just send the period down to our display and have it displayed correctly. - We have to send down special characters so far, - display vow. - Here we're going to replace the period with nothing. - So basically, - we remove it from our string. - So what we're left with is 69 zero f in the case that it's 69.0 degrees. - And so, - just like before with the time we're gonna want to clear a display before we put anything - on it and then we'll sp I send that display vow After we've sent it, - we want to turn on the decimal in the apostrophe. - The apostrophe will kind of give us like the degree symbol, - and I think it looks pretty good. - So again, - we'll do sp I send 77 Is that special character saying we're going to send extra data about - turning on the decimal apostrophe? - And in this case, - I figured that to be hex 22 the other change I made was right When we set up our module, - I want to grab the current temperature right away. - So we added this call here and then if you remember from before where we were sitting our - mode, - we're displaying the temp. - But I've modified this loop where we're clearing for the temperature. - So not only do we get the temperature every five seconds, - but if we're not in the clock mode so we're displaying our temperature, - I want to update the display with the current temperature. - So that's what this if statement here, - does and then just isn't note. - I did add down here in our except for when we exit to clear our display. - I didn't like how when I was cancelling out of the script, - whatever was on the display would stay there. - So I just clear that before we close. - So if we exit out of here and then run our monitor, - remember, - the default is to display the time and yes and military time that his 12 28 in the morning - . - If we come over and we hit our button to display her temperature, - you'll see that we get 69.2 and it's Tata going there between 69.2 and 69.1 degrees - Fahrenheit. - Now I have another thermometer here next to it. - For comparison, - the 31 of the top is the outside temperature, - but the 71 at the bottom is the inside temperature. - I tested this with a few thermometers, - and as it turns out, - our TMP 10 to censor. - At least mine appears to consistently be about two degrees low. - I don't know if it's a defective sensor or what, - but that's what it seems to be. - It will always be about two degrees low anyway, - so that's it. - I think it's displaying really nice. - I think it looks nice with the apostrophe, - is kind of the degree symbol, - and it's updating in real time. - All right, - that wraps up our unit on interfacing with our temperature sensor. - Everything is going really well, - and we've almost got all of the functionality that we want in our little home monitoring - unit. - In the next unit will work on adding motion detection as always, - if you need help or have questions, - the best place to go is on the Skill share website for this class under the community tab, - you can also reach him via email or find me on Twitter 22. Datasheet Review and Hookup (Steps 1-2): - the last piece of hardware functionality that we want to add to our simple monitor is that - of motion detection. - We'd like to create the idea of, - in a way mode so that while we're gone, - are monitor can detect motion and let us know some of you may be thinking, - Why not use a webcam and some open source software to detect motion? - We could easily do that, - but then we wouldn't be using the GP Io header. - And besides, - it's way quarter. - Roll your own and show off your hardware hacking skills to detect motion. - We're going to use the passive infrared or P ir sensor from Parallax. - There are a lot of motion sensors available, - but I like this woman because of its simplicity and power supply range. - We'll go a little faster through this one, - since you're probably starting to get the hang of this. - So here's the data sheet for our sensor. - And what's the first thing that we need to check on? - That's right power. - And here on the first page we see the power requirements are 3.6 volts and the current - draws 130 micro amps. - When Idol and three milligrams. - While active and not driving a load, - the sensor was designed to be able to drive a load like an led or relay when it detects - motion. - So that's why up here, - it says it can source up to 12 million amps at three volts. - We won't need it to drive any load as we'll see in just a minute. - I want to pause here for just a moment to recap on our power usage. - I said in the very early stages of our project that we could only pull 50 million amps of - current from the 3.3 volts supply on rpai otherwise would need help from an external power - supply. - Well, - let's add up. - What we've got so far in a worst case scenario are led draws about four mil amps of current - when it's on our switch, - if we actually configure incorrectly as an output is when it will draw the most current and - that's around 3.3 milligrams, - same thing for our button. - Our display can pull a maximum of eight million amps in our attempt sensor three milligrams - and finally, - our motion sensor can draw up to three milligrams while it's active. - So in a worst case scenario where we've configured something's incorrectly and everything - is running all at once a peak current draw, - our worst case scenario would be about 25 million amps. - It's important to keep track of this when you're working on your own projects. - Now, - even though the pie can provide 50 milligrams, - you'll never want to push it right to the edge. - 90% is a good rule of thumb. - If you're gonna go over that, - you should look into adding external power. - Okay, - back to our motion sensor. - So how do we talk to this thing? - Well, - if we scroll down just a couple of pages, - we see that we have two pins for power. - In the third pin is the signal pin. - It says hi when it detects movement and low when it doesn't detect any movement. - Sounds like the perfect job for a Iot pin on our header configured as an input. - So, - as I was saying before, - we don't need to drive a load with the sensor output pin. - We just need to read it state, - and we'll just use this quick start circuit in the data sheet is our starting point, - we'll make one simple modification to it. - Remember, - we talked about hooking up are switching button. - And what would happen if we accidentally configured our pin to be an output and drove it to - ground or 3.3 bolts? - We could create a short circuit. - The same concept applies here. - What happens if we accidentally configure I open to be an output? - We know the default state is set to zero or ground. - Now, - when our sensor detects motion, - it's going to try to drive that pin high, - and we're back to having a short circuit. - So the only change will make is to add a one kill own protection resistor between our - sensor output pin and the eye open on the raspberry pi. - The only other thing of note here is the warm up time. - It says that it may take our sensor up to 40 seconds without any movement toe. - Learn the room it's in before it's ready to detect motion. - So let's see how well, - hook this up. - As you can see, - I've put my motion sensor on a separate bread board. - That's just to facilitate the overhead views I'm shooting, - and there's nothing to prevent you from hooking it up on the main board. - In fact, - I purposely left room here. - If you'd like to install it there, - there's nothing new here. - We have our ground and 3.3 bolt connections. - And then the output pin on the sensor goes through our one kill own protection resistor toe - . - I open 22. - That's all we'll need. - An important note is that Fritz and didn't have the Parallax motion sensor in the library. - So this is a substitute part with different pen orientation. - The pins are clearly labeled on the censor board, - so make sure you get the connections right. - This is what my overall set up looks like. - And here's a closer view of the sensor connection for those connecting straight to the pie - . - This is what I mocked up with our motion sensor install. - We've completed the hardware build out of the class project before starting the next video - . - Take a moment to hook up your sensor. - Remember to power down your pie First. - Next up, - we'll show you how easy it is to integrate the motion sensor into our pipe on script 23. Script and Test (Steps 3-4): - with our motion sensor connected, - we're ready to integrate it into our monitor script because we're using an eye open - configure. - It isn't input. - There is nothing new to learn here. - You should be a pro this after hooking up your switching button. - We don't really want the motion sensor doing anything while people are around, - because it will constantly detect emotion we don't care about. - We want to be able to arm our system. - For example, - when we leave our house or go to bed, - well, - we still have our switch lying around, - and right now all it does is turn on our led, - which isn't very useful. - So let's repurpose them by making the switch arm the system. - So when the switch is off, - the motion detector won't do anything. - When it's on will put our system into a mode where if it detects motion, - it will turn the led on. - If the led is on when we come back home, - we'll know that the system detected motion while we were gone. - That may seem a little lame, - but try to think about the big picture. - Remember that we have a small Internet connected device on our hands let your imagination - take over in a real application. - Instead of turning on an led, - we might sound an alarm, - send an email or even send a text. - All of those things are possibilities, - but to keep things simple will take the led approach. - For now. - Let's take a look at the changes we made and monitored out pie to integrate our motion - sensor. - It's really pretty simple. - My pies booted back up here. - I'm just going to use CD into the skill share folder and you Nano monitored up. - I I've added nothing new here. - The top. - We're actually going to scroll all the way down to where we set up things like our input - output pins and our protocols just below the eye to see module have this block of three - lines of code that says Set up motion sensor. - We configure the pin to be an input, - just as we've done before. - Using gp ioo dot set up. - It's connected to pin 22 we use gp ioo dot in to configure it as an input. - And then we just need to variables to track the state of our system system armed, - which could be true or false, - depending on our switch and the time it was armed and you'll see how we use that down below - here. - If you remember before this first, - if else block under our wild true was where we returning the led honor off based on the - state of our switch connected to input Pin 16 All we've done is changed the body of the - infidels. - So now, - instead of turning the led honor off if we detected the switches on or equals g p i o dot - high we're gonna check to see if the system is armed. - If it's not already armed, - we're gonna set the time than its armed to now and then Set system armed equals true in the - else block. - Or if the switches back in the off position, - we're gonna set time armed back to none and say that the system is no longer armed by - saying system arm equals false. - Lastly, - here the bottom. - We have just a few lines of code we're gonna check in our wild loop. - If the system is armed. - If it is armed, - we're gonna do this top. - If here now, - a lot of security systems will have a feature where you can arm it while you're in your - house and then gives you a little bit of time to get out. - Well, - we have the same concept here. - We're going to say if the current time, - minus the time that it was armed, - is greater than or equal to 10 or 10 seconds have passed, - then we're gonna monitor. - This will give you, - in this case 10 seconds to get out of the room without the motion detector firing. - You can set this value to anything you want. - If you want a minute or two. - Just change the 10 to 60. - That would be one minute or 120 for two minutes. - Whatever works for you now, - if that amount of time has passed, - we're gonna come to this inner If that says, - if g p i o dot Input on pin 22 is high or a motion detector has detected motion. - We're going to see a gp ioo dot output 12. - Remember, - that's the led and setting it too. - Low turns are led on. - That's all there is to it. - If it detects motion while its armed, - it'll turn. - That led on and leave it on. - That will be our indicator that the system detected motion while we were out. - Go ahead and make these changes. - Save it next. - It out. - All right. - All that's left is to test this out. - Let's do a pseudo python monitor dot pie. - Okay, - you see that? - Our monitor has initialized. - We have the time displaying, - and we can come over here and show how. - If we push our button, - we get the temperature 67 degrees, - and now you'll notice I have this switch in the off position. - It might be a little hard to see, - but it is off. - If we do some motion in front of the sensor, - the sensor will turn on. - You'll see that red light there that's just in led within the motion sensor that says it's - detected motion, - but you'll notice are led is still off. - Do it again. - That's because our system isn't armed now when we are Mitt. - If you remember, - I have 10 seconds where it still won't turn the led on because that's the time that it - allows me to get out of the room. - So let's go ahead and arm it, - and I'll go ahead and do motion in front of it. - Here. - You'll see that the sensors picking up motion but are led is not turning on. - And we'll just wait 10 seconds. - And now let's see what happens if we put some motion in front of it. - You can see are led. - Turned on working just as we hoped it would. - Right. - Let's go ahead and exit out. - That wraps up our unit on hooking up our motion sensor in our next unit. - We're gonna do something a little more interesting than just turn on an led when are - monitored a text motion. 24. Twitter Integration: - our monitor is now hardware complete, - and we're using almost every function the GP Io header provides. - This is my favorite part of a project because once the hardware is implemented and - functioning, - you can really start getting creative. - This is where the pie really shines over other development boards, - in my opinion, - because now we're just experimenting and Software Analytics computer. - Currently, - we're using an led to indicated intrusion when the system is armed and were displaying the - temperature on a tiny display. - We can do so much more. - We could have our hardware send texts or emails. - We could store historical temperature data, - but even better we can start interacting with the Internet. - The cloud is everyone loves to call it. - We could store data there or access our hardware vo website there. - So many possibilities. - I'm sure some of you watching this. - Our experience programmers and the wheels are already turning. - But this is a beginner's class, - and I want to show you a very simple way to get your monitor on the Internet that requires - only a few lines of code. - Let's see how we can get our device up and running on Twitter. - I'd be truly amazed if you were watching this class on skill share and had never heard of - Twitter before. - But just in case somebody out there hasn't Twitter is an online application. - I'm on the about page here, - it says. - Twitter helps you create and share ideas and information instantly, - without barriers. - Essentially, - it allows you to post micro content 140 characters or less. - You can also post images and links and things like that. - How we're going to use Twitter is to post some information about the temperature and to - alert us if motion is detected while we're away. - And to do that we're gonna use a library called twice on Over Here and get Hub, - it says actively maintained pure python rapper for the Twitter, - a p I. - And so we'll just scroll down here to the installation section and it says, - installed twice on via Pip. - And so let's pretend that we don't have any idea what that is. - Some of you may not. - We'll just bring our shell over here. - I'm logged into my raspberry pi and we'll just go ahead and type what they have here Star - that says Pip command not found So what we need to do is we need to install another Python - development tool. - And to do that, - we'll just use Sudo Apt get install like we've done before. - We'll do sudo. - I get install Python Dash Pip, - just a installation tool used for pipe on. - Once that's finished, - we can go ahead and run this command again when I've run this before, - I've had to use pseudo because when the Pip install twice on command runs, - it tries to install things on directories that, - um, - need privileges that we won't have if we run it normally. - So let's just go and do sudo have been stall. - Try it on. - All right, - Once that's finished now what do we do? - Well, - let's come back. - We'll slide this out of the way for a second. - We'll come back toward get home page here, - and there's a lot of information about authentication and a lot. - A lot of things that access this ap. - I interact with users, - and so there's a handshake process where you ask for credentials and things like that. - We're not going to do anything that complicated. - We're gonna scroll down to this basic usage section right here it says all we need to do in - our script is from twice on import wife on and then we set a variable Twitter in this case - equals twice on, - and then we need to pass it some information. - You have an app key, - an app secret and oh, - a token and off token secret To access Twitter from our python script, - we're gonna need to create an application on Twitter. - So to do that, - we're gonna come back over here to Twitter, - and we're gonna go to dev dot twitter dot com slash abs It's going to say, - signing with your Twitter account. - Now, - if you want your python script to post Twitter updates to your own Twitter account, - you can sign in with your account here. - What I've done is create a separate email just for our monitor to use, - So I'm gonna log in with that and create an application under that account, - you can do it Either way. - I have a domain that I own that I can add email addresses to. - But if you don't, - you can just create a Gmail account for your device and use that here so we'll log in these - air name is Pie Home Monitor what I created and I'll enter the password here. - And it will take you to this My applications page. - And you can see it says, - Looks like you haven't created any applications yet, - so we'll just come over here and we do create new application. - Just gonna give it the same name. - Give it a description. - Now the website here it says your applications publicly accessible home page. - It says you can use just a placeholder for now, - but remember to change it later. - I'll just do skill share and then the call back you are Oh, - we're just going to leave empty. - As with anything online, - there's an agreement that you should read and agree to. - We'll see. - Yes, - I agree. - And then everyone's favorite capture. - See if I get that right the first time. - Alright, - Awesome. - Okay, - so now we're in the page that talks about our application. - What we'll do is we'll scroll down now. - Important thing to notice. - Here is the access level, - as read only wanna have re write permissions. - But if you remember back over here and get hub We're talked about app, - key and app secret. - That's right, - here that's also known as our consumer key and our consumer secret. - And if we scroll down just a little bit more, - it says your access token. - Now, - before we create our access token, - which will be the other two pieces of information that we need. - Let's scroll back up and come to settings and will change that. - Permission to read write. - The other option is read, - write in access direct messages. - We don't need that. - We just need read right and we scroll down and we do update this with our application - settings. - Now, - one thing I've noticed in this user interfaces, - if we scroll back down to check, - still says read only that's just because they don't update it right. - If you hit a five here and reload the page, - you'll see that it actually has changed to read, - right. - Let's go back to details, - scroll down, - and now we'll create my access token. - When you create this access token, - sometimes you'll need toe weight even up to a couple of minutes for degenerate on display - on your page here. - As you scroll down, - you can see that it still isn't here, - so we'll just hit F five update. - And here we go. - It gives us our access token, - our access token secret, - and our access level is reading right now, - as it says on both the access token and the consumer, - there's a secret version of that. - These are things that you don't want to share. - So now that we have these four pieces of information, - we're ready to start integrating it into our python script. - So let's bring our console window back over here, - slide the Internet it for window over a little bit so we can kind of still see our keys. - Now, - this is what I really like about ssh ing into my raspberry pi when I'm working on it. - I've got my regular desktop here where I can look things up on the Internet really easily - copy things you can copy and paste through the windows here, - which we'll see in a moment which really makes this nice when we're copying these really - huge keys. - So let's go ahead and CD into are still share directory and open up our monitor file. - Now. - If you remember, - we need Teoh. - Go back and look at it real quick. - We need to do from twice on import oil on. - So actually, - let's go ahead and show this week just to control, - see here and come down. - And in Nano here, - if you just right click it pastes in. - So it's pretty nice, - all right, - and we'll just go down to where we set up everything else. - It's probably a good place to put it. - Just put in her comment, - but and we'll do the variable. - The same will say Twitter, - People's Now we just paste in our app key, - otherwise known as our consumer Keilar consumer secret and then the token and the token - secret. - One thing to note while you're doing this is that these should all be strings, - so you'll have to double quote them. - If you copy him in there without the quotes around them, - you'll get an air when you try to run it. - So let's go ahead and do that real quick, - all right, - that takes care of that. - Now let's just do a simple test. - So when we start our monitor, - let's just haven't update the status in our Twitter feed to say that it's starting up to do - that again, - we refer back to the get home page where it says updating status. - And again we'll just copy this right out of here and pasted in. - We'll do a status of I just powered up, - ready to monitor. - Okay, - so each time we run our script, - it should update the Twitter status to let us know that it's powered up and ready to go. - Let's exit out and see. - And before we run this, - open up another page here and do twitter dot com pie Home monitor. - You can see it's just an empty account here. - We don't have any tweets yet, - do you? - Pseudo python monitor dot pie. - Let's see what happens. - Care monitors running. - It's come back over to our page here. - Well hit F five to refresh. - And there we go. - I just powered up, - Ready to monitor? - Perfect. - As promised in the last video. - Let's use our Twitter account to do something a little more interesting than turning on an - led when our sensor detects motion. - Pull this back over here. - Let's go back into our file and something that I want to mention. - While I was testing this, - I learned something new about the twice on module and actually the Twitter a P I, - and it's that the a p I will look at your recent, - uh, - status updates. - And if you try to post a duplicate status, - the A P I will return a four or three forbidden air. - I don't know what recent means to Twitter, - so I don't know if that's after 20 minutes you can post a status that's the same as it was - 20 minutes ago, - or if that's an hour, - I'm not sure, - But within a few minutes, - if you try to post this, - I just powered up, - ready to monitor? - You'll get a four or three, - your script will exit. - And so we don't want this at the start and just something to be aware of. - As you're putting Twitter integration into your scripts, - make sure that the status is different. - We could leave this status here and give it a unique identify or like we could put - something associated with the date and time down to the second so that it's it's unique - every time it posts, - but just something to be aware of. - We're gonna go ahead and cut this line out altogether, - because what we really want to show is how weaken, - tweet when we detect motion in the system is armed. - So let's come down here to where we detect our motion Right now we're just setting the led - pin too low, - which turns are led on. - This is where we're gonna want to do our twitter integration. - So if the systems arm and we detect motion, - come down here and it seems like we could just add a status update right here. - So if we did twitter dot of dates status, - then we do status equals something. - The problem with this is that this runs is part of our wild That's executing superfast like - we've talked about before. - So right now in this, - when it gets down to this section of code, - it's trying to turn the led on over and over and over hundreds of times a second, - which doesn't matter with the led, - it's already on. - Trying to turn it on again doesn't hurt anything. - But in the case of our Twitter statement here, - we're gonna try and update the status on Twitter hundreds of times every second. - That's not what we want. - So we need some way to meet her that, - and do it on a time interval So what we'll do is what I have. - One more. - If here we got a lot of ifs, - we'll say if, - just like we did a above the current time minus blast Tweet is greater than or equal to 10 - seconds. - So it's been 10 seconds since we've last updated. - Then we'll go ahead and update our status and so that we don't have a repeat. - We'll reset the last tweet time to now and remember, - we need to make this tweet unique. - So let's create some text right here. - We'll say status text equals. - And let's just use the date to help make our status update uni. - So we'll do stir f time so form at the time. - If we use this percent Capital X, - that'll give us the military time, - including seconds. - And so that should be good enough. - We do that and we need to format the local time, - and then we'll just add right here. - I detected motion and then, - while we're at it, - what we go ahead and say by the way the current temp is, - and then what we'll do up the end here is we'll do a stir on our current temp variable - remember, - we're taking that every five seconds. - That's just above that. - Should work. - And then for the status will say, - just set this to the status text. - And because we use this last tweet variable, - we need to come up here right where we do our in our initialization. - We'll just set, - uh, - last tweet to right now. - Okay, - That should do it. - So let's go ahead and exit out of here. - Yes, - and let's redness. - We have to make sure system is armed. - So go ahead and do that, - then will wait 10 seconds. - And once the 10 seconds sir up, - we should be able to do a little bit of motion in front of the sensor and get a status - update on our Twitter account. - So I think that's been about 10 seconds. - Let's go ahead and try this. - Okay? - The led came on. - I have an error in there, - so it's go ahead back in. - I forget use local time is a function. - Let's come all the way back down here. - Oh, - no. - I did call this function. - I misspelled it. - Okay, - there we go. - Local time. - All right. - Save that now. - When we run this again we'll get that warning because I rescript ended prematurely last - time as it sets up the GPO channels. - I will see that there. - That's OK. - It's still running. - Uh, - it's still armed. - I left the switch in the armed position, - so we just need to wait that 10 seconds again. - Okay? - Early D came on and I'll go ahead and exit from our script here and now that's left is to - come over. - I'll move this out of the way and see if our Twitter feed updated. - Let's go ahead and reload this page here and here. - We have our tweet. - It's got the time in front of it. - The text that we put in there, - 68.45 degrees Fahrenheit. - We could have formed out of that a little better and put the F in there. - But you get the point here. - We have completely integrated with Twitter. - We can do lots of cool things now. - I mean, - use your imagination. - You could track the temperature and tweet every hour with the average temp Has been you - could do things like threshold monitoring. - You could say if it gets above 70 degrees, - send a tweet to my feet. - If it gets below 60 degrees, - send a tweet things like that. - So hopefully you find this interesting and that you've got some ideas now that you can - integrate into your own application. - As always, - if you need help or have questions, - the best place to go is on the skill share website For this class under the community tab, - you can also reach him via email or find me on Twitter. 25. Conclusion: - We've covered a lot in this class. - We've gone from having a raspberry pi right out of the box to having a device that can - detect temperature and motion and even communicate on Twitter. - I really hope you've enjoyed the class and have learned some things that will help you - create your own projects. - One thing I failed to mention is that once your hardware is finished, - you can clean up your bread board. - You may have noticed that we had wires everywhere. - Once things are working, - you can clean up the connections with wires, - cut the size or even do that along the way is your building. - I like to wait until the very end, - just in case I decide to relocate something. - Here's what my board looks like after cleaning things up. - I really hope that you apply the things you've learned in this class to make some amazing - projects for yourself. - I wanted to a quick recap of the different protocols we used and list some additional ideas - that you could implement with each. - We started off with the basic input output functionality of our header pins in the class - project. - We use that functionality to turn led on and off, - detect the state of a switch and button and even read the state of our motion sensor. - Basically, - anything that communicates an on off state or can be controlled with a simple on off state - is a good fit here. - Things like relays will allow us to turn big things on and off lights, - appliances and motors. - Just name a few. - You can actually control your home furnace and air conditioner with just a few pins - controlling on off states. - The two communication protocols we covered SP I and I two C will allow you to talk to just - about any module you can imagine. - We use them to talk to a display and temperature sensor, - but that barely scratches the surface. - Their sensors for humidity, - gas, - altitude pressure, - color, - light and many more. - And these protocols aren't just for traditional sensors, - either. - You can use them to talk to things like barcode and fingerprint scanners, - GPS modules and or, - if I D readers, - you can use the principles and patterns we followed in this class to connect almost - anything to your raspberry pi. - And whatever the project, - maybe I'd love to hear about it as a reminder the final code for this class project is up - on Get Hub at the link below. - I've also included the link in the resource section for this year to help you in your own - projects. - Have attached a simple cheat sheet of tips and reminders that you can download and have - handy when you work on your own projects. - If you have questions or just want to share an awesome project, - you're always welcome to contact me via email or Twitter. - But please also post on the community tab for this project on skill share so others can - benefit from your work again. - I want to thank you for enrolling in the class and I invite you to keep learning, - have fun and start creating.