Created
March 23, 2019 05:05
-
-
Save scroix/0acf6c900d0907db1749f7b54e7e74e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from smbus2 import SMBus | |
import time | |
DELAY = 1 | |
# Get i2C bus | |
bus = SMBus(1) | |
# 'i2cdetect -y 1' for address | |
DEVICE_ADDRESS = 0x60 | |
DEVICE_OFFSET = 0x00 | |
BYTES_TO_READ = 2 | |
def convert_to_12bits(data): | |
raw_adc = (data[0] & 0x0F) * 256 + data[1] | |
if raw_adc > 2047 : | |
raw_adc -= 4095 | |
return raw_adc | |
''' | |
def convert(r): | |
return ((r[0] & 15) << 8) + r[1] | |
''' | |
def convert_to_voltage(raw_adc): | |
return raw_adc * 0.00034 | |
def set_voltage(raw_value): | |
data = [] | |
data.append(65) | |
data.append(raw_value >> 4) # MSB | |
data.append((raw_value & 15) << 4) # LSB | |
# Write a block of len(data) bytes to address 80 from offset 0 | |
bus.write_i2c_block_data(DEVICE_ADDRESS, DEVICE_OFFSET, data) | |
# set_voltage(4096) | |
# time.sleep(DELAY) | |
# Read a block of 2 bytes from address 60, offset 0 | |
data = bus.read_i2c_block_data(DEVICE_ADDRESS, DEVICE_OFFSET, BYTES_TO_READ) | |
raw_adc = convert_to_12bits(data) | |
voltage = convert_to_voltage(raw_adc) | |
print("ADC Raw ADC Output: %d" % raw_adc) | |
print("ADC Voltage Output: %.2f V" % voltage) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment