SlideShare a Scribd company logo
Raspberry Pi I/O控制與感測器讀取
Joseph
How to Get Sensor data ?
Analog processing
OPA/
Filter
Sensor
Voltage
ADC
感應元件
CO2,CO,
G-Sensor, Gryo
Thermal, light
….
電能
Continuous Signal
MCU
I2C
UART
SPI
USB
MCU/ MPU
Discrete Signal
動能、
化學能、
熱能、
光能
電阻
電流
電壓
演算法
處理
半導體物理、
材料、光電
元件、化工、
機械、…
MCU/ MPU
GPIO
ADC
Raspberry Pi 2/3 I/O Pin out
3
Raspberry Pi I/O 子板
https://goo.gl/M7A6i9 4
GPIO INTERFACE
5
GPIO – General Program Input/Output
注意輸出及輸入電壓準位
6
GPIO
Setup time & Hold Time
注意電位變化需時間
Selup Time
Hold Time
D lnput to Flop
7
BCM2835 Memory Map
8
GPIO
BCM2835 has 54 GPIO pins
The function select registers are used to define the
operation of the general-purpose I/O pins
There are 6 GPFSEL [5:0], each control the functions
of a block of 10 pins.
• The FSEL{n} field determines the functionality of the nth
GPIO pin.
9
Set GPIO to Alternative Function
6.2 Alternative Function Assignments
GPIO設好Function後, 開始進入Study
10
Register Map of special function
11
#define bit field for s/w
12
USER SPACE DRIVER
13
BCM2835 Low-Level I/O Library
C library for Broadcom BCM 2835 as used in
Raspberry Pi
用 mmap 方式 來實現user space 驅動程式, open
(“dev/mem”,…)
範例程式目錄結構 ==>
14
Hands on
cd pi-sensor/examples/blink
./build.sh ==> 產生執行檔
./blink  看見LED 閃爍!
build.sh (shell script)
使用gcc 編譯C程式,
如: gcc test.c -o test
15
Coding
16
17
Lab: GPIO LED Blinking
cd pi-sensor/examples/blink
./build.sh
./blink
build.sh (shell
script) 使用gcc 編
譯C程式, 如: gcc
test.c -o test
18
Lab : PIR人體感測器 (GPIO Input)
有人經過 時Output訊號1 (感知紅外線波長變化)
19
Lab: 繼電器(Relay) 控制
動手做: 按下Button 時電燈開啓, 放開電燈關閉
20
pi-sensor/examples/blink/relay.c
PWM
Pulse Width Modulation
PWM type Servo 伺服馬達
SG90 (180度Servo) 訊號定義
50Hz (20ms) period, pulse width range 1.0ms~2.0ms
橘 PWM
紅 5V
棕 GND
1.0ms 角度0度, 1.5ms角度是90度, 2.0ms
角度是180度
Lab: PWM
程式: cd pi-sensore/examples/servo/sg90.c
./budild.sh; ./servo
24
bcm2835_gpio_fsel(PWM0,BCM2835_GPIO_FSEL_ALT5); //set gpio for PWM0
bcm2835_pwm_set_clock(1920); //set min frequence =19.2M/1920=10k
bcm2835_pwm_set_mode(0,1,1); //(channel,PWM0 MSEN enable , PWM0 enable)
bcm2835_pwm_set_range(0,200); //set period=20ms
bcm2835_pwm_set_data(0,a); //a=1~20
0.1ms*a
0.1ms*200=20ms
數位式感測器讀取
half-duplex, synchronous bus
它是一種廣播式的 bus
三種通訊速度
1) Standard is 100 Kbps
2) Fast-mode is 400 Kbps
3) high-speed mode supports speeds up to 3.4 Mbps
supports 7-bit and 10-bit address
• 0000xxx , 1111xxx有特殊用途
Master-slave communication
support multi-master
26
What is I2C (Inter-IC) ?
27
硬體接線
28
Start and Stop conditions
•
29
硬體訊號
30
Data format
• Every byte put on the SDA line must be 8-bits long.
• Each byte has to be followed by an acknowledge bit.
31
Link level protocol
Lab : using LA to analyze the protocol
./eep w 0x50 0x10 0x55
./eep r 0x50 0x10
32
apt-get install i2c-tools
i2cdetect -y 1 => Scan I2C BUS 1
Using i2c-tools (open Source )
33
34
Using i2c-tools
EEPROM read/write using i2cset/i2cget
需要I2C功能被開啓
sudo raspi-config  進入[Interfacing
Options]將I2C 開啓
lsmod  應顯示i2c_dev Driver 模組被載入
35
I2C Interface
G-Sensor (STMicro LIS3DH)
Lab : MEMS digital output motion sensor
STMicro (LIS3DH) SPI/I2C Interface
The LIS3DH has dynamically user-selectable
full scales of ±2g/±4g/±8g/±16g
Measuring accelerations with output data
rates from 1 Hz to 5.3 kHz.
37
16bits represents ±2g :
18193
2g1g-1g-2g
36383-18193-36383 0
平放時1g, 2g必須產生外力
G Sensor-Acceleration Meter
可以知道X,Y,Z的加速度變化(加速度的單位:g , 9.8m/s2)
在不同的傾斜角度會輸出不同G值, 故可以用來量測物體的
傾斜角度
38
傾斜角度
39
LIS3DH Register Map
Slave Address 0x19
40
方位
X>0
朝正面 Z>0
Y>0
Read/Write Operation
42
SAD+R/W’: Slave Address+ Read/Write’
SUB: 8-bit sub-address (SUB) : internal register address in chip
If the MSB of the SUB field is ‘1’, the SUB allow multiple data read/writes.
Protocol for LIS3DH
43
MAK is Master acknowledge and NMAK is No Master Acknowledge
0x32 0x28 0x33
X_L
0x32 0xA8
Code Snippet for Read:
程式碼位置: pi-sensorexamplesLIS3DHI2C_Profile.c
Lab: G-sensor app on Raspberry Pi
cd pi-sensor/examples/LIS3DH
chmod +x build.sh ; ./build.sh; ./GSensor
Lab : OLED 輸出
SSD1306Z : I2C Interface , 128x64, 0.94吋
46
,
UART Interface
Air Quality sensors
48
Dust sensors
懸浮微粒 (Particulate Matter) :漂浮在空氣中
類似灰塵的粒狀物
粒徑小於或等於 2.5 微米(μm)的粒子,就稱為
PM2.5,通稱細懸浮微粒。PM 2.5 的直徑還不到
人的頭髮絲粗細的 1/28
PM1.0, PM 2.5, PM10.0
依據空氣中不同的懸浮顆粒個數,推算灰
塵的質量濃度,單位 μg/m3
49
AQI (Air Quality Index)
50
關於 行政院環保署細懸浮微粒管制
http://air.epa.gov.tw/Public/suspended_particles.aspx
PMS3003工作原理
採用鐳射散射原理:當鐳射照射到通過檢測位置的顆粒物
時會產生微弱的光散射,在特定方向上的光散射波形與顆
粒直徑有關,通過不同粒徑的波形分類統計及換算公式可
以得到不同粒徑的實時顆粒物的數量濃度,按照標定方法
得到跟公信單位統一的質量濃度。
51
PMS3003 規格
52
https://www.dfrobot.com/wiki/index.php/PM2.5_laser_dust_sensor_SKU:SEN0177
PMS3003 連接腳位
53
PIN1
UART 接線方式
54
/dev/ttyUSB0
9600, 8N1
PL2303 (usb-to-serial )
PMS3003 腳位
White: RX
Green: TX
Black: GND
Red: 5V
Capture serial data
55
每一秒會輸出 24 byte serial data
In Linux: cat /dev/ttyUSB0 | hexdump
PMS3003 Serial Data Format
56
PM1 = data [5:4]
 PM2.5 =data [7:6]
 PM10 =data[9:8]
解析 24 byte serial data
57
42 4D 00 14 00 02 00 03 00 03 00 02 00 03 00 03 00 C2 00 02 97 00 02 0E
20byte data 含checksum
00 02: PM1.0 : 2ug/m3
00 03: PM2.5 : 3ug/m3
00 03: PM10 : 3ug/m3
ChceckSum (42 4D 00 14 00 02 00 03 00 03 00 02 00 03 00 03 00
C2 00 02 97 00 ) = 0x020E=52610
Demo code
pip install pyserial
cd pi-sensor/examples/pms3003
python dump-serial.py
python g3.py
58
類比式感測器讀取
ADC (MCP3008)
ADC (MCP3008)
SPI Interface
60
Raspberry Pi MCP3008
SPI Analog signal
SPI 線路
61
SPI 為一個Ring 結構:要讀資料,必送資料
{dummy byte}
CPOL: rest state of clock (high or low)
CPHA: First or second SCLK transition
CPOL,CPHA 用來決定latch 資料的時間點
SPI 模式
62
ADC (MCP3008)
2.7V 4-Channel/8-Channel 10-Bit A/D Converters with SPI Serial Interface
4 pseudo-differential input pairs or 8 single-ended inputs
63
64
LSB: 最小刻度
VREF determines the
analog input voltage range
65
SPI COMMUNICATION
66
67
Lab : 讀取ADC 光敏電阻
讀取ADC channel 0
68
LAB: 光敏電阻實驗
cd pi-sensor/examples/ADC;
./build.sh
./adc 0
69
USB Webcam
apt-get install libgd2-noxpm-dev
./configure
make
make install
71
What is the GD library?
https://libgd.github.io
GD is an open source code library for the
dynamic creation of images by programmers.
GD is written in C, and "wrappers" are
available for Perl, PHP and other languages.
GD creates PNG, JPEG, GIF, WebP, XPM, BMP
images, among other formats.
72
73
References
BCM2835 ARM Peripherals Manual
艾鍗 Pi 3 Shield 電路圖
24LC16 Datasheet
MCP3008 Datasheet
LIS3DH Datasheet
74

More Related Content

PDF
[読会]Long tail learning via logit adjustment
PDF
用十分鐘 向jserv學習作業系統設計
PDF
show コマンド結果をパースする方法あれこれ #npstudy
PPTX
検索評価ツールキットNTCIREVALを用いた様々な情報アクセス技術の評価方法
PPTX
Python開発は仮想化しろ
PDF
機械学習ゴリゴリ派のための数学とPython
PDF
Usp友の会勉強会、ジャクソン構造図の巻(前編)
PDF
How it's made: C++ compilers (GCC)
[読会]Long tail learning via logit adjustment
用十分鐘 向jserv學習作業系統設計
show コマンド結果をパースする方法あれこれ #npstudy
検索評価ツールキットNTCIREVALを用いた様々な情報アクセス技術の評価方法
Python開発は仮想化しろ
機械学習ゴリゴリ派のための数学とPython
Usp友の会勉強会、ジャクソン構造図の巻(前編)
How it's made: C++ compilers (GCC)

What's hot (20)

PDF
NetworkXによる語彙ネットワークの可視化
PDF
パケットキャプチャの勘どころ Ssmjp 201501
PDF
GDB Rocks!
PPTX
Msを16倍出し抜くwpf開発2回目
PDF
libuv, NodeJS and everything in between
PDF
外部データラッパによる PostgreSQL の拡張
PPTX
できる!KickstartとAnsible!
PDF
Memory management in Linux kernel
PDF
Solaris (Branded) Zone Internals
PDF
20111015 勉強会 (PCIe / SR-IOV)
PPTX
Wiresharkの解析プラグインを作る ssmjp 201409
PDF
[db analytics showcase Sapporo 2017] A15: Pythonでの分散処理再入門 by 株式会社HPCソリューションズ ...
PDF
MySQLとPostgreSQLの基本的なバックアップ比較
PDF
セキュリティ未経験だったけど入社1年目から Bug Bounty Program 運営に参加してみた
PDF
[GTCJ2018]CuPy -NumPy互換GPUライブラリによるPythonでの高速計算- PFN奥田遼介
ODP
eBPF maps 101
PDF
BPF: Tracing and more
PDF
perfを使ったPostgreSQLの解析(前編)
DOCX
系統程式 -- 第 11 章 嵌入式系統
PDF
PlaySQLAlchemy: SQLAlchemy入門
NetworkXによる語彙ネットワークの可視化
パケットキャプチャの勘どころ Ssmjp 201501
GDB Rocks!
Msを16倍出し抜くwpf開発2回目
libuv, NodeJS and everything in between
外部データラッパによる PostgreSQL の拡張
できる!KickstartとAnsible!
Memory management in Linux kernel
Solaris (Branded) Zone Internals
20111015 勉強会 (PCIe / SR-IOV)
Wiresharkの解析プラグインを作る ssmjp 201409
[db analytics showcase Sapporo 2017] A15: Pythonでの分散処理再入門 by 株式会社HPCソリューションズ ...
MySQLとPostgreSQLの基本的なバックアップ比較
セキュリティ未経験だったけど入社1年目から Bug Bounty Program 運営に参加してみた
[GTCJ2018]CuPy -NumPy互換GPUライブラリによるPythonでの高速計算- PFN奥田遼介
eBPF maps 101
BPF: Tracing and more
perfを使ったPostgreSQLの解析(前編)
系統程式 -- 第 11 章 嵌入式系統
PlaySQLAlchemy: SQLAlchemy入門
Ad

Similar to Raspberry Pi I/O控制與感測器讀取 (20)

PPTX
[5]投影片 futurewad樹莓派研習會 141218
PPTX
Raspberry Pi I/O控制與感測器讀取
PDF
Getting Started With Raspberry Pi - UCSD 2013
PDF
Getting Started with Raspberry Pi - USC 2013
PPTX
用Raspberry Pi 學Linux I2C Driver
PDF
Raspberry Pi - best friend for all your GPIO needs
PDF
Interfacing the Raspberry Pi to the World
PDF
Raspberry Pi GPIO Tutorial - Make Your Own Game Console
PDF
Arduino: Analog I/O
PPT
Arduino Introduction by coopermaa
PPTX
實作Linux Driver移植在樹莓 Pi上:Linux Driver for STMicro LIS3DH
PDF
Raspberry Pi
PDF
IoT Physical Devices and End Points.pdf
PDF
Raspberry Pi - HW/SW Application Development
PPTX
Interfacing two wire adc0831 to raspberry pi2 / Pi3
PPTX
Any Surface Drum Kit
PDF
Linux+sensor+device-tree+shell=IoT !
PDF
SPI Interface
PPTX
Introduction to Arduino
PPTX
Python-in-Embedded-systems.pptx
[5]投影片 futurewad樹莓派研習會 141218
Raspberry Pi I/O控制與感測器讀取
Getting Started With Raspberry Pi - UCSD 2013
Getting Started with Raspberry Pi - USC 2013
用Raspberry Pi 學Linux I2C Driver
Raspberry Pi - best friend for all your GPIO needs
Interfacing the Raspberry Pi to the World
Raspberry Pi GPIO Tutorial - Make Your Own Game Console
Arduino: Analog I/O
Arduino Introduction by coopermaa
實作Linux Driver移植在樹莓 Pi上:Linux Driver for STMicro LIS3DH
Raspberry Pi
IoT Physical Devices and End Points.pdf
Raspberry Pi - HW/SW Application Development
Interfacing two wire adc0831 to raspberry pi2 / Pi3
Any Surface Drum Kit
Linux+sensor+device-tree+shell=IoT !
SPI Interface
Introduction to Arduino
Python-in-Embedded-systems.pptx
Ad

More from 艾鍗科技 (20)

PPTX
AI 技術浪潮, 什麼是機器學習? 什麼是深度學習, 什麼是生成式AI, AI 能力認證
PDF
TinyML - 4 speech recognition
PPTX
Appendix 1 Goolge colab
PPTX
Project-IOT於餐館系統的應用
PPTX
02 IoT implementation
PPTX
Tiny ML for spark Fun Edge
PDF
Openvino ncs2
PDF
Step motor
PDF
2. 機器學習簡介
PDF
5.MLP(Multi-Layer Perceptron)
PDF
3. data features
PPTX
心率血氧檢測與運動促進
PPTX
利用音樂&情境燈幫助放鬆
PPTX
IoT感測器驅動程式 在樹莓派上實作
PPTX
無線聲控遙控車
PPT
最佳光源的研究和實作
PPTX
無線監控網路攝影機與控制自走車
PPTX
Reinforcement Learning
PPTX
Linux Device Tree
PPTX
人臉辨識考勤系統
AI 技術浪潮, 什麼是機器學習? 什麼是深度學習, 什麼是生成式AI, AI 能力認證
TinyML - 4 speech recognition
Appendix 1 Goolge colab
Project-IOT於餐館系統的應用
02 IoT implementation
Tiny ML for spark Fun Edge
Openvino ncs2
Step motor
2. 機器學習簡介
5.MLP(Multi-Layer Perceptron)
3. data features
心率血氧檢測與運動促進
利用音樂&情境燈幫助放鬆
IoT感測器驅動程式 在樹莓派上實作
無線聲控遙控車
最佳光源的研究和實作
無線監控網路攝影機與控制自走車
Reinforcement Learning
Linux Device Tree
人臉辨識考勤系統

Recently uploaded (20)

PPTX
Operating System Processes_Scheduler OSS
PPTX
code of ethics.pptxdvhwbssssSAssscasascc
PPTX
ERP good ERP good ERP good ERP good good ERP good ERP good
PPTX
Embeded System for Artificial intelligence 2.pptx
PPTX
quadraticequations-111211090004-phpapp02.pptx
PPTX
Prograce_Present.....ggation_Simple.pptx
PDF
Smarter Security: How Door Access Control Works with Alarms & CCTV
PPTX
了解新西兰毕业证(Wintec毕业证书)怀卡托理工学院毕业证存档可查的
PDF
-DIGITAL-INDIA.pdf one of the most prominent
PPTX
PLC ANALOGUE DONE BY KISMEC KULIM TD 5 .0
PDF
How NGOs Save Costs with Affordable IT Rentals
PPTX
Computers and mobile device: Evaluating options for home and work
PPTX
Syllabus Computer Six class curriculum s
PPTX
Presentacion compuuuuuuuuuuuuuuuuuuuuuuu
PPTX
Embedded for Artificial Intelligence 1.pptx
PDF
Layer23-Switch.com The Cisco Catalyst 9300 Series is Cisco’s flagship stackab...
PPTX
Wireless and Mobile Backhaul Market.pptx
DOCX
fsdffdghjjgfxfdghjvhjvgfdfcbchghgghgcbjghf
PPTX
Nanokeyer nano keyekr kano ketkker nano keyer
PPTX
Sem-8 project ppt fortvfvmat uyyjhuj.pptx
Operating System Processes_Scheduler OSS
code of ethics.pptxdvhwbssssSAssscasascc
ERP good ERP good ERP good ERP good good ERP good ERP good
Embeded System for Artificial intelligence 2.pptx
quadraticequations-111211090004-phpapp02.pptx
Prograce_Present.....ggation_Simple.pptx
Smarter Security: How Door Access Control Works with Alarms & CCTV
了解新西兰毕业证(Wintec毕业证书)怀卡托理工学院毕业证存档可查的
-DIGITAL-INDIA.pdf one of the most prominent
PLC ANALOGUE DONE BY KISMEC KULIM TD 5 .0
How NGOs Save Costs with Affordable IT Rentals
Computers and mobile device: Evaluating options for home and work
Syllabus Computer Six class curriculum s
Presentacion compuuuuuuuuuuuuuuuuuuuuuuu
Embedded for Artificial Intelligence 1.pptx
Layer23-Switch.com The Cisco Catalyst 9300 Series is Cisco’s flagship stackab...
Wireless and Mobile Backhaul Market.pptx
fsdffdghjjgfxfdghjvhjvgfdfcbchghgghgcbjghf
Nanokeyer nano keyekr kano ketkker nano keyer
Sem-8 project ppt fortvfvmat uyyjhuj.pptx

Raspberry Pi I/O控制與感測器讀取

Editor's Notes

  • #7: Examples (drivers/char/it_dio.c)