Advice on how to connect 2 serial devices on Uno

Hello Everyone's,

Completely new to Arduino. I have a question on how to hook up a HC05 Bluetooth module and a DFPlayer mp3 player together using a Uno board.

I simply want to use my voice command from my phone(already paired with my HC05) and tell the mp3 player to play a specific file.

So far I can use a sketch with my HC05 to control for example turning an LED on an off attached to a specific pin on the Uno, voice recognition works as expected.

I can also use another sketch to play a specific file on my DFPlayer with the press of a button (attached to a Input pin on the UNO).

Now the problem that I have, I don't know how to merge those 2 simple sketches together in order to have both devices( HC05 and DFPlayer being connected on the board at the same time. I understood so far that one device have to be on pin 0 and 1( hardware serial) and the other should be connected on lets say pin 2 and 3 by using the software serial library.

Where i am stuck right now is to how to tell to the Uno board in my sketch which device is connected where, and where to put those command lines so that for example the HC05 is sending its data to a specific rx tx pin and that the board after that send the specific command to the other rx and tx pin connected to the DFPlayer.

Any help greatly appreciated,

Thank You

Hi Sergio
Welcome!
Might as well get started on the right foot... Please read:
https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/681310/2
Start off by showing us the codes you want to merge(note - use code tags!), and maybe draw us a sketch of how you've got it all connected.
Thanks!

Thank You will do.

Sergio

Here is my first sketch for the HC05 Bluetooth voice command

#include <SoftwareSerial.h>
SoftwareSerial BLU(0,1);
String voice;
int Green = 4; //Connect To Pin #4
int Yellow = 2; //Connect To Pin #2
int Red = 3; //Connect To Pin #3
void allon()
{
digitalWrite(Red, HIGH);
digitalWrite(Yellow, HIGH);
digitalWrite(Green, HIGH);
}
void alloff()
{
digitalWrite(Red, LOW);
digitalWrite(Yellow, LOW);
digitalWrite(Green, LOW);
}

void setup()

{
Serial.begin(9600);
BLU.begin(9600);
pinMode(Red, OUTPUT);
pinMode(Yellow, OUTPUT);
pinMode(Green, OUTPUT);
}
void loop()
{
while (Serial.available()) //Check if there is an available byte to read
{ delay(10); //Delay added to make thing stable
char c = Serial.read(); //Conduct a serial read
if (c == '#')
{
break; //Exit the loop when the # is detected after the word
}
voice += c;
}
if (voice.length() > 0)
{
if(voice == "*all led off")
{allon();
}
else if(voice == "*all led on")
{
alloff();
}
else if(voice == "*switch on red")
{
digitalWrite(Red,HIGH);
}
else if(voice == "*yellow on")
{
digitalWrite(Yellow,HIGH);
}
else if(voice == "*green on")
{
digitalWrite(Green,HIGH);
}
else if(voice == "*red on")
{
digitalWrite(Red,LOW);
}
else if(voice == "*yellow off")
{
digitalWrite(Yellow,LOW);
}
else if(voice == "*green off")
{
digitalWrite(Green,LOW);
}
voice=""; //Reset variable
}
}

And here is my 2nd sketch for the DFPlayer simple play command

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

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

 // Create the Player object
 DFRobotDFPlayerMini player;


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

  // Start communication with DFPlayer Mini
  if (player.begin(softwareSerial)) 
 {
 Serial.println("OK");

  // Set volume to maximum (0 to 30).
  player.volume(20);
    
  } 
  else {
  Serial.println("Connecting to DFPlayer Mini failed!");
  }
  }

  void loop() 
  {
  // Play the first MP3 file on the SD card
    player.play(1);
    delay(10000);
   }

I didn't had the time to draw the schematic to show you the pin connection, but it is simply the HC05 connected to the pin 0 and 1 and the DFPlayer to the pin 2 and 3

Do not run Software serial on the hardware serial pins. Just use Serial. You may have to disconnect any device connected to pins 0 and 1 in order to upload programs.

If you connect a device to pina 0 and 1, when you print or write to Serial the device on pins 0 and 1 will get the data and Serial will get the data from that device.

If you connect an instance of SoftwareSerial to pins 2 and 3 named softwareSerial, when you print or write to softwareSerial the device connected to pins 2 and 3 will get the data.

Thanks for responding.

I also read somewhere that I can define 2 serial set of pins, is that possible on the UNO?

Lets say I want to connect my HC05 Bluetooth on pin 2 and 3 and my DFPlayer on pin 4 and 5 of the UNO?

I understand that they can't read or write at the same time but for what I am using them it should not be a problem since the program would be very short.

If that is an option, what code line should i put in there so it will associate the HC05 Bluetooth to the pin 2 and 3 and associate the mp3 player to the the pin 4 and 5 ?

Thank You

See the SoftwareSerial library, and check out the Two Port Receive example.

@sergio2022, per the title of this thread: "Advice on how to connect 2 serial devices on Uno". The best advice is is don't. Instead move to a board that provides enough Hardware UARTs to connect your devices and still leaves the default Serial connection (another Hardware UART or native USB) connection available for uploading code and debugging. Doing this will save you the trouble of dealing with multiple instances of SoftwareSerial (I don't even like to use a single instance).

The Arduino Mega comes to mine. However, my go-to board for something like this would be a Teensy 3.2. It's smaller, more powerful, has more resources, and is cheaper than the Mega.

@sergio2022
Although it is possible, using more than one SoftSerial instance in code is not a good idea.
There are many boards with more than one Hardware Serial - Arduino Mega, Arduino Nano Every etc