Raspberry pi gpio
This presentation is the property of its rightful owner.
Sponsored Links
1 / 24

Raspberry Pi GPIO PowerPoint PPT Presentation


  • 108 Views
  • Uploaded on
  • Presentation posted in: General

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

Download Presentation

Raspberry Pi GPIO

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


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

    • WiFiin 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!


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: chmodugo+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


Pi Setup for Today

Will also need a WiFi or an Ethernet connection

Connect to power adapter not the USB port of your computer

  • 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


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 50mA

Image credit: http://elinux.org/RPi_Low-level_peripherals


The Bigger Picture

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

Diagram includes BCM GPIO references (GPIO.BCM), common

functions, WiringPi pin references, and Pin numbers (GPIO.BOARD).

A cheat nice sheet


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


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).


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


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


Understanding /sys/class/gpio/

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

  • sysfsin 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


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


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


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


Wiring Pin Numbers

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


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 ;

}


Running blink

  • Compile and run the blink program

    gcc -Wall -o blink blink.c -lwiringPi  compile

    sudo ./blink  run

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

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


Accessing memory allocated to the GPIO

  • /dev/memprovides user-level access to SoCmemory

  • Offset 0x20000000 is a address of BCM peripherals

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


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


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


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) ; // wiringPipin 0 is BCM_GPIO 17

delay (1000) ;

softServoWrite (0, 500) ;

delay (1000);

softServoWrite(0, 1000) ;

delay (1000);

}

}


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 a2000uSec (2mSec) pulse (hard right)

      • An input of 500 produces a 1500uSec (1.5 mSec) pulse (stop)


Using the gpio utility

  • The programgpiocan be used in scripts to manipulate the GPIO pins

  • Thegpiocommand 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

    gpiowrite 0 1

  • Sets pin 0 as output and then sets the pin to high

  • More info on the gpio utility


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


  • Login