0

i used to use arduino as a i2c communication device but i'm changing to raspberry and i have a question about how to send and receive data from raspberry to my device

the device in question is a pressure and temperature sensor, the sensor address is 0x78

i need to send 0xAC, wait some time and then request 6 bytes

the arduino code is the following:

Wire.beginTransmission(0x78);
 Wire.write(0xAC);
 Wire.endTransmission();
 delay(50); 
 
 Wire.requestFrom(0x78, 6); // request 6 bytes 
 if (Wire.available() == 6) { // read 6 bytes :status, BridgeDat1, BridgeDat2, BridgeDat3, TempDat1, TempDat2
 data[0] = Wire.read(); //status
 data[1] = Wire.read(); //BridgeDat1
 data[2] = Wire.read(); //BridgeDat2
 data[3] = Wire.read(); //BridgeDat3
 data[4] = Wire.read(); //TempDat1
 data[5] = Wire.read(); //TempDat2
 }

my question is, how to do that in python ??

asked Jan 22, 2021 at 17:34

1 Answer 1

1

Here are some methods using my Python modules.

You can also use the Python smbus module.

pigpio

import time
import pigpio
pi = pigpio.pi()
h = pi.i2c_open(1, 0x78)
pi.i2c_write_device(h, [0xAC])
time.sleep(0.05)
(b, d) = pi.i2c_read_device(h, 6)
print(d[0], d[1], d[2], d[3], d[4], d[5])
pi.i2c_close(h)
pi.stop()

rgpio

import time
import rgpio
sbc = rgpio.sbc()
h = sbc.i2c_open(1, 0x78)
sbc.i2c_write_device(h, [0xAC])
time.sleep(0.05)
(b, d) = sbc.i2c_read_device(h, 6)
print(d[0], d[1], d[2], d[3], d[4], d[5])
sbc.i2c_close(h)
sbc.stop()

lgpio

import time
import lgpio as sbc
h = sbc.i2c_open(1, 0x78)
sbc.i2c_write_device(h, [0xAC])
time.sleep(0.05)
(b, d) = sbc.i2c_read_device(h, 6)
print(d[0], d[1], d[2], d[3], d[4], d[5])
sbc.i2c_close(h)
answered Jan 22, 2021 at 17:56
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.