How to make a controlled duty cycle and frequency output

Hi all. Need a bit of assistance with a project for work. I'm building a fuel control rig for on car use. I have all the data wiring etc I need. I have an arduino Mega and 4d systems touchscreen. I need to be able to provide a 12v 100hz PWM signal at 20% duty (I want 3 options for 20,30 and 50% for future). This signal is fed into a BLDC controller which converts to 3phase PWM.

2 problems. 1.Analogwrite can modify duty cycle but doesnt appear to change the frequency. Is there an easy way to do this? I would also modify this to out put a 25hz frequency further along the system (we use a composite PWM signal for High pressure pump and flow control).
2. The arduino outputs a 5v PWM. As above I need it to be a 12v PWM. Is something like a mosfet able to do this? How would it need to be wired up to integrate with the Mega?

For MOSFET or anything like that, please read

https://www.gammon.com.au/motors

I found that googling

  Arduino 12 volt mosfet circuit

As for the timing, at such frequencies you may be able to consider just computing the output. In a very simple example:

  while (1) {
    digitalWrite(outputPin, HIGH);
    delay(2);

    digitalWrite(outputPin, LOW);
    delay(8); 
  }

There are objections to this dumb approach, but there is def enough time to directly synthesize a 100 Hz variable duty cycle waveform. And watch input devices for changes to the duty cycle.

See "blink without delay" for smarter code to "blink" out a waveform.

Arduino blink without delay

Timing can be done the same way with delayMicroseconds().

HTH

a7

1 Like

In short, no. The standard Arduino functions don't include frequency, only duty cycle.

There are libraries you can install which will allow you to control frequency, but take care. Your project may be on Mega right now, but if you need to switch to some other model of Arduino, the library you choose for PWM frequency on the Mega may not work at all on your new Arduino.

There are functions to set the PWM frequency that are standard on other types of Arduino compatible boards, for example on ESP8266 based boards. Again, if you swap to one of these boards to enable you to control frequency, then you might have a problem if you swap to a different board later.

ESP8266 NodeMCU PWM with Arduino IDE - Dim LED (Analog Output) | Random Nerd Tutorials.

(It seems the ESP8266 PWM frequency can only be set as low as 100Hz).

Well, you can, say on a Uno, change both frequency and duty cycle. See for example https://www.gammon.com.au/forum/?id=11504&reply=6#reply6 . This means manipulation of the timer registers instead of using analogWrite() and your choice of pins is limited.

Very simple on an ESP32.
I inverted the 25Hz signal so you can use just a single N-Channel MOSFET as a low side driver.

image

This does seem a ton easier! How do I combine this with the Arduino that has its own instructions? Is that possible? I've read a bit about UART communications. This is new to me

Indeed.

I am curious about this, however:

void loop() {
  delay(10); // speeds up the simulation
}

When I run the sim, it hits 100 % real time. Removing or not removing the delay() in the loop makes no difference.

a7

Yeah, that's just from the default sketch when you open a new ESP32 project.
If you have a loop with code that heavily loads the simulator, then it does make an improvement.

@robertgriggs , Arduino already works with the ESP32 ... I guess you mean low level AVR or register level commands, in which case this specific code isn't compatible.

Note that the ESP32 has a motor control peripheral (MCPWM) which would be ideally suited for a BLDC controller, however I don't think anyone has written a driver or library for it.

@dlloyd so im guessing its a case of uploading code to ESP32 that says when A is detected do B, when C is detected do D etc? Or am i oversimplifying this?

ChatGPT rubbish removed.

Please only reply to the question @robertgriggs asked using your own expertise.

I thank you.

3 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.