Look at the RasPiO Analog Zero

The RasPiO Analog Zero is an add on ‘hat’  that can be used with any 40 pin Raspberry Pi. This board adds analog input capability to your Raspberry Pi.

The main part of the RasPiO Analog Zero is an MCP3008 8 analog to digital converter. This board allows you add various sensors and devices that you could not normally use such as LDRs, potentiometers, voltages and many sensors that output their value as an analog value

Here is a picture of the board after it has been assembled.

raspio-analog-zero

The MCP3008 analog to digital converter chip is connected to the SPI ports MOSI, MISO, SCLK and CE of your Raspberry PI , you can see that all other pins are made available via an unpopulated single row header.  There is also a small prototyping area with 5v, 3v3 and GND
The lack of analog input / output has always been a bug bear of mine with regards the RAspberry Pi, this low cost board (£11) will add on analog input capability

Code

Save this with a python .py extension

from gpiozero import MCP3008
from time import sleep
 
while True:
    for x in range(0, 8):
        with MCP3008(channel=x) as reading:
            print(x,": ", reading.value)
sleep(0.1)

 

If you want to display a voltage of a channel you would do something like this

from gpiozero import MCP3008
adc = MCP3008(channel=0)
voltage = 3.3 * adc.value
print("channel 0 voltage is: ", voltage)

 

 

Links

http://rasp.io/analogzero/