Dlloydev / ESP32-ESP32S2-AnalogWrite

ESP32 PWM, Servo, Easing and Tone. Smart GPIO pin management and advanced control features.
MIT License
100 stars 17 forks source link

servo.write(pin, value) doesn't produce expected PWM output #41

Closed kirbyny closed 11 months ago

kirbyny commented 11 months ago

Testing environment is Arduino Nano ESP32 (S3), IDE 2.2.1 Attached scope trace taken using GPIO 6, but other GPIO produce same results.

myservo.write(pin, 500);  //produces 5738μs pulse width, expecting ~500μs - see scope trace
myservo.write(pin, 180); //produces 2400μs pulse width, expecting ~2500μs
myservo.write(pin, 0); //produces 544μs pulse width, expecting ~500μs

5_7ms

I'm assuming that I'm mis-reading the docs and simply doing something wrong here.

kirbyny commented 11 months ago

After looking at the source, it appears that PWM values I am seeing are the result of the channel data defined in pwmWrite.h.
Modifying these values produces the expected PMW values.

I am unsure where the original 5.7ms value came from, though changing the values shown below has eliminated the issue. Requesting a value lower than servoMinUs now outputs 2.5ms, though this seems counterintuitive (I would expect it to substitute .5ms rather than 2.5ms).

original code

`    mem_t mem[16] = {  // channel data (see above)
      {255, 1000, 0, 8, 0, 0, 0, 544, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 0, 0, 0, 544, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 0, 1, 0, 544, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 0, 1, 0, 544, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 0, 2, 0, 544, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 0, 2, 0, 544, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 0, 3, 0, 544, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 0, 3, 0, 544, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 1, 0, 0, 544, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 1, 0, 0, 544, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 1, 1, 0, 544, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 1, 1, 0, 544, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 1, 2, 0, 544, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 1, 2, 0, 544, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 1, 3, 0, 544, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 1, 3, 0, 544, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0}
    };`

modified code

    mem_t mem[16] = {  // channel data (see above)
      {255, 1000, 0, 8, 0, 0, 0, 500, 2500, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 0, 0, 0, 500, 2500, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 0, 1, 0, 500, 2500, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 0, 1, 0, 500, 2500, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 0, 2, 0, 500, 2500, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 0, 2, 0, 500, 2500, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 0, 3, 0, 500, 2500, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 0, 3, 0, 500, 2500, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 1, 0, 0, 500, 2500, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 1, 0, 0, 500, 2500, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 1, 1, 0, 500, 2500, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 1, 1, 0, 500, 2500, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 1, 2, 0, 500, 2500, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 1, 2, 0, 500, 2500, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 1, 3, 0, 500, 2500, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
      {255, 1000, 0, 8, 1, 3, 0, 500, 2500, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0}
    };

Are the values of 544/2400 non-arbitrary?