Use ESP32 PWM (ledc) to control electromagnetic coils

lam.le
Posts: 2
Joined: Wed Nov 29, 2023 5:04 pm

Use ESP32 PWM (ledc) to control electromagnetic coils

Postby lam.le » Wed Nov 29, 2023 5:30 pm

Hi everyone,

Previously, I followed this tutorial here (https://www.hackster.io/mircemk/diy-che ... ice-bd972d) to make a simple pulsed electromagnetic field therapy device. It has a Gaqqee PWM module (to generate the PWM signals), feeding to a potentiometer (for the gain), and then to HW-517 mos switch driver (basically, just nmos to switch on/off the coils at different frequencies - 25Hz, or 100Hz, or even 18kHz).

Now, to improve in the next version, I want to add an ESP32 board to programmatically control things and control them over wifi/my phone.
I see this PWM module (from previous version: https://www.aliexpress.com/i/1005002656 ... pt=glo2vnm) has TTL serial communication pins (TXD, RXD). So, thinking, basically I can use ESP32 to control the frequency and the duty cycle that I want.

However, when I research more, it seems ESP32 has internal PWM modules (?).
To be specific, I try to follow this tutorial here: https://randomnerdtutorials.com/esp32-pwm-arduino-ide/
But I am stuck in the loop here. I don't understand what "ledcWrite(ledChannel, dutyCycle)" actually does. Sorry, not coming from an electrician background.

Code: Select all

void loop(){
  // increase the LED brightness
  for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){   
    // changing the LED brightness with PWM
    ledcWrite(ledChannel, dutyCycle);
    delay(15);
  }

  // decrease the LED brightness
  for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){
    // changing the LED brightness with PWM
    ledcWrite(ledChannel, dutyCycle);   
    delay(15);
  }
}
I understand the square wave graph created by PWM module. It stays 1, then goes to 0 -> meaning a duty cycle (am I correct?). But why do, in the above tutorial, we loop through 256 duty cycles and use ledcWrite?
But then in this topic (viewtopic.php?f=12&t=11039), the author just uses the following, without looping:

Code: Select all

void loop() {
  const int dutyCycle = 1024;
  ledcWrite(ledChannel, dutyCycle);

  const int dutyCycle2 = 64;
  ledcWrite(ledChannel2, dutyCycle2);
}
A bit confusing for me here. Can someone please explain how I should use the ledc?
Overall, I think I just want to write square wave patterns (1 and then 0) to 1 of the output pins of the ESP32 board. Then I connect that pin to the pwm input of HW-517 to control the copper coils.

Thank you very much.

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

Re: Use ESP32 PWM (ledc) to control electromagnetic coils

Postby ESP_Sprite » Thu Nov 30, 2023 3:03 am

The loop simply is there to slowly fade the LED from all on to all off - it changes the duty cycle from 255 to 0 in the span of about 4 seconds. It's a nice effect, but it's illustrative rather than required for getting a square wave with a certain duty cycle out of the chip.

lam.le
Posts: 2
Joined: Wed Nov 29, 2023 5:04 pm

Re: Use ESP32 PWM (ledc) to control electromagnetic coils

Postby lam.le » Thu Nov 30, 2023 5:01 am

Thank you very much @ESP_Sprite.
If I call

Code: Select all

ledcWrite(output_pin, 127)
(given resolution is 8) only one time (probably in setup() or with an if check in loop()), the ESP32 board still constantly outputs the square wave pattern (of duty cycle ~50%), doesn't it, if I understand correctly?

So, what I'm thinking. If I want a PWM with a constant freq 5kHz on output pin 1, duty 50%, for example:

Code: Select all

#define output_pin 1
#define freq 5000
#define resolution
#define duty 127
void setup() {
  ledcAttach(output_pin , freq , resolution);
  ledcWrite(output_pin, duty);
}

void loop() {
  // .. nothing for ledc here unless I want to update duty cycles, frequencies, etc. (?)
}

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

Re: Use ESP32 PWM (ledc) to control electromagnetic coils

Postby ESP_Sprite » Fri Dec 01, 2023 5:48 am

Yes, that all seems correct to me.

Who is online

Users browsing this forum: No registered users and 74 guests