Home Learning Python PWM GUI example on a Raspberry PI

Python PWM GUI example on a Raspberry PI

by shedboy71

In this example we create a GUI to set the value for our PWM example – Controlling the Brightness of an LED

 

Schematics

pi-pwm-led_bb

Parts

Name Links
Raspberry Pi 4 Model B Development Board Aliexpress link

Amazon. com link

Ebay search

Starter kit (leds , resistors) SunFounder Super Starter Learning Kit V3.0 for Raspberry Pi
Connecting wire Aliexpress link

Amazon.com link

Ebay link

Code

Save this as pwmgui.py

[codesyntax lang=”python”]

from Tkinter import *
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 500)
pwm.start(100)

class App:
	def __init__(self, master):
		frame = Frame(master)
		frame.pack()
		scale = Scale(frame, from_=0, to=100, orient=HORIZONTAL, command=self.update)
		scale.grid(row=0)

	def update(self, duty):
		pwm.ChangeDutyCycle(float(duty))
		
root = Tk()
root.wm_title('PWM Example')
app = App(root)
root.geometry("200x50+0+0")
root.mainloop()

[/codesyntax]

 

Testing

Run the following by the typing in

sudo python pwmgui.py

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More