Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python example? #17

Open
mattrpav opened this issue Mar 30, 2024 · 2 comments
Open

Python example? #17

mattrpav opened this issue Mar 30, 2024 · 2 comments

Comments

@mattrpav
Copy link

I'm looking to wire up the 2m sensors to a Raspberry Pi for an 8th grade intro to engineering class. Any tips or sample code for reading the sensor via Python?

@mattrpav
Copy link
Author

I have it wired up to a raspberry pi 5. the indicator light is on, and I am able to detect it using i2cdetect.

However, I do not think I am getting a proper read b/c it mostly returns a value of 0. Occasionally, it will output a different value, but not consistently when I waive may hand over the sensor.

Thoughts?

i2cdetect output:

# i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- 29 -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         

Sample code:

import smbus
import time

# I2C channel 1 is connected to the GPIO pins
channel = 1

#  REV-31-1505  defaults to address 0x52
address = 0x29

# Initialize I2C (SMBus)
bus = smbus.SMBus(channel)

while True:
    print('value: ' + str(bus.read_byte(address)))
    time.sleep(1)

Code output:

value: 0
value: 0
value: 1
value: 64
value: 0
value:0

@mattrpav
Copy link
Author

mattrpav commented Apr 1, 2024

Reviewing the C++ code, looks like the I2C device needs to be initialized. Is there a specification on the I2C commands needed to initialize the 2m sensor via i2c?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant