Mechatronics LAB
Published © MIT

How to Interface MCP3008 with raspberry Pi

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 measuri

BeginnerWork in progress1 hour2,516
How to Interface MCP3008 with raspberry Pi

Things used in this project

Hardware components

Raspberry P
×1

Story

Read more

Schematics

How to Interface MCP3008 with raspberry Pi

Code

Code

Python
import spidev
import time

# Create SPI object
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)

Credits

Mechatronics LAB

Mechatronics LAB

75 projects • 44 followers
I am Sarful , I am a Mechatronics Engineer & also a teacher I am Interested in the evolution of technology in the automation industry .

Comments