Struggling with Xiao esp32S3 servo code and other failures

hello,. here is the sitch.

I’m dancing around lot’s of different motors at the moment.
I’ve got an ESP32 and I have the simple goal of motor goes to the position of a potentiometer.

Here is my board

  • blue = 5v
  • red = 3v3
  • black = gnd
  • yellow = pot_left
  • white = pot_right
  • green = servoPWM


This is the motor

Here is the code I’ve been following.

github link to official repo

Here is my ino.

#include <ESP32Servo.h> 

Servo myservo;
int servoPin = 4;
int potPin = A0;
int ADC_Max = 4096;
int val;

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(potPin, INPUT);
  pinMode(servoPin, OUTPUT);
	ESP32PWM::allocateTimer(0);
	ESP32PWM::allocateTimer(1);
	ESP32PWM::allocateTimer(2);
	ESP32PWM::allocateTimer(3);
  myservo.setPeriodHertz(50);
  myservo.attach(servoPin, 500, 2400);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH); //I'm doing something
  val = analogRead(potPin);
  val = map(val, 0, ADC_Max, 0, 180);
  myservo.write(val);
  delay(200);
  digitalWrite(LED_BUILTIN, LOW); //I did it!
  delay(200);
}

No movement, but I am getting the led on pin 4 to flicker… but real slow.
I can tell you it works but only if I use my Arduino Leonardo and the servo library.

Anyone got any thoughts? :slight_smile:

Hey @Pixmusix

If you take out the LED does it give you any better results? Could be that the LED is drawing too much current on that pin to both drive the signal for the servo and the LED.

1 Like

No change.
Does the servo need 5v on the data line? Do I need a mosfet?

the servo should have 3 wires, +V to drive it, Ground and “data” to tell it what position.

Maybe add some debug to print the value of “val” to ensure its changing as the pot changes.

1 Like

Thanks for diving in @Michael99645
through a serial print in the setup clause and It doesn’t come through.
This is bizarre.

Hey Pix,

The code looks good, and I’ve checked all the connections, strange. I suspect that the serial print will reveal the issue once you can get it working. Let us know how it goes!

Thanks @Zach
Man I can’t even get my serial to work.
I’ve added this code.

  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  while(!Serial) {
    digitalWrite(LED_BUILTIN, HIGH); //I'm doing something
    delay(100);
    digitalWrite(LED_BUILTIN, LOW); //I'm doing something
    delay(100);
  }
  Serial.println("hi");

the led flickers while it waits, but it never stops.
:confused:

Would that code either just be in the lead on/off loop for ever OR never enter it and then hit the serial print.

I assume your terminal is at 9600 baud as well ?

Hey @Pixmusix,

Have you confirmed that the serial is connecting when the XIAO is isolated (i.e. taken out of the breadboard)? That should be a pretty easy way to tell if there’s some sort of issue with the circuit itself.

Thanks everyone for chipping in.

I got to the bottom of it.

This was the pinout I was following found here

Notice the Xiao’s pinout is zero indexed.
Arduino IDE, however, must NOT be zero indexed!
i.e.

const int D0 = 1;
const int TX = 7;
const int RX = 8;

So my bug was here:

int servoPin = 4; // Pin4 is D3 on the Pinout diagram.

Solved by moving my green PWM wire across to D3.

My oscilloscope is the best a bijillion dollars I e’er spent.


P.S.
I resolved my zero feedback from the serial monitor issue by sys-killing my computers lock on the Serial Port via terminal. That while(!Serial) {} was causing the Xiao to hang, waiting for a connection that would never be released.

2 Likes