A music player that uses an OLED screen to display the status of our music.
CC BY-NC 4+
Devices and components
Arduino Uno Rev3
10 connecting wires 150 mm male
1K resistance
button
4 GB Micro SD card
0.96" 128x64 I2C OLED display
DFPlayer - A mini MP3 player
Mini speaker 4Ω (3W)
Software and tools
Arduino IDE
Project description
Music Code
This is the music player code.
1#include <Wire.h>
2#include <Adafruit_GFX.h>
3#include <Adafruit_SSD1306.h>
4#include <SoftwareSerial.h>
5#include <DFRobotDFPlayerMini.h>
6
7/******** OLED ********/
8#define SCREEN_WIDTH 128
9#define SCREEN_HEIGHT 64
10#define OLED_RESET -1
11#define OLED_ADDR 0x3C
12
13Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
14
15/******** DFPLAYER ********/
16SoftwareSerial dfSerial(10, 11); // TX, RX
17DFRobotDFPlayerMini dfPlayer;
18
19/******** BUTTONS ********/
20const int btnNext = 2;
21const int btnPlayPause = 3;
22const int btnPrev = 4;
23
24/******** STATE ********/
25bool isPlaying = false;
26int currentTrack = 1;
27
28/******** BUTTON EDGE STATES ********/
29bool lastNextState = HIGH;
30bool lastPrevState = HIGH;
31bool lastPlayState = HIGH;
32
33/******** SETUP ********/
34void setup() {
35 pinMode(btnNext, INPUT_PULLUP);
36 pinMode(btnPlayPause, INPUT_PULLUP);
37 pinMode(btnPrev, INPUT_PULLUP);
38
39 Serial.begin(9600);
40 dfSerial.begin(9600);
41
42 // OLED
43 if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
44 while (true);
45 }
46
47 // DFPlayer
48 if (!dfPlayer.begin(dfSerial)) {
49 showError("DFPlayer FAIL");
50 while (true);
51 }
52
53 dfPlayer.volume(20);
54 delay(500);
55
56 dfPlayer.play(1);
57 isPlaying = true;
58 delay(300);
59
60 currentTrack = dfPlayer.readCurrentFileNumber();
61 updateOLED();
62}
63
64/******** LOOP ********/
65void loop() {
66 handleButtons();
67}
68
69/******** BUTTON HANDLING ********/
70void handleButtons() {
71 bool nextState = digitalRead(btnNext);
72 bool prevState = digitalRead(btnPrev);
73 bool playState = digitalRead(btnPlayPause);
74
75 if (lastNextState == HIGH && nextState == LOW) {
76 nextTrack();
77 }
78
79 if (lastPrevState == HIGH && prevState == LOW) {
80 prevTrack();
81 }
82
83 if (lastPlayState == HIGH && playState == LOW) {
84 togglePlayPause();
85 }
86
87 lastNextState = nextState;
88 lastPrevState = prevState;
89 lastPlayState = playState;
90}
91
92/******** PLAYER CONTROLS ********/
93void nextTrack() {
94 dfPlayer.next();
95 delay(300); // DFPlayer needs time
96 currentTrack = dfPlayer.readCurrentFileNumber();
97 isPlaying = true;
98 updateOLED();
99}
100
101void prevTrack() {
102 dfPlayer.previous();
103 delay(300);
104 currentTrack = dfPlayer.readCurrentFileNumber();
105 isPlaying = true;
106 updateOLED();
107}
108
109void togglePlayPause() {
110 if (isPlaying) {
111 dfPlayer.pause();
112 isPlaying = false;
113 } else {
114 dfPlayer.start();
115 isPlaying = true;
116 }
117 updateOLED();
118}
119
120/******** OLED ********/
121void updateOLED() {
122 display.clearDisplay();
123 display.setTextSize(1);
124 display.setTextColor(SSD1306_WHITE);
125
126 display.setCursor(0, 0);
127 display.println("DFPlayer Mini");
128
129 display.setCursor(0, 20);
130 display.print("Track: ");
131 display.println(currentTrack);
132
133 display.setCursor(0, 40);
134 display.print("Status: ");
135 display.println(isPlaying ? "Playing" : "Paused");
136
137 display.display();
138}
139
140void showError(const char* msg) {
141 display.clearDisplay();
142 display.setCursor(0, 30);
143 display.println(msg);
144 display.display();
145}
Downloadable files
Wiring diagram
GND for DFPlayer can also access other GNDs
file.None
Note: Content and images are from: https://projecthub.arduino.cc/, with some modifications.
If you want it removed due to copyright reasons, please leave a comment. Thank you.
I want to share this article more widely so that everyone knows about Arduino and your project.