Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Miranda Hansen
Published © GPL3+

Pmod MIC Audio Visualizer

Build a dynamic audio visualizer that responds to music, voices and other auditory input using Pmod MIC, Arduino Uno and Processing.

BeginnerFull instructions provided15 hours1,943
Pmod MIC Audio Visualizer

Things used in this project

Story

Read more

Schematics

Circuit Diagram

PmodMIC VCC-->5V on UNO
PmodMIC GND-->GND on UNO
PmodMIC SCK --> Digital Pin 13 on UNO
PmodMIC MISO--> Digital Pin 12 on UNO
PmodMIC N/A--> No Connection
PmodMIC SS --> Digital Pin 10 on UNO

Code

Arduino Code

Arduino
For use with the Pmod MIC circuit. Upload this code to your board first, and keep it connected to the computer before attempting to launch the Processing code. If you are stuck feel free to peek at the serial monitor in the Arduino IDE to make sure it is sending data. The normal numbers I was getting when I did this were around 2000-2200.
/************************************************************************
*
* Test of the Pmod 
*
*************************************************************************
* Description: Pmod_MIC3
* The sound captured by the module is displayed in the serial plotter.
*
*
* Material
* 1. Arduino Uno
* 2. Pmod MIC3
* Code courtesy of this project and it's authors Alex Wong and Martha Migliacio of Digilent Inc
* https://www.hackster.io/56469/using-the-pmod-mic3-with-arduino-uno-fc0daa
************************************************************************/

#define CS 10 // Assignment of the CS pin

#include <SPI.h> // call library

int i;
byte recu[3]; // storage of data of the module
int X;
long somme = 0;

void setup()
{
 Serial.begin(9600); // initialization of serial communication
 SPI.begin(); // initialization of SPI port
 SPI.setDataMode(SPI_MODE0); // configuration of SPI communication in mode 0
 SPI.setClockDivider(SPI_CLOCK_DIV16); // configuration of clock at 1MHz
 pinMode(CS, OUTPUT);
}

void loop()
{
 digitalWrite(CS, LOW); // activation of CS line
 delayMicroseconds(20);
 for (i=0;i<2;i=i+1)
    {
    recu[i] = SPI.transfer(0); // send 2 data to retrieve the value of the two-byte conversion
    delayMicroseconds(20);
    }
 digitalWrite(CS, HIGH); // deactivation of CS line
 X = recu[1]; // X is on 12 bit format
 X |= (recu[0] << 8);
 
for(int i=0; i<32; i++) // Development of the sound curve
 {
   somme = somme + X;
 }

 somme >>= 5;
 Serial.println(somme); // Display in serial plotter
 delay(10);
}

AudioVisualizer.pde

Processing
Run this after you have connected the Arduino and MIC circuit, and uploaded the PmodMIC code. If you are having issues with it selecting the correct serial port, change the [1] on line 25 to a [0].
/*
* Author: Miranda Hansen and Joshua Cummings
* File: Visualizer.pde
* Date: 2019-01-15
* License: MIT
* Description: Reads serial data and displays it as a bar graph.
*/

import processing.serial.*;

// Configurable Graph Variables
int barCount = 20; // Max number of bars
int barWidth = 20; // In Pixels
int centerLineY = 400; // Set the Y position of our center line
int barPadding = 2; // Adjust the space between each bar
int scale = 10; // Value to divide the serial values by. Larger values results in a shorter graph.

// Global Variables
Serial myPort;
String val;   
StringList numbers;

void setup() {
  size(600, 800);
  String portName = Serial.list()[1]; // Adjust index to proper serial port
  myPort = new Serial(this, portName, 9600);
  numbers = new StringList();
}

void draw() {
  if ( myPort.available() > 0) { // Check if there is data available
    val = myPort.readStringUntil('\n'); // Read serial data
    println(val);  
    
    // Clear the screen
    background(50);
    fill(255, 0, 0);      

    if (val != null) {      
      if (numbers.size() < barCount) { // Start of the program, just fill the array until we reach the target size
        numbers.append(val.trim());
      } else {
        for (int i = 0; i < numbers.size() - 1; i++){ // We've filled the list, shift values up 1 index
          numbers.set(i, numbers.get(i+1));
        }
        numbers.set(numbers.size()-1, val.trim()); // Update the last value with our new serial value
      }
  }

  int posX = 0;
  for (int i = 0; i < numbers.size(); i++){ // For each value in our array, draw a rectangle with the appropriate graph settings
    rect(posX, centerLineY, barWidth, Integer.parseInt(numbers.get(i)) / scale);
    rect(posX, centerLineY - Integer.parseInt(numbers.get(i)) / scale, barWidth, Integer.parseInt(numbers.get(i)) / scale);
    posX = posX + barWidth + barPadding;
  }
  // delay(100); //optional delay to slow speed of bars for better visual effect.
  }
  myPort.clear(); // Remove any serial data that queued to prevent the buffer from filling ahead of how fast we can draw the values
}

Credits

Miranda Hansen

Miranda Hansen

3 projects • 7 followers
Thanks to Joshua Cummings .

Comments