This article describes developing a GUI program for displaying ADC data on raspberry PI using Python and QT. The development environment uses PyCharm to develop Python code remotely, and then QtCreator to write the QML interface.

1. New projects

1.1. New construction project

Open PyCharm and create a new project adcMeasure as follows:

1.2. Add the Python main program

Adcmeasure. py main program is as follows:

import os
import sys
from pathlib import Path

from PySide2.QtCore import Qt, QObject, Slot
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtWidgets import QApplication

from adc_mcp3424 import MCP3424


class Controler(QObject) :
    def __init__(self) :
        super().__init__()

        self.MCP3424 = MCP3424(6, address=0x68, rate=18)

    @Slot()
    def exit(self) :
        sys.exit()

    @Slot(result=float)
    def get_adc1(self) :
        adc1 = self.MCP3424.read_raw(1)
        adc1 = adc1/100.00
        # print("adc1:",adc1)
        return adc1
    
......


if __name__=='__main__':
    a = QApplication(sys.argv)
    a.setOverrideCursor(Qt.BlankCursor)

    engine = QQmlApplicationEngine()

    controler = Controler()
    context = engine.rootContext()
    context.setContextProperty("_Controler", controler)

    engine.load(os.fspath(Path(__file__).resolve().parent / "ui/adcMeasure.qml"))

    if not engine.rootObjects():
        sys.exit(-1)

    sys.exit(a.exec_())
Copy the code
  • Create a Controler class to obtain ADC values and pass them to the GUI for display.
1.3. Add interface files
  • Add the UI folder to the project and create a new main.qml file.
import QtQuick 2.11
import QtQuick.Window 2.4
import QtQuick.Controls 2.4
import QtQuick.Controls.Styles 1.4
import QtQuick.Extras 1.4
import QtGraphicalEffects 1.0
import QtCharts 2.2

ApplicationWindow{
    id:root
    width: 800
    height: 480
    visible: true
//    visibility: Window.FullScreen

    background: Rectangle{
        anchors.fill: parent
        color: "# 101010"
    }

    Button{
        id:btnexit
        background: Rectangle{
            color: "#a01010"
            anchors.fill: parent
            radius:12
        }
        width: 48
        height: 48
        anchors{
            top: parent.top
            right: parent.right
            topMargin: 8
            rightMargin: 8
        }
        Text {
            text: qsTr("X")
            anchors.centerIn: parent
            font{
                pointSize: 32
            }
            color: "white"
        }
        onClicked: {
            _Controler.exit(a); } } Text { id: title text:qsTr("ADC Measure")
        anchors{
            top:  parent.top
            horizontalCenter: parent.horizontalCenter
            topMargin: 20
        }
        font{
            pointSize: 24
        }
        color: "#a0a0a0"
    }

    ChartView{
        id:cv
        width:600
        height:400

        anchors{
            top:title.bottom
            topMargin:10
            left:parent.left
            leftMargin:40
        }
        antialiasing: true
        theme: ChartView.ChartThemeDark

        property int  timcnt: 0
        property double  valueCH1: 0
        property double  valueCH2: 0
        property double  valueCH3: 0
        property double  valueCH4: 0

        ValueAxis{
            id:xAxis
            min: cv.timcnt < 10 ? 0 : cv.timcnt - 10
            max: cv.timcnt < 10 ? 10 : cv.timcnt + 1
            tickCount: 11
            labelFormat: "%d"
        }

        ValueAxis{
            id:yAxis
            min: 0
            max: 500
            tickCount: 1
            labelFormat: "%d"
        }

        LineSeries {
            name: "CH1"
            id:lines1
            axisX: xAxis
            axisY: yAxis
            width: 3
            color: "#1267D4"
        }

        LineSeries {
            name: "CH2"
            id:lines2
            axisX: xAxis
            axisY: yAxis
            width: 3
            color: "#8D7A1F"
        }

        LineSeries {
            name: "CH3"
            id:lines3
            axisX: xAxis
            axisY: yAxis
            width: 3
            color: "#8A1E1D"
        }

        LineSeries {
            name: "CH4"
            id:lines4
            axisX: xAxis
            axisY: yAxis
            width: 3
            color: "#C21FE4"
        }

        Timer{
            id:tm
            interval: 1000
            repeat: true
            running: true
            onTriggered: {
                cv.timcnt = cv.timcnt + 1
                cv.valueCH1 = _Controler.get_adc1()
                cv.valueCH2 = _Controler.get_adc2()
                cv.valueCH3 = _Controler.get_adc3()
                cv.valueCH4 = _Controler.get_adc4()

                lines1.append(cv.timcnt,cv.valueCH1)
                lines2.append(cv.timcnt,cv.valueCH2)
                lines3.append(cv.timcnt,cv.valueCH3)
                lines4.append(cv.timcnt,cv.valueCH4)

                console.log("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --")
                console.log("qml adc value1:",cv.valueCH1)
                console.log("qml adc value2:",cv.valueCH2)
                console.log("qml adc value3:",cv.valueCH3)
                console.log("qml adc value4:",cv.valueCH4)

            }
        }
    }
}

Copy the code
  • The ChartView module is used to display ADC values, and a Timer Timer is established to obtain ADC values once per second and then add them to the curve for display.

2. Execute the program

2.1. Upload the program to Raspberry PI

Right-click on the project and upload the project file to raspberry PI:

2.2. ADC Hardware

The Raspberry PI connects to an I2C interface ADC module, the MCP3424 used here, which has a four-channel maximum accuracy of 18 bits. Four adjustable rheostat devices are connected to the four channels of the ADC to test the changing analog values by adjusting the resistance.

2.3. Execution procedures

After uploading, run the following command in the raspberry PI folder to execute the program:

python3 adcMeasure.py
Copy the code

The following output is displayed:

When adjusting the resistance, you can see the curve:

Complete code:

github

  • Video effect:

www.bilibili.com/video/BV1jM…