Reputation: 21
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?
Upvotes: 0
Views: 1011
Reputation: 8410
Your while(Ture):
is obviously wrong.
You are writing the same command - 0x48 0x00
to the A/D for both the sensors - that can't be correct, unless it samples all inputs on each read?
Are you sure you have the correct SMBUS address - 0x48
?
What is this command 0x00
meant to do?
To clarify, SMBUS commands will consist of address in hex, command byte(s) in hex
. You might want to read up on SMBUS / I2C addressing: This article is quite good.
I would imagine that at the very least you need to send a command to select one of the Ain
inputs and then another to sample it. 0x00
looks more like a reset.
Have you read the documentation for the A/D?
Upvotes: 2