I have been trying to get my python code to work with my sensor to add graphs and excel functions, but I keep getting an IO error 121 for line data = bus.read_i2c_block_data(0x78,4). My java code works, which means that the sensor is communicating with the pi.
I have pasted both the python and java code for reference.
PYTHON:
import smbus
import time
Get I²C bus
bus = smbus.SMBus(1)
bus.write_quick(0x78)
data = bus.read_i2c_block_data(0x78,4)
Convert the data
pres = ((data[0] & 0xFF) * 256) + data[1]
temp = ((data[2] * 256) + (data[3] & 0xFF)) / 32
pressure = (pres - 1638.0) / (13107.0 / 10.0)
cTemp = ((temp * 200.0) / 2048) - 50.0
fTemp = (cTemp * 1.8 ) + 32
Output data to screen
print ("Pressure : %.2f mbar ") %pressure
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;
import java.io.IOException;
JAVA:
public class AMS5812
{
public static void main(String args[]) throws Exception
{
// Create I²C bus
I²C Bus Bus = I²C Factory.getInstance(I2CBus.BUS_1);
// Get I²C device, AMS5812 I²C address is 0x78(120)
I²C Device device = Bus.getDevice(0x78);
// Read 4 bytes of data
// pressure msb, pressure lsb, temp msb, temp lsb
byte[] data = new byte[4];
device.read(data, 0, 4);
// Convert the data
double pressure = ((data[0] & 0xFF)* 256 + (data[1] & 0xFF));
double temp = ((data[2] & 0xFF)* 256 + (data[3] & 0xFF));
pressure = ((pressure - 3277.0) / ((26214.0) / .0725));
double cTemp = ((temp - 3277.0) / ((26214.0) / 110.0)) - 25.0;
double fTemp = (cTemp * 1.8 ) + 32;
double pressurewc = 0.703070 * pressure;
// Output data to screen
System.out.printf("Pressure is : %.2f wc%n", pressurewc);
System.out.printf("Pressure is : %.2f PSI%n", pressure);
System.out.printf("Temperature in Celsius : %.2f C%n", cTemp);
System.out.printf("Temperature in Fahrenheit : %.2f F%n", fTemp);
The sensor does not show up when I run I²C detect -y 1, nothing shows up since the sensor’s address is higher than 0x77. When I run the Java code, I get the temp and pressure from the sensor. I’ve compared the readings with an actual pressure sensor and the readings match. Could I be missing something in my python code?