Reputation: 111
I am using a Raspberry Pi to create a simple graph that shows analog readings from a potentiometer via the GPIO pins. I created a small circuit that can overcome the RPi's inability to read analog signal. There is a small problem with the plotting itself. The code I use is shown below.
# include RPi libraries in to Python code
import RPi.GPIO as GPIO
import time
import matplotlib.pyplot as plt
from drawnow import drawnow
# instantiate GPIO as an object
GPIO.setmode(GPIO.BCM)
# define GPIO pins with variables a_pin and b_pin
a_pin = 18
b_pin = 23
gainF = []
gainString = 0
plt.ion()
x_axis = 0
def makeFig():
plt.ylim(200,210)
plt.xlim(0,100)
plt.title('Readings')
plt.grid(True)
plt.ylabel('Gain')
print(gainString)
print(x_axis)
plt.plot(gainString, x_axis)
plt.show()
#plt.plot(gainString, 'ro-', label='Gain dBm')
# create discharge function for reading capacitor data
def discharge():
GPIO.setup(a_pin, GPIO.IN)
GPIO.setup(b_pin, GPIO.OUT)
GPIO.output(b_pin, False)
time.sleep(0.005)
# create time function for capturing analog count value
def charge_time():
GPIO.setup(b_pin, GPIO.IN)
GPIO.setup(a_pin, GPIO.OUT)
count = 0
GPIO.output(a_pin, True)
while not GPIO.input(b_pin):
count = count +1
return count
# create analog read function for reading charging and discharging data
def analog_read():
discharge()
return charge_time()
# provide a loop to display analog data count value on the screen
while True:
print(analog_read())
gainString = analog_read()
x_axis = x_axis + 1
#dataArray = gainString.split(',')
#gain = float(dataArray[0])
#gainF.append(gain)
makeFig()
plt.pause(.000001)
time.sleep(1)
#GPIO.cleanup()
This code displays the increasing x axis and the y axis readings from the makeFig() function, but the graph that opens up does not display anything. It remains the same. Anything I need to change in the code? Thanks.
Upvotes: 0
Views: 282
Reputation: 339230
You are attempting to plot a line plot of single values. This is the same as
plt.plot([1],[5])
which does not show up, because a line needs two points at least to become a line.
You may use a marker to display single points in case this is what you're after
plt.plot([1],[5], marker="o")
Upvotes: 1