Unfortunately, Raspberry Pi does not have a built-in ADC (analog-to-digital converter) for reading analog sensors such as potentiometers. And, sometimes, it can be really frustrating to work on electronics projects without being able to utilize any analog sensor which is providing necessary features to the project.
However, there is a way to read an analog sensor properly with Raspberry Pi: adding an external ADC (analog-to-digital converter) to the circuit. In this tutorial, I will show how to use an MCP3008 analog-to-digital converter (external ADC) with the Adafruit CircuitPython MCP3xxx library, due to its efficiency and simple usage with Raspberry Pi modules.
The Microchip Technology Inc. MCP3004/3008 devices are successive approximation 10-bit Analogto-Digital (A/D) converters with on-board sample and hold circuitry. The MCP3004 is programmable to provide two pseudo-differential input pairs or four single-ended inputs. The MCP3008 is programmable to provide four pseudo-differential input pairs or eight single-ended inputs. Differential Nonlinearity (DNL) and Integral Nonlinearity (INL) are specified at ±1 LSB. Communication with the devices is accomplished using a simple serial interface compatible with the SPI protocol. The devices are capable of conversion rates of up to 200 ksps. The MCP3004/3008 devices operate over a broad voltage range (2.7V - 5.5V). Low-current design permits operation with typical standby currents of only 5 nA and typical active currents of 320 µA. The MCP3004 is offered in 14-pin PDIP, 150 mil SOIC and TSSOP packages, while the MCP3008 is offered in 16- pin PDIP and SOIC packages.
You can find more information about the MCP3008 from here.
If needed, add a voltage divider between the sensor and the MCP3008 channel:
I used the Adafruit CircuitPython MCP3xxx library on Raspberry Pi to read analog sensors with MCP3008.
But, you will need to install the Adafruit_Blinka library that provides the CircuitPython support in Python to use the mentioned library.
sudo pip3 install adafruit-blinka
If it is needed, you have to enable the SPI on Raspberry Pi Configuration Settings before executing the command.
Once that's done, from your command line run the following command to install the Adafruit CircuitPython MCP3xxx library.
sudo pip3 install adafruit-circuitpython-mcp3xxx
Then, make the pin connections as follows:
- Include the required modules.
- Create the SPI bus, the cs (chip select), the mcp object, and the analog inputs connected to the input pins on the MCP3008.
- Read analog sensor values from the channel 0.
- Get the channel voltage.
- Start the loop and print the analog value and the channel voltage recursively.
import busio import digitalio import board import adafruit_mcp3xxx.mcp3008 as MCP from adafruit_mcp3xxx.analog_in import AnalogIn # Create the SPI bus spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI) # Create the cs (chip select) cs = digitalio.DigitalInOut(board.D5) # Create the mcp object mcp = MCP.MCP3008(spi, cs) # Create analog inputs connected to the input pins on the MCP3008. channel_0 = AnalogIn(mcp, MCP.P0) def evaluateSensorValue(): # Read analog sensor values from the channel 0. sensor_value = channel_0.value # Get the channel voltage. channel_voltage = channel_0.voltage # Print the sensor value and the channel voltage. print('Analog Read: ' + str(sensor_value)) print('Channel Voltage: ' + str(channel_voltage) + 'V') # Start the loop. while True: evaluateSensorValue()
Click here to inspect my electronics project in which I used an MCP3008 ADC to read data from an analog sensor (MQ-135 Air Quality Sensor).