Can Raspberry Pi read Analog Input?
Of course NOT! but it CAN. The Raspberry Pi has GPIO digital pins where it can read the data either high or low ie., 0 or 1. The analog sensors such as piezoelectric sensors, thermistors, potentiometers, pressure sensors, etc give us the raw value. The analog sensors work mostly based on external factors.
The analog sensor measures the amount of pressure applied to the sensor is called the pressure sensor. The sensor which detects the vibration, the stress is proportional to the voltage in the piezoelectric sensor. Hence, a definite purpose is served for each and every sensor. the Raspberry Pi fails at this moment to read the analog sensor data since it has only digital pins.
In Arduino Uno and ESP8266, an ADC helps to convert the analog raw value to digital sensor and processes the data, but in RPi there are no digital pins present.
Analog Readings : Basic Recap
The analogread() reads the value from the specific analog pin. The development boards used in the experiment have a 10-bit analog to digital converters. This indicates that it will meet the range of 0V and the operating voltage of 5V or 3.3V mapped into the integer values between 0 and 1023. In some boards, a 12-bit Analog to Digital Converter is used. In that case, the operating voltage is mapped into the integer values of 0 and 4095.
It only takes about 100 microseconds to read one single analog input. Hence the maximum reading rate is 10,000 times per second. Each analog pin is a 10-bit ADC and it can store 1024 values. For instance, if the operating voltage is 5V with a 10-bit ADC then it yields a resolution of 0.49mV per unit. This calculation reports a ratiometric value. The 5V can be assumed to be the integer value of 1023 and 0V will be the integer value of 0.
Resolution of the ADC/System voltage = ADC Reading/ Analog Voltage Measured |
The System Voltage and the Resolution of the ADC are known from the development board. The ADC reading is measured from the analog devices connected to the analog pin. The analog voltage can be calculated from the ratio. For instance, if the ADC reading is 500 and the System voltage is 3.3 V which has a 10-bit ADC then the analog voltage measured is 1.61V. Alternately, the Analog Voltage can be measured using a digital multimeter.
An approach using MCP3008 ADC chip
Since RPi is a digital-only system and no Analog pins are available, an Analog-to-Digital would be required to interface and such interface would allow RPi to read the analog data fetched by sensors, which are associated with RPi.
The SPI protocol is used for communication with the analog pins. This can be done by installing the SPI package in the terminal window. Under the configuration menu, the SPI is enabled. The MCP3008 chip is an ADC and is connected to the Raspberry Pi. It has totally 16 pins, CH0- CH1 i.e, pin 1- pin 8 is the analog pins and rest of the pin from 9- 16 have a whole range of different pins. The pin 9 is called the DGND which is the digital ground, pin 10 is the chip select, pin 11 DIN is the data in and pin 12 DOUT is the data out from the Raspberry Pi, pin 13 is the clock pin, pin14 is the analog ground pin. The pin 15 is the VREF which is the analog reference voltage and the pin 16 is the VDD. Both the pins are wired to the 3.3 V.
Example:
Below is a circuit of the Raspberry Pi connected with the MCP3008, Piezoelectric sensor and a resistor of 1 Megaohm. Later the setup is fixed on the tap to detect the vibrations from the pipe. The aim is to obtain the information of whether the water is flowing inside the tap or not.
The Raspberry Pi’s GPIO pins are digital and the inputs are set to either high or low. Therefore by using Analog to Digital Converters, analog inputs can be read. The MCP3008 is placed on the breadboard with the notch on the top considering Pin 1 starts from the left of the notch.
There are totally 8 analog pins (CH0 to CH8) available to connect to the analog input devices to read their values. At a time, eight analog sensor inputs can be connected. The VDD and the V-REF are connected to the 3.3 V while AGND and DGND pins are grounded. The CLK is wired to the SCLK. The DOUT and DIN pins are connected to MISO and MOSI respectively. Finally, the CS pin is wired to CE0.
Python Code:
# Importing modules
import spidev # To communicate with SPI devices
from time import sleep
import datetime
# To add delay
# Start SPI connection
spi = spidev.SpiDev() # Created an object
spi.open(0,0)
# Read MCP3008 data
def analogInput(channel):
spi.max_speed_hz = 1350000
adc = spi.xfer2([1,(8+channel)<<4,0])
data = ((adc[1]&3) << 8) + adc[2]
return data
# Below function will convert data to voltage
def Volts(data):
volts = (data * 3.3) / float(1023)
volts = round(volts, 2) # Round off to 2 decimal places
return volts
while True:
temp_output = analogInput(1) # Reading from CH0
temp_volts = Volts(temp_output)
x = datetime.datetime.now()
print("vibration : {} {} ({}V)".format(temp_output,x,temp_volts))
if (temp_output > 200):
print ("The tap is ON")
sleep(5)