Translate

MCP3008 ADC Interfacing with Raspberry Pi (Analog Input Guide)

 
1. Introduction

The Raspberry Pi does not have built-in analog input pins. To read analog sensors like LM35, LDR, or soil moisture sensors, we use an ADC (Analog to Digital Converter).

The MCP3008 is a popular 8-channel ADC that communicates using SPI protocol, allowing Raspberry Pi to read analog values easily.


2. Components

  • Raspberry Pi 4 Model B
  • MCP3008 ADC IC
  • Breadboard
  • Jumper wires
  • Analog sensor (LM35 / LDR optional)

3. Circuit and Connections

Raspbery Pi
Raspberry Pi Pinout Diagram

MCP3008 ADC Pinout Diagram
MCP3008 ADC Pinout Diagram

Pinout Diagram MCP3008 ADC Module
Pinout Diagram MCP3008 ADC Module

MCP3008 to Raspberry Pi (SPI Connection)

MCP3008 PinFunction    Raspberry Pi Pin
16VDD            3.3V
15VREF            3.3V
14AGND            GND
13CLK    GPIO11 (SCLK)
12DOUT    GPIO9 (MISO)
11DIN    GPIO10 (MOSI)
10CS/SHDN    GPIO8 (CE0)
9DGND            GND

Analog Input Pins

ChannelPin
CH0–CH7Pins 1–8

👉 Connect analog sensor output to any channel (e.g., CH0)


4. Detailed Step By Step Circuit Working

  1. Analog sensor produces variable voltage (0–3.3V).
  2. MCP3008 converts analog signal into 10-bit digital value (0–1023).
  3. Raspberry Pi communicates via SPI protocol.
  4. Data is sent serially from MCP3008 to Raspberry Pi.
  5. Pi processes and displays the digital value.

5. Libraries to be Included

Enable SPI:

sudo raspi-config

→ Interface Options → SPI → Enable

Install libraries:

pip3 install spidev

6. Code (Python)

import spidev
import time

spi = spidev.SpiDev()
spi.open(0, 0) # Bus 0, Device 0
spi.max_speed_hz = 1350000

def read_channel(channel):
adc = spi.xfer2([1, (8 + channel) << 4, 0])
data = ((adc[1] & 3) << 8) + adc[2]
return data

try:
while True:
value = read_channel(0) # Read from CH0
print("Analog Value:", value)
time.sleep(1)

except KeyboardInterrupt:
spi.close()

7. Detailed Step By Step Code Working

  • spi.open(0,0) → Opens SPI bus
  • xfer2() → Sends and receives data
  • (8 + channel) << 4 → Selects ADC channel
  • Combines received bytes → gets 10-bit value
  • Loop continuously reads analog input

8. Tips

  • Always use 3.3V (NOT 5V)
  • Double-check SPI pin connections
  • Use proper grounding
  • Keep wires short for stable readings
  • Test using potentiometer before sensors

9. Uses

  • Reading analog sensors (LM35, LDR, Gas sensors)
  • IoT data acquisition systems
  • Industrial monitoring
  • Robotics sensor integration
  • Environmental monitoring

10. Conclusion

The MCP3008 ADC is essential for enabling analog input on Raspberry Pi. It bridges the gap between analog sensors and digital processing, making it a must-have for advanced projects.

à´¤ുà´Ÿà´•്à´•à´•്à´•ാർക്à´•ാà´¯ി ഇലക്à´Ÿ്à´°ോà´£ിà´•്à´¸് ലളിതമാà´¯ി പഠിà´•്à´•ാം.

Empowering students in Kerala with hands-on technical skills.