DFPLAYER MINI Makes my Servo move at ARDUINO startup?

Hello Everyone,

I have the DFPlayer Mini and am trying to use my Arduino Mega to play a few sounds and make a servo move with the use of a button. Everything works just the way I want it to, however whenever I plugin the Arduino the servo moves 60 degrees one direction and back again. The servo was not doing this until I added the DFPlayer code.

Any assistance would be greatly appreciated.

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

Servo Myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 10;    // variable to store the servo position

// Use pins 2 and 3 to communicate with DFPlayer Mini
static const uint8_t PIN_MP3_TX = 19; // Connects to module's RX 
static const uint8_t PIN_MP3_RX = 18; // Connects to module's TX 
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);

// Create the Player object
DFRobotDFPlayerMini player;

void setup() {
  pinMode(8,INPUT);
  Myservo.attach(9);  // attaches the servo on pin 9 to the servo object

  // Init USB serial port for debugging
  Serial.begin(9600);
  // Init serial port for DFPlayer Mini
  softwareSerial.begin(9600);

  // Start communication with DFPlayer Mini
  (player.begin(softwareSerial));
  
    // Set volume to maximum (0 to 30).
    player.volume(25);
    // Play the first MP3 file on the SD card
    player.play(1);
}

void loop()
{
  if(digitalRead(8)==LOW){
    player.play(2);
    Myservo.write(30); //ending position of arm to the back of bee, higher the number futher back toward the bee it will go. 11 - 30 only.
    delay(2000);
    Myservo.write(10); //ending position of the arm to the front of bee, higher the number futher back toward the bee it will go. 29 to 10 only.
    delay(2000);
    Myservo.write(30);
    delay(2000);
  }
  else
    
Myservo.write(0);
  
}

As it is written here, Pin 18 cannot be used as Rx pin on Arduino Mega:

Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).

I don't know what happens if you use another pin (pin 18), maybe it interferes with pin 9?

But why do you use SoftwareSerial at Arduino Mega at all? The Mega has sufficent hardware serial lines.

Thank you MicroBahner, I have tried other PINs so that is not the issue. How would I rewrite the code to not use the SoftwareSerial. If you can direct me to the right location I will start working on the code. :slight_smile:

Just use them like you are doing with Serial0 (aka Serial) and wire it up to the proper pins

void setup() {
  pinMode(8,INPUT);
  Myservo.attach(9);  // attaches the servo on pin 9 to the servo object

  // Init USB serial port for debugging
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial2.begin(9600);
  Serial3.begin(9600);
...

@montgomery4512, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with your project; see About the Installation & Troubleshooting category.

This line moves your servo to 90. I suspect that adding the DFPlayerMini is just allowing your servo to move a noticeable amount before you reach "Myservo.write(0);". To fix that, put:
Myservo.write(0);
just before:
Myservo.attach(9);

Just after. First you attach, then set a value

Nope. It's perfectly OK to set the desired position before you turn on the servo pulses. Why jerk the servo around by telling it to go to 90 when you want it to start at 0? :slight_smile:

1 Like

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