Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Control servo position and read distance sensor using GUI.
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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment