Skip to content

Commit

Permalink
I2CVoltage factor added
Browse files Browse the repository at this point in the history
  • Loading branch information
elschnorro77 authored Apr 20, 2020
1 parent 28251b8 commit ed3d3cc
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions read_pcf8591.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,21 @@ def measure_voltage(ts_sensor):
# 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

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

# measure once just for fun
_v = PCF8591.read_byte(address)

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

if 'ts_field' in ts_sensor and isinstance(voltage, (int, float)):
fields[ts_sensor["ts_field"]] = round(voltage, 4)
Expand Down Expand Up @@ -68,10 +76,18 @@ def get_raw_voltage(ts_sensor):
# 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

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 # convert 8 bit number to voltage 16.5/256 | 16.5V max voltage for 0xff (=3.3V analog output signal)
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)

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

0 comments on commit ed3d3cc

Please sign in to comment.