287Views8Replies

Author Options:

Arduino/ESP8266/servo noise when idle Answered

Hello everyone

My project involves a robot with LEDs, servos and an Arduino with a Sparkfun ESP8266 shield on top. Everything else works fine, I send web requests to the robot and LEDs are blinking or the servos are moving.

The problem is that when idle I have noise on the servo. I have tried 
1. Pull-up and pull-down resistors
2. Providing external power to the servos
3. Putting an aluminium foil between Arduino and the shield
and the problem persists

Commenting out parts of my code I isolated that the noise appeared upon uncommenting this part of my code

void serverDemo(){
ESP8266Client client = server.available(500);
}

but I cannot understand why. 

Could you please propose me any idea?

Below I have the entire code



#include <SoftwareSerial.h>
#include <SparkFunESP8266WiFi.h>
#include <Servo.h>

const char mySSID[] = "xxxx";
const char myPSK[] = "xxxx";
ESP8266Server server = ESP8266Server(80);
Servo myservo1;
Servo myservo2;
int pos = 0;    // variable to store the servo position
int pos2 = 180;
int led1 = 12;
int led2 = 13;

const String html = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n\r\n"
"<!DOCTYPE HTML>\r\n"
"<html>\r\n"
"</html>\r\n";

void setup(){
Serial.begin(9600);
initializeESP8266();
connectESP8266();
displayConnectInfo();
serverSetup();
myservo1.attach(11); 
myservo2.attach(10); 
pinMode(led1, OUTPUT);    
pinMode(led2, OUTPUT);
//testing
myservo1.write(45);  
delay(15);
myservo1.write(80);      
}

void loop(){
serverDemo();
}

void initializeESP8266(){
int test = esp8266.begin();
if (test != true)
{
Serial.println(F("Error talking to ESP8266."));
errorLoop(test);
}
Serial.println(F("ESP8266 Shield Present"));}

void connectESP8266(){
int retVal = esp8266.getMode();
if (retVal != ESP8266_MODE_STA)
{
retVal = esp8266.setMode(ESP8266_MODE_STA);
if (retVal < 0)
{
Serial.println(F("Error setting mode."));
errorLoop(retVal);
}
}
Serial.println(F("Mode set to station"));
retVal = esp8266.status();
if (retVal <= 0)
{
Serial.print(F("Connecting to "));
Serial.println(mySSID);
retVal = esp8266.connect(mySSID, myPSK);
if (retVal < 0)
{
Serial.println(F("Error connecting"));
errorLoop(retVal);
}
}}

void displayConnectInfo(){
char connectedSSID[24];
memset(connectedSSID, 0, 24);
int retVal = esp8266.getAP(connectedSSID);
if (retVal > 0)
{
Serial.print(F("Connected to: "));
Serial.println(connectedSSID);
}
IPAddress myIP = esp8266.localIP();
Serial.print(F("My IP: ")); Serial.println(myIP);}

void serverSetup(){
server.begin();
Serial.print(F("Server started! Go to "));
Serial.println(esp8266.localIP());
Serial.println();}

void errorLoop(int error){
Serial.print(F("Error: ")); Serial.println(error);
Serial.println(F("Looping forever."));
for (;;)
;}

void serverDemo(){
ESP8266Client client = server.available(500);
if (client)
{
String req;
while (client.connected())
{
if (client.available())
{
String c = client.readStringUntil('\r');
req += c;
Serial.println(req);
if (req.indexOf("hands") != -1){
hands_up();
}
if (req.indexOf("leds") != -1){
leds_flash();
}
}
}
client.print(html);
delay(1);
client.stop();
}
}

void leds_flash(){
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
delay(200);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
delay(200);
}

void hands_up(){
for (pos = 0; pos <= 30; pos += 1) {
pos2 = 180 - pos;
myservo1.write(pos2);            
myservo2.write(pos);            
delay(15);                     
}
for (pos = 30; pos >= 0; pos -= 1) {
pos2 = 180 - pos;
myservo1.write(pos2);            
myservo2.write(pos);            
delay(15);                     
}
}

Thank you!

Discussions

What exactly do you mean by "noise on the servo" ?

I mean that when idle the servo moves up and down fast just like the pin is floating. This is why I have tried whatever I found about this but to no result.

That does not sound right, the servo should be stable without a signal.
You do have a seperate power supply for the servos??
I only see two possible faults:
1) Cheap servos with crappy electronics inside - you never know what you get these days...
2) Noisy PWM impulse, here you could writeyour own instead of relying on the Arduino interrups.

Without oscilloscope hard to troubleshoot.
Did you check what Sparkfun offers in return? I mean if their code is listed the I think you are not the first to us it and someone might have had the problem fixed already.

Thank you very much for your thoughts and time.

Yes, I have a different power supply for the servos.

Cheap servos, well that is a thing. So far I have been using SG90 without problems. Perhaps this project has crossed its limits. Is there any inexpensive and reliable servo you could propose me? I could of course buy and try but if I could restrict my options this would be great :-)

I am not sure I have understood the rest. I surely do not have an oscilloscope to try myself. You mean instead of using Sparkfun library for ESP8266, to write my own?

Again thank you

Driving a servo only requires the PWM pulse and the right amount of them.
First I would check if you can use the servos with that library in some other code.
Maybe something simple like flashing some random LED'S and after each run to make the servo go bit in defined steps.
If that works it could mean the interupts used for the servos hav timing problem in your original code.
Could be due to the rest being too CPU hungry.
If that causes similar noise problems I would try a simple servo code from the Arduino playground just to see if the servos work fine with that.
In case they do abandon the sparkfun library and try a different one.
Last resort would be trying a different servo, but can't recommend any as I don't use them much.

Writing your own routing to generate PWM pulses to drive the servo instead of the original routine or librarywill make sure you have the signal length and duration to driv them properly - after some basic testing to see how fast you can go.
But that is for the well advanced and should not be required for less than 5 servos.

Thank you

Before adding the wifi shield in my project, I had tried with LEDs and servos only and it was OK. Beside that, if you notice closely in the code above, I have added some "testing" lines in setup void function for the servo. These were for the times I commented the ESP8266 lines. Just before uncommenting the "ESP8266Client client = server.available(500)" line the servos were fine. So, it seems to me that this line somehow is associated with the noise.

CPU hungry - seems plausible

Try different library - I am considering searching for another shield or generally wifi module :-(

Thank you very much for your proposals and guidelines. I will come back with feedback.

Well that explains some things...
The Wifi stuff needs accurate timing, so it will be prefered over other things.
In return the servo controls might get distorted or badly timed signals.
Check the Arduino playground for options on how to control servo motors without the library you use and see if it improves on the problem.
Better than buying new stuff if you ask me ;)

Your reference to timing was crucial.

I made some more google searching and I found some more discussions. Among them, even if not explicitly for timing, a guy proposed to keep the servos detached all the time and attach only when needed. Well, this idea suits well with my project. Servos are attached on a specific web request only, they move and then they are detached.

Thank you very much for all the information, ideas and the insight you have provided me. I really appreciate it.