Github Repo | C Header | C source | JS source |
---|---|---|---|
mongoose-os-libs/arduino-adafruit-pwm-servo | api_pwm_servo.js |
Tested and works on esp8266/esp32
16-Channel 12-bit PWM/Servo Driver – I2C interface – PCA9685 Module lets you add more IOs to your microcontroller boards. The Module has chainable I2C interface meaning for even more IOs more boards can be daisy chained. Its and excellent product for Robots that require lots of Servo motors to control using single microcontroller.See product page link for more information about the hardware.
mos.yml, add:
config_schema:
- ["i2c.enable", true]
libs:
- origin: https://github.com/mongoose-os-libs/arduino-adafruit-pwm-servo
init.js, add:
load('api_pwm_servo.js');
main.c, add:
#include "mgos_arduino_PWMServoDriver.h"
Adafruit_PWMServoDriver.create()
Create an instance of PWM servo driver, which has the methods described below.
Example:
load("api_pwm_servo.js");
let myServo = Adafruit_PWMServoDriver.create();
myServo.close()
Close a servo instance; no other methods can be called on this instance
after calling close()
.
Return value: none.
myServo.begin()
Reset onewire and servo state. Return value: none.
myServo.setPWMFreq(freq)
This function can be used to adjust the PWM frequency (from 40 to 1000 Hz), which determines how many full 'pulses' per second are generated by the IC. Stated differently, the frequency determines how 'long' each pulse is in duration from start to finish, taking into account both the high and low segments of the pulse.
Frequency is important in PWM, since setting the frequency too high with a very small duty cycle can cause problems, since the 'rise time' of the signal (the time it takes to go from 0V to VCC) may be longer than the time the signal is active, and the PWM output will appear smoothed out and may not even reach VCC, potentially causing a number of problems.
Return value: none.
Example:
load("api_pwm_servo.js");
let myServo = Adafruit_PWMServoDriver.create();
myServo.begin();
myServo.setPWMFreq(100);
myServo.setPWM(channel, on, off)
This function sets the start (on
) and end (off
) of the high segment of
the PWM pulse on a specific channel. You specify the 'tick' value
between 0..4095 when the signal will turn on, and when it will turn of.
channel
(a number from 0
to 15
) indicates which of the 16 PWM
outputs should be updated with the new values.
Return value: none.
Example:
load("api_pwm_servo.js");
let myServo = Adafruit_PWMServoDriver.create();
myServo.begin();
// Generate square wave at 100 Hz
myServo.setPWMFreq(100);
myServo.setPWM(0, 0, 2047);
myServo.setPin(channel, val, invert)
This is a wrapper for myServo.setPWM()
, where on
will always be 0,
and off
is the val
given to this function. Additionally, invert
can be set to true
to invert the output.
Return value: none.
Example:
load("api_pwm_servo.js");
let myServo = Adafruit_PWMServoDriver.create();
myServo.begin();
// Generate square wave at 100 Hz
myServo.setPWMFreq(100);
myServo.setPin(0, 2047, false);
edit this doc