Difference between revisions of "BakeBit - Rotary Angle Sensor"

From FriendlyELEC WiKi
Jump to: navigation, search
(特性)
(示例程序(1):LED Fade)
Line 23: Line 23:
 
|}
 
|}
  
== 示例程序(1):LED Fade ==
+
== Code Sample(1): LED Fade ==
  
这个项目将使用[[BakeBit - Rotary Angle Sensor]]模块,来动态调节LED的亮度。
+
A [[BakeBit - Rotary Angle Sensor]] module is needed in this test case.
  
=== 硬件连接 ===
+
=== Hardware Connection ===
简单的将 LED 模块插入 D5接口,将 [[BakeBit - Rotary Angle Sensor]] 插入 A0接口,如下面这样:
+
Connect the LED module to the NEO-Hub at D5 and the [[BakeBit - Rotary Angle Sensor]] module to the NEO-Hub at A0:
  
 
::{| class="wikitable"
 
::{| class="wikitable"
Line 36: Line 36:
 
|}
 
|}
  
=== 示例源代码 ===
+
=== Source Code ===
  
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
Line 89: Line 89:
 
[https://github.com/friendlyarm/BakeBit/blob/master/Software/Python/bakebit_rotary_angle_sensor.py Github]
 
[https://github.com/friendlyarm/BakeBit/blob/master/Software/Python/bakebit_rotary_angle_sensor.py Github]
  
=== 运行示例 ===
+
=== Run Code Sample ===
+
 
假设你已经参考[http://wiki.friendlyarm.com/bakebit bakebit教程]安装了BakeBit源代码,<br />
+
Before you run the code sample you need to follow the steps in [http://wiki.friendlyarm.com/bakebit bakebit tutorial] to intall the BakeBit package.<br />
要运行示例程序,可以在开发板上进入 BakeBit/Software/Python目录,运行bakebit_rotary_angle_sensor.py:
+
Enter the "BakeBit/Software/Python" directory and run the "bakebit_rotary_angle_sensor.py" program:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
cd ~/BakeBit/Software/Python
 
cd ~/BakeBit/Software/Python
Line 99: Line 99:
  
  
=== 运行结果 ===
+
=== Observation ===
  
旋转电位器,LED亮度会随着电位器的角度变化而变化。
+
When you rotate the rotary angle sensor module the LED’s luminance will change accordingly.
  
 
== 示例程序(2):Servo and Rotary Angle Sensor ==
 
== 示例程序(2):Servo and Rotary Angle Sensor ==

Revision as of 10:42, 13 December 2016

查看中文

1 Introduction

Rotary Angle Senso
  • The BakeBit - Rotary Angle Sensor module is a rotary sensor module. It acts as a variable resistor or rheostat.When the nub is rotated the resistance will change.

2 Hardware Spec

  • Standard 2.0mm pitch 4-Pin BakeBit Interface

BakeBit - Rotary Angle Senso

  • Pin Description:
Pin Description
GND Ground
5V 5V Supply Voltage
NC Not Connected
SIG Signal

3 Code Sample(1): LED Fade

A BakeBit - Rotary Angle Sensor module is needed in this test case.

3.1 Hardware Connection

Connect the LED module to the NEO-Hub at D5 and the BakeBit - Rotary Angle Sensor module to the NEO-Hub at A0:

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

3.2 Source Code

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 Run Code Sample

Before you run the code sample you need to follow the steps in bakebit tutorial to intall the BakeBit package.
Enter the "BakeBit/Software/Python" directory and run the "bakebit_rotary_angle_sensor.py" program:

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


3.4 Observation

When you rotate the rotary angle sensor module the LED’s luminance will change accordingly.

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 相关资料