Control Servo and Display Sensor’s Reading Using GUI on Raspberry Pi

This time, we will try to include actuator control and sensor’s reading in GUI. From the interface, user is able to control servo motor position and reading sensor’s data.

Introduction

This time, we will try to include actuator control and sensor’s reading in GUI. From the interface, user is able to control servo motor position and reading sensor’s data.

Video

This video will show how to control servo and display sensor’s reading using GUI on Raspberry Pi.

Hardware Preparation

This is the list of items used in the video.

Sample Program

This is the sample code to control servo and display sensor’s reading using GUI on Raspberry Pi.

from gpiozero import Servo, DistanceSensor
from guizero import App, Box, Text, PushButton, Slider
from time import sleep
servo = Servo(17, 0, 0.0005, 0.0025)
sensor = DistanceSensor(echo=18, trigger=27)
def ServoPosition(slider_value):
servo.value = int(slider_value) / 90
def my_user_task():
print("Distance: {0:.2f}cm".format(sensor.distance * 100))
reading_text.value = "{0:.2f}".format(sensor.distance * 100)
app = App(title="Servo GUI", width=350, height=150, layout="auto")
instruction_text = Text(app, text="Drag slider below to control servo position.")
instruction_text.repeat(1000, my_user_task)
servo_position = Slider(app, command=ServoPosition, start=-90, end=90, width='fill')
distance_text = Text(app, text="Distance (cm):")
reading_text = Text(app, text="---")
designby_text = Text(app, text="Idris - Cytron Technologies", align='bottom')
app.display()

Thank You

References:

  1. E Raspberry Pi: Getting started with GUIs
  2. GUI library package – guizero
  3. Library to control GPIO on Raspberry Pi – gpiozero

Thanks for reading this tutorial. If you have any technical inquiries, please post at Cytron Technical Forum.

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

BLTouch Installation for Ender 3 with 32-bit V4.2.2 Board
Pick and Send Random Meal’s Option and Locations through Telegram Bot Using Grove WiFi 8266 on micro:bit
DIY Automated Vacuum Cleaner Using REKA:BIT With Micro:bit
Rainbow Spark in Mini House using Maker Uno.
TinyML on Arduino using Edge Impulse
Tutorials of Cytron Technologies Scroll to Top