Bukit Mertajam, Penang

SoftwareSerial conflicting with Servo interrupt

INTRODUCTION

Have you ever noticed that Software Serial cannot operate when your Arduino is running a servo motor? That is because of a timer conflict between Software Serial and Servo Interrupt.

REQUIRED HARDWARE

SOFTWARE REQUIRED

ADDITIONAL INFORMATION

QUESTION 1: How we can overcome this problem?

  • By programming.

1. Use the detach() function after every sending servo command.

The detach function stops communication between servo and Arduino to give operational time to software serial.

Example:

servo.detach();[correct]
servo.detach(6);[incorrect]

NOTE: Don’t put anything in the (). Just leave it blank.

 

3. Use delay in the coding.

The servo won’t shake when the delay is >=800 in the coding.

Explanation: when the time delay is short, the servo and the software serial will conflict. This is compared to the longer time delay, the signal collision occurred is lesser.

Can detach() used to remove the vibrate servo while <=800 delay??
Let’s figure it……

Answer: Yes we can. By adding detach() in the coding to make the servo stop even during the short time delay.

Question 2: Can we use other than pin number 9 and 10 on Arduino to control servo?

Yes, you can use all 20 input and output ports on Arduino to control servo. In this tutorial, we are using Arduino pin number 5 and 6 for servo and pin number 2 and 3 for PS2 Shield. If you are using the PS2 shield, just make sure Tx and Rx pin number are not used for servo connection.

CIRCUIT DIAGRAM

The connection of the circuit:

Pin number on ArduinoServo
51
62

For PS2 shield, place mini jumper to pin 2 and 3. 

NOTE: Adapter 5V 2A is used for this project. This to ensure enough current supplied to the servo. 

PROCEDURE

STEP1

Stack the Arduino with the Cytron PS2 Shield correctly.

STEP2

Construct a circuit for the servo on the breadboard. You can refer the connection of the circuit above.

STEP3

Male to male jumper wire is used to connect from servo circuit to the Cytron PS2 Shield.

STEP4

Plug in the wireless PS2 to the Cytron PS2 Shield.

Upload the coding to the Arduino and you can start playing with it. 🙂

You can download the coding here.

CODE OVERVIEW

There are three sample Arduino sketces. First sample is to show that when you use delay <= 800 and no “detach()” in the coding it will cause the servo to shake. For second sample, servo is no longer shaking when use delay >= 800 and put “detach()” in the coding. The last sample is, the delay in the programming is >=800 so, the servo is not shaking.

THE SERVO IS  SHAKING

The delay is <=800 and no detach() in the coding.

#include Servo.;
#include SoftwareSerial.h;
#include Cytron_PS2Shield.h;
int X;

Cytron_PS2Shield ps2(2, 3); // SoftwareSerial: Rx and Tx pin
Servo servo1; // Define servo 1
Servo servo2; // Define servo 2

void setup()
{

Serial.begin(9600);
ps2.begin(9600);

}

void loop()
{

X = ps2.readButton(PS2_JOYSTICK_RIGHT_X_AXIS);

servo1.attach(5); // Set servo1 to digital pin 5
servo2.attach(6); // Set servo2 to digital pin 6

Serial.print(X);
Serial.print(&amp;quot; &amp;quot;);
if (X==0)
{
first(); // Function to move the servo 180 degree if joystick ps2=0
}
else
{
third(); //Function to make the servo static if the value from the joystick is not 0.
}

}
void first() {
servo1.write(180);
servo2.write(180);
delay(800);
}

void third(){
servo1.write(0);
servo2.write(0);
delay(800);
}

THE SERVO IS NOT SHAKING

  • By Delay >800
#include Servo.h;
#include SoftwareSerial.h;
#include Cytron_PS2Shield.h;
int X;

Cytron_PS2Shield ps2(2, 3); // SoftwareSerial: Rx and Tx pin
Servo servo1; // Define servo 1
Servo servo2; // Define servo 2

void setup()
{
Serial.begin(9600);
ps2.begin(9600);
servo1.attach(5); // Set servo1 to digital pin 5
servo2.attach(6); // Set servo2 to digital pin 6
}

void loop()
{ // Loop through motion tests
X = ps2.readButton(PS2_JOYSTICK_RIGHT_X_AXIS);
Serial.print(X);
Serial.print(&amp;quot; &amp;quot;);

if (X==0)
{
first(); // Function to move the servo 180 degree if joystick ps2=0

}
else
{
third(); //Function to make the servo static if the value from the joystick is not 0.

}

}
void first() {

servo1.write(180);
servo2.write(180);
delay(2000);
}

void third(){

servo1.write(0);
servo2.write(0);
delay(2000);
}
  • Delay in the coding is <=800 and detach() is added in the coding
#include Servo.h;
#include SoftwareSerial.h;
#include Cytron_PS2Shield.h;
int X;

Cytron_PS2Shield ps2(2, 3); // SoftwareSerial: Rx and Tx pin
Servo servo1; // Define servo 1
Servo servo2; // Define servo 2

void setup()
{

Serial.begin(9600);
ps2.begin(9600);

}

void loop()
{
servo1.detach(); //detach the servo1
servo2.detach(); //detach the servo2
X = ps2.readButton(PS2_JOYSTICK_RIGHT_X_AXIS);

servo1.attach(5); // Set servo1 to digital pin 5
servo2.attach(6); // Set servo2 to digital pin 6

Serial.print(X);
Serial.print(&quot; &quot;);
if (X==0)
{
first(); // Function to move the servo 180 degree if joystick ps2=0
}
else
{
third(); //Function to make the servo static if the value from the joystick is not 0.
}

}
void first() {

servo1.write(180);
servo2.write(180);
delay(800);
}

void third(){
servo1.write(0);
servo2.write(0);
delay(800);
}

If you have further inquiry about this tutorial, please post it in our technical forum as we seldom check the comment section in tutorial site.

VIDEO

Buy

Related Posts

Comments (3)

What does this line mean? it cause error when compiling

What does this line mean? it cause error when compiling

Serial.print(&quot; &quot;);

Leave a comment