Using Servos with Vixen and Arduino

I definitely recommend adding upper and lower limits in the Arduino code. They can save the servos and are pretty easy to add.

Another possibility to handle the end of a sequence (assuming that the incoming data sets the servo to 0), is to manually map in the Arduino code to replace a 0 input to a value that will center the servo. I would assume this value would be a 127 or 128.

This line of code:
Code:
      servo1.write(map(channelValues[0], 0,255, 89, 110), 75); //Schroeder 90-110, speed set to "75"
takes the incoming value between 0 - 255 and maps (limits) it to 89 - 110 servo position. The "75" is for a slower speed if you use the variable speed servo library <VarSpeedServo.h> and not the standard servo library.

I wish the ESP32 supported the VarSpeedServo.h library.
 
Do you have an Esp module to run servo code on ?

Yes. A have a few, but the variable speed library doesn't work on that board. The Arduino IDE won't accept library for that board.

Here is my project and my code for an ESP32 for pure sine wave output from an old transformer, done with a phone: (Some older trains are best run on AC (sine wave); supposed to preserve the older universal motors.)



https://www.josephrampolla.com/servotransformer.html

Code:
// by Joseph P Rampolla, Sept. 2022
#include <ESP32Servo.h>
#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
Servo myservo;
int servoPin = 13;
BluetoothSerial SerialBT;
char cmd[100];
int cmdIndex;

int X;


void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32SineWaveServo"); //Bluetooth device name (any name)
  Serial.println("The device started, now you can pair it with bluetooth!");

  myservo.attach(13);  // servo pin on the ESP32 (servo needs its own 5-6 VDC power supply 1 amp
  myservo.write(132); // throttle at lowest setting, config slider 132 min to 11 max on RoboRemoFree app, ID "t" or any character 


  cmdIndex = 0;

  X = 132;
}

void loop() {



  if (SerialBT.available()) {
    char c = (char)SerialBT.read();
    if (X == 0) {(X = 132);};



    if (c == '\r' || c == '\n') {
      cmd[cmdIndex] = 0;
      cmdIndex = 0;
      X = (atoi(cmd + 2));


      if (X == 0) {(X = 132);};  // prevents servo jamming
      myservo.write(X);

    } else {
      cmd[cmdIndex++] = c;

    }

  }
}
 
Last edited:
Impressive displays!!!!! A tremendous effort!!!!!
Thanks for the suggestions, Martin! Right now I am concentrating on the servos with Vixen. My main hobby is electric trains at Christmas. Vixen offers an easier way of automation with on-the-fly adjustments. I want to try my hand at audioanimatronics. I am learning all the ins and outs of Vixen. Writing a sequence is tedious, but a lot of fun!

Take care, Joe.
The only difference between using ESPv4 and your Arduino is that the computer running Vixen does not need to be wired to the controller. You would use DDP as the controller in Vixen and have vixen send changes just like you are trying to do today except they would go over WiFi. That means you can have more controllers with less work on the SW and more focus on the display.
 
The only difference between using ESPv4 and your Arduino is that the computer running Vixen does not need to be wired to the controller. You would use DDP as the controller in Vixen and have vixen send changes just like you are trying to do today except they would go over WiFi. That means you can have more controllers with less work on the SW and more focus on the display.

Thanks! So you program it with the Arduino IDE? I still need that variable speed library, otherwise the servo speed is too fast and jerky. So you select ESPixelStick V4 as the board?
 
Upper and lower limits are built into the ESPv4 code as well as setting the PWM frequency. The SW transforms 0-255 intensity values into a min/max range you define on the UI. The code runs on the TTGO T8, LoLin ESP32 Pro and thirty other platforms.

No we are not using the Arduino IDE. V4 has been implemented using the PlatformIO Extension to VSCode. Setup instructions are included in the readme's. You would only need to modify one file (GPIO_DEFS) for the platform you are using. That file turns on/off support for the 16PWM port servo board.
 
Upper and lower limits are built into the ESPv4 code as well as setting the PWM frequency. The SW transforms 0-255 intensity values into a min/max range you define on the UI. The code runs on the TTGO T8, LoLin ESP32 Pro and thirty other platforms.

No we are not using the Arduino IDE. V4 has been implemented using the PlatformIO Extension to VSCode. Setup instructions are included in the readme's. You would only need to modify one file (GPIO_DEFS) for the platform you are using. That file turns on/off support for the 16PWM port servo board.

Thanks, again, Martin!!!!!!!
 
A few years back I played with servos and controlling them via e1.31 and Ws 2811 pixel pcbs.
At the time I was very fortunate to have help from members of this Forum.

Martin has simplified the setup process for the Esp32 with his v4 firmware
combined with the 16 port servo board which
I'm yet to try it though .



The type/model of servo is a large part of performance .
This was the best servo I had found at the time .

 
Hi Everyone,

I hesitate to show this very short clip because I haven't done anything more than a few seconds, and it's not synced.


I lost valuable time with stupid mistakes, but this video is just to show range of movement. Boy, you really get to know your audio very well in this tedious process!

So this set-up originally (couple years ago) was built to test these tiny servos under heavy usage; I cobbled together the servos mounted on foam-core into a stage or train station platform of sorts, like patch-work, rather cramped placement, covered everything in paper so it looked a little more presentable. I needed something very compact. Unlike a large, impactful display, this is almost in miniature.

This is just a test of speeds and using Vixen. When I look at it critically, the random motion of my original code isn't very different. I don't shy away from time-consuming projects, but this progresses at a snails pace

Take care, Joe
 
Joe, I've lost track of what your goals are at the moment. It should be fairly easy to port the VarSpeedServo library to the ESP8266/ESP32 in the Arduino IDE paradigm, but it seems that Martin might be leading you away from using serial input using the Arduino IDE.
 
Joe, I've lost track of what your goals are at the moment. It should be fairly easy to port the VarSpeedServo library to the ESP8266/ESP32 in the Arduino IDE paradigm, but it seems that Martin might be leading you away from using serial input using the Arduino IDE.

You've lost track? LOL I am jumping around more than my servos!!!

So, I had gotten the Bluetooth transceiver (HC-06) to work briefly with Vixen and the Arduino in my first efforts, no physical connection to the computer. Now I cannot get that going again. However, sometimes with just the regular USB connection, serial will not initiate. I have to restart my computer, open then close and open Vixen again. BTW, the pulse effect works well -- 2 parts, the second part I invert so the result is slow movement builds the returns to the default position. Perhaps the variable speed library isn't necessary.

I'm wondering if there is a baud rate that works best? So I have tried 2 different Win 11 machines. I've lost too much time on that the wireless approach for now. (I am matching the baud rates of the transceiver, Vixen and Arduino serial, and assigning the correct ports, Arduino IDE not open. I can pair, just now serial is temperamental regardless of the connection.)

I like the Bluetooth on the ESP32 and it works well for me in other projects.

I need to focus on one idea at a time. Writing the sequences is really time consuming, as everyone knows. I need to get to the halfway point so I can copy and paste the second part without much adjusting, hopefully.

Regarding my goals -- I want to learn everything. And I am trying to add a miniature drum set for Linus; need to 3D print that.

Thanks, take care, Joe
 
Hi Folks,

Well, a breakthrough for me! So I got the ESP32 working with the servos over the built-in Serial Bluetooth, so no connection to the computer to write or play a sequence, really wirelessly!!!!! I am always leery of having my computer connected to the boards with the power supplies for the servos and relays connected. The HC-06 transceiver is rather temperamental with the Arduino. Glad the ESP32 has it built in.



Code:
#define numChannels 10
#define NUM_SERVOS 10
#include <ESP32Servo.h>
#include "BluetoothSerial.h"

//not sure if the following is needed; got it in an example piece of code
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif*

BluetoothSerial SerialBT;

Servo  myservo1;
Servo  myservo2;
Servo  myservo3;
Servo  myservo4;
Servo  myservo5;
Servo  myservo6;
Servo  myservo7;
Servo  myservo8;
Servo  myservo9;
Servo  myservo10;

void setup() {

  Serial.begin(115200);
  SerialBT.begin("ESP32VixenServo"); //any name, your choice
  
  //pinMode (11,OUTPUT);
  //digitalWrite(11,HIGH);
  
  myservo1.attach(2);  // some pins interfere with the Serial Bluetooth
  myservo2.attach(4);  //Only pins 2,4,12-19,21-23,25-27,32-33 are recommended
  myservo3.attach(12);
  myservo4.attach(13);
  myservo5.attach(32);
  myservo6.attach(33);
  myservo7.attach(25);
  myservo8.attach(26);
  myservo9.attach(27);
  myservo10.attach(14);


  myservo1.write(95);  //neutral position
  myservo2.write(95);
  myservo3.write(95);
  myservo4.write(95);
  myservo5.write(95);
  myservo6.write(95);
  myservo7.write(95);
  myservo8.write(95);
  myservo9.write(95);
  myservo10.write(95);


}


void loop() {
  while (!SerialBT.available());

  //set Vixen header to '~!'. 
  for ( ; ; )
  {
    if (SerialBT.read() == '~') {
      break;
    }
  }

  while (!SerialBT.available());
  for ( ; ; )
  {
    if (SerialBT.read() == '!') {
      break;
    }
  }

  while (!SerialBT.available());


  byte channelValues[numChannels];
  for (byte iChn = 0; iChn < numChannels; iChn++)
  {
    while (!SerialBT.available());
    channelValues[iChn] = SerialBT.read();
  }


  myservo1.write(map(channelValues[0], 0, 255, 89, 110)); //Schroeder 90-110
  myservo2.write(map(channelValues[1], 0, 255, 90, 110)); //Patty     90-110
  myservo3.write(map(channelValues[2], 0, 255, 90, 110)); //sally     90-110
  myservo4.write(map(channelValues[3], 0, 255, 90, 110)); //Linus     90-110
  myservo5.write(map(channelValues[4], 0, 255, 90, 110)); //PigPen    90-110
  myservo6.write(map(channelValues[5], 0, 255, 85, 105)); //Frnklin   85-105
  myservo7.write(map(channelValues[6], 0, 255, 90, 110)); //Lucy      90-110
  myservo8.write(map(channelValues[7], 0, 255, 90, 110)); //Chuck     90-110
  myservo9.write(map(channelValues[8], 0, 255, 75, 98)); //Snoopy    75-98
  myservo10.write(map(channelValues[9], 0, 255, 90, 110));
  //digitalWrite(11,channelValues[9]); // for relay for myservo power supply

}

So it's not elegant code, but it works, wirelessly. Some lines might not be needed. Certain pins interfere with the Bluetooth initiating, not sure if I still need to move things around.

Once I got the code on the ESP32, I disconnected from the computer and powered the board with a USB adapter. Then paired it with the computer (name you created), see what port it's connected to (with the device manager), then use that in Vixen.

Long day futzing!!! But got it working. Only problem is that I ordered 3 Arduinos and now don't need them for this, except for that variable speed library, which is awesome!

Wireless is great for me in my house; don't know what range it has outdoors or what will interfere with classic Bluetooth.

Take care, Joe.
 
Last edited:
Hi Everyone,

I hesitate to show this very short clip because I haven't done anything more than a few seconds, and it's not synced.


I lost valuable time with stupid mistakes, but this video is just to show range of movement. Boy, you really get to know your audio very well in this tedious process!

So this set-up originally (couple years ago) was built to test these tiny servos under heavy usage; I cobbled together the servos mounted on foam-core into a stage or train station platform of sorts, like patch-work, rather cramped placement, covered everything in paper so it looked a little more presentable. I needed something very compact. Unlike a large, impactful display, this is almost in miniature.

This is just a test of speeds and using Vixen. When I look at it critically, the random motion of my original code isn't very different. I don't shy away from time-consuming projects, but this progresses at a snails pace

Take care, Joe

These look great ,nice work .
Always thought the peanuts gang made Christmas .

Since you posted about code for the ESP32 and Servos , You motivated me to see if I can get something working that is pixel driven .
Ultimately the EspixelStick firmware solves this I believe , though the challenge here is part of the fun !

9 - 10 Servos is a Gpio hogg to say the least . :):
 
These look great ,nice work .
Always thought the peanuts gang made Christmas .

Since you posted about code for the ESP32 and Servos , You motivated me to see if I can get something working that is pixel driven .
Ultimately the EspixelStick firmware solves this I believe , though the challenge here is part of the fun !

9 - 10 Servos is a Gpio hogg to say the least . :):

Thanks!! This was my first effort with servos about 5 years ago:


Then this was using servos on their sides for a rocking motion:


I need to get these synced to music. I have soldered to headers on the Arduino, now I have to move them to screw terminals that might fit the ESP32 pins. I have a ton Dupont wires but many are defective and fail.

I hope I can use that main sequence on a different profile in Vixen.

If you encounter an issue on the ESP32, just try a different pin.

Take care, Joe.
 
You are doing some really cool stuff there Joe .
I always wanted to get a servo and Esp32 to do stuff like that and wirelessly !

Fortunately , I solved this dilemma .
3 days playing with this mess and I have the Esp32 mini driving pixels -rgb elements and servos together nicely .
This is fun as a project but it most likely won't fly for a time sensitive light show .

3:15 am here ,so I will clean up the code tomorrow and post it for you .

 
You are doing some really cool stuff there Joe .
I always wanted to get a servo and Esp32 to do stuff like that and wirelessly !

Fortunately , I solved this dilemma .
3 days playing with this mess and I have the Esp32 mini driving pixels -rgb elements and servos together nicely .
This is fun as a project but it most likely won't fly for a time sensitive light show .

3:15 am here ,so I will clean up the code tomorrow and post it for you .

Thanks, looking forward to seeing your code!!!
 
Back
Top