Difference between revisions of "BakeBit - Rotary Angle Sensor/zh"

From FriendlyELEC WiKi
Jump to: navigation, search
(相关资料)
(示例程序(2):Servo and Rotary Angle Sensor)
 
(6 intermediate revisions by the same user not shown)
Line 21: Line 21:
 
|SIG  || 信号
 
|SIG  || 信号
 
|}
 
|}
==硬件连接==
+
== 示例程序(1):LED Fade ==
使用标准4 Pin连接线与NEO Hub连接。参考下图连接模块:
+
 
[[File:BakeBit - Rotary Angle Sensor_nanopi_NEO.jpg|frameless|600px|BakeBit - Rotary Angle Sensor_nanopi_NEO]]
+
这个项目将使用[[BakeBit - Rotary Angle Sensor]]模块,来动态调节LED的亮度。
==代码说明==
+
 
 +
=== 硬件连接 ===
 +
简单的将 LED 模块插入 D5接口,将 [[BakeBit - Rotary Angle Sensor]] 插入 A0接口,如下面这样:
 +
 
 +
::{| class="wikitable"
 +
|-
 +
|[[File:LED Fade-1.jpg |frameless|300px]]    || [[File:LED Fade-2.jpg |frameless|300px]]
 +
|-
 +
|}
 +
 
 +
=== 示例源代码 ===
 +
 
 +
<syntaxhighlight lang="python">
 +
import time
 +
import bakebit
 +
 
 +
# Connect the BakeBit Rotary Angle Sensor to analog port A0
 +
# SIG,NC,VCC,GND
 +
potentiometer = 0
 +
 
 +
# Connect the LED to digital port D5
 +
# SIG,NC,VCC,GND
 +
led = 5
 +
 
 +
bakebit.pinMode(potentiometer,"INPUT")
 +
bakebit.pinMode(led,"OUTPUT")
 +
time.sleep(1)
 +
 
 +
# Reference voltage of ADC is 5v
 +
adc_ref = 5
 +
 
 +
# Vcc of the BakeBit interface is normally 5v
 +
bakebit_vcc = 5
 +
 
 +
# Full value of the rotary angle is 300 degrees, as per it's specs (0 to 300)
 +
full_angle = 300
 +
 
 +
while True:
 +
    try:
 +
        # Read sensor value from potentiometer
 +
        sensor_value = bakebit.analogRead(potentiometer)
 +
 
 +
        # Calculate voltage
 +
        voltage = round((float)(sensor_value) * adc_ref / 1023, 2)
 +
 
 +
        # Calculate rotation in degrees (0 to 300)
 +
        degrees = round((voltage * full_angle) / bakebit_vcc, 2)
 +
 
 +
        # Calculate LED brightess (0 to 255) from degrees (0 to 300)
 +
        brightness = int(degrees / full_angle * 255)
 +
 
 +
        # Give PWM output to LED
 +
        bakebit.analogWrite(led,brightness)
 +
 
 +
        print("sensor_value = %d voltage = %.2f degrees = %.1f brightness = %d" %(sensor_value, voltage, degrees, brightness))
 +
    except KeyboardInterrupt:
 +
        bakebit.analogWrite(led,0)
 +
        break
 +
    except IOError:
 +
        print ("Error")
 +
</syntaxhighlight>
 +
[https://github.com/friendlyarm/BakeBit/blob/master/Software/Python/bakebit_rotary_angle_sensor.py Github]
 +
 
 +
=== 运行示例 ===
 +
 +
假设你已经参考[http://wiki.friendlyarm.com/bakebit bakebit教程]安装了BakeBit源代码,<br />
 +
要运行示例程序,可以在开发板上进入 BakeBit/Software/Python目录,运行bakebit_rotary_angle_sensor.py:
 +
<syntaxhighlight lang="bash">
 +
cd ~/BakeBit/Software/Python
 +
sudo python bakebit_rotary_angle_sensor.py
 +
</syntaxhighlight>
 +
 
 +
 
 +
=== 运行结果 ===
 +
 
 +
旋转电位器,LED亮度会随着电位器的角度变化而变化。
 +
 
 +
== 示例程序(2):Servo and Rotary Angle Sensor ==
 +
这个示例演示用电位器(Rotary Angle Sensor)来控制舵机的转动。<br />
 +
该示例是配合[[BakeBit - Servo]]来实现的,可参考[[BakeBit - Servo]]页面。
 +
 
 +
== 示例程序(3):LED Bar and Rotary Angle Sensor ==
 +
这个示例实现类拟音量调节的效果,用到电位器模块和LED Bar。<br />
 +
该示例是配合[[BakeBit - LED Bar]]来实现的,可参考[[BakeBit - LED Bar]]页面。
 +
 
 
==相关资料==
 
==相关资料==
 
*[Schematic]([http://wiki.friendlyarm.com/wiki/images/0/01/03-SCHEMATIC_Rotary_Angle_Senso.pdf BakeBit - Rotary Angle Sensor.pdf])
 
*[Schematic]([http://wiki.friendlyarm.com/wiki/images/0/01/03-SCHEMATIC_Rotary_Angle_Senso.pdf BakeBit - Rotary Angle Sensor.pdf])
 +
*[BakeBit Github项目](https://github.com/friendlyarm/BakeBit)
 +
*[BakeBit Starter Kit手册](http://wiki.friendlyarm.com/bakebit/bakebit_starter_kit_manual_cn.pdf)

Latest revision as of 09:45, 28 November 2016

English

1 介绍

Rotary Angle Senso
  • BakeBit - Rotary Angle Senso是一个旋转角度传感器模块,模块使用了一个旋转电位器。通过改变接入电路部分电阻线的长度来改变接入部分的电阻值,从而改变输出端的分压。

2 特性

  • 使用标准的2.0mm 4 Pin BakeBit接口

BakeBit - Rotary Angle Senso

  • 引脚说明:
名称 描述
GND
5V 电源5V
NC
SIG 信号

3 示例程序(1):LED Fade

这个项目将使用BakeBit - Rotary Angle Sensor模块,来动态调节LED的亮度。

3.1 硬件连接

简单的将 LED 模块插入 D5接口,将 BakeBit - Rotary Angle Sensor 插入 A0接口,如下面这样:

LED Fade-1.jpg LED Fade-2.jpg

3.2 示例源代码

import time
import bakebit
 
# Connect the BakeBit Rotary Angle Sensor to analog port A0
# SIG,NC,VCC,GND
potentiometer = 0
 
# Connect the LED to digital port D5
# SIG,NC,VCC,GND
led = 5
 
bakebit.pinMode(potentiometer,"INPUT")
bakebit.pinMode(led,"OUTPUT")
time.sleep(1)
 
# Reference voltage of ADC is 5v
adc_ref = 5
 
# Vcc of the BakeBit interface is normally 5v
bakebit_vcc = 5
 
# Full value of the rotary angle is 300 degrees, as per it's specs (0 to 300)
full_angle = 300
 
while True:
    try:
        # Read sensor value from potentiometer
        sensor_value = bakebit.analogRead(potentiometer)
 
        # Calculate voltage
        voltage = round((float)(sensor_value) * adc_ref / 1023, 2)
 
        # Calculate rotation in degrees (0 to 300)
        degrees = round((voltage * full_angle) / bakebit_vcc, 2)
 
        # Calculate LED brightess (0 to 255) from degrees (0 to 300)
        brightness = int(degrees / full_angle * 255)
 
        # Give PWM output to LED
        bakebit.analogWrite(led,brightness)
 
        print("sensor_value = %d voltage = %.2f degrees = %.1f brightness = %d" %(sensor_value, voltage, degrees, brightness))
    except KeyboardInterrupt:
        bakebit.analogWrite(led,0)
        break
    except IOError:
        print ("Error")

Github

3.3 运行示例

假设你已经参考bakebit教程安装了BakeBit源代码,
要运行示例程序,可以在开发板上进入 BakeBit/Software/Python目录,运行bakebit_rotary_angle_sensor.py:

cd ~/BakeBit/Software/Python
sudo python bakebit_rotary_angle_sensor.py


3.4 运行结果

旋转电位器,LED亮度会随着电位器的角度变化而变化。

4 示例程序(2):Servo and Rotary Angle Sensor

这个示例演示用电位器(Rotary Angle Sensor)来控制舵机的转动。
该示例是配合BakeBit - Servo来实现的,可参考BakeBit - Servo页面。

5 示例程序(3):LED Bar and Rotary Angle Sensor

这个示例实现类拟音量调节的效果,用到电位器模块和LED Bar。
该示例是配合BakeBit - LED Bar来实现的,可参考BakeBit - LED Bar页面。

6 相关资料