Top Banner
Raspberry Pi 研習營 CAVE Education 徐豐智 E - mail:jesusvictory777@cavedu,com 1
53

[5]投影片 futurewad樹莓派研習會 141218

Jul 17, 2015

Download

Engineering

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: [5]投影片 futurewad樹莓派研習會 141218

Raspberry Pi 研習營

CAVE Education

徐豐智

E-mail:jesusvictory777@cavedu,com

1

Page 2: [5]投影片 futurewad樹莓派研習會 141218

Session 6: GPIO

http://elinux.org/RPi_Low-

level_peripherals

2

Page 3: [5]投影片 futurewad樹莓派研習會 141218

Introduction

• R-Pi offers lower-level interfaces intended to

connect more directly with chips and subsystem

modules

• Signals on the 2x13 header pins include SPI,

I2C, serial UART, 3V3 and 5V power

• Not "plug and play" and require care to avoid

miswiring

• The pins use a 3.3V logic level and are not

tolerant of 5V levels, such as you might find on a

5V powered Arduino

3

Page 4: [5]投影片 futurewad樹莓派研習會 141218

GPIO

4

Page 5: [5]投影片 futurewad樹莓派研習會 141218

• Not yet software-enabled are the

flex cable connectors with CSI

(camera serial interface) and DSI

(display serial interface), and a

serial link inside the HDMI

connector called CEC

• They provide 8 GPIO pins plus

access to I²C, SPI, UART), as well

as +3.3 V, +5 V and GND supply

lines5

Page 6: [5]投影片 futurewad樹莓派研習會 141218

RPi Serial Connection

• The following parameters are needed to connect to the Pi console, and

apply on both Linux and Windows.

– Speed (baud rate): 115200

– Bits: 8

– Parity: None

– Stop Bits: 1

– Flow Control: None

6

http://elinux.org/RPi_Serial_Connection

Page 7: [5]投影片 futurewad樹莓派研習會 141218

Connection to a microcontroller

or other peripheral• The TxD and RxD signals can also be connected directly

to similar signals on a microcontroller board like the

Arduino provided the signals are all at 3V3 levels.

• It's still a good idea to put 2K2 series resistors in the

lines to prevent damage when two outputs are

connected together, which could also happen if a GPIO

input pin is accidentally programmed as output.

• If your microcontroller uses 5V logic levels, level

conversion is usually necessary - see 'Connecting to a

PC' for details.

7

Page 8: [5]投影片 futurewad樹莓派研習會 141218

控制GPIO腳位

http://makezine.com/projects/tutori

al-raspberry-pi-gpio-pins-and-

python/

8

Page 9: [5]投影片 futurewad樹莓派研習會 141218

安裝GPIO for python函式庫

• 安裝python函式庫

• $sudo apt-get install RPi.GPIO

• Python的.py檔匯入函式庫

• import RPi.GPIO as GPIO

9

Page 10: [5]投影片 futurewad樹莓派研習會 141218

GPIO example:LED blink

10

• 接上LED,注意GPIO針腳是否接對

Page 11: [5]投影片 futurewad樹莓派研習會 141218

GPIO example:LED blink

• 新增.py檔,gpio_led.py

11

Page 12: [5]投影片 futurewad樹莓派研習會 141218

GPIO example:LED blink

• 設定初始狀態,GPIO針腳

• import RPi.GPIO as GPIO #匯入函式庫

• import time #匯入函式庫

• GPIO.setwarnings(False) #關閉偵錯模式

• GPIO.setmode(GPIO.BCM) #設定腳位代號

• GPIO.setup(17,GPIO.OUT) #17號為輸出腳

12

Page 13: [5]投影片 futurewad樹莓派研習會 141218

GPIO example:LED blink

• LED燈閃爍設定

• while True: #無限迴圈

• GPIO.output(17,True) #17腳輸出電壓(3.3V)

• time.sleep(2) #停頓2秒

• GPIO.output(17,False) #17腳不輸出電壓(0V)

• time.sleep(2)

13

Page 14: [5]投影片 futurewad樹莓派研習會 141218

GPIO example:Button

14

• 接上Button,注意GPIO針腳是否接對

Page 15: [5]投影片 futurewad樹莓派研習會 141218

GPIO example:Button

• 更改gpio_led.py

15

Page 16: [5]投影片 futurewad樹莓派研習會 141218

GPIO example:Button

• 設定初始狀態,GPIO針腳

• import RPi.GPIO as GPIO #匯入函式庫

• import time #匯入函式庫

• GPIO.setwarnings(False) #關閉偵錯模式

• GPIO.setmode(GPIO.BCM) #設定腳位代號

• GPIO.setup(18,GPIO.IN) #18號為輸入腳

16

Page 17: [5]投影片 futurewad樹莓派研習會 141218

GPIO example:Button

• 偵測Button是否被按下

• while True:

• input_value=GPIO.input(18) #讀取18號輸入腳,並存入變數中

• if input_value ==False: #如果讀取為0V

• print( "The button has been pressed" )

• while input_value == False: #讀取為0V時,進入迴圈

• input_value = GPIO.input(18)

17

Page 18: [5]投影片 futurewad樹莓派研習會 141218

GPIO example:Led_Button

• Button+LED

18

Page 19: [5]投影片 futurewad樹莓派研習會 141218

GPIO example:Led_Button

• 新增.py檔,gpio_button.py

19

Page 20: [5]投影片 futurewad樹莓派研習會 141218

GPIO example:Led_Button

• 設定初始狀態,GPIO針腳

• import RPi.GPIO as GPIO #匯入函式庫

• import time #匯入函式庫

• GPIO.setwarnings(False) #關閉偵錯模式

• GPIO.setmode(GPIO.BCM) #設定腳位代號

• GPIO.setup(18,GPIO.IN) #18號為輸入腳

• GPIO.setup(17,GPIO.OUT) #17號為輸出腳

• GPIO.output(17,False) #17號為0V(燈不亮)

20

Page 21: [5]投影片 futurewad樹莓派研習會 141218

GPIO example:Led_Button

• 設定初始狀態,GPIO針腳

• while True:

• input_value=GPIO.input(18) #儲存18號輸入腳(按鈕)於變數

• if input_value ==False: #如果按鈕被按下

• print( "The button has been pressed,Lighting LED." )

• GPIO.output(17,True) #17號輸出腳給電(LED燈亮)

• while input_value == False: #按鈕被按下時進入迴圈

• input_value = GPIO.input(18) #讀取按鈕的狀態,存於變數中

• print( "The button has been pressed,Extiguishing LED." )

• if input_value == True : #如果按鈕沒被按下

• GPIO.output(17,False) # 17號輸出腳為零(LED燈暗)

21

Page 22: [5]投影片 futurewad樹莓派研習會 141218

GPIO控制直流馬達

22

Page 23: [5]投影片 futurewad樹莓派研習會 141218

(1)使用PWM控制直流馬達

23

Page 24: [5]投影片 futurewad樹莓派研習會 141218

(1)pwm.py

• 新增檔案

• pwm.py

24

Page 25: [5]投影片 futurewad樹莓派研習會 141218

(1)pwm.py

• GPIO.setup(18,GPIO.OUT)

• #設定通道

• p = GPIO.PWM(18,100)

• #設定頻率

• p.start(b)

• #設定Dutycycle

25

Page 26: [5]投影片 futurewad樹莓派研習會 141218

(2)直流馬達+驅動IC

• 接上馬達驅動IC,L293D。

26

Page 27: [5]投影片 futurewad樹莓派研習會 141218

(2)直流馬達+驅動IC

• 注意樹莓派宣告的位置

27

Page 28: [5]投影片 futurewad樹莓派研習會 141218

(2)直流馬達+驅動IC

• 接上馬達驅動IC,L293D。

28

Page 29: [5]投影片 futurewad樹莓派研習會 141218

(2)直流馬達+驅動IC

• 連接Rpi電源

• 與馬達外接電源

29

Page 30: [5]投影片 futurewad樹莓派研習會 141218

(2)直流馬達+驅動IC

• 連接直流馬達

30

Page 31: [5]投影片 futurewad樹莓派研習會 141218

(2)直流馬達+驅動IC

• 新增檔案L293D.py

31

Page 32: [5]投影片 futurewad樹莓派研習會 141218

(2)直流馬達+驅動IC

• 第二組馬達連接IC

32

Page 33: [5]投影片 futurewad樹莓派研習會 141218

(2)直流馬達+驅動IC

• 連接第二組馬達

33

Page 34: [5]投影片 futurewad樹莓派研習會 141218

(3)pwm+驅動IC

• 新增檔案pwm2.py

• 可從pwm.py改

34

Page 35: [5]投影片 futurewad樹莓派研習會 141218

Raspberry Pi - Installing a

RS232 Serial Port

http://www.savagehomeautomatio

n.com/projects/raspberry-pi-

installing-a-rs232-serial-port.html

35

Page 36: [5]投影片 futurewad樹莓派研習會 141218

Raspberry Pi - RS232 Serial

Interface Options (Revisit)

SOFTWARE USE OF THE

SERIAL PORT

36

Page 37: [5]投影片 futurewad樹莓派研習會 141218

make of backup of the two files

that we intend to modify• sudo cp /boot/cmdline.txt

/boot/cmdline.bak

• sudo cp /etc/inittab /etc/inittab.bak

37

Page 38: [5]投影片 futurewad樹莓派研習會 141218

Next

• we need to remove the "console=ttyAMA0,115200" and

"kgdboc=ttyAMA0,115200" configuration parameters from the

"/boot/cmdline.txt" configuration file.

• To edit the file use this command:

$ sudo nano /boot/cmdline.txt• The file probably contains this default configuration line:

– dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200

console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

• After removing the two configuration parameters, it will look similar

to this:

– dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4

elevator=deadline rootwait

38

Page 39: [5]投影片 futurewad樹莓派研習會 141218

Last

• edit the "/etc/inittab" file and comment out

the use of the "ttyAMA0" serial port.

• To edit the file use this command:

$ sudo nano /etc/inittab

39

Page 40: [5]投影片 futurewad樹莓派研習會 141218

Now towards the bottom of the file, look for a

configuration line that includes the "ttyAMA0" port

address

40

Page 41: [5]投影片 futurewad樹莓派研習會 141218

Place a pound sign ("#") in front of the line to comment it

out. With a pound sign ("#") at the beginning of the line,

Linux will ignore this configuration line.

• Save the "/etc/inittab" file and then issue

this command to reboot the Raspberry Pi:

$ sudo reboot

41

Page 42: [5]投影片 futurewad樹莓派研習會 141218

• Now you are ready to use the serial port

with a software application and the

operating system won't interfere with the

port.

• Use the device address "ttyAMA0" in your

application to access this serial port.

42

Page 43: [5]投影片 futurewad樹莓派研習會 141218

Project:Create an internet

controlled robot using

Livebotshttp://www.instructables.com/id/Cr

eate-an-internet-controlled-robot-

using-Livebots/step1/Have-Fun/

43

Page 44: [5]投影片 futurewad樹莓派研習會 141218

Advanced GPIO for the

Raspberry Pi

https://pypi.python.org/pypi/RPIO

Extends RPi.GPIO with PWM,

GPIO interrups, TCP socket

interrupts, command line tools and

more

44

Page 45: [5]投影片 futurewad樹莓派研習會 141218

RPIO is an advanced GPIO module

for the Raspberry Pi• PWM via DMA (up to 1µs resolution)

• GPIO input and output (drop-in replacement for RPi.GPIO)

• GPIO interrupts (callbacks when events occur on input gpios)

• TCP socket interrupts (callbacks when tcp socket clients send data)

• Command-line tools rpio and rpio-curses

• Well documented, fast source code with minimal CPU usage

• Open source (LGPLv3+)

45

Page 46: [5]投影片 futurewad樹莓派研習會 141218

RPIO的範例網站

http://pythonhosted.org/RPIO/

46

Page 47: [5]投影片 futurewad樹莓派研習會 141218

接線圖

47

Page 48: [5]投影片 futurewad樹莓派研習會 141218

Installation

• $ sudo apt-get install python-setuptools

• $ sudo easy_install -U RPIO

48

Page 49: [5]投影片 futurewad樹莓派研習會 141218

LXTerminal中,使用nano編輯程式test.py

49

Page 50: [5]投影片 futurewad樹莓派研習會 141218

50

Page 51: [5]投影片 futurewad樹莓派研習會 141218

下指令 $ sudo python test.py,接到pin 11的LED以每0.1秒閃爍

51

Page 52: [5]投影片 futurewad樹莓派研習會 141218

思考RPIO.cleanup()指令

釋放GPIO的channel

52

Page 53: [5]投影片 futurewad樹莓派研習會 141218

How to use GPIOs on

raspberry pi (Simple I/O, PWM

and UART)

https://sites.google.com/site/semill

eroadt/raspberry-pi-tutorials/gpio

53