I have an interesting problem. I want to control ESC and servo with Arduino. I'm using HC-12 to communicate with 2 Arduino and I'm using SoftwareSerial on the nano.

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

SoftwareSerial mySerial(2, 3); // RX, TX
Servo esc,servo1;

String okunan;
int escval;

void setup() {

void loop() {
   while (mySerial.available()) {
    delay(3);  //delay to allow buffer to fill 
      char c = mySerial.read();  //gets one byte from serial buffer
      okunan += c; //makes the string readString

    //escval= map(okunan.substring(0,okunan.indexOf(',')).toInt(),0,1023,,2000);




When I use a potentiometer, It works successfully. But When I use with serial and if three digit value(between 0-179, for example, 120) comes from serial(HC-12), motor works and stops continuous and irregular. If the value is under three digit there is no problem.

Also, if I delete the ESC variable(servo) or add a new servo to code, it always starts and stops interestingly(Using or not it doesn't matter. If I add Servo servo2 etc., it is going crazy.

I've tried on Uno. That is the same.

Thanks in advance...

  • Did you look at the source code for SoftwareSerial and Servo? What does SoftwareSerial do when it detects a start bit? Receives the character in the ISR. And how does that affect Servo? It blocks the Servo ISR and delays pulse generation. Commented Dec 9, 2017 at 16:29

1 Answer 1


Like Mikael Patel said, SoftwareSerial disables interrupts for the entire character receive time. Bad juju.

This answer lists the alternatives:

  • HardwareSerial is always the best. Simply connect the device to pins 0 & 1 and use the pre-defined Serial variable. On some Arduinos, there are extra HardwareSerial ports, called Serial1, Serial2, etc. The UNO only has Serial. Serial would not interfere with the Servo.

  • AltSoftSerial is the best of the software serial libraries. Only one instance is allowed, and it is must be used on the Input Capture pins (pins 8 & 9 for a Nano). It would not interfere with the Servo.

  • My NeoSWSerial is next best. It works on any two pins, but only at baud rates 9600, 19200 and 38400. Receiving characters does not disable interrupts, but transmitting does (i.e., TX would interfere with the Servo).

  • SoftwareSerial is the worst choice. It works on any two pins, but it is very inefficient. It disables interrupts for the entire time that a character is being sent OR received, and it cannot transmit and receive at the same time (unlike all other choices). This can interfere with other parts of your sketch, other device communications, or with libraries (e.g., Servo).

In your case, I would suggest connecting the HC-12 to pins 0/1 and use Serial. No software serial library required, but you will have to disconnect pin 0 to upload new sketches over USB. Some people put a switch in that wire so it's easy to upload a new sketch.

If you need debug prints, I would suggest using AltSoftSerial and an extra TTL Serial-to-USB adapter (aka FTDI module) to your PC. Then print your debug messages to the AltSoftSerial variable:

#include <AltSoftSerial.h>
AltSoftSerial debugPort; // always pins 8 & 9


void loop()
  if (something) {
    debugPort.println( F("something happened!") );

Or, if the HC-12 connection is receive only, don't connect pin 1 (TX). Then your debug prints won't go to the HC-12.

BTW, don't use delay and don't use String. They will get you into trouble sooner or later.

  • Thank you very much. Can I hardware serial on nano or uno? Do I have to eject USB? Commented Dec 9, 2017 at 20:59
  • Yes, you can use HardwareSerial (0 & 1) on a Nano or UNO. You only have to disconnect pin 0 from the HC-12 when you upload a new sketch over USB. You do not have to disconnect the USB cable when running the sketch (reconnect pin 0 after the upload completes).
    – slash-dev
    Commented Dec 10, 2017 at 1:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.