DSC04263

SoftwareSerial conflicting with Servo interrupt

DSC04263

INTRODUCTION

Did you ever know that software serial cannot operate during the servo signal is high? WHY?? This is because of signal collision will occur between both signals when we give a command of software serial to operate during signal of the servo is high.

Problem: Most of the people said, the problem is because of the timer conflict between the software serial and the servo interrupt.

servo ON

When the servo is On, the signal is high.

servo off

When the servo is Off, the signal is low.

HARDWARE REQUIRED

SOFTWARE REQUIRED

  • Arduino software

ADDITIONAL INFORMATION

QUESTION 1: How we can overcome this problem?

  • By Theory.

Software serial cannot operate when the servo signal is high. This is because there will be signal collision, which the signal is confusing to operate two signal at the same time. The solution is, we should separate both of the signal timing operation. For your information, the pulse does not represent the voltage applied, the pulse just the conveying information.

  • By programming.

1. adding detach() in the coding.

This will make the servo stop during detach() to give space for software serial to operate.

Example:

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

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

2. put delay in the coding.

The servo is not shaking when the delay is >=800 in the coding.

Explanation: when the time delay is short, the servo and the software serial conflicting will occur. 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.This tutorial are using Arduino pin number 6 and 5 for servo and pin number 2 and 3 for PS2 Shield.  If you use PS2 shield, make sure Tx and Rx pin number are not used for servo connection.

CIRCUIT DIAGRAM

fritzing

The connection of the circuit:

Pin number on Arduino Servo
5 1
6 2

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

DSC04256

STEP1

Stack the Arduino with the Cytron PS2 Shield correctly.

DSC04258

STEP2

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

DSC04260

STEP3

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

DSC04261

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.

[code lang=”c” highlight=””]
#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);
}[/code]

THE SERVO IS NOT SHAKING

  • By Delay >800

[code lang=”c” highlight=””]

#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);
}[/code]

  • Delay in the coding is <=800 and detach() is added in the coding

[code lang=”c” highlight=””]
#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);
}[/code]

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 Post

Arduino Uno with ESPresso Lite V2

ESPresso Lite V2 with AT Firmware

IntroCytronProject1-concentrate

Controlling Servo via Android Smartphone

GABUNG

How to choose a suitable motor?

2015-07-06 15.10.32

Using Cytron PS2 Shield with Arduino Edubot

3 thoughts on “SoftwareSerial conflicting with Servo interrupt

Leave a Reply

Your email address will not be published. Required fields are marked *