Skip to content

Instantly share code, notes, and snippets.

@antonvh
Last active September 29, 2015 20:02
Show Gist options
  • Select an option

  • Save antonvh/c81c247fc03029a1ba6a to your computer and use it in GitHub Desktop.

Select an option

Save antonvh/c81c247fc03029a1ba6a to your computer and use it in GitHub Desktop.

Revisions

  1. antonvh revised this gist Sep 29, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion get_voltage.py
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ def get_voltage():
    vratio = (last_4 << 6) | first_6

    # Now we can calculate the battery voltage like so:
    ratio = 0.0179 # this is 0.1/5.5V Still have to find out why...
    ratio = 0.0179 # This is an empyrical value based on several different batteries
    voltage = vratio * ratio

    return voltage
  2. antonvh revised this gist Sep 29, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion get_voltage.py
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ def get_voltage():
    vratio = (last_4 << 6) | first_6

    # Now we can calculate the battery voltage like so:
    ratio = 0.01818 # this is 0.1/5.5V Still have to find out why...
    ratio = 0.0179 # this is 0.1/5.5V Still have to find out why...
    voltage = vratio * ratio

    return voltage
  3. antonvh revised this gist Sep 29, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion get_voltage.py
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ def get_voltage():
    return voltage

    except:
    return False
    return 0.0

    if __name__ == "__main__":
    print get_voltage()
  4. antonvh revised this gist Sep 28, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion get_voltage.py
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ def get_voltage():

    # from this data we need the last 4 bits and the first 6.
    last_4 = data & 0b1111 # using a bit mask
    first_6 = data >> 10 # left shift 10 because data is 16 bits
    first_6 = data >> 10 # right shift 10 because data is 16 bits

    # together they make the voltage conversion ratio
    # to make it all easier the last_4 bits are most significant :S
  5. antonvh revised this gist Sep 28, 2015. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions get_voltage.py
    Original file line number Diff line number Diff line change
    @@ -11,10 +11,11 @@ def get_voltage():
    :return: voltage (float)
    """
    bus = smbus.SMBus(1) # SMBUS 1 because we're using greater than V1.
    address = 0x48
    # time.sleep(0.1) #Is this necessary?
    try:
    bus = smbus.SMBus(1) # SMBUS 1 because we're using greater than V1.
    address = 0x48
    # time.sleep(0.1) #Is this necessary?

    # read data from i2c bus. the 0 command is mandatory for the protocol but not used in this chip.
    data = bus.read_word_data(address, 0)

  6. antonvh revised this gist Sep 18, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions get_voltage.py
    Original file line number Diff line number Diff line change
    @@ -18,16 +18,16 @@ def get_voltage():
    # read data from i2c bus. the 0 command is mandatory for the protocol but not used in this chip.
    data = bus.read_word_data(address, 0)

    # from this data we need the last 4 bites and the first 6.
    last_4 = data & 0b1111 # using a byte mask
    # from this data we need the last 4 bits and the first 6.
    last_4 = data & 0b1111 # using a bit mask
    first_6 = data >> 10 # left shift 10 because data is 16 bits

    # together they make the voltage conversion ratio
    # to make it all easier the last_4 bits are most significant :S
    vratio = (last_4 << 6) | first_6

    # Now we can calculate the battery voltage like so:
    ratio = 0.01818
    ratio = 0.01818 # this is 0.1/5.5V Still have to find out why...
    voltage = vratio * ratio

    return voltage
  7. antonvh revised this gist Sep 18, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion get_voltage.py
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ def get_voltage():

    # together they make the voltage conversion ratio
    # to make it all easier the last_4 bits are most significant :S
    vratio = ((last_4 << 6) | first_6) / 1024.0
    vratio = (last_4 << 6) | first_6

    # Now we can calculate the battery voltage like so:
    ratio = 0.01818
  8. antonvh revised this gist Sep 18, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion get_voltage.py
    Original file line number Diff line number Diff line change
    @@ -35,5 +35,5 @@ def get_voltage():
    except:
    return False

    if __name__ == "main":
    if __name__ == "__main__":
    print get_voltage()
  9. antonvh renamed this gist Sep 18, 2015. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions gistfile1.txt → get_voltage.py
    Original file line number Diff line number Diff line change
    @@ -27,10 +27,13 @@ def get_voltage():
    vratio = ((last_4 << 6) | first_6) / 1024.0

    # Now we can calculate the battery voltage like so:
    RPi_voltage = 5.0
    voltage = RPi_voltage / vratio
    ratio = 0.01818
    voltage = vratio * ratio

    return voltage

    except:
    return False
    return False

    if __name__ == "main":
    print get_voltage()
  10. antonvh revised this gist Sep 18, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    import smbus

    def get_voltage():
    """
    Reads the digital output code of the MCP3021 chip on the BrickPi+ over i2c.
  11. antonvh created this gist Sep 18, 2015.
    34 changes: 34 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    def get_voltage():
    """
    Reads the digital output code of the MCP3021 chip on the BrickPi+ over i2c.
    Some bit operation magic to get a voltage floating number.

    If this doesnt work try this on the command line: i2cdetect -y 1
    The 1 in there is the bus number, same as in bus = smbus.SMBus(1)
    Google the resulting error.

    :return: voltage (float)
    """
    bus = smbus.SMBus(1) # SMBUS 1 because we're using greater than V1.
    address = 0x48
    # time.sleep(0.1) #Is this necessary?
    try:
    # read data from i2c bus. the 0 command is mandatory for the protocol but not used in this chip.
    data = bus.read_word_data(address, 0)

    # from this data we need the last 4 bites and the first 6.
    last_4 = data & 0b1111 # using a byte mask
    first_6 = data >> 10 # left shift 10 because data is 16 bits

    # together they make the voltage conversion ratio
    # to make it all easier the last_4 bits are most significant :S
    vratio = ((last_4 << 6) | first_6) / 1024.0

    # Now we can calculate the battery voltage like so:
    RPi_voltage = 5.0
    voltage = RPi_voltage / vratio

    return voltage

    except:
    return False