esp32 arduino pwm (duty and frequency)

lepass7
Posts: 3
Joined: Mon Sep 11, 2017 7:54 pm

esp32 arduino pwm (duty and frequency)

Postby lepass7 » Mon Sep 11, 2017 8:04 pm

Dear all,
I would like to be able to control led output through PWM frequency like this function:

Code: Select all

function led(r,g,b,frequency)
     --RED(GPIO15),GREEN(GPIO12),BLUE(GPIO14)
     pwm.setduty(6,g)
     pwm.setduty(5,b)
     pwm.setduty(8,r)
     pwm.setclock(6, frequency)
     pwm.setclock(5, frequency)
     pwm.setclock(8, frequency)  
end
THe previous code is written in lua. With this function I was able to control the colour of the RGB led (setduty) and also the blinking frequency (setclock).

How can I convert this function into arduino esp32 function?
Fro the moment I am using this function:

Code: Select all

void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
  // calculate duty, 8191 from 2 ^ 13 - 1
  uint32_t duty = (8191 / valueMax) * _min(value, valueMax);

  // write duty to LEDC
  ledcWrite(channel, duty);
}
and I can write a color on my rgb led by calling the following commands:

Code: Select all

ledcAnalogWrite(LEDC_CHANNEL_0_R, 0);
ledcAnalogWrite(LEDC_CHANNEL_1_G, 0);
ledcAnalogWrite(LEDC_CHANNEL_2_B, 255);
With this function I cxan create almost any colour but I cannot get my led to blink.

Any ideas?

ESP_Sprite
Posts: 9723
Joined: Thu Nov 26, 2015 4:08 am

Re: esp32 arduino pwm (duty and frequency)

Postby ESP_Sprite » Tue Sep 12, 2017 1:03 am

Moved to the Arduino subforum

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: esp32 arduino pwm (duty and frequency)

Postby kolban » Tue Sep 12, 2017 4:56 am

I think you might be served by thinking in terms of "Arduino" as opposed to LUA. For example, see the following tutorial for Arduino:

https://www.arduino.cc/en/Reference/AnalogWrite

I believe the intent of the Arduino libraries for the ESP32 is to allow Arduino sketches written for an ATMEGA Arduino (i.e. a real "Arduino") to run "as-is" which compiled against the ESP32 Arduino libraries.

Outside of the Arduino environment, you have the ESP-IDF framework which has a driver called "ledc" that has been specifically engineered for LED brightness control.

There are also C++ wrapper available that attempt to encapsulate some of the complexities for easier consumption:

https://github.com/nkolban/esp32-snippe ... tils/PWM.h
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Who is online

Users browsing this forum: No registered users and 57 guests