This tutorial demonstrates how to connect analog sensors to the Raspberry Pi. In particular, I’ll be using a water level/detection sensor and a 200 PSI pressure sensor.
Unlike Arduinos, the Raspberry Pi doesn’t have an ADC, which stands for Analog to Digital Converter. An ADC measures voltage on a pin and translates it to a number. You can plug an analog sensor directly into an Arduino using the analog ADC pins. The Raspberry Pi requires extra steps.
Most devices that you connect to the raspberry PI are either digital or analog. For example, in my DHT-22 tutorial, the temperature humidity sensor is digital. When it’s 25.2 degrees, the DHT22 will send the Pi the number 25.2. At a low level, the communication is ones and zeros or highs and lows but the associated software libraries are usually user friendly and the wiring is minimal. On the other hand, analog sensors signal voltage or resistance depending on the reading. The Koolance SEN-AP006G temperature sensor which I used in my liquid cooled computer monitor, acts as a variable resistor. Its resistance changes as the temperature changes. At 25.2 degrees it has a resistance of about 49kΩ. The vendor provides a chart so you can look up the corresponding temperature from the resistance. Adding a 10KΩ fixed resistor creates a voltage divider that would output about .56V which could be read by an ADC.
So why use an analog sensor if it’s more work and the Pi doesn’t even have an ADC? Among other reasons, they are often easier to get, some are better suited for specific environments and they may be cheaper. Sometimes there isn’t a digital equivalent.
Here’s an analog water level and an analog pressure sensor that I got an eBay. I did search around for comparable digital equivalents, but this is the best I could find at a reasonable price. Both devices output a voltage on the S pin which corresponds to a water level or a pressure value. I’m going to hook them up in my house to warn me if there’s a leak, and to monitor the pressure of the plumbing system.
There are several approaches you can take to connect analog devices to the Pi. You could try an RC charging circuit, but it would only work with sensors that act like resistors such as the Koolance SEN-AP006G. It would not work with the above level and pressure sensors because they output a voltage. The charging circuit counts the program cycles required to charge a capacitor which varies depending on resistance. It works better on microcontrollers than on SoCs. They tend to be inaccurate on the Pi because its operating system can interfere with the counting especially in Python and on multi-core chips found on newer Pi’s. Here’s a sample circuit but I don’t recommend this approach:
The python code for an RC charging circuit toggles the GPIO monitoring the analog signal between output and input. Then it counts the number of program cycles until GPIO goes high. This number corresponds to a voltage.
Another solution is to use a microcontroller with an ADC such as an Arduino. It can poll the analog sensors and transmit the results via SPI to the Pi. This is a more advanced technique and I’ve already covered it in my previous video Raspberry Pi AVR Programmer & SPI Tutorial. Therefore this video will focus on dedicated ADC chips such as the MCP3002 and the ADS1115. These devices are inexpensive and very easy to use.
The MCP3002 is a 10-bit resolution SPI analog to digital converter chip with 2 ADC channels.
The MCP3002 is connected using Pi’s hardware SPI GPIO pins. DIN goes to GPIO 10 (MOSI: master out slave in). DOUT goes to GPIO 9 (MISO: master in slave out). CLK goes to GPIO 11 (SCK: serial clock). CS goes to GPIO 8 (chip enable zero often called chip select). VDD pin goes to 3.3 volts on the Pi and the VSS goes to a ground pin. The water level sensor’s ‘S’ pin is connected to CH0 (ADC channel 0). The plus and minus pins go to 3.3V and ground respectively. An optional bi-color LED connected in series with 330Ω resistors to GPIO 14 and 15 provides feedback. The schematic shows a Raspberry Pi A, but this set up will work on any Pi.
For SPI and I2C communication, I recommend you start with a fresh install of the latest version of Raspbian. It comes with all the necessary software libraries installed such as spidev. Also please make sure it is up-to-date:
The only software requirements is to enable the SPI and I2C interfaces. From the Pi’s menu click Preferences – Raspberry Pi Configuration . Click the Interfaces tab and enable the SPI interface for the MCP3002 and the I2C interface for the ADS1115. I have more detailed tutorials on both SPI and I2C communication protocols if you’re interested.
Fortunately, the latest version of Raspbian no longer requires super user privileges to access the GPIO pins which simplifies things. Adafruit does make a python library for the MCP3008 which is an 8 channel version of the MCP3002. However, during testing I found that it was not completely compatible. Therefore, I wrote the poll_sensor function below. It reads a specified ADC channel and returns the 10 bit result. The following program polls the sensor every 2 seconds and displays the voltage and 10 bit data value to the shell. A bi-color LED is also used to indicate the presence of water (green: none, yellow: some water, red: more water).
Here’s the water detection circuit on the breadboard.
I found that the water level sensor is not very accurate, but it is good for water detection. It would also be possible to use a water pressure sensor to measure water level by monitoring the pressure at the bottom of a water tank. I’m using a 200 PSI sensor to measure my household plumbing pressure which can reach 180 PSI. For water level, you would use a much lower pressure sensor.
For the pressure sensor hook up, I’ll use an ADS1115 breakout board. It’s a 4 channel I2C ADC. It’s 15 bit resolution is more accurate than the 10 bit MCP3002. It supports power supplies from 2 to 5.5 volts which works well because the pressure sensor runs at 5 volts. I’ll also use an Adafruit I2C-safe level shifter between the 3.3 volt Pi and the ADS1115 running at 5 volts. In theory, I could hook up the ADS1115 without a level shifter for the I2C lines but it would be out of spec and I would probably have to desolder the pull-up resistors from the breakout board. Hence, I feel it’s safer and more reliable to use the inexpensive shifter. You can damage the Pi by exposing a GPIO pin to 5 volts.
The ADS1115 is connected to the Pi using the I2C lines SDA (GPIO2) and SCL (GPIO3). The Pi’s SDA and SCL connect to the lower voltage side of the level shifter on ports A1 and A2. The ADS1115’s SDA and SCL connect to the higher voltage side of the shifter on corresponding ports B1 and B2. The shifter resides between the Pi and the ADS1115 and shifts the voltages appropriately to protect the Pi and ensure reliability. The LV (lower voltage) pin on the shifter is connected a 3.3 volt pin on the Pi. The shifer’s HV (higher voltage) pin and the ADS1115’s VDD are connected to a 5 volt pin on the Pi. All 3 boards share a ground. The pressure sensor plus pin goes to 5 volts and the minus pin goes to ground. The ‘S’ pin is connected to A0 on the ADS1115 which is ADC channel zero. An LCD display is used for visual feedback. For more information on using LCD displays with the Pi, please see my LCD display tutorial.
Adafruit provides a python library for the ADS1115 that works great and is easy to use. The Adafruit–ads1x15 library is easily installed using pip.
Adafruit also provides a great python library for LCD displays which I have used in several of my projects. It’s now even easier to install. Again just use pip to install the Char LCD library.
Next i2cdetect is used to ensure the wiring is OK and to obtain the hex address for the ADS1115. The 1 argument designates bus 1. On older Pi’s this could be zero.
The matrix shows the ADS1115 is connected correctly on hex address 0x48.
The python code imports the Adafruit_CharLCD libary and instantiates and lcd specifying the GPIO pins and display size. The Adafruit_ADS1x15 library is imported and an adc is instantiated specifying the hex address 0x48 obtained from the i2cdetect utility and bus number 1. The gain is set to 2/3 which is for reading voltages up to 6.144V. Table 3 in the datasheet has different gain values if you are reading lower voltages. Please note that you should never allow voltages higher than the VDD pin to come in contact with an ADC channel. Since the ADS1115 is running at 5 volts, it could be damaged by a voltage on one of the ADC channels exceeding 5.3 volts. The main loop polls the sensor and displays the results to the LCD display.
Voltage is calculated from the 15 bit returned value by dividing by 32,767 for 10 bit resolution and then multiplying by 6.144 for the maximum gain voltage. When I first tested the sensor I determined that the relationship between PSI and voltage was linear and determined the line equation using y=mx+b. There is a great online line equation calculator on WebMath that automatically solves for the line equation using 2 points. It provides the answer and a complete explanation of how to solve for the equation. For this sensor, PSI = 50 x volts – 25. Plugging in the returned voltage returns the PSI reading. The display shows voltage, the data reading, PSI and BAR.
A viewer named Robert sent me some pics of a well monitoring system he designed and built using this tutorial. He sunk a pipe into his underground well and is using an analog pressure sensor to measure the air pressure of the pipe. A Raspberry Pi uses the pressure readings to calculate water level, usage and generate reports.