air101#
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 air101_chip specification_v1.1.pdf, it can be seen that Air101 has 5 PWM channels, the frequency range is 3Hz-160kHz, and the maximum accuracy of duty cycle is 1/256
We can adjust the brightness of the LED small light by pwm different duty cycle output
Hardware preparation#
Air101 Development Board
LED small lamp
Wiring Schematic
PWM0 ------ +
Air101 LED
GND ------ -
Software part#
Interface documentation can be referred to:pwm library
Turn on pwm output#
Turn on the pwm output of the PWM0 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 0, set the pulse frequency to 1kHz, the frequency division accuracy to 100, and the duty cycle to 100 = 10% continuous output
pwm.open(0, 1000, 10, 0, 100) -- The small lamp glows slightly
sys.wait(1000)
-- Turn on pwm channel 0, set the pulse frequency to 1kHz, the frequency division accuracy to 100, and the duty cycle to 50/100 = 50% continuous output
pwm.open(0, 1000, 50, 0, 100) -- Small light medium brightness
sys.wait(1000)
-- Turn on pwm channel 0, set the pulse frequency to 1kHz, the frequency division accuracy to 100, and the duty cycle to 100/100=100 continuous output
pwm.open(0, 1000, 100, 0, 100) -- 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 50%
for i = 0, 50 do
pwm.open(0, 1000, i, 0, 100)
sys.wait(10)
end
sys.wait(1000)
-- Duty cycle continuously reduces the brightness of small lamps from 50% to 0%
for i = 50, 0, -1 do
pwm.open(0, 1000, i, 0, 100)
sys.wait(10)
end
sys.wait(1000)
end
end)
sys.run()