Chat freely about anything...

User avatar
By alonewolfx2
#2640 how can i use pwm on gpio pins? anyone help me? anyone share simple pwm example?
User avatar
By EUA
#4696 You need to edit some settings.
For example you need to pin # by::

Code: Select all#define PWM_0_OUT_IO_MUX  PERIPHS_IO_MUX_GPIO2_U
#define PWM_0_OUT_IO_NUM  2
#define PWM_0_OUT_IO_FUNC FUNC_GPIO2
#define PWM_CHANNEL 1

PWM_CHANNEL 1 means you are gonna use only one PWM in your program.

in user_init, you can:
Code: Select alluint8_t duty=0;
pwm_init( 150, &duty);
pwm_start();


Than in anywhere to adjust PWM:

Code: Select allpwm_set_duty(duty, 0);
pwm_start();

You have to run pwm_start function after changing duty cycle.
Otherwise it doesn't run.


Here is sample code:
Code: Select all#include <ets_sys.h>
#include <osapi.h>
#include <os_type.h>
#include <gpio.h>

#include "user_interface.h"

#define user_procTaskPrio        0
#define user_procTaskQueueLen    1
os_event_t    user_procTaskQueue[user_procTaskQueueLen];

// see eagle_soc.h for these definitions
#define PWM_0_OUT_IO_MUX  PERIPHS_IO_MUX_GPIO2_U
#define PWM_0_OUT_IO_NUM  2
#define PWM_0_OUT_IO_FUNC FUNC_GPIO2

#define PWM_CHANNEL 1

LOCAL uint8_t led_state=0;
LOCAL uint8_t duty=0;

//Main code function for heartbeat LED on GPIO2
void ICACHE_FLASH_ATTR
loop(os_event_t *events)
{
    os_delay_us(10000);

   led_state ? duty-- : duty++;

    if (duty >= 255 ) led_state=1;
    else if (duty <= 10 ) led_state=0;

   pwm_set_duty(duty, 0);
   pwm_start();

    system_os_post(user_procTaskPrio, 0, 0 );
}

void user_init(void)
{
   pwm_init( 150, &duty);
    pwm_start();

    //Start os task
    system_os_task(loop, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen);

    system_os_post(user_procTaskPrio, 0, 0 );
}
User avatar
By riban
#8252 What library are you referring to with your example? I can't find pwm_init (or anything with "pwm" in the title) in any of the esp_iot_rtos_sdk libraries which I believe are from the current SDK and based on the original IOT SDK.