Skip to content

Commit

Permalink
fix raw measuement
Browse files Browse the repository at this point in the history
  • Loading branch information
JavanXD committed May 20, 2020
1 parent 2b3bebe commit ec5e3f7
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions read_pcf8591.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,16 @@ def get_raw_voltage(ts_sensor):
# AIN2 => Pin 2 (default)
# AIN3 => Pin 3

# Configure PCF8591
if 'I2CVoltage' in ts_sensor:
if int(ts_sensor['I2CVoltage']) == 5:
factor = 1.5151515151515151
else:
factor = 1
else:
factor = 1
#factor = 0.09765625 # 25V/256 (=5V analog output signal)
factor = 16.5/256 # 16.5V/256 | 16.5V max voltage for 0xff (=3.3V analog output signal)
if 'I2CVoltage' in ts_sensor and ts_sensor['I2CVoltage'] is not None:
# convert 8 bit number to voltage
factor = float(ts_sensor['I2CVoltage']) / 256

PCF8591.write_byte(address, 0x40+pin) # set channel to AIN0, AIN1, AIN2 or AIN3

Voltage_8bit = PCF8591.read_byte(address) # = i2cget -y 1 0x48
voltage = Voltage_8bit*0.064453125*factor # convert 8 bit number to voltage 16.5/256 | 16.5V max voltage for 0xff (=3.3V analog output signal)
voltage = Voltage_8bit*factor # convert 8 bit number to voltage

if isinstance(voltage, (int, float)):
voltage=round(voltage, 4)
Expand Down

0 comments on commit ec5e3f7

Please sign in to comment.