Twitching servo using softwareserial

Hi.
I'm making a trollingmotor control with 2 arduino nano and 2 hc-05. But when everthing is connected the servo twitches. After googling i find out that softwareserial is the problem and i want to change to hardwareserial instead, but i don't know how to do it.

Here is the sketch for the receiver

#include <SoftwareSerial.h>

#define BT_SERIAL_TX 4

#define BT_SERIAL_RX 3

SoftwareSerial BluetoothSerial(BT_SERIAL_TX, BT_SERIAL_RX);

#include <Servo.h>

Servo myservo;

unsigned int val ;

void setup()

{

Serial.begin(9600);

BluetoothSerial.begin(9600);

myservo.attach(10);

}

void loop() // run over and over

{

if (BluetoothSerial.available()>0){

val = BluetoothSerial.read();

myservo.write(val);

}

}

And the transmitter

#include <SoftwareSerial.h>
#define BT_SERIAL_TX 4
#define BT_SERIAL_RX 3
SoftwareSerial BluetoothSerial(BT_SERIAL_TX, BT_SERIAL_RX);
int potpin = A0;
  
unsigned int val;
void setup()
{
 Serial.begin(9600);
 BluetoothSerial.begin(9600);
}
void loop()
{
 val = analogRead(potpin);
 val = map(val, 0, 1023, 0, 179);
 BluetoothSerial.write(val);
  
 delay(100);
}

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags).

edit the post and select the code part and press the </> icon in the tool bar to mark it as code. It's barely readable as it stands. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)

you may get more relevant responses if you retitle the thread to include your software serial question

the arduino nano has only one hardware serial port and it's used to upload the code so you would have to unplug the BT module every time you want to upload the code.

Software Serial should work at 9600 bauds , just don't use the shotgun approach. Send the new value only when it has changed to minimise traffic. (assuming you don't have to update at fast speed)

Hi,
This might help ServoTimer2 Library, it doesn't use the same timer as software serial I gather.
https://create.arduino.cc/projecthub/ashraf_minhaj/how-to-use-servotimer2-library-simple-explain-servo-sweep-512fd9

Tom... :smiley: :+1: :coffee: :australia:

Hi again.
I finally solved the problem by using the PWMservo library, now the servo goes smooth and no twitches.
Thank you for all your help.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.