How can I make the analogWave library play nice with millis()?

I'm trying to use the analogWave library to output a square wave for a PWM signal to an electric motor driver. That seems to work fine.

The problem is that it seems to mess up the timer that is required for the millis() (and micros()) function call. millis() now returns a constant value. Is there any way to either tell the analogWave to use a different timer mechanism or to access a different timer to get a millisecond tick count from the arduino board?

I'm using an Arduino UNO R4 WiFi.

If I had that problem, I would look at the datasheet to see if there are any notes etc that explain that behaviour.

the source code is available on GitHub and the begin function seems to be the one selecting the timer

If I remember well when I had a Quick Look, the Arduino UNO R4 two types of timers:

  • the General-Purpose Timer (aka GPT) which is a 16-bit timer with multiple channels, capable of counting up and down simultaneously.
  • the Asynchronous General-Purpose Timer (aka AGT) which is a 16-bit timer with two channels, AGT0 and AGT1.

AGT0 is always used for timekeeping and millis() / micros() depends on it.

The thing is that AGT0 can be configured to trigger AGT1 which is useful if you want to create a hardware based 32-bit timer.

May be the analogWave library depends on the 32 bit timer and this overwrites the configuration of AGT0

this would need to be investigated

If you simply need a pwm-square-wave,
using the hardware-pwm of the microcontroller itself should be sufficient.

Do you have special requirements for the pwm-frequency?

After a quick googling should be pretty simple to adjust the frequency

Hey Stefan. Thanks for the input. The reason that I had tried the analogWave library was that I wanted a PWM frequency of around 15 - 20 kHz. Using the PWM described in the link you provided seems to do that without messing up the timer that supports the millis() call.

(I said "seems to do that" because I don't have an oscilloscope to verify the actual frequency, but my system works as expected.)

Problem solved!

Can your digital multimeter measure frequency?

Or do you have a second microcotroller?

I am pretty sure that a Uno R4 has a lot of inrterupts to measure time with microsecond resolution.

So you could measure time between to LOW-HIGH transitions

Stefan,
At your suggestion, I tried using pulseIn to measure the length of the high and low parts of the square wave when setting the frequency to 20 kHz. They always sum to 50 microsec at various duty cycles, so I was able to confirm the 20 kHz output frequency.

Thanks, again.

When running motors and I need a PWM, this is how I do it. (Done it)

Notice in the valueChange() function there is a setPercentage() call that's where you would put in your PWM value. The knob, in this example, is setting freq.

And yes, completely NON BLOCKING.

-jim lee

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