Raspberry Pi, Python & i2c Analogue to Digital Converter (ADC)

A quick little post for my future reference really.  Wanted to get an i2C Analogue to Digital Converter (ADC) working on a Raspberry Pi.  No real application as yet, but sure to be one in the future.

Most of the information to make this work was taken from posts by Grumpy Mike on the Raspberry Pi forums.  Got a couple of PCF8591s nice & cheep from eBay.

Here’s the PCF8591 Datasheet.

Figure 2 below shows the schematic:

Figure 2 - Schematic

Below is the very basic and non-defensive Python code to simply read the voltage provided by the pot to ADC channel 0 and print a proportional value between 0 & 255:

#Read a value from analogue input 0
#in A/D in the PCF8591P @ address 0x48
from smbus import SMBus

bus = SMBus(0)

print("Read the A/D")
print("Ctrl C to stop")
bus.write_byte(0x48, 0) # set control register to read channel 0
last_reading =-1

while(0 == 0): # do forever
    reading = bus.read_byte(0x48) # read A/D
    if(abs(last_reading - reading) > 2):
        last_reading = reading

Another picture of the breadboard lashup below:

2 thoughts on “Raspberry Pi, Python & i2c Analogue to Digital Converter (ADC)

  1. i noticed you connected your ADC to 5V, where the raspberry pi gpio is 3.3V. Is this going to damage the I2C port on the pi because the ADC is going to be communicating back at 5V? I haven’t tried anything like this, so i don’t know, but i just thought it might be potentially harmful. Let me know what you think. Thanks!

  2. This could be a risk. Was my first attempt at this sort of thing and was pleased just to get it to work. You could wire this from the 3.3V supply then your ADC range would cover 3.3V rather than 5V.

Leave a Reply

Your email address will not be published. Required fields are marked *