Embedded Software Development with Python and the Raspberry Pi Class 4: Controlling Raspberry Pi Peripherals with Python August 21, 2014 Jacob Beningo, CSDP © All Images and content in this presentation may not be used or copied without express written permission of the owner.
Course Overview Introduction to Python Python Fundamentals An Overview of the Raspberry Pi Controlling Raspberry Pi Peripherals with Python An Internet of Things Weather Station Example
Session Overview Controlling Raspberry Pi Peripherals with Python The Raspberry Pi Ecosystem Library Setup GPIO Setup and Example I2C UART SPI Analog Sensors Pi Camera
Ecosystem
Library Setup Python Raspberry Pi Libraries Installation Steps Provide code modules to easily access hardware Easy to Install Installation Steps Update Packages Install GPIO Library Install git Install optional Adafruit Python Libraries
Library Setup – Update Packages sudo apt-get update
Library Setup – Install Python 2.7 sudo apt-get install python-dev
Library Setup – Install GPIO sudo apt-get install python-rpi.gpio
Library Setup - Optional Adafruit Libraries Useful interfaces for experimentation Install git sudo apt-get install git Clone Adafruit repository git clone http://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git cd Adafruit-Raspberry-Pi-Python-Code ls
Library Setup - Optional
A GPIO Example Goals Setup Toggle an LED using Python Adjustable blink rate Use of ctrl-c to exit application Setup Use nano GPIO18 as an output Use python sleep()
A GPIO Example
Setting Up I2C Add I2C modules sudo nano /etc/modules Don’t forget to reboot after this step! sudo reboot
Setting Up I2C Install I2C tools sudo apt-get install python-smbus sudo apt-get install i2c-tools Modify /etc/modprobe.d/raspi-blacklist.conf Don’t forget to restart after saving sudo reboot
Setting Up I2C sudo i2cdetect –y 1
Setting Up the UART UART0 by default is setup for Kernel Debugging
Setting Up the UART Run the UART Script Steps Disconnects the UART from Kernel Steps sudo wget https://raw.github.com/lurch/rpi-serial-console/master/rpi-serial-console -O /usr/bin/rpi-serial-console && sudo chmod +x /usr/bin/rpi-serial-console rpi-serial-console status Rpi-serial-console disable sudo reboot The script modifies the entries for ttyAMA0 in /boot/cmdline.txt /etc/inittab
Setting Up the UART Install python-serial Most likely already installed and up to date sudo apt-get install python-serial
Setting Up SPI Install Spi Wrapper mkdir python-spi cd python-spi wget https://raw.github.com/doceme/py-spidev/master/setup.py wget https://raw.github.com/doceme/py-spidev/master/spidev_module.c sudo python setup.py install
Setting Up SPI
Reading Analog Sensors Model: A / B / B+ Model: B+ Only
Reading Analog Sensors MCP3008 ADC Chip
Pi Camera Enable Camera Sudo raspi-config Reboot Python Test Script
Going Further Download Template Materials for Web Tutorials YouTube Python Doxygen Script Template Personal Weather Station Python Script Raspberry Pi Example Python Scripts Web Tutorials YouTube From www.beningo.com under Blog and Articles > Software Techniques > Embedded Software with Python
Contact Information P.O. Box 400 Linden, Michigan 48451 : jacob@beningo.com : 810-844-1522 : Jacob_Beningo : Beningo Engineering : JacobBeningo : Embedded Basics Jacob Beningo Principal Consultant © All Images and content in this presentation may not be used or copied without express written permission of the owner.