In this example we create a GUI to set the value for our PWM example – Controlling the Brightness of an LED
Schematics
Parts
Name | Links |
Raspberry Pi 4 Model B Development Board | Aliexpress link |
Starter kit (leds , resistors) | SunFounder Super Starter Learning Kit V3.0 for Raspberry Pi |
Connecting wire | Aliexpress link |
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