Page 1 of 1

ESP32-C3-Wroom-02 VSC - PlatformIO - Problem generating 4 PWM signals with the same starting front.

Posted: Tue Apr 25, 2023 6:37 am
by Almidev
Hi,

He looked a little in the forum before posting. I didn’t know how to find what I was looking for, so I came here to find out.

My specifications:
- I need to generate 4 PWM signals 25 kHz.
- They must start at the same time.

So I started by doing a search. I found indications with "ledc". Ok I started a code.
I can’t generate the 4 signals. One is frozen at 1 logic and doesn’t go down.
They are stripped using this method.

I’m looking for a way to keep the ledcSetup() and ledcAttachPin() configuration working. But I would have to write a solution to replace the 4 ledcWrite() that do not do the job properly.

At the limit, I have a test code to show you what I did, a copy of the statement I made with scanastudio. Maybe someone can help me recode a little in order to generate the 4 signals correctly.

platformio.ini

Code: Select all

[env:esp32-c3-devkitm-1]
platform = espressif32
board = esp32-c3-devkitm-1
framework = arduino
upload_port = COM[3]

;Used here to enable the use of the serial link: Serial.print("")
build_flags = 
	-DARDUINO_USB_MODE=1
	-DARDUINO_USB_CDC_ON_BOOT=1
main.cpp

Code: Select all

#include <Arduino.h>

// GPIO REEL
#define IO0 0
#define IO1 1
#define IO2 2
#define IO3 3
#define IO4 4
#define IO5 5
#define IO6 6
#define IO7 7
#define IO8 8
#define IO9 9        // Boot / Led for default
#define IO10 10

// PWM DECLARATION CHANNEL
#define IO0_PWM_CH0 0
#define IO1_PWM_CH1 1
#define IO2_PWM_CH2 2
#define IO3_PWM_CH3 3
#define IO4_PWM_CH4 4
#define IO5_PWM_CH0 0

// Variables
const uint16_t  frequency   = 25000; // Hz - 25 kHz
const uint8_t   resolution  = 8;
const int       dutyCycle   = 150;

void setup() {
  // put your setup code here, to run once:

  // PWM 1
  ledcSetup(IO0_PWM_CH0, frequency, resolution);
  ledcAttachPin(IO0, IO0_PWM_CH0);

  // PWM 2
  ledcSetup(IO1_PWM_CH1, frequency, resolution);
  ledcAttachPin(IO1, IO1_PWM_CH1);

  // PWM 3
  ledcSetup(IO2_PWM_CH2, frequency, resolution);
  ledcAttachPin(IO2, IO2_PWM_CH2);

  // PWM 4
  ledcSetup(IO3_PWM_CH3, frequency, resolution);
  ledcAttachPin(IO3, IO3_PWM_CH3);
}

void loop() {
  // put your main code here, to run repeatedly:

  /**
   * @brief We are looking to generate 4 identical signals starting at the same time.
   * */
  ledcWrite(IO0_PWM_CH0, 50);
  ledcWrite(IO1_PWM_CH1, 50);
  ledcWrite(IO2_PWM_CH2, 50);
  ledcWrite(IO3_PWM_CH3, 50);

  delay(100); // For measurement

  ledcWrite(IO0_PWM_CH0, 0);
  ledcWrite(IO1_PWM_CH1, 0);
  ledcWrite(IO2_PWM_CH2, 0);
  ledcWrite(IO3_PWM_CH3, 0);
  
  /* At this place, I’d like to do this */
  
  // Same PWM started at the same time
  //write(IO0_PWM_CH0, 50, IO1_PWM_CH1, 50, IO2_PWM_CH2, 50, IO3_PWM_CH3, 50);

  // PWM started at the same time with a different frequency
  // write(IO0_PWM_CH0, 50, IO1_PWM_CH1, 100, IO2_PWM_CH2, 150, IO3_PWM_CH3, 200);

  while(1); // Stop generating signal
}
001-APSAE-Quadruple-PWM-Monotop-Scana.png
001-APSAE-Quadruple-PWM-Monotop-Scana.png (94.5 KiB) Viewed 1291 times
Thank you so much for your help.

Re: ESP32-C3-Wroom-02 VSC - PlatformIO - Problem generating 4 PWM signals with the same starting front.

Posted: Sat May 13, 2023 7:08 am
by Almidev
Hi,

I’ve come a long way on the use of LEDC.

It turns out that timing by cannaux is risky. On some frequencies, they synchronize randomly.

I ended up doing something, but:
  • hpoint is innaccessible from the ledc library (see probably to overload the library), with a little help, why not.
  • Perhaps review the architecture of the code so that synchro/deasynchro can be done properly.
  • Rectify the use of timers to make it less problematic even when we are forced to use delay().
Hoping to help you