PWM(pulse width modulation) is a method for repeatedly switching a terminal on and off using fixed frequency. The percentage of on/off time can be changed. This makes it possible to send angle information to the servo motor, or to change the brightness of the LED or the strength of the motor outputting it at very high speed by PWM.
Three parameters are required for PWM:
- IO to output PWM
- Pulse width (percentage or time)
First, go to obniz.getFreePwm() to get unused PWM. The number of available PWM depends on the device. It is usually less than the IO, and is independent of the PWM number and the IO number. For example, in obniz Board series, there are six PWM from PWM0 to PWM5. PMW0 and IO0 is unrelated, so PWM can be used at IO1. You can start PWM at any IO using pwm.start(). pwm.freq() sets the frequency. The started PWM can be stopped by pwm.end().
You can modulate the signal by setting the time the signal is on, using pwm.duty() to set the ratio of the time or pwm.pulse() to specify the time by msec. Duty cycle is as follows.