Fork me on GitHub

PWM

This library provides Pulse Width Modulation - PWM hardware interface.

C API Reference mgos_pwm.h

#include "mgos_pwm.h"


bool mgos_pwm_set(int pin, int freq, float duty);

Set and control the PWM.

Params: pin: GPIO number. freq: Frequency, in Hz. 0 disables PWM on the pin. duty: Duty, in %, how long to spend in "1" state. Must be between 0 and 100 (inclusive). 0 is "always off", 100 is "always on", 50 is a square wave. Return: true - SUCCESS, false - FAIL.

Note: On esp32 we use 8 channels and 4 timers. Each mgos_set_pwm call with new pin number assigns a new channel. If we already have timer running at the specified frequency, we use it instead of assigning a new one.

bool mgos_pwm_set_double(int pin, int freq, double duty);

It's a wrapper for correct using a float param by ffi'g.

JAVASCRIPT API Reference api_pwm.js

load("api_pwm.js");


PWM.set(pin, freq, duty)

Set and control the PWM. pin is a GPIO pin number, freq is frequency, in Hz. freq 0 disables PWM on the pin. duty is duty in %, how long to spend in "1" state. Must be between 0 and 100 inclusive. 0 is "always off", 100 is "always on", 50 is a square wave. Return: true - success, false - failure.

Example:

PWM.set(pin, 50, 2.73);

Note: on ESP32 we use 8 channels and 4 timers. Each PWM.set() call with new pin number assigns a new channel. If we already have a timer running at the specified frequency, we use it instead of assigning a new one.