raspberry pi gpio

24
Raspberry Pi GPIO Pin naming conventions Using sysfs Using the Wiring library Git and Github

Upload: muniya

Post on 05-Feb-2016

92 views

Category:

Documents


1 download

DESCRIPTION

Raspberry Pi GPIO. Pin naming conventions Using sysfs Using the Wiring library Git and Github. Pi Overview. So far we have tried to setup a “headless” connection to your Pi in this classroom. Serial with the FTDI cable Ethernet on the CS LAN WiFi i n Rhodes-Robinson Hall - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Raspberry Pi GPIO

Raspberry Pi GPIO

Pin naming conventionsUsing sysfsUsing the Wiring libraryGit and Github

Page 2: Raspberry Pi GPIO

Pi Overview• So far we have tried to setup a “headless” connection to your Pi

in this classroom.– Serial with the FTDI cable– Ethernet on the CS LAN– WiFi in Rhodes-Robinson Hall

• a hidden network managed by ITS• You must demonstrate your ability to connect to your Pi using

at least two of these three methods in order to receive a passing grade in this class.– This can be done at any point through out the remainder of

the semester---I will keep a list on Moodle• There are now over ten workstations configured in the CS lab

(RRO 223) to support a monitor, keyboard, and mouse connection. – This is an easier to get things working properly

• try it out, but put all the cables back!

Page 3: Raspberry Pi GPIO

Pi Overview• We’ve learned a little about the Pi

– BCM 2835 processor, 3.3V (3V3) power on pins, SD card is like the hard drive,…

• We’ve learned a little about Linux– The root directory: / , the super user designation:

sudo, change permissions: chmod ugo+x filename, …• We’ve learned a little about networking

– ssh [email protected]– ifconfig– The contents of the file: /etc/network/interfaces

• Today, our focus is on using the Pi as an embedded system controller

Page 4: Raspberry Pi GPIO

Pi Setup for Today

• Because the cobbler connector has a notch, you can only put the cable in the right way

• But, it is possible to put the cable in upside down on the Raspberry Pi

Pin 1 is the colored wire-must connect to pin 1 on the Pi

Connect to power adapter not the USB port of your computer

Will also need a WiFi or an Ethernet connection

Page 5: Raspberry Pi GPIO

RPi General Purpose IO (GPIO) Pins• 17 GPIO pins brought out onto the P1 header

• most have alternated functions • two pins for UART; two for I2C; six for SPI

• All 17 pins can be GPIO (i.e., INPUT or OUTPUT)• all support interrupts• internal pull-ups & pull-downs for each pin• I2C pins have onboard pull-ups • using them for GPIO may not work

• Pins are 3.3V not 5V like on the Arduino• They are connected directly to the

Broadcom chip • Sending 5V to a pin may kill the Pi• Maximum permitted current draw from

a 3.3V pin is 50mAImage credit: http://elinux.org/RPi_Low-level_peripherals

Page 6: Raspberry Pi GPIO

Diagram includes BCM GPIO references (GPIO.BCM), common functions, WiringPi pin references, and Pin numbers (GPIO.BOARD).A cheat nice sheet

The Bigger Picture(image credit: http://pihw.wordpress.com/2013/01/30/sometimes-it-can-be-simple)

Page 7: Raspberry Pi GPIO

Using the GPIO Pins

• There are two different methods to read or write these pins using Linux– Creating a file-type access in the file system – Write/read memory addresses allocated to the GPIO

peripheral of the SoC using pointers• Memory locations can be found in the

datasheet for the BCM2835

• We can use the Wiring library to help with both

Page 8: Raspberry Pi GPIO

Connect an LED using a resistor between GPIO 17 (P1-11) and GND

The LED will initially be off because the GPIO pins are initialized as inputs at power-on (except for TXD).

Page 9: Raspberry Pi GPIO

Using the File System• Create and run the following shell script (blink.sh)

using sudo: sudo ./blink.sh

#!/bin/shecho 17 > /sys/class/gpio/exportecho out > /sys/class/gpio/gpio17/directionwhile truedo        echo 1 > /sys/class/gpio/gpio17/value

sleep 1        echo 0 > /sys/class/gpio/gpio17/value

sleep 1done

Make the pin available for other applications using with the command: echo 17 > /sys/class/gpio/unexport

Page 10: Raspberry Pi GPIO

More Detail• Create a shell script using nano:

– nano blink.sh– Cut and paste the previous slide to nano window– Ctrl-w to save then Ctrl-x to exit nano– Change the permissions on blink.sh: chmod 755 blink.sh– Run blink.sh: sudo ./blink.sh (in directory where blink.sh is

stored)• After running the script your LED should be blinking endlessly.

Give the command: Ctrl-c Ctrl-c to abort the script• All of the commands in the script can be issued one at a time

on the command line; beginning by giving the commands: sudo -i to run a root shell---notice the change in the prompt

• Look at the files and their contents in directory /sys/class/gpio/ and its subdirectories --- see next slide

Page 11: Raspberry Pi GPIO

Understanding /sys/class/gpio/

• In Linux everything is a file: /dev/ttyUSB0, /sys/class/net/eth0/address, /dev/mmcblk0p2,…

• sysfs in a kernel module providing a virtual file system for device access at /sys/class– provides a way for users (or code in the user-space) to

interact with devices at the system (kernel) level• A demo• Advantages / Disadvantage

– Allows conventional access to pins from userspace– Always involves mode switch to kernel, action in kernel,

mode switch to use, and could have a context switch– Much slower the digitalWrite()/digitalRead() of Arduino

Page 12: Raspberry Pi GPIO

A C program to do the same thing

• GPIO with sysfs on Raspberry Pi (Part 2) • Code on Github• Beware: the code assumes a Rev1 pinout

Page 13: Raspberry Pi GPIO

Github

• The heart of GitHub is Git, an open source project started by Linux creator Linus Torvalds

• Git manages and stores revisions of projects– Think of it as a filing system for every draft of a

document

• Git is a command line tool – GitHub provides a Web-based graphical interface

• Basic functionality

Page 14: Raspberry Pi GPIO

Introducing the WiringPi library• A GPIO access library written in C for the

BCM2835– Writes/reads the base address of the memory

allocated to the GPIO • Similar to the Wiring library in the Arduino used

to make common IO operations easier• Features:

– command-line utility gpio– supports analog reading and writing– More

• Install the Wiring Pi library following these instructions

Page 15: Raspberry Pi GPIO

Wiring Pin Numbers

Image credit: https://projects.drogon.net/raspberry-pi/wiringpi/pins/

Page 16: Raspberry Pi GPIO

Blinking lights with Wiring#include <stdio.h>#include <wiringPi.h>

// LED Pin - wiringPi pin 0 is BCM_GPIO 17.#define LED 0

int main (void) { printf ("Raspberry Pi blink\n") ; wiringPiSetup () ; // note the setup method chosen pinMode (LED, OUTPUT) ;

for (;;) { digitalWrite (LED, HIGH) ; // On delay (500) ; // mS digitalWrite (LED, LOW) ; // Off delay (500) ; } return 0 ;}

Page 17: Raspberry Pi GPIO

Running blink

• Compile and run the blink programgcc -Wall -o blink blink.c -lwiringPi compilesudo ./blink run

• Runs forever---kill with the command ctrl-c ctrl-c

• Note: One of the four wiring setup functions must be called at the start of your program or your program will not work correctly

Page 18: Raspberry Pi GPIO

Accessing memory allocated to the GPIO

• /dev/mem provides user-level access to SoC memory

• Offset 0x20000000 is a address of BCM peripherals

• wiringPi.c writes to that area of memory to control the pins

Page 19: Raspberry Pi GPIO

Controlling a Servo with the Pi• Controlling the servos requires PWM, aka Pulse Width Modulation• The Arduino does this very well, the Raspberry Pi does it less well

– The Arduino program has complete control of the microcontroller • when it is running loop() nothing else can use the CPU

– Except for interrupt handlers written as part of the Arduino program

– On the Raspberry Pi, your program runs within a Linux OS• The Linux OS may switch to running another program!

– But you can change your program’s scheduling priority• Some ways of getting the Pi to give the impression that it is a real time

system and to do PWM ‘properly’:– Gordon Henderson has

written about an improvement to the WiringPi library to allow threaded PWM on every GPIO pin taking up 0.1% of the CPU each

– Rahul Kar has blogged about using the WiringPi library and PWM– WiringPi recommends ServoBlaster

Page 20: Raspberry Pi GPIO

Connect a Parallax Servo

Servo Connector: Black – Pi’s ground Red – Pi’s 5V White – signal on GPIO 17

Image credit: http://www.parallax.com/

NOTE: For a single small servo you can take the 5 volts for it from the Pi header, but doing anything non-trivial with four servos connected pulls the 5 volts down far enough to crash the Pi

Page 21: Raspberry Pi GPIO

Using WiringPi’s servo example#include <stdio.h>#include <errno.h>#include <string.h>#include <wiringPi.h>#include <softServo.h>int main () { if (wiringPiSetup () == -1) { // setup to use Wiring pin numbers fprintf (stdout, "oops: %s\n", strerror (errno)) ; return 1 ; } softServoSetup (0, 1, 2, 3, 4, 5, 6, 7) ; // wiringPi pin numbers for (;;) { softServoWrite (0, 0) ; // wiringPi pin 0 is BCM_GPIO 17 delay (1000) ; softServoWrite (0, 500) ; delay (1000); softServoWrite (0, 1000) ; delay (1000); }}

Page 22: Raspberry Pi GPIO

Running servo.c• To compile: gcc -Wall -o servo servo.c wiringPi/wiringPi/softServo.c compile softServo.c -IwiringPi/wiringPi path to softServo.c -lwiringPi include wiring library• To run: sudo ./servo• Calling softServoWrite () ;

– The 1st input is the pin number– The 2nd input should be from 0 (hard left) to 1000 (hard right). – The 2nd input refers to the number of microseconds of the pulse.

• An input of 0 produces a 1000uSec (1mSec) pulse (hard left)• An input of 1000 produces a 2000uSec (2mSec) pulse (hard right)• An input of 500 produces a 1500uSec (1.5 mSec) pulse (stop)

Page 23: Raspberry Pi GPIO

Using the gpio utility

• The program gpio can be used in scripts to manipulate the GPIO pins

• The gpio command is designed to be called by a normal user without using the sudo command or logging in as root

• Try at the command line:gpio mode 0 out gpio write 0 1

• Sets pin 0 as output and then sets the pin to high• More info on the gpio utility

Page 24: Raspberry Pi GPIO

And There’s more

• WiringPi provides support for C programming• There’s a lot of support for programming in

Python:– http://openmicros.org/index.php/articles/94-

ciseco-product-documentation/raspberry-pi/217-getting-started-with-raspberry-pi-gpio-and-python

– http://learn.adafruit.com/playing-sounds-and-using-buttons-with-raspberry-pi/install-python-module-rpi-dot-gpio