SpotifyMin and Audio Visualizer

by JeevanSingh07 in Circuits > Arduino

23 Views, 1 Favorites, 0 Comments

SpotifyMin and Audio Visualizer

20240617_141749.jpg

Hello, my name is Jeevan and today I'll be showing off my final project. SpotifyMin is a Spotify controller using an Arduino Uno. SpotifyMin uses RFID tags, push buttons, and a potentiometer to manage Spotify playback seamlessly. This system allows users to control play/pause, skip to the next or previous track, adjust the volume, and switch between playlists using RFID tags. This project also includes an audio visualizer designed to work with SpotifyMin.

As an avid music enthusiast, I spend about three hours a day listening to music. When presented with the opportunity to create any project for my Grade 11 Computer Engineering Final Summative, I saw it as the perfect chance to channel my creativity and passion for music into a functional and exciting project. SpotifyMin combines my love for music with my interest in engineering, allowing me to explore and innovate in a field I am passionate about.


To succeed in creating this project, it is advised that you are familiar with C++ and Python.

Supplies

You will also need a decent bit of wiring.

*All prices above refer to a quantity of 1 of the objects or the price of them in a pack.

Wiring the Audio Visualizer

12.png
20240618_082021.jpg

The first thing we'll make is the Audio Visualizer. This is the easiest part of the project. Add 12 1KΩ Resistors left to right going over the gap of the breadboard. Line up these resistors with the anodes (The Anode is the longer end of the LED) of the 12 green LEDs like pictured above. Ensure you attach ground to the cathode (Shorter end) of each LED, remember to attach your ground to the top of the breadboard. Now connect ports 13-2 to the breadboard in order to line up with the Resistors as pictured above. Connect your 5v and ground to the breadboard. The final step is attaching the sound sensor. The sig pin on the sound sensor should be connected to the A0 port on the Arduino. Then connect the ground and power to the sound sensor. You've completed the hardware portion of the audio visualizer!

Coding the Audio Visualizer

1.png
7.png
71.png
72.png

First we will declare our variables. All 12 LEDs are assigned to a different pin on the Arduino. The sound sensor is declared and assigned the A0 pin. We also have 2 delays declared as t, and t1.

We then have to fill out the setup. Our only input will be the sound sensor. All LEDs are declared as output. Serial.begin(9600) initializes serial communication (send and receive data through the serial port.

The void loop starts by reading and mapping the sound sensor value. It reads the analog value from the sound sensor with int sensorValk = analogRead(soundsensor); and maps this value from the default range 0-720 to the range 0-100 using int sensorVal = map(sensorValk, 0, 720, 0, 100);. The LEDs are controlled based on the updated sound value.

For the first condition, if (sensorVal < 60), if the mapped sound value is less than 60, LEDs led1 and led12 turn on using digitalWrite(led1, HIGH); digitalWrite(led12, HIGH); and the program waits for 25 milliseconds with delay(t);. If the sound value is 60 or higher, the LEDs turn off with digitalWrite(led1, LOW); digitalWrite(led12, LOW); and the program waits for 5 milliseconds. This is repeated for all LEDs. Now you might be wondering why the code is (sensorVal < 60). You would think that if the microphone picks up audio the value would go higher, but it is in fact the opposite. When the microphone picks up sound, the value goes lower.

Wiring SpotifyMin Part 1

73.png

To get started with SpotifyMin, first place 3 pushbuttons and wire them to power and ground, ensure you use a resistor for the ground connection. These push buttons will be your "Previous", "Pause/Play", and "Next" buttons from left to right. The first button connects to 2 on the Arduino, the second button connects to 7, and the third button connects to 4.

Wiring SpotifyMin Part 2

74.png

For the second step in wiring, add your potentiometer. This is connected to ground and power (No need for a resistor). The middle pin of the potentiometer is connected to the A5 pin on the Arduino.

Wiring SpotifyMin Part 3

75.png

Now we add our common anode RGB LED. Because the RGB LED is common anode, the middle left pin goes to power. R (Far left) goes to 6, G (Middle right) goes to 3, and B (Far right) goes to 5.

Wiring SpotifyMin Part 4

76.png

Now add your DC Motor and connect it to pin 9 on the Arduino, then connect ground to the motor.

Wiring SpotifyMin Part 5

20240618_082227.jpg
20240618_082743.jpg

Finally, we have the last wiring portion of the project. To connect the RFID to your Arduino you must use jumper cables to connect each wire to the Arduino. First I will tell you the pin on the RFID, then I will tell you where to place it on your Arduino.


The VCC pin goes to the 3.3V pin.

RST goes to pin 8.

GND goes to GND.

MISC goes to pin 12.

MOSI goes to pin 11.

SCK goes to pin 13.

NSS goes to pin 10.

The IRQ pin is not used.


Congratulations!

You've finished wiring SpotifyMin.

Arduino Code for SpotifyMin Part 1

2.png

Starting with our Libraries and Declarations.


The SPI.h libarary lets the Arduino communicate using the Serial Peripheral Interface protocol.

The MFRC522.h Library is used to read the RFID tags. This library can be installed here.


Moving on, we define the Reset (RST) pin and the Slave Select (SS) pins for the RFID sensor.

MFRC522 mfrc522(SS_PIN, RST_PIN); This is a class and is used to communicate with the RFID.


After that, we declare our pins. You may realize the pin "cd", this refers to our DC Motor.


Finally, we have 2 boolean statements. These are used to track the "Pause/Play" button's state. We use the state of that button to determine whether or not the DC Motor will spin. When it is playing the Motor spins, when it isn't playing the motor will stop, much like a record player.

Arduino Code for SpotifyMin Part 2

1.png

Serial.begin(9600); is used to work with serial communications

SPI.begin(); Initializes the serial peripheral interface bus

 mfrc522.PCD_Init(); Initializes the RFID


Then we setup our pins as normal.


All controls are input devices. (Pause/Play, Previous, Next, Volume)

The r, g, b, and cd are all output.


We then have a line that prints a "Scan an RFID tag" message. This only happens once.

Arduino Code for SpotifyMin Part 3

1.png

Check for New Cards:

  • if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()): This checks if a new RFID card is present and reads its serial number.

Read and Print Card UID:

  • Serial.print("UID tag: ");: Prints the UID of the detected RFID card.
  • The for loop reads each byte of the card's UID, formats it as a hexadecimal string, and appends it to the content string.
  • content.toUpperCase();: Converts the UID string to uppercase.
  • Serial.println(content);: Prints the full UID string.

if (content == "35B4E015"): If the UID matches "35B4E015", print "Rebirth" to the serial monitor and wait for 1 second. This is also the same for the second playlist but it has different values.

Be sure to replace the codes and the playlist names with your personal RFID codes and playlists.

bool currentButtonState = digitalRead(p);: Reads the current state of the play/pause button.

if (currentButtonState != lastButtonState): Checks if the current button state is different from the last recorded state. This detects when the button has been pressed or released.

if (currentButtonState == HIGH): If the button is pressed (assuming HIGH means pressed), toggle the playPauseState.

playPauseState = !playPauseState;: Toggles the state from play to pause or pause to play.

Control cd Pin:

  • if (playPauseState): If playPauseState is true, set cd to a PWM value of 150 (turn on or spin up).
  • else: If playPauseState is false, set cd to 0 (turn off or stop).
  • delay(50); // Debounce delay: Waits 50 milliseconds to debounce the button press.


lastButtonState = currentButtonState;: Updates the last button state to the current state for the next iteration of the loop.

Arduino Code for SpotifyMin Part 4

1.png

The "Play/Pause', "Next", and "Previous" codes all work the same, they use a simple if statement to check if the button was pressed. If a button is pressed then a message is sent to the serial monitor which gets sent to Python which then acts on Spotify.

We also have the portion of the code for our volume.

We read the input coming from the A5 pin. This value ranges from 0 to 1023. We then map that value to 0 to 100.

Arduino Code for SpotifyMin Part 5

1.png

This code uses the volume to control the colors of the RGB led. This can be manipulated and changed to whatever color you want.

Preparing Python Part 1

apps.23372.14374839563883736.98eac5ce-53c8-45b6-9ba3-7f6a7e9c0f4b.png

To get started with Python, you can download it from the Microsoft store here.

After you download Python, simply follow the instructions on the screen from the install wizard.

Preparing Python Part 2

1_6Dhu1H4t028lOGbaZuyRCw.png

Now you need an IDE to work with Python. For this project, I used PyCharm which you can download here.

Again, follow the install wizard and complete the download.

Preparing Python Part 3

When you open PyCharm you will get the option to make a new project.

Make a new project and name it whatever you want.


Go to the .venv folder under the project and right click.

Go to "New" and make a Python file.

Preparing Python Part 4

1.png

Locate the terminal in the bottom left. It should look like a box with a >_ in it.


Then type this into the terminal.

pip install spotipy pyserial pynput

Python Code for SpotifyMin Part 1

1.png

Any imports at the top refer to importing libraries. Much like Arduino code


The Spotify API credentials are your unique codes.


When setting up your serial connections, ensure you are using the right port. I used port 9 on my computer. When you use Arduino your port should pop up in the Arduino IDE.


We then use the serial communication from the Arduino IDE to receive commands and execute them on Spotify.

Python Code for SpotifyMin Part 2

1.png

This part of the code is for the RFID.

When your Python code retrieves your playlist name from the serial monitor it will play the playlist your have connected to the tag. It does this by using your playlist url. Ensure your playlist names are the exact same on the Arduino IDE and on the Python Code.

Getting Your Client ID and Client Secret Part 1

1.png

First create a Spotify Developer account. Go to the Spotify Developer Dashboard here.

Getting Your Client ID and Client Secret Part 2

1.png

Create a new application by pressing the create an app button.

Fill out the information required and click create.

Getting Your Client ID and Client Secret Part 2

1.png

Go to settings for your app and press basic information. You should be presented with your Client ID and Secret ID at the top of the page.

The Final Step

Fill in any blanks in the code with your unique playlists and IDs.


The first time you run this code you will get an error and will be redirected to the web. Copy the url and paste it into PyCharm and your code will run.


Thank you for reading and good luck!