Page 123a
import gpiod
from time import sleep
chip = gpiod.Chip("0")
line = chip.get_line(4)
line.request(consumer="myprog.py", type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0])
period = 20
duty = 25
ontime = period/1000 * duty / 100
offtime = period/1000 - ontime
while(True):
line.set_value(1)
sleep(ontime)
line.set_value(0)
sleep(offtime)
Remember to change "0" to "4" if you are running on a Pi 5.
Page 123b
import gpiod
from time import sleep
import threading
def softPWM(line, period, duty):
ontime = period/1000 * duty / 100
offtime = period/1000 - ontime
while(True):
line.set_value(1)
sleep(ontime)
line.set_value(0)
sleep(offtime)
chip = gpiod.Chip("0")
line = chip.get_line(4)
line.request(consumer="myprog.py", type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0])
period = 20
duty = 75
IntThread = threading.Thread(target=softPWM, args=(line, period, duty))
IntThread.start()
while(True):
print("working", flush=True)
sleep(2)
Remember to change "0" to "4" if you are running on a Pi 5.
Page 127
import subprocess
import io
def checkPWM():
indicator = "pwm-2chan"
command = ["sudo", "dtoverlay", "pwm-2chan"]
temp = subprocess.Popen(["sudo", "dtparam", "-l"], stdout=subprocess.PIPE)
output = str(temp.communicate())
print(output)
if output.find(indicator) == -1:
temp = subprocess.Popen(command, stdout=subprocess.PIPE)
output = str(temp.communicate())
print(output)
return
checkPWM()
fdw = io.open("/sys/class/pwm/pwmchip0/export", "wb", buffering=0)
fdw.write(b"0")
fdw.close()
fdw = io.open("/sys/class/pwm/pwmchip0/pwm0/period", "wb", buffering=0)
fdw.write(b"10000000")
fdw.close()
fdw = io.open("/sys/class/pwm/pwmchip0/pwm0/duty_cycle", "wb", buffering=0)
fdw.write(b"8000000")
fdw.close()
fdw = io.open("/sys/class/pwm/pwmchip0/pwm0/enable", "wb", buffering=0)
fdw.write(b"1")
fdw.close()
Page 128
import subprocess
import io
def checkPWM():
indicator = "pwm-2chan"
command =["sudo", "dtoverlay", "pwm-2chan","pin=18", "func=2", "pin2=12","func2=4"]
temp = subprocess.Popen(["sudo", "dtparam", "-l"], stdout = subprocess.PIPE)
output = str(temp.communicate())
print(output)
if output.find(indicator)!=-1:
temp = subprocess.Popen(["sudo","dtoverlay", "-R", "pwm-2chan"], stdout = subprocess.PIPE)
temp = subprocess.Popen(command, stdout = subprocess.PIPE)
output = str(temp.communicate())
print(output)
return
checkPWM()
fdw = io.open("/sys/class/pwm/pwmchip2/export", "wb", buffering=0)
fdw.write(b"0")
fdw.close()
fdw = io.open("/sys/class/pwm/pwmchip2/pwm0/period","wb", buffering=0)
fdw.write(b"10000000")
fdw.close()
fdw = io.open("/sys/class/pwm/pwmchip2/pwm0/duty_cycle", "wb", buffering=0)
fdw.write(b"8000000")
fdw.close()
fdw = io.open("/sys/class/pwm/pwmchip2/pwm0/enable", "wb", buffering=0)
fdw.write(b"1")
fdw.close()
fdw = io.open("/sys/class/pwm/pwmchip2/export", "wb", buffering=0)
fdw.write(b"2")
fdw.close()
fdw = io.open("/sys/class/pwm/pwmchip2/pwm2/period", "wb", buffering=0)
fdw.write(b"10000000")
fdw.close()
fdw = io.open("/sys/class/pwm/pwmchip2/pwm2/duty_cycle", "wb", buffering=0)
fdw.write(b"8000000")
fdw.close()
fdw = io.open("/sys/class/pwm/pwmchip2/pwm2/enable","wb", buffering=0)
fdw.write(b"1")
fdw.close()
Page 130
import subprocess
import io
from time import sleep
import os
Pi5=True
class Pwm:
def __init__(self, channel,f,duty,Pi5=False):
if not(channel==0 or channel==1):
return
if Pi5:
self.path="/sys/class/pwm/pwmchip2/"
else:
self.path="/sys/class/pwm/pwmchip0/"
self.chan = str(channel)
indicator = "pwm-2chan pin=12 func=4 pin2=13 func2=4"
command =["sudo", "dtoverlay", "pwm-2chan","pin=12", "func=4", "pin2=13","func2=4"]
temp = subprocess.Popen(["sudo", "dtparam", "-l"], stdout = subprocess.PIPE)
output = str(temp.communicate())
print(output)
if output.find(indicator)==-1:
temp = subprocess.Popen(command, stdout = subprocess.PIPE)
output = str(temp.communicate())
print(output)
if not(os.path.exists(self.path+"pwm"+self.chan)):
fdw = io.open(self.path+"export", "w")
fdw.write(self.chan)
fdw.close()
while not(os.path.exists(self.path+"pwm"+self.chan+ "/enable")):
pass
self.fdwp = io.open(self.path+"pwm"+self.chan+ "/period", "w")
self.setFreq(f)
self.fdwd = io.open(self.path+"pwm"+self.chan+ "/duty_cycle", "w")
self.setDuty(duty)
def setFreq(self,f):
self.f=int(1000000000/f)
self.fdwp.write(str(self.f))
self.fdwp.flush()
def setDuty(self,duty):
self.duty=int(self.f*duty/100)
self.fdwd.write(str(self.duty))
self.fdwd.flush()
def enableChan(self):
fdw = io.open(self.path+"pwm"+self.chan+"/enable", "w")
fdw.write("1")
fdw.close()
def disableChan(self):
fdw = io.open(self.path+"pwm"+self.chan+"/enable", "w")
fdw.write("0")
fdw.close()
def closeChan(self):
self.fdwd.close()
self.fdwp.close()
fdw = io.open(self.path+"unexport", "w")
fdw.write(self.chan)
fdw.close()
Page 141
import subprocess
import io
from time import sleep
import os
Pi5=True
class Pwm:
def __init__(self, channel,f,duty,Pi5=False):
if not(channel==0 or channel==1):
return
if Pi5:
self.path="/sys/class/pwm/pwmchip2/"
else:
self.path="/sys/class/pwm/pwmchip0/"
self.chan = str(channel)
indicator = "pwm-2chan"
command =["sudo", "dtoverlay", "pwm-2chan"]
temp = subprocess.Popen(["sudo", "dtparam", "-l"], stdout = subprocess.PIPE)
output = str(temp.communicate())
print(output,flush=True)
indicator = "pwm-2chan pin=12 func=4 pin2=13 func2=4"
command =["sudo", "dtoverlay", "pwm-2chan"]
temp = subprocess.Popen(["sudo", "dtparam", "-l"], stdout = subprocess.PIPE)
output = str(temp.communicate())
print(output,flush=True)
command =["sudo", "dtoverlay", "pwm-2chan","pin=12", "func=4", "pin2=13","func2=4"]
temp = subprocess.Popen(["sudo", "dtparam", "-l"], stdout = subprocess.PIPE)
output = str(temp.communicate())
print(output)
if output.find(indicator)==-1:
temp = subprocess.Popen(command,
stdout = subprocess.PIPE)
output = str(temp.communicate())
print(output)
if not(os.path.exists(self.path+"pwm"+self.chan)):
fdw = io.open(self.path+"export", "w")
fdw.write(self.chan)
fdw.close()
while not(os.path.exists(self.path+"pwm"+self.chan+"/enable")):
pass
self.fdwp = io.open(self.path+"pwm"+self.chan+"/period", "w")
self.setFreq(f)
self.fdwd = io.open(self.path+"pwm"+self.chan+"/duty_cycle", "w")
self.setDuty(duty)
def setFreq(self,f):
self.f=int(1000000000/f)
t=str(self.f)
self.fdwp.write(str(self.f))
self.fdwp.flush()
def setDuty(self,duty):
self.duty=int(self.f*duty/100)
self.fdwd.write(str(self.duty))
self.fdwd.flush()
def enableChan(self):
fdw = io.open(self.path+"pwm"+self.chan+"/enable", "w")
fdw.write("1")
fdw.close()
def disableChan(self):
fdw = io.open(self.path+"pwm"+self.chan+"/enable", "w")
fdw.write("0")
fdw.close()
def closeChan(self):
self.fdwd.close()
self.fdwp.close()
fdw = io.open(self.path+"unexport", "w")
fdw.write(self.chan)
fdw.close()
def inverChan(self):
fdw = io.open(self.path+ "pwm"+self.chan+"/polarity", "w")
fdw.write("inversed")
fdw.close()
def normalChan(self):
fdw = io.open(self.path+ "pwm"+self.chan+"/polarity", "w")
fdw.write("normal")
fdw.close(