Corlotean CatalinAlin Budeanu
Published

Application for distance measurement

It is an desktop application for distance measurement with RGB LED indicator.

BeginnerShowcase (no instructions)417
Application for distance measurement

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Resistor 1k ohm
Resistor 1k ohm
×1
Resistor 220 ohm
Resistor 220 ohm
×3
Resistor 2.21k ohm
Resistor 2.21k ohm
×1
RGB Diffused Common Cathode
RGB Diffused Common Cathode
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×9

Software apps and online services

Raspbian
Raspberry Pi Raspbian

Story

Read more

Schematics

Schematic using Fritzing

Breadboard

Code

main.py

Python
import sys

from PyQt5 import QtWidgets, QtGui
from PyQt5.QtCore import QSize, Qt, QTimer
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QMainWindow, QLabel, QLineEdit, QMessageBox, QApplication
from PyQt5.QtWidgets import QPushButton

import RPi.GPIO as GPIO
from gpiozero import RGBLED
import time
import signal


class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.init_UI()
        
        GPIO.setmode(GPIO.BCM)
        self.red_margin = [0] * 2
        self.yellow_margin = [0] * 2
        self.green_margin = [0] * 2

        self.pinTrigger = 18
        self.pinEcho = 24
        self.pinRed = 17
        self.pinGreen = 27
        self.pinBlue = 22
        self.ledRGB = RGBLED(17, 27, 22)
        self.colorRed = (1, 0, 0)
        self.colorGreen = (0, 1, 0)
        self.colorYellow = (1, 1, 0)
        self.distance = 0
        GPIO.setup(self.pinTrigger, GPIO.OUT)
        GPIO.setup(self.pinEcho, GPIO.IN)

    def init_UI(self):
        self.setMinimumSize(QSize(800, 270))
        self.setWindowTitle("Application for distance measurement")

        button_start = QPushButton('Start', self)
        button_start.clicked.connect(self.start_measure)
        button_start.setStyleSheet("background-color: green")
        button_start.resize(100, 32)
        button_start.move(350, 10)

        button_set_red = QPushButton('Set', self)
        button_set_red.clicked.connect(self.set_red_parameters)
        button_set_red.resize(60, 32)
        button_set_red.move(300, 72)

        button_set_yellow = QPushButton('Set', self)
        button_set_yellow.clicked.connect(self.set_yellow_parameters)
        button_set_yellow.resize(60, 32)
        button_set_yellow.move(300, 132)

        button_set_green = QPushButton('Set', self)
        button_set_green.clicked.connect(self.set_green_parameters)
        button_set_green.resize(60, 32)
        button_set_green.move(300, 192)

        # red
        label_red = QLabel(self)
        label_red.setText('Red')
        label_red.setFont(QFont("Arial", 20, QFont.Black))
        label_red.setStyleSheet("color: red")
        label_red.move(50, 70)

        label_red_line = QLabel(self)
        label_red_line.setText('-')
        label_red_line.setFont(QFont("Arial", 20, QFont.Black))
        label_red_line.setStyleSheet("color: red")
        label_red_line.move(205, 70)

        self.textbox_red_left = QLineEdit(self)
        self.textbox_red_left.setFont(QFont("Arial", 18, QFont.Black))
        self.textbox_red_left.setText("0")
        self.textbox_red_left.resize(65, 30)
        self.textbox_red_left.move(130, 73)

        self.textbox_red_right = QLineEdit(self)
        self.textbox_red_right.setFont(QFont("Arial", 18, QFont.Black))
        self.textbox_red_right.setText("0")
        self.textbox_red_right.resize(65, 30)
        self.textbox_red_right.move(225, 73)

        #yellow
        label_yellow = QLabel(self)
        label_yellow.setText('Yellow')
        label_yellow.setFont(QFont("Arial", 20, QFont.Black))
        label_yellow.setStyleSheet("color: #FFCD00")
        label_yellow.move(10, 130)

        label_yellow_line = QLabel(self)
        label_yellow_line.setText('-')
        label_yellow_line.setFont(QFont("Arial", 20, QFont.Black))
        label_yellow_line.setStyleSheet("color: #FFCD00")
        label_yellow_line.move(205, 130)

        self.textbox_yellow_left = QLineEdit(self)
        self.textbox_yellow_left.setFont(QFont("Arial", 18, QFont.Black))
        self.textbox_yellow_left.setText("0")
        self.textbox_yellow_left.resize(65, 30)
        self.textbox_yellow_left.move(130, 133)

        self.textbox_yellow_right = QLineEdit(self)
        self.textbox_yellow_right.setFont(QFont("Arial", 18, QFont.Black))
        self.textbox_yellow_right.setText("0")
        self.textbox_yellow_right.resize(65, 30)
        self.textbox_yellow_right.move(225, 133)
        #green
        label_green = QLabel(self)
        label_green.setText('Green')
        label_green.setFont(QFont("Arial", 20, QFont.Black))
        label_green.setStyleSheet("color: green")
        label_green.move(15, 190)

        label_green_line = QLabel(self)
        label_green_line.setText('-')
        label_green_line.setFont(QFont("Arial", 20, QFont.Black))
        label_green_line.setStyleSheet("color: green")
        label_green_line.move(205, 190)

        self.textbox_green_left = QLineEdit(self)
        self.textbox_green_left.setFont(QFont("Arial", 18, QFont.Black))
        self.textbox_green_left.setText("0")
        self.textbox_green_left.resize(65, 30)
        self.textbox_green_left.move(130, 193)

        self.textbox_green_right = QLineEdit(self)
        self.textbox_green_right.setFont(QFont("Arial", 18, QFont.Black))
        self.textbox_green_right.setText("0")
        self.textbox_green_right.resize(65, 30)
        self.textbox_green_right.move(225, 193)

        self.label_distance = QLabel(self)
        self.label_distance.setText('0 cm')
        self.label_distance.setFont(QFont("Arial", 70, QFont.Black))
        self.label_distance.setStyleSheet("color: red")
        self.label_distance.setAlignment(Qt.AlignCenter)
        self.label_distance.setMinimumSize(450, 150)
        self.label_distance.move(350, 72)
        self.show()
        
    def start_measure(self):
        self.timer = QTimer()
        self.timer.setInterval(500)
        self.timer.timeout.connect(self.update_distance)
        self.timer.start()
        while True:
            self.set_distance()
            QApplication.processEvents()
            
    def update_distance(self):
        self.set_led_color(self.distance)
        
    def set_red_parameters(self):
        try:
            self.red_margin[0] = self.parse_int(self.textbox_red_left.text())
            self.red_margin[1] = self.parse_int(self.textbox_red_right.text())
            self.textbox_yellow_left.setText(str(self.red_margin[1]))
        except Exception:
            QMessageBox.about(self, "Erroare", "Eroare: Date introduse gresit, ati folosit caractere sau numarul introdus este negativ")


    def set_yellow_parameters(self):
        try:
            self.yellow_margin[0] = self.parse_int(self.textbox_yellow_left.text())
            self.yellow_margin[1] = self.parse_int(self.textbox_yellow_right.text())
            self.textbox_red_right.setText(str(self.yellow_margin[0]))
            self.textbox_green_left.setText(str(self.yellow_margin[1]))
        except Exception:
            QMessageBox.about(self, "Erroare", "Eroare: Date introduse gresit, ati folosit caractere sau numarul introdus este negativ")


    def set_green_parameters(self):
        try:
            self.green_margin[0] = self.parse_int(self.textbox_green_left.text())
            self.green_margin[1] = self.parse_int(self.textbox_green_right.text())
            self.textbox_yellow_right.setText(str(self.green_margin[0]))
        except Exception:
            QMessageBox.about(self, "Erroare", "Eroare: Date introduse gresit, ati folosit caractere sau numarul introdus este negativ")


    def parse_int(self, string):
        string_int = int(string)
        if string_int < 0 :
            raise Exception('')
        return string_int

    def set_distance(self):
        GPIO.output(self.pinTrigger, True)
        
        time.sleep(0.00001)
        GPIO.output(self.pinTrigger, False)
        
        startTime = time.time()
        stopTime = time.time()
        
        while 0 == GPIO.input(self.pinEcho):
            startTime = time.time()

        while 1 == GPIO.input(self.pinEcho):
            stopTime = time.time()
        
        TimeElapsed = stopTime - startTime
        self.distance = int((TimeElapsed * 34300) / 2)


    def set_led_color(self, distance):
        if self.green_margin[0] <= distance and distance <= self.green_margin[1]:
            self.label_distance.setStyleSheet("color: green")
            self.label_distance.setText(str(distance) + " cm")
            self.ledRGB.color = self.colorGreen
        elif self.yellow_margin[0] <= distance and distance < self.yellow_margin[1]:
            self.label_distance.setStyleSheet("color: #FFCD00")
            self.label_distance.setText(str(distance) + " cm")
            self.ledRGB.color = self.colorYellow
        elif self.red_margin[0] <= distance and distance < self.red_margin[1] :
            self.label_distance.setStyleSheet("color: red")
            self.label_distance.setText(str(distance) + " cm")
            self.ledRGB.color = self.colorRed
        else:
            self.label_distance.setStyleSheet("color: black")
            self.label_distance.setText(str(distance) + " cm")
            self.ledRGB.color = self.colorGreen

    def closeEvent(self, event):
        GPIO.output(self.pinRed , False)
        GPIO.output(self.pinGreen, False)
        GPIO.output(self.pinBlue, False)
        GPIO.cleanup()
        event.accept()
        exit()



if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit( app.exec_() )

Credits

Corlotean Catalin
1 project • 1 follower
Alin Budeanu
0 projects • 0 followers

Comments