Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Ver. 2 Department of Engineering Science Lab 2 – Interfacing with Pi Interfacing with Raspberry Pi 3 Model B A. Objectives 1. Learn about basics of Python programming 2. Learn how to use Python and Shell scripts to control GPIO ports on the Pi B. Time of Completion This laboratory activity is designed for students with very little knowledge of Raspberry Pi and it is estimated to take about 3 hours to complete. C. Requirements 1. A Raspberry Pi 3 Model 3 2. 32 GByte MicroSD card à Give your MicroSD card to the lab instructor for a copy of Ubuntu. 3. USB adaptor to power up the Pi D. Pre-Lab 1. Review about Python programing. http://www.learnpython.org/ - Write a simple Python program to print 1-10 on your screen. 2. Learn the difference between difference version of Python: https://wiki.python.org/moin/Python2orPython3 3. How many GOIP does your PI have? F. Farahmand 9/30/2016 1 Ver. 2 Department of Engineering Science Lab 2 – Interfacing with Pi E. Lab In this lab we learn how to use shell scripting and Python programing to access GPIO ports on Pi 3. The basic idea is to understand that we can use command lines to access and configure GPIO ports. Alternatively, as shown in the diagram, we can use shell scripting to run a Python program that can access and configure GPIO ports. Let’s see how this works. 1. Access your Pi Shell Script (.sh) Python Program (.py) GPIO (Configuration) Place your 32 Gbyte MicroSD card that you received from your instructor in the appropriate slot on the Pi. Power up the Pi using GPIO the Micro-USB adaptor. At this point you should see the RED LED. Connect the Pi to an active LAN line, using the 10/100 Ethernet LAN connector. Note that the GREEN LED on the LAN connector is should be on. Using SSH access your Pi, as described in the previous lab. Let’s assume it is 192.168.1.73. Assuming your Pi is connected to the same LAN, the IP address of the Pi is going to be within the same sub-domain (in this case 192.168.1.xx). Run the following commands: ping 192.168.1.73 arp –a Run a few other Linux commands to ensure everything is ok. 2. Explore Python on your Pi In this section we explore which version of Python has been installed on your Pi and then we create a simple Python program. • Check which version of Python is available to you using python -V. • Create a simple Python script 1: o Using vi command create a file called test.py and type the following in the file: #!/usr/bin/python print "Hello, World!"; o Change the mode of the file to writeable: chmod 777 test.py. o Run the script: ./test.py. o What do you see? • Write a simple Python program to calculate the sum of the numbers 1 to 10. What is it? 1 For more information see https://www.raspberrypi.org/documentation/usage/python/ F. Farahmand 9/30/2016 2 Ver. 2 Department of Engineering Science Lab 2 – Interfacing with Pi 3. Using GPIO Ports In this section we learn how to access GPIO ports on your Pi. There are 40 pins that you can access directly through the onboard connector. First enter gpio -v to get information about you Pi. Answer the following information: • • What revision of Pi you have? How much memory is available to you? Then try gpio readall to get information about ALL GPIO ports. You will see a table as below. See how this table is mapped on the physical pins on the board. Pay attention to pin 1. Answer the following information: • • • How many GPIO ports are actually available? How many 5V pins are available? How many of the pin are ground pins? Physical Layout GPIO Pin Layout In the above figure, note that aach side has three columns. The outermost column, headed wiringPi Pin (wPi) refers to the pin number in the wiring Pi code. The middle one, headed BCM refers to the pin number of the BCM2835 chip. Also note that this is the pin number used when addressing the GPIO using the/sys/class/gpio interface. The innermost column, Name is the name of the function of the pin. The central column contains the pin numbers on the header on the board. Pin 1 is the 3.3v power supply on the P1 connector (Rev. 1 and Rev. 2 boards), and Pin 1 is the 5v power supply on the P5 connector on Rev. 2 boards only. If you look at the pins chart above, you can see that wiringPi pin 0 is GPIO-17. wiringPi pin 1 is GPIO-18, wiringPi pin 2 is GPIO-21 an so on. To use the GPIO pin numbering then you need to pass the -g flag to the gpio program. Let’s clarify this by an example: F. Farahmand 9/30/2016 3 Ver. 2 Department of Engineering Science Lab 2 – Interfacing with Pi Connect your Raspberry Pi to an LED as suggested in the figure. Use GPIO.0=wPi.0 on your Pi. Change GPIO.0 to an output port: gpio mode 0 out. In this case mode 0 is referring GPIO.0. Then, run the following commands: gpio write 0 1 gpio write 0 0 Note that at this point your LED should be turned ON and then turned OFF. Read the status of all the port using gpio readall. What is the status of GPIO.0? It is also possible to use the g flag, referring to the BCM: gpio -g write 17 1 gpio -g write 17 0 At this point create a file called LED_blink.py and type in the following code in there. Run the file. Your LED should be blinking. At this point build the following circuit and run the following program. Note that when you press the switch the terminal shows the message: Button Pressed PROGRAMING EXERCISE: Modify the program such that when the press is switched the message shows up only one. #!/usr/bin/env python # Import proper libraries import RPi.GPIO as GPIO import time # Use BCM pin numbering scheme GPIO.setmode(GPIO.BCM) # Set pin 17 as the INPUT GPIO.setup(17, GPIO.IN) while True: if (GPIO.input(17)): print("Button Pressed") F. Farahmand 9/30/2016 4 Ver. 2 Department of Engineering Science Lab 2 – Interfacing with Pi It is possible to run a Python or Shell script from a separate Python program. For example, consider the following python program that pings a server. We call this myping.py. We can also write simple Shell scrip, called myping.sh. #!/bin/bash myhostname="google.com" ping -c 1 $myhostname #!/usr/bin/python import os hostname = "google.com" #example response = os.system("ping -c 1 " + hostname) #and then check the response... if response == 0: print hostname, 'is up!' else: print hostname, 'is down!' myping.sh file myping.py file Any of these programs can be called from a Python program using the following commands: #!/usr/bin/env python import os import sys execfile('myping.py') os.system("sh myping.sh") PROGRAMING EXERCISE: Write a Python program that pings google.com only once every time an external switch is pressed. At this point you should be able to execute any Linux command using a switch. It is also possible to write a Shell script to accept argument. Run this example and see what it does. #!/bin/bash echo -n "Is this a good question (y/n)? " read answer if echo "$answer" | grep -iq "^y" ;then echo Yes else echo No fi PROGRAMING EXERCISE: Write a program such that when someone pings you your LED on the Pi starts blinking. PROGRAMING EXERCISE: Write a program such that if the Pi has no valid IP address, the LED starts blinking. F. Credits Special thanks to online resources and all SSU students who assisted putting together this lab. E. References F. Farahmand 9/30/2016 5