PC/CP320 Physical Computing

Raspberry Pi Python GPIO and Serial Console Introduction


  1. To introduce Python programming on the Raspberry Pi
  2. To become familiar with Raspberry Pi GPIO pins and BOARD and BCM numbering schemes
  3. To learn nano editor commands
  4. To introduce using the serial console on the Raspberry Pi





The Rapsberry Pi GPIO pins can easily be damaged with improper handling. For this lab, you will start by only connecting an oscilloscope to a GPIO pin.

  1. Testing the Serial adapter driver installation

    Connect the TTL-serial cable via the USB cable to the computer,
    Open the Device Manager and check to see what COM port the adapter appeared on.

  2. Open putty, and set it for serial on the COM port just noted and set the baud rate to 115200.

  3. Any time you connect the cobbler, make sure you have it in the correct orientation!!!

    Connect the TTL-serial cable to the Raspberry Pi as follows:
    • Don't connect the red wire.
    • Connect the black wire to GROUND.
    • Connect the white wire to TxD. Use a white wire for the connection!
    • Connect the green wire to RxD. Use a green wire for the connection!

  4. Plug in the Pi, and you should see the boot process in the serial console and get a login prompt as expected.

    Show this to the lab instructor.

  5. Running the python test programs

    Since there may be code for some other purpose already on the Pi, delete it by typing:
    rm test.py
    Type in the modified python example show in lecture using nano.
    nano test.py
    (Save and exit nano with CTRL-X.)

  6. Connect the oscilloscope to the GPIO pin to observe the output.
    Don't forget to connect the ground lead to one of the GPIO ground pins.

    To run a program, type
    sudo python test.py
    Note: If the program doesn't access GPIO pins, you don't need sudo.
    To exit a program type CTRL-C.

  7. Modify the program to use the same pin GPIO18 but using the Broadcom numbering scheme. Show the modified working program to the lab instructor.

  8. Testing program speed

    Reduce the sleep time in the program and rerun the program to see how fast the program can go. What is the maximum output frequency you can produce this way?

    Demonstrate the output using the scope and calculating the frequency to the lab instructor.

  9. Using Pulse Width Modulation on GPIO18

    Broadcom pin GPIO18 has the ability to produce asquare wave output using Pulse Width Modulation or PWM. Once started, a square wave will be produced without the program having to toggle the pin explicitly. To set it up, after the pin is set to output
    p = GPIO.PWM(12, 60)
    will set up the pin for a 60Hz waveform.
    will turn it ON with a 50% duty cycle and
    will turn it OFF.

    will change the duty cycle to 20% without stopping it and
    will change the frequncy to 100Hz without stopping.

    Note: When you write this program, what you want to do inside a loop is to periodically change the duty cycle. (Fo instance, every second change it from a high to a low duty cycle, and vice versa.) All of the setup only needs to be done once, and stopping the channel only needs to be done when the program exits.

    Now connect the LED and resistor to the pin, and see how changing the duty cycle can change the brightness.

    Demonstrate the output using the scope and LED and the effect of changing the duty cycle to the lab instructor.

  10. Shut down the pi using
    sudo shutdown -h now
    When shutdown is complete, you can disconnect power and put everything away.


To view pdf documents, you can download Adobe Acrobat Reader .
get Acrobat Reader
If you need to update a browser, you might try Firefox which is Get Firefox!
Since this page uses cascading style sheets for its layout, it will look best with a browser which supports the specifications as fully as possible.

If you are looking for an office package, with a word processor, spreadsheet, etc., you might try LibreOffice which is Get LibreOffice!

Go to the main page for the Department of Physics and Computer Science.

Valid XHTML 1.1

Valid CSS!

(Level AA)

Wilfrid Laurier University