Example Code for Arduino-I2C Control

Last revision 2025/12/30

This page provides an example code for Arduino-I2C control.

Hardware Preparation

  • Arduino board
  • DFR0112 Player
  • IIC cables
  • Micro SD card (1 GB, FAT formatted)

Software Preparation

  • Arduino IDE (any version supporting the Wire library)
  • Wire library (included in the Arduino IDE by default)

Wiring Diagram

DFR0112 connection diagram---IIC

Other Preparation Work

  • The sd card must be formated at FAT format.
  • YOU MUST Create a "sound" directory
  • put mp3/wav/midi files under the SD card
  • make sure the length of file name do not exceed 8 letters.
  • Put jumper to IIC mode.
  • Connect the player to Arduino board via IIC.
  • Burn the sample code to the board.
  • Open the serial monitor of Arduino and send commands:
    • p--pause
    • s--continue
    • n--next song
    • u--previous song
    • m--play song named"yes"
    • Note that no return except "OK" is returned under IIC mode.

Sample Code


#include <Wire.h>
#include <stdlib.h>

#define ArduinoPlayer_address 0x35  //ArduinoPlayer I2C address 0x35 (default)

void TwiSend(const char  *cmd)  //I2C Command
{
  char len = 0;
  len = strlen(cmd); //Calculate the length of the command
  Wire.beginTransmission(ArduinoPlayer_address); // ArduinoPlayer I2C address
  while(len--)
  {
    Wire.send(*(cmd  ));
  }
  Wire.endTransmission();    // stop transmitting
}
void setup()
{
  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(9600);
  delay(2000);//Wait for 2 seconds
  Serial.println("Ready");
  TwiSend("\\:v 255\\r\\n");    // set the volume, from 0 (minimum)-255 (maximum)
}

//Receive control command from serial
void loop()
{
  int val;
  if(Serial.available() > 0)
  {
    val=Serial.read();
    switch(val)
    {
    case 'p':      // Pause
      TwiSend("\\:p\\r\\n");
      Serial.println("OK");
      break;
    case 's':     // Continoue to play
      TwiSend("\\:s\\r\\n");
      Serial.println("OK");
      break;
    case  'n':
      TwiSend("\\:n\\r\\n");  // Play next
      Serial.println("OK");
      break;
    case 'u':
      TwiSend("\\:u\\r\\n"); // Play previous
      Serial.println("OK");
      break;
    case  'm':      //Play
      //The volume must be set before playing the sound
      TwiSend("\\:v 250\\r\\n");    // set the volume, from 0 (minimum)-255 (maximum)
      TwiSend("\\yes\\r\\n");
      Serial.println("OK");
      break;
    default:
      break;
    }
  }
}

Result

Open the serial monitor of Arduino (baud rate 9600) and send commands. The serial monitor will return "OK" for each valid command. For example:

  • Sending 'p' will pause the playback and return "OK".
  • Sending 'n' will play the next song and return "OK".
  • Sending 'm' will play the song named "yes" (if present in the "sound" directory) and return "OK".

Was this article helpful?

TOP