0

MCP3008 project complete
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

MCP3008 project complete

by BillPretty on Fri Apr 09, 2021 4:32 pm

I have completed my interface and plotting project, and thought I should share the results with the group.
I have included a schematic and opensource code associated with the project.
As far as the schematic is concerned, Resistors R1 and R2 form a voltage divider which is used to measure the +5V input voltage.
Capacitor C1 is used to form a crude low pass filter. The actual value of C1 will depend on how much ripple is on your 5V supply.
The same configuration is used to measure the 3.3V rail. The circuit is powered by a 4.5V to 6V battery pack.

Code: Select all | TOGGLE FULL SIZE
#sudo apt-get install python3-matplotlib
#sudo apt-get install python3-gi-cairo
#sudo pip3 install adafruit-circuitpython-mcp3xxx
#sudo pip3 install adafruit-blinka

from gpiozero import CPUTemperature
from time import sleep, strftime, time
import matplotlib.pyplot as plt
import os
import busio
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn

# create the spi bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)

# create the cs (chip select)
cs = digitalio.DigitalInOut(board.D22)

# create the mcp object
mcp = MCP.MCP3008(spi, cs)

# create cpu object
cpu = CPUTemperature()

# Initialise the subplot function using number of rows and columns
figure, axs = plt.subplots(3)

# create an analog input channel on pin 0
chan0 = AnalogIn(mcp, MCP.P0)

# create an analog input channel on pin 1
chan1 = AnalogIn(mcp, MCP.P1)

plt.ion()

x1 = []
x2 = []
x3 = []

y1 = []
y2 = []
y3 = []

while True:
   
    # Plot temperature
    # Round off to nearest .1C
    temp = round(cpu.temperature,1)
    y1.append(temp)
    x1.append(time())
    axs[0].set_title("Core Temperature")
    axs[0].plot(x1,y1, 'tab:red')
    print ('Core Temperature:' + str(temp) + 'C')

    # Plot +5V
    # Get the ADC measurement and X2 for voltage divider
    # Round off to two decimal places
    adc=round(chan0.voltage,2) * 2
    y2.append(adc)
    x2.append(time())
    axs[1].set_title("5V Rail")
    axs[1].plot(x2,y2, 'tab:blue')
    print('ADC 0 Voltage: ' + str(adc) + 'V')

    # Plot +3.3V
    # Get the ADC measurement and X2 for voltage divider
    # Round off to two decimal places
    adc=round(chan1.voltage,2) * 2
    y3.append(adc)
    x3.append(time())
    axs[2].set_title("3.3V Rail")
    axs[2].plot(x3,y3, 'tab:blue')
    print('ADC 1 Voltage: ' + str(adc) + 'V')
   
    # Hide x labels and ticks for plots
    for ax in axs.flat:
        ax.label_outer()
       
    plt.pause(1)




[img]
Schematic.jpg
Schematic.jpg (43.96 KiB) Viewed 2927 times

[/img]

BillPretty
 
Posts: 21
Joined: Sun Mar 07, 2021 2:44 pm

Re: MCP3008 project complete

by BillPretty on Sun May 16, 2021 3:31 pm

Figure_4.png
Plot of stress test
Figure_4.png (29.48 KiB) Viewed 1988 times


This is a plot I captured while running a stress test test on the Pi 3.
The software running on the Pi was a video motion capture program, which uses the new Pi Camera.

BillPretty
 
Posts: 21
Joined: Sun Mar 07, 2021 2:44 pm

Please be positive and constructive with your questions and comments.