0

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.

3
  • Can't run any of this to check, but you are only saving the most recent read? Make gainString (is it really a string?) a list gainString = [] and append the read value to it in your loop gainString.append(analog_read()). x_axis can then just be x_axis = range(len(gainString)). Let me know how that goes and post a screenshot of the wrong graph if you can. Commented Dec 22, 2017 at 9:03
  • I changed the code like you instructed, its given below. while True: print(analog_read()) #gainString = analog_read() gainString.append(analog_read()) x_axis = range(len(gainString)) #x_axis = x_axis + 1 #dataArray = gainString.split(',') #gain = float(dataArray[0]) #gainF.append(gain) makeFig() plt.pause(.000001) time.sleep(1) It created an output of an array like below 90 [195, 189] range(0, 2) 91 [195, 189, 96] range(0, 3) which keeps going, but the graph is still empty Commented Dec 26, 2017 at 4:31
  • @KenSyme I am getting this error at the beginning of the code, does it affect the program in any way? analog read is 180 gainString = [183] x_axis = [1] /usr/lib/python3/dist-packages/matplotlib/backend_bases.py:2445: MatplotlibDeprecationWarning: Using default event loop until function specific to this GUI is implemented warnings.warn(str, mplDeprecation) Commented Dec 26, 2017 at 5:27

1 Answer 1

1

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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.