Raspberry Pi GPIO Pin naming conventions Using sysfs

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 – Wi. Fi 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!

Pi Overview • We’ve learned a little about the Pi – BCM 2835 processor, 3. 3 V (3 V 3) 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 pi@your. Last. Name-pi. cs. unca. edu – 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 Wi. Fi or an Ethernet connection Connect to power adapter not the USB port of your computer Pin 1 is the colored wiremust connect to pin 1 on the Pi • 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

RPi General Purpose IO (GPIO) Pins • 17 GPIO pins brought out onto the P 1 header • most have alternated functions • two pins for UART; two for I 2 C; 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 • I 2 C pins have onboard pull-ups • using them for GPIO may not work • Pins are 3. 3 V not 5 V like on the Arduino • They are connected directly to the Broadcom chip • Sending 5 V to a pin may kill the Pi • Maximum permitted current draw from a 3. 3 V pin is 50 m. A 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, Wiring. Pi 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 So. C using pointers • Memory locations can be found in the datasheet for the BCM 2835 • We can use the Wiring library to help with both

Connect an LED using a resistor between GPIO 17 (P 1 -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/sh echo 17 > /sys/class/gpio/export echo out > /sys/class/gpio 17/direction while true do echo 1 > /sys/class/gpio 17/value sleep 1 echo 0 > /sys/class/gpio 17/value sleep 1 done 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 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/tty. USB 0, /sys/class/net/eth 0/address, /dev/mmcblk 0 p 2, … • 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 digital. Write()/digital. Read() 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 Rev 1 pinout

Github • The heart of Git. Hub 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 – Git. Hub provides a Web-based graphical interface • Basic functionality

Introducing the Wiring. Pi library • A GPIO access library written in C for the BCM 2835 – 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 <wiring. Pi. h> // LED Pin - wiring. Pi pin 0 is BCM_GPIO 17. #define LED 0 int main (void) { printf ("Raspberry Pi blinkn") ; wiring. Pi. Setup () ; // note the setup method chosen pin. Mode (LED, OUTPUT) ; for (; ; ) { digital. Write (LED, HIGH) ; // On delay (500) ; // m. S digital. Write (LED, LOW) ; // Off delay (500) ; } return 0 ; }

Running blink • Compile and run the blink program gcc -Wall -o blink. c -lwiring. Pi compile sudo. /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

Accessing memory allocated to the GPIO • /dev/mem provides userlevel access to So. C memory • Offset 0 x 20000000 is a address of BCM peripherals • wiring. Pi. 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 Wiring. Pi library to allow threaded PWM on every GPIO pin taking up 0. 1% of the CPU each – Rahul Kar has blogged about using the Wiring. Pi library and PWM – Wiring. Pi recommends Servo. Blaster

Connect a Parallax Servo Connector: Black – Pi’s ground Red – Pi’s 5 V 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 Wiring. Pi’s servo example #include <stdio. h> #include <errno. h> #include <string. h> #include <wiring. Pi. h> #include <soft. Servo. h> int main () { if (wiring. Pi. Setup () == -1) { // setup to use Wiring pin numbers fprintf (stdout, "oops: %sn", strerror (errno)) ; return 1 ; } soft. Servo. Setup (0, 1, 2, 3, 4, 5, 6, 7) ; // wiring. Pi pin numbers for (; ; ) { soft. Servo. Write (0, 0) ; // wiring. Pi pin 0 is BCM_GPIO 17 delay (1000) ; soft. Servo. Write (0, 500) ; delay (1000); soft. Servo. Write (0, 1000) ; delay (1000); } }

Running servo. c • To compile: gcc -Wall -o servo. c wiring. Pi/soft. Servo. c compile soft. Servo. c -Iwiring. Pi/wiring. Pi path to soft. Servo. c -lwiring. Pi include wiring library • To run: sudo. /servo • Calling soft. Servo. Write () ; – The 1 st input is the pin number – The 2 nd input should be from 0 (hard left) to 1000 (hard right). – The 2 nd input refers to the number of microseconds of the pulse. • An input of 0 produces a 1000 u. Sec (1 m. Sec) pulse (hard left) • An input of 1000 produces a 2000 u. Sec (2 m. Sec) pulse (hard right) • An input of 500 produces a 1500 u. Sec (1. 5 m. Sec) pulse (stop)

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

And There’s more • Wiring. Pi 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-andusing-buttons-with-raspberry-pi/install-pythonmodule-rpi-dot-gpio
- Slides: 24