How to use serial in raspberry pico with arduino ide

I am using raspberry pico in arduino ide 18.2, i use the library :slight_smile:

#include "Arduino.h"
#include <DFPlayerMini_Fast.h>

#ifdef ARDUINO_ARCH_MBED_RP2040
// For the Arduino MBedOS Core, we must create Serial2
UART Serial2(0,1, NC, NC);
#else
/For the Earl Philhower core ( https://github.com/earlephilhower )
// Serial2 is already defined.
#endif

int buttonPrev     = 10;
int buttonNext     = 11;
int buttonVolMenos = 12;
int buttonVolMais  = 13;
int estadoBt       = 0;

DFPlayerMini_Fast myMP3;

void setup()
{
   pinMode(LED_BUILTIN, OUTPUT);
   pinMode(buttonNext,INPUT_PULLUP);
   pinMode(buttonPrev,INPUT_PULLUP);
   pinMode(buttonVolMenos,INPUT_PULLUP);
   pinMode(buttonVolMais,INPUT_PULLUP);

Serial2.begin(9600);
  myMP3.begin(Serial2);
  //Ajusta o volume para 19"
  myMP3.volume(19);
  delay(10);
  
  myMP3.play(1); // toca a primeira musica
void loop()
{
if(!myMP3.isPlaying()){
    myMP3.playNext();
    delay(2000);
 }

  estadoBt = digitalRead(buttonPrev); 
  if (estadoBt == LOW ) {
     myMP3.playPrevious();
     delay(2000);
  }
  delay(100);

  estadoBt = digitalRead(buttonNext); 
  if (estadoBt == LOW ) {
     myMP3.playNext();
     delay(2000);
  }
  delay(100);
  
  estadoBt = digitalRead(buttonVolMenos); 
  if (estadoBt == LOW ) {
     myMP3.decVolume();
  }
  delay(100);
  
  estadoBt = digitalRead(buttonVolMais); 
  if (estadoBt == LOW) {
     myMP3.incVolume();
  }
  delay(100);
}

i can't make the dfplayer module work with raspberry pico, i already tried with arduino uno and it works, but using softwareserial.h, another thing is that i don't need to use serial by software with raspberry pico i just wanted to know which serial pinout works in raspberry pico with arduino ide and if i can initialize as serial.begin(9600)

Do you need a serial to communicate to the PC Arduino Monitor on USB or to other module such as DFPlayer via board pins ?
For USB Arduino Monitor you should initialise and use Serial instance.

I do not have <DFPlayerMini_Fast.h> but in the code, I missed a "/" in

/For the Earl Philhower core ( earlephilhower (Earle F. Philhower, III) · GitHub )

and a "}" at the end of

myMP3.play(1); // toca a primeira musica

i want use dfplayer, i don´t need serial with pc and arduino

What's RP2040 core do you use? `Arduino MBedOS Core or Earl Philhower one?

the code is incomplete but the code conplete is:

#include "Arduino.h"
#include <DFPlayerMini_Fast.h>

//TODOS OS COMANDOS DO MÓDULO ESTÃO AQUI
//void playNext();
//void playPrevious();
//void play(uint16_t trackNum);
//void stop();
//void playFromMP3Folder(uint16_t trackNum);
//void playAdvertisement(uint16_t trackNum);
//void stopAdvertisement();
//void incVolume();
//void decVolume();
//void volume(uint8_t volume);
//void EQSelect(uint8_t setting);
//void loop(uint16_t trackNum);
//void playbackSource(uint8_t source);
//void standbyMode();
//void normalMode();
//void reset();
//void resume();
//void pause();
//void playFolder(uint8_t folderNum, uint8_t trackNum);
//void volumeAdjustSet(uint8_t gain);
//void startRepeatPlay();
//void stopRepeatPlay();
//void repeatFolder(uint16_t folder);
//void randomAll();
//void startRepeat();
//void stopRepeat();
//void startDAC();
//void stopDAC();
//void sleep();
//void wakeUp();
//
//bool isPlaying();
//int16_t currentVolume();
//int16_t currentEQ();
//int16_t currentMode();
//int16_t currentVersion();
//int16_t numUsbTracks();
//int16_t numSdTracks();
//int16_t numFlashTracks();
//int16_t currentUsbTrack();
//int16_t currentSdTrack();
//int16_t currentFlashTrack();
//int16_t numTracksInFolder(uint8_t folder);
//int16_t numFolders();
//
//void setTimeout(unsigned long threshold);
//void findChecksum(stack *_stack);
//void sendData();
//void flush();
//int16_t query(uint8_t cmd, uint8_t msb=0, uint8_t lsb=0);
//bool getStatus();
//bool parseFeedback();
//
//void printStack(stack _stack);

////////
/*
#if !defined(UBRR1H)
#include <SoftwareSerial.h>
//SoftwareSerial mySerial(11, 10); // RX, TX
SoftwareSerial mySerial(9,8); // RX, TX

#endif
*/
#ifdef ARDUINO_ARCH_MBED_RP2040
// For the Arduino MBedOS Core, we must create Serial2
UART Serial2(0,1, NC, NC);
#else
// For the Earl Philhower core ( https://github.com/earlephilhower )
// Serial2 is already defined.
#endif

int buttonPrev     = 10;
int buttonNext     = 11;
int buttonVolMenos = 12;
int buttonVolMais  = 13;
int estadoBt       = 0;

DFPlayerMini_Fast myMP3;

void setup()
{
   pinMode(LED_BUILTIN, OUTPUT);
   pinMode(buttonNext,INPUT_PULLUP);
   pinMode(buttonPrev,INPUT_PULLUP);
   pinMode(buttonVolMenos,INPUT_PULLUP);
   pinMode(buttonVolMais,INPUT_PULLUP);
/*
#if !defined(UBRR1H)
  mySerial.begin(9600);
  myMP3.begin(mySerial);
#else
  Serial.begin(9600);
  myMP3.begin(Serial1);
#endif
*/
  Serial2.begin(9600);
  myMP3.begin(Serial2);
  //Ajusta o volume para 19"
  myMP3.volume(19);
  delay(10);
  
  myMP3.play(1); // toca a primeira musica
}

void loop()
{
  //digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  //delay(1000);                       // wait for a second
  //digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
 // delay(1000); 

 //digitalWrite(LED_BUILTIN, HIGH);
 if (buttonPrev==LOW)
 {
  digitalWrite(LED_BUILTIN,HIGH);
  /*
  delay(200);
  digitalWrite(LED_BUILTIN, LOW);
  delay(200);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(200);
  digitalWrite(LED_BUILTIN, LOW);
  delay(200);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(200);
  digitalWrite(LED_BUILTIN, LOW);
  delay(200);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(200);
  digitalWrite(LED_BUILTIN, LOW);
  delay(200);
  */
  
 }
 if(!myMP3.isPlaying()){
    myMP3.playNext();
    delay(2000);
 }

  estadoBt = digitalRead(buttonPrev); 
  if (estadoBt == LOW ) {
     myMP3.playPrevious();
     delay(2000);
  }
  delay(100);

  estadoBt = digitalRead(buttonNext); 
  if (estadoBt == LOW ) {
     myMP3.playNext();
     delay(2000);
  }
  delay(100);
  
  estadoBt = digitalRead(buttonVolMenos); 
  if (estadoBt == LOW ) {
     myMP3.decVolume();
  }
  delay(100);
  
  estadoBt = digitalRead(buttonVolMais); 
  if (estadoBt == LOW) {
     myMP3.incVolume();
  }
  delay(100);
}

but i compile the code and have error, i am using serial library wrong, but i don´t understand the use this library in pico.
error: Serial2' was not declared in this scope

Could you show your complete error output?

now i am compile and not have error

i have install the Earl Philhower library, but i don´t know this serial is rigth, with arduino uno i am using Softserial and is ok

Connect your DFPlayer to pins 8 and 9 of RP2040 board.
You don't require to change anything in the code - it looks correct.

i just connect the dfplayer and raspberry pico and 4 button, and i am power with external power 5v, i have vcc 5v in Vsys off raspberry pico and protoboard power for dfplayer. but when on the circuit the dfplayer do for son and go to off, the pico this on and dfplayer off.

when I feed the circuit, initially the dfplayer wants to play the first music, but very quickly the dfplayer turns off, the light doesn't turn on anymore and the raspberry stays on.

I thought the dfplayer was burned, but then I make the same circuit with arduino and it works normally. the only difference is that I put a 10k resistor in tx of the dfplayer.

please read the post #11

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