ELERIUS

Hello OPENPLC community,

I hope this message finds you all well. I am currently working with OPENPLC and I am using an ESP32 as my hardware platform. I've been making good progress so far, but I've run into a challenge that I could use some guidance on.

Specifically, I'm looking to generate PWM (Pulse Width Modulation) outputs using my ESP32 in conjunction with OPENPLC. I've reviewed the available documentation and resources, but I haven't been able to find a clear step-by-step guide on how to achieve this.

If any of you have experience working with OPENPLC and ESP32 to generate PWM outputs, I would greatly appreciate your insights. It could be related to configuring the OPENPLC software, utilizing specific libraries, or any other relevant information.

Reply 0 0
thiagoralves
Simply write to an analog output (%QW0). PWM capable pins work on OpenPLC as analog outputs. You control the width of the PWM pulse with the value that goes inside the %QW variable. Since %QW are defined as UINT, 100% width = 65535
Reply 0 0
ELERIUS
I hope you're all doing well. I'm working on a project involving an L298N motor driver module to control a motor's speed. While I've successfully programmed the motor speed control using Arduino, I'm facing challenges when trying to achieve the same functionality with OPENPLC.
In my setup:
I'm using an L298N motor driver to control a motor's speed.
The motor responds well to PWM control when programmed with Arduino IDE.
I've connected everything correctly and tested it successfully with Arduino IDE.
I've set up OPENPLC to generate PWM signals and connected them to the L298N module.
However, the issue I'm facing is that the motor's speed doesn't vary as expected when using OPENPLC. Strangely, the motor turns on and runs when the analog input is set to a high value, but it doesn't respond to lower values. It's as if the PWM signal isn't affecting the motor's speed control.
I've attached a screenshot of my OPENPLC IDE setup for reference. If anyone has encountered a similar situation or has insights into potential solutions, I'd greatly appreciate your input.
Thank you for your time and assistance.
Best regards,
[203128%2F8314450%2Fmceclip0]
Reply 0 0
thiagoralves
Make sure your analog input is covering the full voltage input. My guess is that, for example, if you have a potentiometer connected to your analog input, it may be providing only a small amount of the analog range your board accepts. Perhaps a good way to test this is to set up Modbus TCP over WiFi, and then use Radzio to monitor the values of ENTRADA (IW0 - Modbus Input Register 0) and SALIDA (QW1 - Modbus Holding Register 1)
Reply 0 0
ELERIUS
Make sure your analog input is covering the full voltage input. My guess is that, for example, if you have a potentiometer connected to your analog input, it may be providing only a small amount of the analog range your board accepts. Perhaps a good way to test this is to set up Modbus TCP over WiFi, and then use Radzio to monitor the values of ENTRADA (IW0 - Modbus Input Register 0) and SALIDA (QW1 - Modbus Holding Register 1)
I made sure that the voltage range at the input covered all the analog input values of OPENPLC. I will attach a photo showing that the maximum value of the analog input is indeed displayed. I used a multimeter to measure the analog voltage at the output of my ESP32, and it does vary between 0 and 3.3V. However, for some strange reason, the motor speed doesn't change. I tested it with the Arduino IDE, and it works, but not with OPENPLC. I suspect that it's not generating the PWM signal.
 
[203128%2F8314556%2Fmceclip0]
Reply 0 0
thiagoralves
Have you changed the IO pin mapping on the upload dialog? I wonder if you have used a pin that is not PWM capable... I don't have an ESP32 to test, so I can't help much with the driver, but there is a chance the OpenPLC ESP32 driver is wrong as well. Have you tried uploading this code to an Arduino Uno?
Reply 0 0
ELERIUS
I tested the analog output of the ESP32 in the Arduino IDE, and it works correctly for PWM. I used the same output pin that I'm using in OPENPLC, and that pin is indeed compatible with PWM. What I'm thinking is that perhaps the analog output in OPENPLC doesn't generate PWM on that ESP32 pin; it might only vary the voltage without generating the PWM signal.
Reply 0 0
richw
ELERIUS wrote:
I made sure that the voltage range at the input covered all the analog input values of OPENPLC. I will attach a photo showing that the maximum value of the analog input is indeed displayed. I used a multimeter to measure the analog voltage at the output of my ESP32, and it does vary between 0 and 3.3V. However, for some strange reason, the motor speed doesn't change. I tested it with the Arduino IDE, and it works, but not with OPENPLC. I suspect that it's not generating the PWM signal.
 
[203128%2F8314556%2Fmceclip0]
Which pin number are you using on your ESP32? There are some pins that are input only, that might be causing your issue.
Reply 0 0
richw
richw wrote:
Which pin number are you using on your ESP32? There are some pins that are input only, that might be causing your issue.
The only way to see if PWM is working would be to hook up a scope.
Reply 0 0
thiagoralves
Can you post here your Arduino code that is working?
Reply 0 0
thiagoralves
Nevermind! Indeed the OpenPLC ESP32 driver does not generate PWM waves, but outputs voltage using ESP32 internal DAC:
    for (int i = 0; i < NUM_ANALOG_OUTPUT; i++)
    {
        if (int_output[i] != NULL) 
            dacWrite(pinMask_AOUT[i], (*int_output[i] / 256));
    }
 
You would have to change that line of code your esp32.cpp file (editor/arduino/src/hal/esp32.cpp) to actually generate a PWM signal instead of calling dacWrite
Reply 1 0
ELERIUS
Nevermind! Indeed the OpenPLC ESP32 driver does not generate PWM waves, but outputs voltage using ESP32 internal DAC:
 for (int i = 0; i < NUM_ANALOG_OUTPUT; i++)
    {
        if (int_output[i] != NULL) 
            dacWrite(pinMask_AOUT[i], (*int_output[i] / 256));
    }
 
You would have to change that line of code your esp32.cpp file (editor/arduino/src/hal/esp32.cpp) to actually generate a PWM signal instead of calling dacWrite
Where can I make that modification?
Reply 0 0
ELERIUS
I found her! But what should I write to generate the PWM?
Reply 0 0
onder.yurekli
ELERIUS wrote:
I found her! But what should I write to generate the PWM?
you could use ledc to generate pwm function but you have to set up ledc function pwmfrq and resolution at the hardware initialization part.
Reply 0 0
Reply