air105#
This chapter describes how to use the pwm library for LuatOS
Introduction#
Pulse width modulation (PWM), is the English “Pulse Width Modulation” abbreviation, referred to as pulse width modulation, is the use of microprocessor digital output to control the analog circuit of a very effective technology. Simply, it is the control of pulse width.
Looking up Air105 chip data sheet_1.1.pdf, we can see that Air105 has 8 PWM channels
We can adjust the brightness of the LED small light by pwm different duty cycle output
Hardware preparation#
Air105 Development Board
LED small lamp
Wiring Schematic
PWM5 ------ +
Air105 LED
GND ------ -
Software part#
Interface documentation can be referred to:pwm library
Turn on pwm output#
Turn on the pwm output of the PWM5 channel, set different duty cycles, the small lights will have different brightness
PROJECT = "pwm"
VERSION = "1.0.0"
sys = require("sys")
sys.taskInit(function()
while true do
-- Turn on pwm channel 5, set the pulse frequency to 1kHz, the frequency division accuracy to 1000, and the duty cycle to 10/1000 = 1% continuous output
pwm.open(5, 1000, 10, 0, 1000) -- The small lamp glows slightly
sys.wait(1000)
-- Turn on pwm channel 5, set the pulse frequency to 1kHz, the frequency division accuracy to 1000, and the duty cycle to 500/1000 = 50% continuous output
pwm.open(5, 1000, 500, 0, 1000) -- Small light medium brightness
sys.wait(1000)
-- Turn on pwm channel 5, set the pulse frequency to 1kHz, the frequency division accuracy to 1000, and the duty cycle to 1000/1000=100 continuous output
pwm.open(5, 1000, 1000, 0, 1000) -- Small lamp with high brightness
sys.wait(1000)
end
end)
sys.run()
Breathing light effect#
The effect of breathing light is the effect that the small lamp is lit up little by little and extinguished little by little when it is never lit. We can continuously increase the duty cycle to make the small lamp light up little by little and then continuously decrease the duty cycle to make the small lamp extinguish little by little.
The code is as follows
PROJECT = "pwm"
VERSION = "1.0.0"
sys = require("sys")
sys.taskInit(function()
while true do
-- The duty cycle continuously improves the brightness of small lamps from 0% to 30%
for i = 0, 300 do
pwm.open(5, 1000, i, 0, 1000)
sys.wait(10)
end
sys.wait(1000)
-- Duty cycle continuously reduces the brightness of small lamps from 30% to 0%
for i = 300, 0, -1 do
pwm.open(5, 1000, i, 0, 1000)
sys.wait(10)
end
sys.wait(1000)
end
end)
sys.run()