PC/CP320 Physical Computing
Raspberry Pi Python GPIO and Serial Console
Introduction
Objectives
- To introduce Python programming on the Raspberry Pi
- To become familiar with Raspberry Pi GPIO pins and
BOARD and BCM numbering schemes
- To learn nano editor commands
- To introduce using the serial console on the Raspberry Pi
Preparation
Equipment
- Raspberry Pi
- TTL-serial to USB adapter
- digital multimeter (DMM)
- LED and 1k resistor. (Depending on brightness, you may
need a smaller resistor.)
- breadboard
Procedure
|
The Rapsberry Pi GPIO pins can easily be damaged with
improper handling. For this lab, you will
start by
only connecting
a digital meter
to a GPIO pin.
|
-
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.
- Open putty, and set it for
serial on the COM port just noted and set the
baud rate to 115200.
-
Any time you connect anything to the
GPIO pins, make
sure you know 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!
- 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.
-
Here are some common commands for linux. Note that the cursor up
and down keys take you through a history of previously used commands.
Common linux commands
|
Command
|
Description
|
Example
|
ls
|
list files in directory
|
ls
*.py
|
cp
|
copy a file
|
cp
test.py newtest.py
|
mv
|
move (or rename) a file
|
mv
oldtest.py newtest.py
|
sudo
|
execute program as superuser
|
sudo
python3 test.py
|
mount
|
mount external (flash) drive
|
mount
/mnt/usb
|
umount
|
unmount external (flash) drive
|
umount
/mnt/usb
|
shutdown
|
shutdown computer properly
|
sudo shutdown
-h now
|
Here are some common commands for nano.
Common nano commands
|
Command
|
Description
|
CTRL-K
|
cut line(s)
|
CTRL-U
|
uncut line(s) (i.e. paste)
|
CTRL-X
|
save and quit
|
CTRL-O
|
save without quitting
|
-
Running the first python test program
- Download gpio_test_basic.py
according to the serial
console wifi instructions.
Note: An alternative way to copy a program is to
- Open the file on the PC
- Start the nano editor
on the Raspberry Pi
- Copy the program from the window on the PC and paste
it into the terminal window. (On the PC, copy with CTRL-C
and paste in the terminal window by clicking the right
mouse button.)
- Exit nano giving the file whatever
name you want.
(A good choice would be gpio_test_basic.py. )
- Connect the
meter
to the GPIO pin to observe the
output.
Don't forget to connect the ground lead to one of
the
GPIO ground pins as well.
- To run a program, type
python3 gpio_test_basic.py
(Note: typing "python" instead of "python3" will use
python 2 instead.)
Note: In some versions of Raspbian, you may need
sudo to run programs which
access GPIO pins.
What is the voltage on the GPIO pin when it is HIGH?
What is the voltage on the GPIO pin when it is LOW?
- To exit a program type CTRL-C.
- Now try to re-run the program. What happens?
GPIO pins are not automatically released when a program
stops. In order for this to happen you must release them
before the program exits.
- Download gpio_test_2.py
- Run the program, as before, and then exit out. Rerun it and
see what happens.
This program makes sure that GPIO pins are released at the
end. This is a template you should always
follow.
- Now connect the LED and resistor on a breadboard, and
use the circuit in place of the meter to observe the program
output.
-
Make a copy of the program so that you can edit the copy.
To do this, type
cp gpio_test_2.py test.py
Now you can edit test.py
using nano.
To cut a line in nano, type CTRL-K.
To uncut (i.e. paste) a line in nano, type CTRL-U.
(Save and exit nano with CTRL-X.)
(To save without exiting type CTRL-O.)
-
Modify the program to use the same pin GPIO18
but using the Broadcom
numbering scheme.
Show the modified working program to
the lab
instructor.
-
Backing up test programs to flash drive
If you have a flash drive, in sert in into one of the USB ports
and type
mount /mnt/usb
You should be able to use the cp
command to copy files to the flash drive.
You should be able to use the ls
command to see that the files were copied to the flash drive.
Before you remove the drive, be sure to unmount
it
using
umount /mnt/usb
Show this to the lab instructor.
-
Proper shutdown
Shut down the pi using
sudo shutdown -h now
When shutdown is complete, all of the onboard LEDS will stop
flashing,
so you can turn off and disconnect power and put
everything away.
Wilfrid Laurier University
© 2019 Wilfrid Laurier University