My Servo just jitters

I tried the stock back and forth code, and now a pot driven circuit, and in both cases the servo just moves back and forth as fast as it can. I wouldn't quite call it 'jitter', it's a pretty big movement....

I'm using an ESP32-S3 board, and this servo:

This is the code:

#include <ESP32Servo.h>

// create servo object to control a servo
Servo myservo;

// variable to store the servo position
int pos = 0;

// GPIO pin used to connect the servo
// Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33
int servoPin = 2;

// GPIO pin used to connect the potentiometer
// Recommended ADC pins on the ESP32 include 0,2,4,12-15,32-39; 34-39 are recommended for analog input
int potPin = 42;

// Variable to store the value from the analog pin
int val;

void setup() {
  // Allow allocation of all timers
  ESP32PWM::allocateTimer(0);
  ESP32PWM::allocateTimer(1);
  ESP32PWM::allocateTimer(2);
  ESP32PWM::allocateTimer(3);

  // Standard 50hz servo
  myservo.setPeriodHertz(50);

  // attach the servoPin to the servo object and set the sweep range
  // For SG90, 500 and 2400 might be more suitable
  // different servos may require different min/max settings
  myservo.attach(servoPin, 500, 2400);
}

void loop() {
  val = analogRead(potPin);            // read the value of the potentiometer (0 to 4096 for 12-bit ADC)
  val = map(val, 0, 4096, 0, 180);     // scale it to use it with the servo (0 to 180)
  myservo.write(val);                  // set the servo position according to the scaled value
  delay(200);                          // wait for the servo to reach the position
}

Running the servo on 5v 2A external power supply, running the board via USB power.

Is there something messed up in the code? The wiring seems to be right, BC the servo is moving. It's just doing the same thing regardless of the programming. I can't verify the period is in fact 50hz. The link above is the only documentation I have. I did try 330hz, and that gave the same result.

I guess it's not impossible that I have a defective servo....


Here is what my wiring looks like...

try val = map(val, 0, 4095, 0, 180);

Did you see the specs for the servo? It can draw 3.5 amps every time it begins to move. That is called the stall current for the motor. Monitor the servo voltage with an oscilloscope to see the big voltage drops. This is not necessarily causing your control problem, but may be related.

The 5V 2A external power supply is inadequate.

The servo is rated at 6.0 and 7.2V, and briefly draws the stall current every time it starts moving. From the product page:

Stall current (at locked): 6.0V--2.5-3A;7.2V--2.8-3.5A;

Make sure that you connect all the grounds.

I thought stall current was if it was under such a heavy load it can't move. It's having no trouble moving.... there is no load except the horn.

I'll try a bigger psu. I don't have an oscilloscope.

I edited the map value to 4095. Did not change....

The stall current is drawn any time the shaft is not rotating, which is the case at startup, and when the shaft is locked.

This is true of all brushed DC motors. When the shaft is not rotating, the windings are not generating back EMF (look it up).

Ref. post #2 picture:
Shouldn't the blue pot wire be connected to GND, not TX?

Okay I DID have a missing ground, thanks!!! I did know that I needed to connect the ground from the PSU to the board, but still didn't do it.

Now the servo moves instantly to a spot and holds just fine. The pot is not doing anything yet. It may have been in the TX input at some point, but I'd tried switching things around before I put the ground wire on.

I put a meter on the pot, and it is 10k, and working as expected. I attached the ground to the main ground bus, just in case, and the center pin goes to 42.

But the pot is still doing nothing.

use a few Serial.println statements to list the ADC value of val and the one after map()

I tried this code and nothing is printing.... been a while since I did any arduino programming, but this looked like it should work.

Pot is connected to ground, pin42 and 3v3.

#include <ESP32Servo.h>

// create servo object to control a servo
Servo myservo;

// variable to store the servo position
int pos = 0;

// GPIO pin used to connect the servo
// Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33
int servoPin = 2;

// GPIO pin used to connect the potentiometer
// Recommended ADC pins on the ESP32 include 0,2,4,12-15,32-39; 34-39 are recommended for analog input
int potPin = 42;

// Variable to store the value from the analog pin
int val;

void setup() {
  // Allow allocation of all timers
  ESP32PWM::allocateTimer(0);
  ESP32PWM::allocateTimer(1);
  ESP32PWM::allocateTimer(2);
  ESP32PWM::allocateTimer(3);

  // Standard 50hz servo
  myservo.setPeriodHertz(50);

  Serial.begin(9600);

  // attach the servoPin to the servo object and set the sweep range
  // For SG90, 500 and 2400 might be more suitable
  // different servos may require different min/max settings
  myservo.attach(servoPin, 500, 2400);
}

void loop() {
  val = analogRead(potPin);            // read the value of the potentiometer (0 to 4096 for 12-bit ADC)
  Serial.print("orig val: ");
  Serial.println(val);
  val = map(val, 0, 4095, 0, 180);     // scale it to use it with the servo (0 to 180)
  Serial.print("mapped val: ");
  Serial.println(val);
  myservo.write(val);                  // set the servo position according to the scaled value
  delay(200);                          // wait for the servo to reach the position
}

okay, needed to set the baud rate both in code, AND in the window.

Now I'm getting zeroes. Is there something else I need to set on the potpin?

my ESP32-S3 datasheet pinout says pin 42 is not ADC unless you re-assign it

therefore you could try using a valid ADC pin or re-assign an ADC pin to 42

edit: apologies for being pedantic, go ahead and use pin 7 instead of 42 and let us know

Oh I think I got pin2 and 42 mixed up at some point,

It works, yay!

Now I need to figure out a better PSU....

THANKS!!!!

P.S. Kudos on the snark arrest ;)

cheers! I am happy for you