{ "metadata": { "name": "RaspBerryPi" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "The Awesome RaspberryPI\n", "=======================\n", "From raspberypy.org:\n", "The Raspberry Pi is a credit-card sized computer that plugs into your TV and a keyboard. It is a capable little computer which can be used in electronics projects, and for many of the things that your desktop PC does, like spreadsheets, word-processing and games. It also plays high-definition video. We want to see it being used by kids all over the world to learn how computers work, how to manipulate the electronic world around them, and how to program.\n", "\n", "This notebook is going to just focus on using the Raspbery Pi for electronics projects." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Blink!!\n", "-------\n", "Well start with something simple, lets blink an LED. To do this, we're going to connect the Raspberry Pi's General Purpouse Input Output (GPIO) #18 up to an LED.\n", "\n", "So, first we connect the RaspBerry Pi's GPIO18 pin to the long wire on the LED. GPIO18 is connected to pin 12 on the Raspberry PI (yes it is confusing but frequently the number the CPU uses to reference a GPIO pin is not the same as the pin number it is connected to). \n", "\n", "Next we need to connect the short pin of the LED to a component called a resistor. The other end of the resistor connects to the ground pin (the ground acts like the negative side of a battery). A resistor as the name implies resists the flow of electricity. We need this resistor because the Raspberry PI GPIO pins operate at 3.3 volts but most LEDs can only handle 2 volts or so. This resistor is frequently called a current limiting resistor since that's what it's doing.\n", "\n", "Now that it's all wired up, when we tell the Raspberry PI to turn on GPIO18 it will turn the switch on electricity will flow from the GPIO18 pin, into the long lead of the LED, out, through the current limiting resistor and finally to the ground.\n", "\n", "Before we use a GPIO pin we have two do two things. \n", "\n", "1. Set the pin numbering we're using (the python module can use either the board pin numbers GPIO.BOARD or the logical GPIO pin numbers GPIO.BCM)\n", "2. Set the mode the port should be in, this can be Input if it's going to be getting values from the pin, Output if it's going to be setting the pin to specific values." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Blink an LED on pin 18.\n", "# Connect a low-ohm (like 360 ohm) resistor in series with the LED.\n", "\n", "import RPi.GPIO as GPIO\n", "import time\n", "\n", "# A variable so we can change the PIN number for this script in once place\n", "# if we move the LED to a different pin.\n", "PIN = 18\n", "\n", "# Set the pin to do output\n", "GPIO.setmode(GPIO.BCM)\n", "GPIO.setup(PIN, GPIO.OUT)\n", "\n", "\n", "# Loop forever blinking our led\n", "while True:\n", " # Switch the pin off for half of a second\n", " GPIO.output(PIN, 0)\n", " time.sleep(.5)\n", "\n", " # Now turn it back on\n", " GPIO.output(PIN, 1)\n", " time.sleep(.5)\n" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "KeyboardInterrupt", "evalue": "", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;31m# Switch the pin off for half of a second\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0mGPIO\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moutput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mPIN\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 20\u001b[0;31m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m.5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 21\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0;31m# Now turn it back on\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mKeyboardInterrupt\u001b[0m: " ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Oooh, bright lights\n", "-------------------\n", "Although the computer is only able to turn the LED off or on, it is possible for it to rapidly cycle the LED to change the brightness. This is frequently called Pulse Width Modulation (PWM) and we hooked the LED to GPIO18 because that specific GPIO pin has a hardware PWM circuit which does a better job than cycling the LED in software.\n", "\n", "The Raspberry PI GPIO module allows us to change how frequently the LED stays on by changing it's duty cycle. Below we show an LED going brigher and dimmer." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import time\n", "import RPi.GPIO as GPIO\n", "\n", "# Set our pin to output mode\n", "GPIO.setmode(GPIO.BOARD)\n", "GPIO.setup(12, GPIO.OUT)\n", "\n", "# Set the PWM frequency in Hz (cycles per second)\n", "p = GPIO.PWM(12, 60) # channel=12 frequency=60Hz\n", "\n", "# Start doing PWM\n", "p.start(0)\n", "\n", "try:\n", " # Loop forever\n", " while True:\n", " # Increase the duty cycle (percentage of time the pin is on) from\n", " # 1% to 100%. This will cause the LED to get brighter\n", " for dc in range(0, 101, 1):\n", " p.ChangeDutyCycle(dc)\n", " time.sleep(0.05)\n", "\n", " # Now decrease the duty cycle (time the pin is on) back to 1. This will\n", " # cause the LED to dim\n", " for dc in range(100, -1, -1):\n", " p.ChangeDutyCycle(dc)\n", " time.sleep(0.05)\n", "except KeyboardInterrupt:\n", " pass\n", "p.stop()\n", "GPIO.cleanup()\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Interfacing with More than one thing\n", "====================================" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#!/usr/bin/python3\n", "\n", "import RPi.GPIO as GPIO\n", "import time\n", "import sys\n", "from range_finder import RangeFinder\n", "\n", "# Set our pin to output mode\n", "GPIO.setmode(GPIO.BOARD)\n", "GPIO.setup(12, GPIO.OUT)\n", "\n", "# Set the PWM frequency in Hz (cycles per second)\n", "p = GPIO.PWM(12, 60) # channel=12 frequency=60Hz\n", "\n", "# Start doing PWM\n", "p.start(0)\n", "\n", "\n", "if __name__ == '__main__':\n", " sensor = RangeFinder()\n", "\n", " print('Running main')\n", " while True:\n", " percent = sensor.average_distance_percent\n", " print(100-percent)\n", " if percent == 100:\n", " p.ChangeDutyCycle(1)\n", " elif percent < 100:\n", " p.ChangeDutyCycle(100-percent)\n", "\n", " time.sleep(.1)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Running main\n", "93.2866157239" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "93.3810570661" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "93.500181941" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "93.3397389789" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "93.3671055042" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "93.3204214316" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "93.3070064682" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "93.4234483504" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "93.3375925847" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "93.311835855" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "93.3563735335" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "93.2952013004" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "93.3424219716" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "93.3429585701" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "93.3472513584" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "93.333836395" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "93.3375925847" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "93.3408121759" ] }, { "ename": "KeyboardInterrupt", "evalue": "", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0;32mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Running main'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 24\u001b[0;31m \u001b[0mpercent\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msensor\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0maverage_distance_percent\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 25\u001b[0m \u001b[0;32mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m100\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mpercent\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 26\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mpercent\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m100\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/home/pi/presentation/range_finder.py\u001b[0m in \u001b[0;36maverage_distance_percent\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 90\u001b[0m \u001b[0;34m@\u001b[0m\u001b[0mproperty\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 91\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0maverage_distance_percent\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 92\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0maverage_distance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0munit\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'percent'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 93\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 94\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0maverage_distance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0munit\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'inch'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/home/pi/presentation/range_finder.py\u001b[0m in \u001b[0;36maverage_distance\u001b[0;34m(self, unit)\u001b[0m\n\u001b[1;32m 106\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 107\u001b[0m \u001b[0mtot\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdistance_inch\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m12.0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 108\u001b[0;31m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0.1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 109\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mtot\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msamples\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 110\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mKeyboardInterrupt\u001b[0m: " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "import RPi.GPIO as GPIO\n", "import time\n", "\n", "# A variable so we can change the PIN number for this script in once place\n", "# if we move the LED to a different pin.\n", "PIN = 7\n", "\n", "# Set the pin to do output\n", "GPIO.setmode(GPIO.BCM)\n", "GPIO.setup(PIN, GPIO.OUT)\n", "\n", "\n", "# Loop forever blinking our led\n", "while True:\n", " # Switch the pin off for half of a second\n", " GPIO.output(PIN, 0)\n", " time.sleep(5)\n", "\n", " # Now turn it back on\n", " GPIO.output(PIN, 1)\n", " time.sleep(5)\n", "\n", "GPIO.cleanup()" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "KeyboardInterrupt", "evalue": "", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;31m# Switch the pin off for half of a second\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0mGPIO\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moutput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mPIN\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 20\u001b[0;31m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 21\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0;31m# Now turn it back on\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mKeyboardInterrupt\u001b[0m: " ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "import RPi.GPIO as GPIO\n", "import time\n", "\n", "# A variable so we can change the PIN number for this script in once place\n", "# if we move the LED to a different pin.\n", "PIN = 7\n", "\n", "# Set the pin to do output\n", "GPIO.setmode(GPIO.BCM)\n", "GPIO.setup(PIN, GPIO.OUT)\n", "GPIO.output(PIN, 1)\n", "\n", "#GPIO.cleanup()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }