You want to measure an analog voltage or Need to add analog inputs.  Today I show you How to Interface MCP3008 with raspberry pi for measuring analog value.


≡ Why we need MCP3008:


The Raspberry Pi computer does not have a way to read analog inputs. It's a digital-only computer. Compare this to the Arduino, AVR or PIC microcontrollers that regularly have 6 or more analog inputs! Analog inputs are handy because many sensors are analog outputs, so we require a way to form the Pi analog-friendly.

We'll do that by wiring up an MCP3008 chip to it. The MCP3008 acts like a "bridge" between digital and analog. It has 8 analog inputs and the Pi can query it utilizing 4 digital pins. That produces it a perfect addition to the Pi for joining basic sensors like photocells, FSRs or potentiometers, thermistors, etc.! Let's check the datasheet of the MCP3008 chip.

MCP3008


 ≡ Components Required :


1.Raspberry pi

2.Breadboard

3. Connecting wire

5. Power Supply

6. Resistors

7.MCP3008

This book will help you to gain more knowledge of Raspberry pi  Software and Hardware Problems and Solutions

Raspberry Pi Cookbook


≡ Circuit diagram MCP3008 raspberry Pi:


Shows the arrangement of components on the breadboard

 

 Using an MCP3008 ADC IC with a Raspberry Pi Using an MCP3008 ADC IC with a Raspberry Pi

VDD (power) and DGND (digital ground) to power the MCP3008 chip. We moreover require four "SPI" data pins: DOUT (Data Out from MCP3008), CLK (Clock stick), Din (Information In from Raspberry Pi), and /CS (Chip Select). At long last of course, a source of analog data. We'll be utilizing the fundamental 10k trim pot.
 
The MCP3008 has a few more pins we got to interface: AGND (analog ground, utilized now and then in precision circuitry, which typically not) interfaces to GND, and VREF (analog voltage reference, utilized for changing the "scale" - we want the total scale, so tie it to 3.3V). Below could be a wiring chartInterface the 3.3V cobbler pin to the left + rail and the GND pin to the correct - rail. Interface the taking after pins for the MCP chip
MCP3008 chipRaspberry Pi
VDD3.3V
VREF3.3V
AGNDGND
CLKSCLK
DOUTMISO
DINMOSI
 CSGPIO Pin 22
 
Next, connect up the potentiometer.
    1 (left) goes to 3.3v 
    2 (middle) connects to MCP3008 CH0 (analog input #0) 
    3 (right) connects to GND 

≡ Code MCP3008 raspberry pi 


Before trying the program, make beyond any doubt you have got SPI enabled and the SPIPython library introduced . Open an editor (nano ) and glue within the taking after code
import spidev, time

spi = spidev.SpiDev()
spi.open(0, 0)

def analog_read(channel):
    r = spi.xfer2([1, (8 + channel) << 4, 0])
    adc_out = ((r[1]&3) << 8) + r[2]
    return adc_out

while True:
    reading = analog_read(0)
    voltage = reading * 3.3 / 1024
    print("Reading=%d\tVoltage=%f" % (reading, voltage))
    time.sleep(1)

The interesting part of the program is contained within the analog_read function. This takes a parameter that ought to be between 0 and 7 and specifies which of the eight analog inputs down the lefthand side of the chip should be read. The bit control sets up an ask for the suitable channel and then sends the bits to the MCP3008, which peruses the resultant information:

Reading=0 Voltage=0.000000
Reading=126 Voltage=0.406055

The MCP3008 has 10-bit ADCs, so after you take a reading, it gives you a number between and 1023. The test program changes over this into a voltage reading by increasing the reading by the voltage range (3.3V) and then dividing it by 1,024. You can combine any of the taking after formulas that utilize the MCP3008 to allow readings to be taken from up to eight sensors.

If you want to know more about raspberry pi then click on the link below

RASPBERRY PI TUTORIALS FOR BEGINNERS