Reputation: 48
Building a device to read Analog to Digital Voltages using PCF8591 Analog to Digital Converter (ADC).
The ADC connects via i2c and successfully provides HEX values that correspond to values between 0 and 255 for various input voltages when connected via terminal:
pi@raspberrypi:~ $ sudo i2cget -y 0 0x48
0x38
pi@raspberrypi:~ $ sudo i2cget -y 0 0x48
0x3a
pi@raspberrypi:~ $ sudo i2cget -y 0 0x48
0x44
pi@raspberrypi:~ $ sudo i2cget -y 0 0x48
0x3d
0x38 = 56 ; 0x3a = 58 .. etc.
When running the same application through Python, I obtain an error.
Below is my source Code:
import time
import smbus
i2c_ch = 0 #channel we're running on with i2c
#address on the I2C bus of the ADC
i2c_address = 0x48
bus = smbus.SMBus(i2c_ch)
# Print out temperature every second
while True:
temperature = bus.read_i2c_block_data(i2c_address, 0)
print(temperature)
time.sleep(1)
This is the Ouput:
[0, 0, 16, 12, 9, 7, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 5, 3, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 5, 3, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 12, 8, 6, 5, 3, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 12, 9, 7, 5, 3, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 6, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 5, 3, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 9, 7, 6, 4, 3, 3, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
What are these errors, what do they mean and how can I update my Python script to correctly read the Analog to Digital Converter over I2C?
Upvotes: 0
Views: 1468
Reputation: 48
The below codes works, issue was threefold:
Some documentation also suggests that you first read the pin once, before reading it again, something to do with the first read just reads what is there previously.
Code below will successfully read voltage on a scale from 0 - 255:
bus = smbus.SMBus(0) # (0) Ensures the i2c bus used in the Pi
bus.write_byte(0x48, 0x02) # Before you can read, you need to write
# 0x02 represents Analog in Pin #2
while True:
temperature = bus.read_byte_data(i2c_address, 0x02)
print(temperature)
time.sleep(1)
This works for all inputs on PCF8591 Analog to Digital Converter (ADC).
Upvotes: 0
Reputation: 2169
According to the docs, read_i2c_block_data
will return an array containing 32 bytes.
To read a single byte at a time you need to use read_byte_data
while True:
temperature = bus.read_byte_data(i2c_address, 0)
print(f"byte: {temperature}, hex: {temperature.hex()}")
time.sleep(1)
Upvotes: 1