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

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

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

      7:52
    • 4. The GPIO Header

      9:29
    • 5. Setup Basic Input/Output

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

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

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

      3:20
    • 9. Safety Reminder

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

      7:45
    • 11. Switch Integration (Steps 3-5)

      10:03
    • 12. Button Integration (Steps 6-7)

      8:20
    • 13. Soldering Headers

      3:27
    • 14. Intro to SPI (Step 1)

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

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

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

      26:43
    • 18. I2C Basics (Step 1)

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

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

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

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

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

      6:07
    • 24. Twitter Integration

      19:39
    • 25. Conclusion

      2:39
12 students are watching this class

Project Description

Create a Simple Home Monitoring Device

Class Introduction

  1. Welcome

    Thank you for enrolling and welcome to class

  2. Where to Get Help

    Please share questions on the Community section for the class so others can benefit.

    You are welcome to email me at:

    [email protected]

  3. What We Will and Won't Cover

    We Will Cover:

    • Setup and Configuration of the Pi
    • Basic I/O
    • SPI
    • I2C
    • Connecting to Twitter

    We Won't Cover:

    • In depth electronics
    • How to program
  4. Start Thinking of Your Student Project

    Create your student project. Even if you haven't settled on one you can still create a draft version of it that you can edit later.

Setting Up Your Pi

  1. Download NOOBS

    You can download NOOBS from the Raspberry Pi website:

    http://www.raspberrypi.org/downloads

  2. Format SD Card

    On Windows and Mac use the SD Card Formatting tool. For Linux it is recommended that you use gparted and make sure to format the card as FAT32. Using FAT16 will not work.

  3. Prepare your SD Card with NOOBS

    Once formatted all you have to do is copy the downloaded NOOBS files to your SD card. Remember to copy the contents of the NOOBS folder to the SD card and not the top level folder.

  4. Unbox and Hook Up Pi

    Connect power, ethernet, a keyboard and video source to your Pi. You can optionally install your Pi in a case but it is not required for this project. If you do use a case make sure you have easy access to the header pins.

    Safety Tip: If you are not using a case make sure you don't put your Pi on a surface that can conduct electricity like metal. Doing so will likely ruin your Raspberry Pi. 

    Time Saver: I recommend using a surge protector with a switch if you have one. Plug your Pi into that and then you can turn it on and off via the surge protector switch instead of having to plug/unplug from the wall. If you don't have one I recommend connecting and disconnecting your Pi from the outlet end and not using the connector on the board.

  5. Install Raspbian

    Insert the prepared SD card into the Raspberry Pi. Make sure it is all the way in and power up your Pi.

    From the NOOBS menu use the keyboard arrow keys to highlight Raspbian and the spacebar to select it. Once selected press 'i' to begin the installation process.

    Time Saver: While Raspbian is installing you can use the 'l' key to bring up the language menu and '9' to set the keyboard layout. Doing so during the install will save you from having to do it later.

  6. Add WiFi Adapter (Optional)

    If you have a USB WiFi adapter for your Raspberry Pi you should install it now. While internet access is required for the class project it does not have to be via WiFi. Leaving the ethernet cable connected will work just fine.

  7. Test Internet Connection

    We will need access to the internet for this project. If you are using a WiFi dongle make sure it is configured and working at this point before continuing.

    ping google.com

  8. Test SSH (Optional)

    If you want to SSH into your Pi to work on the class project you should test that now. If you want to continue using the monitor and keyboard you can skip this project step.

    Default credentials for Raspbian are:

    username: pi

    password: raspberry

  9. Update your Pi

    This will ensure that everything is up to date before you begin on your project.

    sudo apt-get update

    sudo apt-get upgrade

The GPIO Header

  1. Locate the GPIO Header

    ae69fd90

  2. Identify Pi Revision

    Board Revision History

  3. How Pins are Numbered

    When looking at the header vertically the pins are numbered like you read a book; left to right, top to bottom.

  4. Label Header Pins

    Select one of the methods described in the video or choose your own. Either way it is very important not to get the pins mixed up as you can easily ruin your Pi.

  5. Setup Your Testbed

    Whether you use jumper wires or a cable to make connections, now is a good time to get your Pi and breadboard set up in a location that is well lit and easily accessible.

    631c941b

  6. Understand Pin Do's and Don'ts

    DO:

    • Use female jumper wires
    • Verify the board revision
    • Verify connections before making them

    DON'T:

    • Make connections while Pi is turned on
    • Connect 5V to the I/O pins
    • Touch the header while powered on

Setup Input and Output

  1. Verify Python is installed

    From the command line type python --version. This class will be using version 2.7.3.

  2. Install python-dev package

    sudo apt-get install python-dev

  3. Test RPi.GPIO

    We can test the library by getting the board revision. Launch a python shell with sudo python and then do the following commands:

    import RPi.GPIO as GPIO

    GPIO.RPI_REVISION

Output: Hello LED

  1. Understand the 2 Ways to Connect an LED

    The Pi output pin can be the voltage source or the ground source.

  2. Plan Breadboard Layout of LED Circuit

    I use Fritzing to mock up my circuits before connecting them. This helps me verify the connections and see how much space things will take up.

  3. Make LED Connections

    For more information about LEDs and connecting them check out this site.

    315a3580

  4. Understand GPIO.BOARD vs GPIO.BCM

    Referencing the header pins in GPIO.BOARD mode will work on any version of the Pi because it simply refers to the physical pin number. Using GPIO.BCM mode requires you to reference the correct pin name based on the board revision you are using.

    We will use GPIO.BOARD mode for this class.

  5. Use RPi.GPIO to turn the LED on/off

    import RPi.GPIO as GPIO

    GPIO.setmode(GPIO.BOARD)

    GPIO.setup(12, GPIO.OUT)

    GPIO.output(12,1)

    GPIO.output(12,0)

Input: Add a Switch and Button

  1. Understand Switch and Button Connections

    We use the switch to simply connect the input pin to 3.3V which is a digital 1 or to ground which is a digital 0. Remebering that a button is just a momentary switch will allow us to use the same circuit design for our button that we use for our switch.

  2. Connect Switch

    Make sure your Raspberry Pi is turned off.

    bd2bb530

  3. Test Switch Functionality

    Verify the that switch is working as we expected.

  4. Create monitor.py

    For the rest of the class I'll be using nano text editor for editing this file. If you've never used it before it might help to check out the Quick Guide to nano

  5. Make Switch Control LED

    For this step you will modify the code to have the switch turn the LED on and off.

  6. Connect Button

    Connect the button in the same way we connected the switch.

  7. Test Button

    With the button connected we need to test the switch. We add a software "debounce" to avoid triggering the push code multiple times.

Solder Break

  1. Make Headers for Temp and Display Modules

    If you have male headers available I recommend using them on the breakout boards of the temp and display modules however hookup wire works just fine if it's all you have.

  2. Solder Headers to Temp Board

    Don't let this scare you. If you've never soldered before that's alright. You can do this! Read the tutorial linked in the Resource section below and take your time.

  3. Solder Header to Display Board

    Remember NOT to solder the 2-pin header used to level the board for soldering.

Setup SPI

  1. Gain Basic Understanding of SPI Protocol

    Think of the SPI protocol like you would any other standard protocol like USB or Thunderbolt. It requires certain physical connections (MOSI, MISO, CLK, etc) and then follows a certain specification. The most important points to remember are:

    • It's a Master/Slave Protocol
    • There are different clock modes available
    • Speed is controlled by the Master
    • You have to write a byte to read a byte
  2. Download and Compile SPI Test Program

    wget https://raw.github.com/torvalds/linux/master/Documentation/spi/spidev_test.c gcc -o spidev_test spidev_test.c ./spidev_test -D /dev/spidev0.0

  3. Try the Loopback Test

    Don't forget to connect the MOSI pin to the MISO pin on the pi. ** Always unplug the Pi before making connections **

  4. Remove SPI From Blacklist

    The file to modify is /etc/modprobe.d/raspi-blacklist.conf

  5. Reboot Pi

    sudo reboot

  6. Confirm SPI Test Works

    This time when you run the loopback test you should get a block of data.

Add the Display

  1. Review Display SPI Info

    We need to know what connections to make, what mode to run in, speed limitations, and power requirements. See the Resource section below for a link to the datasheet.

  2. Connect Display to Breadboard

    ** Turn off the Pi before making connections **

    c94f694a

  3. Send Data to Display Using Echo

    echo -ne "\x01\x02\x03" > /dev/spidev0.0

  4. Clear the Display Using Echo

    echo -ne "\x76" > /dev/spidev0.0

  5. Install spidev Module

    The links for the two files are:

    https://raw.github.com/doceme/py-spidev/master/spidev_module.c

    https://raw.github.com/doceme/py-spidev/master/setup.py

  6. Test spidev Module

    To test via the python shell use the code below:

    import spidev

    spi = spidev.SpiDev()

    spi.open(0,0)

    spi.xfer2([0x01,0x02,0x03])

    spi.xfer2([0x76])

    spi.close()

  7. Write spi_send Function

    For this step you need to write a function called spi_send that will allow us to send data to modules using the SPI protocol.

  8. Write clear_display Function

    To make clearing our display easy we'll write a function that we can use.

  9. Add Time Display Functionality

    As part of the class project we want to show the current time on the display. In this step you will write the code to do that.

  10. Set Timezone

    Use tzselect from the terminal to figure out what to add to your .profile file

  11. Enable Colon Display

    spi_send([0x77, 0x10])

Configuring I2C

  1. Learn Basics of I2C Protocol

    Think of the I2C protocol like you would any other standard protocol like USB or Thunderbolt. It requires certain physical connections (SDA, SCL, power) and then follows a certain specification. The most important points to remember are:

    • It's a Master/Slave Protocol
    • All slaves need a unique address
    • Speed is controlled by the Master clock
    • Multiple devices can share the same 2 lines
  2. Add I2C to Startup Modules

    Added the following lines to /etc/modules:

    i2c-bcm2708

    i2c-dev

  3. Remove I2C From Blacklist

    In /etc/modprobe.d/raspi-blacklist.conf comment out the I2C line with a #

  4. Install python-smbus and i2c-tools Packages

    sudo apt-get install python-smbus

    sudo apt-get install i2c-tools

  5. Reboot Raspberry Pi

    sudo reboot

Install the Temperature Sensor

  1. Review Sensor Datasheet

    TMP102 Datasheet

  2. Hook up Sensor

    2852c03c

  3. Verify Sensor is Connected

    sudo i2cdetect -y 1

  4. Test Sensor Communication

    Using the python shell, verify that we can retrieve data from our temperature sensor.

  5. Add Function to Get Temperature

    We'll need to rely heavily on the datasheet for this one as we'll need to manipulate the return value into the correct format.

  6. Show Temp on SPI Display

    Write a function that will take a temperature reading and put it on the display.

Don't Move! Add Motion Detector

  1. Review Motion Sensor Datasheet

    Motion Sensor Datasheet

  2. Hookup Motion Sensor

    47f4881b

  3. Add Motion Detection to Script

    We want our switch to "arm" the system and use the LED to let us know if it detected motion while we were gone.

  4. Test Motion Sensor Functionality

    Run the script with your code changes and verify that everything works as expected. Test the monitor in "home" and "away" mode.

Make it Tweet

  1. Create Twitter Account For Device (Optional)

    Only do this if you want your device to have it's own twitter account. If you want the device to use your personal Twitter profile for tweeting you can skip this step.

  2. Install pip

    sudo apt-get install python-pip

  3. Install Twython

    In the video we use pip but you can also use easy_install or even pull it down from Github.

    sudo pip install twython

  4. Create Twitter App

    Your twitter app is how your device will tweet.

  5. Tweet Motion and Temp Reading

    Use twython in the script to post a status update to your Twitter feed. You can get as creative as you want and do not have to follow the video implementation. Use your imagination.

Summary

  1. Start Creating

    Thank you so much for enrolling in this class. I hope you've enjoyed it as much as I enjoyed teaching it. Good luck and have fun creating.

Additional Resources