Please note: Friday June 18 is a holiday celebrating Juneteenth, please allow extra time for your order to arrive and plan accordingly.
0

Python Plotting problem
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Python Plotting problem

by BillPretty on Sun Mar 14, 2021 3:46 pm

I am having problems with the following code, running on a Raspberry pi 3
I can plot the graphs individually so I know the hardware is working.
When I run this code I see four graphs but nothing is plotted.

Code:
Code: Select all | TOGGLE FULL SIZE
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, axis = plt.subplots(2, 2)

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

while True:
    x= time()   
   
    # plot temperature
    temp = cpu.temperature
    axis[0,0].plot(x,temp)
    plt.draw()
    print ('Core Temperature:' + str(temp))
    plt.pause(1)

    # plot +5V
    adc=chan0.voltage
    axis[0,1].plot(x,chan0.voltage)
    plt.draw()
    print('ADC Voltage: ' + str(chan0.voltage) + 'V')
    plt.pause(1)

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

Re: Python Plotting problem

by BillPretty on Wed Mar 17, 2021 2:57 pm

Well folks I got the software working.
The hardware uses a two 4.99K 1% resistors as a voltage divider, to measure the 5 volt input.
The user can easily use the same type of divider to measure the 3.3 volt rail.

The working code is shown below:

Code:

#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(2)

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

plt.ion()

x1 = []
x2 = []

y1 = []
y2 = []


while True:

# plot temperature
temp = cpu.temperature
y1.append(temp)
x1.append(time())
axs[0].set_title("Core Temperature")
axs[0].plot(x1,y1)
print ('Core Temperature:' + str(temp))


# plot +5V
# Get the ADC measurement X2 for voltage divider
adc=chan0.voltage * 2
y2.append(adc)
x2.append(time())
axs[1].set_title("5V Rail")
axs[1].plot(x2,y2)
print('ADC Voltage: ' + str(adc) + 'V')
plt.pause(1)

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

Please be positive and constructive with your questions and comments.