Python PWM GUI example

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

Code

Save this as pwmgui.py

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()

 

Testing

Run the following by the typing in

sudo python pwmgui.py

SHARE
Previous articleBasic Python GPIO GUI example
Next articleRaspberry Pi Sense HAT