Tag Archives: ESP8266 PWM code

ESP8266 PWM on One Output

According to the ESP8266 API - you have control over ports 12,13 and 15 for PWM.

I'm interested only in using GPIO15 as I have uses for the other port bits - so I took PWM.H and removed reference to the other two ports - leaving only GPIO15. I changed PWM_CHANNEL to 1 and deleted references to the other two pins.

#ifndef __PWM_H__
#define __PWM_H__
#define PWM_CHANNEL 1
struct pwm_single_param {
   uint16 gpio_set;
   uint16 gpio_clear;
   uint16 h_time;
};
struct pwm_param {
   uint16 period;
   uint16 freq;
   uint8 duty[PWM_CHANNEL];
};
#define PWM_DEPTH 255
#define PWM_1S 1000000
#define PWM_0_OUT_IO_MUX PERIPHS_IO_MUX_MTDO_U
#define PWM_0_OUT_IO_NUM 15
#define PWM_0_OUT_IO_FUNC FUNC_GPIO15
void pwm_init(uint16 freq, uint8 *duty);
void pwm_start(void);
void pwm_set_duty(uint8 duty, uint8 channel);
uint8 pwm_get_duty(uint8 channel);
void pwm_set_freq(uint16 freq);
uint16 pwm_get_freq(void);
#endif

I then performed an init... and LO - 0% duty cycle out of GPIO15

Here's my code..

uint8_t PWM_CH[]= {0};
uint16_t freq = 1000;
PWM_CH[0]=0;
pwm_init(freq, PWM_CH);
pwm_set_duty(pwm_CH[0], 0);
pwm_start();

and to change the duty cycle (actual values 0-255)

PWM_CH[0]=X;
pwm_set_duty(pwm_CH[0], 0);
pwm_start();

Voila. The otherwise wasted GPIO15 now usable.

Facebooktwittergoogle_pluspinterestlinkedin