I want to generate a signal with about 50 to 60 kHz. I tried it with severall microprovessors:
Arduino is too slow, ESP32 with 240 MHz is good, Teensy 4.0 is better...
My problem is that even when I can generate the signal it is not stable. I think there are interrupts behind which I cannot stop. I already use:
I've tried to make the signal in a separate processor-core. It doesn't help.
I've also tried to se interrupts to make the signal: Doesn't help.
I made it with a separate task (with high priority): Doesn't help.
Is there a way to make such a signal stable with a code like this (or anything else?):
I don't know but the write statements and the looping take some time and it's probably too much time relative to 2 microseconds. ...If it doesn't come-out even and you get errors and jitter.
If there's a way to timer-interrupt every Xus that might work better.
And to be "exact" the generated frequency usually has to divide the clock frequency evenly. (I've forgotten my math... a "factor"?)
have a look at direct-digital-synthesis
years ago I used a pair of DDS modules to generate a pair of 1MHz sine waves with a preise phase difference for a medical experiemnt
have a look at Arduino-Controlled-AD9833-Function-Generator
e.g. 100KHz sine wave
you can select sine, square or triangle
Thanks for the many answers.
Unfortunately, there is no suitable solution among them.
Solution with interrupts: Doesn't solve the jitter-problem.
Solution with PWM: Would work, but only for one signal. I need 2 digital signals synchronized. But the PWM outputs are independend from each other.
AD9833 has the same problem: Only one output.
This are the 2 signals I need (without jitter):
The frequency and duty cycle of the two signals are the same, but they must be just as out of phase.
I see you're using the same timer, which is good (maybe). Can't simulate on Wokwi because of an issue with the simulator.
I strongly suggest using a channel pair ch 0,1...2,3...4,5...6,7...8,9...10,11...12,13...14,15 to ensure
Jitter is within 150ns. If you inadvertently choose a channel that's on another timer, then the jitter will be up to several orders of magnitude worse!
For improved performance, use:
const int OutChannel1 = 0;
const int OutChannel2 = 1;
instead of:
const int OutChannel1 = 0;
const int OutChannel2 = 8;
Could also get better than only 8-bit resolution if needed.
Yes, sorry. I thought when I can make a signal without jitter I can make every signal.
But I have learned a lot from your answers and other readers will also learn how to make such signals, when they read the answers.
Hi [dlloyd], first I have tried with channel 0 and 1. But it doesn't work. I always get the exact same signal on both pins. That's why I used channel 0 and channel 8.
I have now installed your library Version 4.3.4.
When I compile your script I get this error-message:
..\Documents\Arduino\libraries\ESP32_ESP32S2_AnalogWrite\src\pwmWrite.cpp: In member function 'void Pwm::tone(uint8_t, uint32_t, uint16_t, uint16_t)':
..\Documents\Arduino\libraries\ESP32_ESP32S2_AnalogWrite\src\pwmWrite.cpp:85:62: error: 'ledcChangeFrequency' was not declared in this scope
ledcChangeFrequency(ch, frequency, mem[ch].resolution);
^
..\Documents\Arduino\libraries\ESP32_ESP32S2_AnalogWrite\src\pwmWrite.cpp: In member function 'void Pwm::ledc_attach_with_invert(uint8_t, uint8_t)':
..\Documents\Arduino\libraries\ESP32_ESP32S2_AnalogWrite\src\pwmWrite.cpp:308:3: error: 'ledc_channel_config_t' has no non-static data member named 'flags'
};
^
exit status 1
Compilation error: exit status 1
Why would there be increased jitter if the second channel is on a different timer?
Aren't they all synchronous with the same clock?
Solution with PWM: Would work, but only for one signal. I need 2 digital signals synchronized. But the PWM outputs are independend from each other.
Similarly, while the PWM outputs are controlled by timers that all operate from the same clock as well. They're less "independent" than you might think! You might have to adjust some initial values to account for the setup time, but after that the outputs should pretty much stay synchronized...