PWM Period = ON Time + OFF Time
Duty Cycle = (ON Time / Period) * 100
PWM Sequence of PWM3 | Channel No in PWM3 | Device |
---|---|---|
PWM16 |
0 |
Backlight |
PWM17 |
1 |
DC Motor |
PWM18 |
2 |
Buzzer |
/sys/class/pwm/ | -- pwmchip<base> | -- pwm<no> | -- period | -- duty_cycle ` -- enable
/sys/class/pwm/ | -- pwmchip<base> | -- pwm<no> | -- period | -- duty_cycle ` -- enable
$ echo 2 > /sys/class/pwm/pwmchip16/export
$ echo 2 > /sys/class/pwm/pwmchip16/unexport
$ echo 1000000 > /sys/class/pwm/pwmchip16/pwm2/period
$ echo 500000 > /sys/class/pwm/pwmchip16/pwm2/duty_cycle
$ echo 1 > /sys/class/pwm/pwmchip16/pwm2/enable
Following code will control the tone of buzzer.
import time ch = "2" pwm_path = "/sys/class/pwm/pwmchip16/" export_path = pwm_path+"export" unexport_path = pwm_path+"unexport" period_path = pwm_path+"pwm"+ch+"/period" duty_path = pwm_path+"pwm"+ch+"/duty_cycle" enable_path = pwm_path+"pwm"+ch+"/enable" export = open(export_path, "w") export.write(ch) export.close() period = open(period_path, "w") period.write("1000000") period.close() duty_cycle = open(duty_path, "w") duty_cycle.write("500000") duty_cycle.flush() enable = open(enable_path, mode="w") enable.write("1") enable.flush() time.sleep(5) duty_cycle.write("0") duty_cycle.close() enable.write("0") enable.flush() enable.close() unexport = open(unexport_path, "w") unexport.write(ch) unexport.close()
from os.path import exists __pwm_path = "/sys/class/pwm/pwmchip16/pwm{}" __pwm_export_path = "/sys/class/pwm/pwmchip16//export" __pwm_unexport_path = "/sys/class/pwm/pwmchip16/unexport" __pwm_period_path = "/sys/class/pwm/pwmchip16/pwm{}/period" __pwm_dutycycle_path = "/sys/class/pwm/pwmchip16/pwm{}/duty_cycle" __pwm_enable_path = "/sys/class/pwm/pwmchip16/pwm{}/enable" def cat(file): file = open(file, "r") value = file.read() file.close() return value def echo(value, file): file = open(file, "w") file.write(value) file.close() def pwm_export_channel(channel): if not exists(__pwm_path.format(channel)): echo(str(channel), __pwm_export_path) def pwm_unexport_channel(channel): if exists(__pwm_path.format(channel)): echo(str(channel), __pwm_unexport_path) def pwm_set_period(channel, period): echo(str(period), __pwm_period_path.format(channel)) def pwm_set_dutycycle(channel, dutycycle): echo(str(dutycycle), __pwm_dutycycle_path.format(channel)) def pwm_enable(channel): echo(str(1), __pwm_enable_path.format(channel)) def pwm_disable(channel): echo(str(0), __pwm_enable_path.format(channel))
#!/usr/bin/python import time from pwm import * BUZZER = 2 pwm_export_channel(BUZZER) melody = [379218, 379218, 100000, 379218, 100000, 477782, 379218, 100000, 318878, 100000, 100000, 100000, 637754, 100000, 100000, 100000, 477782, 100000, 100000, 637754, 100000, 100000, 75814, 100000, 100000, 568182, 100000, 506072, 100000, 536192, 568182, 100000, 637754, 379218, 379218, 284092, 100000, 35792, 318878, 100000, 379218, 100000, 477782, 100000, 379218, 100000, 477782, 425712, 506072, 100000, 100000, 379218, 379218, 100000, 379218, 100000, 477782, 379218, 100000, 318878, 100000, 100000, 100000, 637754, 100000, 100000, 100000, 284092, 100000, 35792, 318878, 100000, 379218, 100000, 477782, 100000, 379218, 100000, 477782, 425712, 506072, 100000, 100000, ] note = [0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.111, 0.111, 0.111, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.111, 0.111, 0.111, 0.111, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083, 0.083] count = 0 while count < 83: pwm_set_period(BUZZER, melody[count]) duty = melody[count]//2 pwm_set_dutycycle(BUZZER, duty) pwm_enable(BUZZER) time.sleep(note[count]) pwm_set_dutycycle(BUZZER, 0) time.sleep(note[count]) count += 1 pwm_unexport_channel(BUZZER)
#!/usr/bin/python from pwm import * import time MOTOR = 1 pwm_export_channel(MOTOR) period_ns = 10000000 pwm_set_period(MOTOR, period_ns) while True: for duty in [30, 100]: duty_ns = int(period_ns * duty / 100) pwm_set_dutycycle(MOTOR, duty_ns) pwm_enable(MOTOR) time.sleep(5) pwm_disable(MOTOR) pwm_unexport_channel(MOTOR)