I'm using raspberry pi 3 b+ and si7021 to get temperature and humidity. But now I have a problem that I don't know how to use wiringpi correctly.
This is my code:
import sys
import time
import wiringpi
I2C_ADDR = 0x40
SI7021_REG_NO_HOLD_HUMIDITY = 0xF5
SI7021_REG_NO_HOLD_TEMPERATURE = 0xF3
wiringpi.wiringPiSetup()
fd = wiringpi.wiringPiI2CSetup(0x40)
#fd = wiringpi.wiringPiI2CSetupInterface("/dev/i2c-0", I2C_ADDR)
while True:
try:
print(wiringpi.wiringPiI2CWriteReg8(fd,0x40,0xF3))
time.sleep(0.3)
print(fd, wiringpi.wiringPiI2CReadReg8(fd, 0x40))
# print(wiringpi.wiringPiI2CRead(0x40))
time.sleep(0.5)
except KeyboardInterrupt:
sys.exit(0)
The problem is I will always get 0 or negative value after wiringpi.wiringPiI2CWriteReg8(fd,0x40,0xF3)
and wiringpi.wiringPiI2CReadReg8(fd, 0x40)
executed. According to their manual, this means an error.
I'm quite sure that my connection is correct since I can use i2cget
to get a correct value. Can someone tell me what is wrong in my code? Thanks in advance.
The slave address that is mentioned in the datasheet is already passed to the wiringpi library in the
wiringPiI2CSetup(0x40)
call. You do not need to repeat it, it is implicitly passed asfd
. Given the wiringpi documentation you‘re trying to set register 0x40 to value 0xF3. Instead you should usewiringPiI2CWrite
to issue the command. And similarly usewiringPiI2CRead
afterwards.