I am trying to use Raspberry Pi 2 to connect two sensors and let them work. They are a DFROBOT analog light sensor and a DFROBOT analog sound sensor.
I am also using a AD converter, and the converter connect to SCL and SDA pins on Raspberry Pi.
The converter has four Ains, so I can use two of them, one connect to light sensor, one connect to sound sensor.
Could somebody help me with the Python code? I want to let my Pi receive 1 second for Ain1(Light sensor) and 1 second for Ain2(Sound sensor), alternately.
Here is my code, but It seems that it doesn't work well.
import time
import datetime
from smbus import SMBus
bus = SMBus(1)
def readSoundSensor():
return bus.read_byte(0x48)
def runSoundSensor():
bus.write_byte(0x48,0x00)
last_reading = -1
sound = readSoundSensor()
timestamp= datetime.datetime.utcnow()
record = str(timestamp) + ":" +str(sound)
print "Sound: "+record
def readLightSensor():
return bus.read_byte(0x48)
def runLightSensor():
bus.write_byte(0x48,0x01)
last_reading = -1
light = readLightSensor()
timestamp= datetime.datetime.utcnow()
record = str(timestamp) + ":" +str(light)
print "Light: "+record
while(Ture):
runSoundSensor()
time.sleep(1)
runLightSensor()
time.sleep(1)
My Pi has some data, but I data is not what I want, and is not as when I connect just a single sound/light sensor.
I am totally new in Raspberry Pi, and I don't know anything about address 0x48, etc, I just learn from some guide. Could someone help me where I was wrong?
答案 0 :(得分:2)
您的while(Ture):
显然是错误的。
您正在为两个传感器的A / D写相同的命令 - 0x48 0x00
- 这是不正确的,除非它在每次读取时对所有输入进行采样?
您确定拥有正确的SMBUS地址 - 0x48
吗?
这个命令0x00
的意图是什么?
为了澄清,SMBUS命令将由address in hex, command byte(s) in hex
组成。您可能想要了解SMBUS / I2C寻址:This文章非常好。
我认为至少你需要发送一个命令来选择一个Ain
输入,然后另一个输入来对其进行采样。 0x00
看起来更像是重置。
您是否阅读过A / D的文档?