Reading Analog Signal Using MCP3008 On Raspberry Pi

This tutorial will show you on how to read analog signals using MCP3008 on Raspberry Pi.

INTRODUCTION

Maybe you’re not interested to write 2 different program on 2 different controller platform as shown in I2C Raspberry Pi Arduino – Reading Analog Signal. Another option, you can read the analog signals using MCP3008 (Analog to Digital – SPI) on Raspberry Pi. This tutorial will show you how.

VIDEO

This video shows how to read analog signals using MCP3008 on Raspberry Pi.

HARDWARE PREPARATION

Wiring Connection

1. MCP3008

MCP3008 PinsConnections
1 (CH0)Potentiometer analog signal pin
2 (CH1)LM35 analog signal pin
3 (CH2) 
4 (CH3) 
5 (CH4) 
6 (CH5) 
7 (CH6) 
8 (CH7) 
9 (DGND) GND (Maker pHAT)
10 (CS/SHDN) CE0 (Maker pHAT)
11 (DIN)MOSI (Maker pHAT)
12 (DOUT)MISO (Maker pHAT)
13 (CLK)SCLK (Maker pHAT)
14 (AGND) GND (Maker pHAT)
15 (AREF)3.3V (Maker pHAT)
16 (VDD) 3.3V (Maker pHAT)

2. Potentiometer

Potentiometer PinsConnections
13.3V (Maker pHAT)
2CH0 (MCP3008)
3GND (Maker pHAT)

3. LM35

LM35 PinsConnections
15V (Maker pHAT)
2CH1 (MCP3008)
3GND (Maker pHAT)

Software Preparation

First, you need to enable I2C and SPI module in Raspberry Pi. Then instal MCP3008 library from Adafruit. Write following command on Terminal.

sudo apt-get update
sudo apt-get install build-essential python-dev python-smbus git
git clone https://github.com/adafruit/Adafruit_Python_MCP3008.git
cd Adafruit_Python_MCP3008
sudo python setup.py install

Sample Code

This is the sample code to display calculated data on I2C LCD.

import I2C_LCD_driver
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
from time import sleep
mylcd = I2C_LCD_driver.lcd()
# Hardware SPI configuration:
SPI_PORT = 0
SPI_DEVICE = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
try:
while True:
adcPot = mcp.read_adc(0)
voltage = adcPot * 0.00322
mylcd.lcd_display_string("Pot:%.2fV " % voltage, 1)
adcLm35 = mcp.read_adc(1)
celsius = adcLm35 * 0.322
mylcd.lcd_display_string("LM35:%.2f%sC" % (celsius, chr(223)), 2)
sleep(0.5)
except KeyboardInterrupt:
mylcd.lcd_clear()
view raw MCP3008_I2CLCD.py hosted with ❤ by GitHub

Thank you

References

  1. Adafruit – Raspberry Pi Analog to Digital Converters
  2. How to Display on I2C LCD Using Raspberry Pi

Thanks for reading this tutorial. If you have any technical inquiry, please post at Cytron Technical Forum.

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

In-Home Security System using Maker Uno.
Learn to program Arduino using Tinkercad Circuit.
DIY Digital Alarm Clock Using REKA:BIT With Micro:bit
Display Internet Time (NTP) on micro:bit
DIY Interactive Robot Using REKA:BIT With Micro:bit
Tutorials of Cytron Technologies Scroll to Top