0

I am trying to build Raspberry Pi based Oscilloscope.I am using raspberry Pi 3 B+ with Python 3.5 installed.I followed below link to perform this practical

https://circuitdigest.com/microcontroller-projects/raspberry-pi-based-oscilloscope/

My python script is executed successfully,as follows

reading ADS channel 0: value

enter image description here

But problem is unable to display graph on screen.I already installed matplotlib and drawnow packages .

I want to display graph on screen.As I am not aware about this much, so please help me to resolve this.

Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
Manorama
  • 39
  • 1
  • 9
  • Can you not invoke any matplotlib plot to show or is this specific to this script in use? I.e. if you run `python -c "import matplotlib.pyplot as plt; plt.plot([1,2,3]); plt.show()"`, would you also not see any plot window? – ImportanceOfBeingErnest Aug 11 '18 at 10:44
  • I created def makeFig function ,in that include following lines "plt.ylim(-5000,5000); plt.title('Osciloscope'); plt.grid(True); plt.ylabel('ADC outputs'); plt.plot(val, 'ro-', label='lux'); plt.legend(loc='lower right') " after this I called above function using "drawnow(makefig)" drawnow() is used to draw/plot live graphs. – Manorama Aug 11 '18 at 11:29
  • Yes I know. And it's not working in your case. That is why I was asking what happens if you type the line from my first comment into the terminal. – ImportanceOfBeingErnest Aug 11 '18 at 11:37
  • okk..if I typed your line in terminal,then is shows graph on screen (a slant line).I also tried same trick earlier,in simple examples for graph demo ,there was no issue.Particularly in this oscilloscope, graph was missing ,so I am confused. – Manorama Aug 11 '18 at 17:23

1 Answers1

0

It's hard to know from the outside what exactly the problem could be. I do have some doubts about the usefulness of the drawnow package. So I would leave that one out and see if a usual animation works.

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
#import numpy
# Import the ADS1x15 module.
import Adafruit_ADS1x15
# Create an ADS1115 ADC (16-bit) instance.
adc = Adafruit_ADS1x15.ADS1115()

GAIN = 1
val = [ ]

# Start continuous ADC conversions on channel 0 using the previous gain value.
adc.start_adc(0, gain=GAIN)
print('Reading ADS1x15 channel 0')

fig, ax = plt.subplots()
ax.set_ylim(-5000,5000)
ax.set_title('Osciloscope')
ax.grid(True)
ax.set_ylabel('ADC outputs')

line, = ax.plot([], 'ro-', label='Channel 0')
ax.legend(loc='lower right')

def update(cnt):
    # Read the last ADC conversion value and print it out.
    value = adc.get_last_result()
    print('Channel 0: {0}'.format(value))
    # Set new data to line
    line.set_data(list(range(len(val))), val)
    ax.relim()
    ax.autoscale_view()
    #Store values for later
    val.append(int(value))
    if(cnt>50):
        val.pop(0)

ani = FuncAnimation(fig, update, interval=500)
plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • I tried your above script.but again it gives me some error."_tkinter.TclError: couldn't connect to display ":10.0" .To run above script,Is it required to install any other libraries or packages in Raspberry Pi?? – Manorama Aug 11 '18 at 18:00
  • No. If you can show a normal plot, this script should also work. So you are getting no error when showing a simple plot, but you get this error when running the above script? – ImportanceOfBeingErnest Aug 11 '18 at 18:09
  • yes.simple graph scripts works without errors.But when I run your above script I got below error `Reading ADS1x15 channel 0 No protocol specified No protocol specified ........ File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1823, in __init__ self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use) _tkinter.TclError: couldn't connect to display ":10.0" ` – Manorama Aug 11 '18 at 18:17
  • That error comes from the ADC, while the previous error comes from the plotting window. Which one is it then? – ImportanceOfBeingErnest Aug 11 '18 at 18:20
  • previously I am executing your script with **sudo command** ,So it gives me above errors.But just now I executed same script without sudo,and it works.I used command `python demp.py`.I displays graph on screen.Thank u. But again now I have query , why it works without sudo. – Manorama Aug 11 '18 at 18:28
  • and even when i try to execute same script graphically it gives error `RuntimeError: module compiled against API version 0xc but this version of numpy is 0xa` **But on Console same script works fine.**without any errors. Why so ?? – Manorama Aug 11 '18 at 18:33
  • What does "execute same script graphically" mean? – ImportanceOfBeingErnest Aug 11 '18 at 18:34
  • using Python IDLE – Manorama Aug 11 '18 at 18:38
  • any ways..thank you so much for this help. is animation is replacement for drawnow? – Manorama Aug 11 '18 at 18:40
  • Animation existed before `drawnow`. I do not know why drawnow was written (it's not part of matplotlib) and it seems to cause more problems than it solves. So my intuition is that it's completely unnecessary. – ImportanceOfBeingErnest Aug 11 '18 at 18:42