Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. QtonPi
  4. Reading input frequency and drawing using pyqtgraph
QtWS25 Last Chance

Reading input frequency and drawing using pyqtgraph

Scheduled Pinned Locked Moved Unsolved QtonPi
8 Posts 3 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maanas
    wrote on last edited by
    #1

    I have to read a square waveform of frequency 100Hz to 20kHz on GPIO of raspberry. I have checked using piscope the waveform at the required GPIO. Now, I need to draw a GUI window with some information regarding this square waveform. After reading online at various sites, I have finalized pyqtgraph and now I am trying to learn through some examples. In following code, I append a random value at some fixed time to the curve.

    from PyQt5 import QtWidgets, QtCore
    from pyqtgraph.Qt import QtGui
    import pyqtgraph as pg
    import sys
    import os
    from random import randint
    
    class MainWindow(QtWidgets.QMainWindow):
        def __init__(self,*args,**kwargs):
            super(MainWindow,self).__init__(*args,**kwargs)
            
            self.graphWidget = pg.PlotWidget()
            self.setCentralWidget(self.graphWidget)
            
            pen = pg.mkPen(color='r', width=3)
    
            styles = {'color': '#f00', 'font-size': '20px'}
            self.graphWidget.setLabel('left', 'Temperature (°C)', **styles)
            self.graphWidget.setLabel('bottom', 'Hour (H)', **styles)
    
            self.graphWidget.setTitle("Frequency Plotting", color="b", size="30pt")
            
            # Add grid
            self.graphWidget.showGrid(x=True, y=True)
            self.x = list(range(100))  # 100 time points
            self.y = [randint(0, 100) for _ in range(100)]  # 100 data points
    
            self.lineCurve = self.graphWidget.plot(self.x, self.y,pen=pen)
            self.timer = QtCore.QTimer()
            self.timer.setInterval(100)
            self.timer.timeout.connect(self.update_plot_data)
            self.timer.start()
    
        def update_plot_data(self):
            # self.x = self.x[1:]  # Remove the first y element.
            # self.x.append(self.x[-1] + 1)  # Add a new value 1 higher than the last.
            
            self.y = self.y[1:]  # Remove the first
            self.y.append(randint(0, 100))  # Add a new random value.
    
            self.lineCurve.setData(self.x, self.y)  # Update the data.
    
    
    def main():
        app = QtWidgets.QApplication(sys.argv)
        window = MainWindow()
        window.show()
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()
    

    Now, I am stuck at how to combine reading square waveform frequency from GPIO and appending this to the array for updating to the waveform. Also, instead of single centralwidget, I require to add some labels with vertical layout.

    jsulmJ 1 Reply Last reply
    0
    • M Maanas

      I have to read a square waveform of frequency 100Hz to 20kHz on GPIO of raspberry. I have checked using piscope the waveform at the required GPIO. Now, I need to draw a GUI window with some information regarding this square waveform. After reading online at various sites, I have finalized pyqtgraph and now I am trying to learn through some examples. In following code, I append a random value at some fixed time to the curve.

      from PyQt5 import QtWidgets, QtCore
      from pyqtgraph.Qt import QtGui
      import pyqtgraph as pg
      import sys
      import os
      from random import randint
      
      class MainWindow(QtWidgets.QMainWindow):
          def __init__(self,*args,**kwargs):
              super(MainWindow,self).__init__(*args,**kwargs)
              
              self.graphWidget = pg.PlotWidget()
              self.setCentralWidget(self.graphWidget)
              
              pen = pg.mkPen(color='r', width=3)
      
              styles = {'color': '#f00', 'font-size': '20px'}
              self.graphWidget.setLabel('left', 'Temperature (°C)', **styles)
              self.graphWidget.setLabel('bottom', 'Hour (H)', **styles)
      
              self.graphWidget.setTitle("Frequency Plotting", color="b", size="30pt")
              
              # Add grid
              self.graphWidget.showGrid(x=True, y=True)
              self.x = list(range(100))  # 100 time points
              self.y = [randint(0, 100) for _ in range(100)]  # 100 data points
      
              self.lineCurve = self.graphWidget.plot(self.x, self.y,pen=pen)
              self.timer = QtCore.QTimer()
              self.timer.setInterval(100)
              self.timer.timeout.connect(self.update_plot_data)
              self.timer.start()
      
          def update_plot_data(self):
              # self.x = self.x[1:]  # Remove the first y element.
              # self.x.append(self.x[-1] + 1)  # Add a new value 1 higher than the last.
              
              self.y = self.y[1:]  # Remove the first
              self.y.append(randint(0, 100))  # Add a new random value.
      
              self.lineCurve.setData(self.x, self.y)  # Update the data.
      
      
      def main():
          app = QtWidgets.QApplication(sys.argv)
          window = MainWindow()
          window.show()
          sys.exit(app.exec_())
      
      
      if __name__ == '__main__':
          main()
      

      Now, I am stuck at how to combine reading square waveform frequency from GPIO and appending this to the array for updating to the waveform. Also, instead of single centralwidget, I require to add some labels with vertical layout.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Maanas Not sure what the problem is exactly?
      Can you already read GPIO?

      "I require to add some labels with vertical layout" - what is the problem with that?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Maanas
        wrote on last edited by
        #3

        I have read GPIO frequency using pigpio library. Now how to update it in pyqtgraph? The given example appends random value. But how to draw square waveform of the read frequency and also update it regularly.

        After setting centralWidget, I am not able to change layout or add vertical layout. How to do that?

        JonBJ jsulmJ 2 Replies Last reply
        0
        • M Maanas

          I have read GPIO frequency using pigpio library. Now how to update it in pyqtgraph? The given example appends random value. But how to draw square waveform of the read frequency and also update it regularly.

          After setting centralWidget, I am not able to change layout or add vertical layout. How to do that?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Maanas said in Reading input frequency and drawing using pyqtgraph:

          After setting centralWidget, I am not able to change layout or add vertical layout. How to do that?

          Set your central widget to e.g. a plain QWidget and then add desired layouts + sub-widgets on that as per normal.

          M 1 Reply Last reply
          1
          • M Maanas

            I have read GPIO frequency using pigpio library. Now how to update it in pyqtgraph? The given example appends random value. But how to draw square waveform of the read frequency and also update it regularly.

            After setting centralWidget, I am not able to change layout or add vertical layout. How to do that?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Maanas said in Reading input frequency and drawing using pyqtgraph:

            and also update it regularly

            I don't know how exactly you're reading GPIO. You could use a QTimer and add new values you cololected from GPIO to your graph on each timeout.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • M Offline
              M Offline
              Maanas
              wrote on last edited by
              #6

              I am only reading frequency value using pigpio module. I have to draw square waveform with updated frequency. The waveform display need not to be real time i.e. I can sample the frequency and later on change the waveform accordingly after some time. My query is how to draw this square waveform.

              1 Reply Last reply
              0
              • JonBJ JonB

                @Maanas said in Reading input frequency and drawing using pyqtgraph:

                After setting centralWidget, I am not able to change layout or add vertical layout. How to do that?

                Set your central widget to e.g. a plain QWidget and then add desired layouts + sub-widgets on that as per normal.

                M Offline
                M Offline
                Maanas
                wrote on last edited by
                #7

                @JonB Can you please give an example

                JonBJ 1 Reply Last reply
                0
                • M Maanas

                  @JonB Can you please give an example

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @Maanas
                  Of adding layouts and then widgets to a QWidget? No, you must know perfectly well how to do that. There's nothing to say.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved