0

I'm new to electronics but I have a Rigol DS1054Z oscilloscope and I'm experimenting to help learn. I've created an incredibly simple sketch using an ESP32 and Arduino IDE:

void setup() {
  pinMode(32, OUTPUT);
}

void loop() { delayMicroseconds(10); digitalWrite(32, LOW); delayMicroseconds(10); digitalWrite(32, HIGH); }

However, the square wave is incredibly "shaky" or unstable looking. Why is that?

oscilloscope

SofaKng
  • 151
  • 1
  • 5

3 Answers3

1

The display moving is what we call jitter. It is normal with the Arduino and expected. The pulses are not even because of the background tasks such as the interrupt service routine (that does the millies etc whether you use them or not). When an interrupt occurs it causes the processor to stop, save its state, do a new task, then restore its state and continue. During the interrupt service routine (ISR) the processor uses time to execute instructions, this delay causes the jittering. Trust your scope, as usual it is correct. If you use one of the timer outputs the jitter will go away because they are not normally adjusted the just keep going. PWM is one of the more common modes of doing this but there are many others you can also use.

Gil
  • 1,863
  • 9
  • 17
0

This isn't an answer, but you could compare it to hardware PWM to test the limits of the rest of the system.

Simple ESP32 PWM per https://www.electronicshub.org/esp32-pwm-tutorial/

int freq = 48000;
int ledChannel = 0;
int resolution = 8;  // 48000*2^8 < 80Mhz on channel 0 per https://www.electronicshub.org/esp32-pwm-tutorial/

void setup() {

ledcSetup(ledChannel, freq, resolution); ledcAttachPin(32, ledChannel); ledcWrite(ledChannel, 127); }

void loop() { }

Dave X
  • 2,350
  • 15
  • 29
0

Jitter like that is usually caused by the different amount of time it takes to go through loop(). Remember that the tick timer takes time away from loop() at an interval that is not synchronized to the loop() time.