树莓派管脚有三种编码方式:
- 第一种是Board编码,这种就是按照树莓派主板上引脚排针编号。分别对应1~40号排针。
- 第二种是BCM编码,这种方式是参考 Broadcom SOC 的通道编号,侧重CPU寄存器,是用BCM库或者python编程常采用这种编码。
- 第三种是WPI编码,就是WiringPi编码,把扩展GPIO端口从0开始编码,在使用wiringPi库编程会使用这种编码。
此扩展板采用的编号为BCM编玛
使用教程
- 如何在树莓派上扩展板上使用IIC和SPI以及舵机接口。
准备
- 硬件
- 1 x 树莓派控制板
- 1 x IO Expansion HAT for Raspberry Pi
- 1 x HDMI线
- 1 x 显示屏
- 1 x 键盘鼠标
IIC 16X2 RGB LCD KeyPad HAT
使用例程前需要下载驱动库,并运行。
在终端中,依次键入如下指令,并回车:
cd ~
git clone https://github.com/DFRobot/DFRobot_RaspberryPi_Expansion_Board.git
IIC使用步骤
1、将Gravity:IO Expansion HAT for Raspberry Pi安装在树莓派板上,树莓派系统默认没有开启IIC和SPI外设,需要手动进行开启:(SPI开启方式与IIC相同)
在树莓派系统终端输入命令:sudo raspi-config
依次“键盘回车键”选择:【 Interfacing Options 】(或者【 Advanced Options 】)->【 I2C 】->【 Yes 】->【 OK 】->【 Finish 】:
2、IIC接口使用教程
- a.启动树莓派的I2C接口。如已开启,可跳过该步骤。
打开终端(Terminal),键入如下指令,并回车:
sudo raspi-config
然后用上下键选择“ 5 Interfacing Options ”, 按回车进入,选择 “ P5 I2C ”, 按回车确认“ YES ”即可。然后重启树莓派主控板。
- b.安装Python依赖库与git,树莓派需要联网。如已安装,可跳过该步骤。
在终端中,依次键入如下指令,并回车:
sudo apt-get update
sudo apt-get install build-essential python-dev python-smbus git
在终端中,依次键入如下指令,并回车:
cd ~
git clone https://github.com/DFRobot/DFRobot_RaspberryPi_Expansion_Board.git
舵机控制教程
Gravity:IO Expansion HAT for Raspberry Pi拥有4路PWM接口,方便用户在树莓派上使用
在终端中,依次键入如下指令回车安装库:
cd ~
git clone https://github.com/DFRobot/DFRobot_RaspberryPi_Expansion_Board.git
在安装好库后
- 首先确认你的树莓派连上网络
- 然后打开IDE可在下载的文件夹发现如下程序 demo_servo.py
# -*- coding:utf-8 -*-
'''
# demo_servo.py
#
# Connect board with raspberryPi.
# Run this demo.
#
# Connect servo to one of pwm channels
# All or part servos will move to 0 degree, then move to 180 degree, then loop
# Test Servo: https://www.dfrobot.com/product-255.html
# Warning: Servos must connect to pwm channel, otherwise may destory Pi IO
#
# Copyright [DFRobot](http://www.dfrobot.com), 2016
# Copyright GNU Lesser General Public License
#
# version V1.0
# date 2019-3-28
'''
import time
from DFRobot_RaspberryPi_Expansion_Board import DFRobot_Expansion_Board_IIC as Board
from DFRobot_RaspberryPi_Expansion_Board import DFRobot_Expansion_Board_Servo as Servo
board = Board(1, 0x10) # Select i2c bus 1, set address to 0x10
servo = Servo(board)
''' print last operate status, users can use this variable to determine the result of a function call. '''
def print_board_status():
if board.last_operate_status == board.STA_OK:
print("board status: everything ok")
elif board.last_operate_status == board.STA_ERR:
print("board status: unexpected error")
elif board.last_operate_status == board.STA_ERR_DEVICE_NOT_DETECTED:
print("board status: device not detected")
elif board.last_operate_status == board.STA_ERR_PARAMETER:
print("board status: parameter error")
elif board.last_operate_status == board.STA_ERR_SOFT_VERSION:
print("board status: unsupport board framware version")
if __name__ == "__main__":
while board.begin() != board.STA_OK: # Board begin and check board status
print_board_status()
print("board begin faild")
time.sleep(2)
print("board begin success")
servo.begin() # servo control begin
while True:
print("servo move to 0")
servo.move(board.ALL, 0)
time.sleep(1)
print("servo move to 180")
servo.move(board.ALL, 180)
time.sleep(1)
chan_list = [board.CHANNEL1, board.CHANNEL2]
# chan_list = [1, 2]
print("part servos move to 0")
servo.move(chan_list, 0)
time.sleep(1)
print("part servos move to 180")
servo.move(chan_list, 180)
time.sleep(1)
|
可以直接复制上程序到其自带的python编译器直接运行
也可通过终端直接找到程序通过
cd ~ /DFRobot_RaspberryPi_Expansion_Board/raspberry
python demo_servo.py命令运行
结果可观察到舵机从0°到180°和180°到0°反复移动,之后可ctrl+c退出
IO口配置
- IO口配置同理舵机教程将下列代码保存后,在终端输入命令运行
import RPi.GPIO as GPIO
import time
import atexit
blinkPin=27
atexit.register(GPIO.cleanup)
GPIO.setmode(GPIO.BCM)
GPIO.setup(blinkPin,GPIO.OUT)
while True:
GPIO.output(blinkPin,GPIO.HIGH)
time.sleep(1)
GPIO.output(blinkPin,GPIO.LOW)
time.sleep(1)
|
此时在扩展板27脚插上LED灯可观察到LED不停亮灭
串口使用教程
1.通过串口接线实现PC对树莓派远程控制
除了树莓派和扩展板还需准备如下配件
然后按照下图进行连接
在电脑上确认树莓派连接到的COM口
如果电脑无法识别,可下载更新USB转TTL驱动,在百度上搜索下载即可
在电脑上下载putty点击此处下载(打开后选择Windows一栏下64位下载即可)
打开后按照上图所示进行配置,点击Open
打开成功以后界面如上,之后输入用户名和登录密码,注意密码在输入的时候是看不见的,登录成功后显示如下
自此就可以从电脑对树莓派进行终端控制
自此就可以从电脑对树莓派进行终端控制
模拟量读取
# -*- coding:utf-8 -*-
'''
# demo_adc.py
#
# Connect board with raspberryPi.
# Run this demo.
#
# All or part adc channels value will print on terminal
#
# Copyright [DFRobot](http://www.dfrobot.com), 2016
# Copyright GNU Lesser General Public License
#
# version V1.0
# date 2019-3-28
'''
import time
from DFRobot_RaspberryPi_Expansion_Board import DFRobot_Expansion_Board_IIC as Board
board = Board(1, 0x10) # Select i2c bus 1, set address to 0x10
def board_detect():
l = board.detecte()
print("Board list conform:")
print(l)
''' print last operate status, users can use this variable to determine the result of a function call. '''
def print_board_status():
if board.last_operate_status == board.STA_OK:
print("board status: everything ok")
elif board.last_operate_status == board.STA_ERR:
print("board status: unexpected error")
elif board.last_operate_status == board.STA_ERR_DEVICE_NOT_DETECTED:
print("board status: device not detected")
elif board.last_operate_status == board.STA_ERR_PARAMETER:
print("board status: parameter error")
elif board.last_operate_status == board.STA_ERR_SOFT_VERSION:
print("board status: unsupport board framware version")
if __name__ == "__main__":
board_detect() # If you forget address you had set, use this to detected them, must have class instance
# Set board controler address, use it carefully, reboot module to make it effective
'''
board.set_addr(0x10)
if board.last_operate_status != board.STA_OK:
print("set board address faild")
else:
print("set board address success")
'''
while board.begin() != board.STA_OK: # Board begin and check board status
print_board_status()
print("board begin faild")
time.sleep(2)
print("board begin success")
board.set_adc_enable()
# board.set_adc_disable()
while True:
# All channels read, return a list contains adc values
chan_list = [board.CHANNEL1, board.CHANNEL2,board.CHANNEL3,board.CHANNEL4] # channel list declare
# chan_list = [1, 2, 3, 4]
val_list = board.get_adc_value(chan_list) # Part channels raed
print("part channels read:")
for chan, val in zip(chan_list, val_list):
print("channel: %d, value: %d, valtage: %.2fV" %(chan, val, float(val) / 4096.0 * 3.3))
print("")
time.sleep(2)
|
常见问题
还没有客户对此产品有任何问题,欢迎通过qq或者论坛联系我们!
更多问题及有趣的应用,可以 访问论坛 进行查阅或发帖。
|
更多
- 有趣的应用链接
- 相关下载链接
- 推荐阅读链接
- 旧版本维库的链接
[Link DFRobot商城购买链接]
|