venerdì 28 ottobre 2011

Arduino and processing led blinking following music rythm

Today I'm writing about a simple project with Arduino and Porcessing to blink 5 LEDs following the music rythm.
To do it we need an audio librari for processing called minim, (created by Damien di Fede) and we need the Firmata library for communicating to Arduino.
For this project you need:
-Arduino board
-5 LEDs (1,8-2v)
-5 220ohm resistors
-some wires
-arduino library for processing (
-minim audio library(

First of all prepare the simple connections on your breadboard following this image:

Then open the arduino software and upload the standardFirmata sketch (File>examples>Firmata>StandardFirmata).
Now, before writing the processing code I'd like to tell you something aboute the libraries we're going to use: Firmata is a library made for communciating between processing and Arduino. When you run Firmata in arduino, you can call arduino functions from processing using the arduino library for processing.
The minim library is an audio library for processing, I think it's the best library about audio and sound for processing because it's easy to use but have lots of functions. For the reference chek this link:
Now download the libraries and write the following code in a processing sketch:

import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*; //Import ddf.minim library
import processing.serial.*; //Import serial library
import cc.arduino.*; //Import Arduino library

Arduino arduino;
Minim minim;
AudioPlayer song;
FFT fft;
//I create the objects

//To play another song change the song_file value
String song_file = "song.mp3";
int xmax = 600; //window width
int ymax = 300;//window height

void setup()
  size(xmax, ymax);
  minim = new Minim(this);
  arduino = new Arduino(this, Arduino.list()[1], 57600);
  song = minim.loadFile(song_file);;
  fft = new FFT(song.bufferSize(), song.sampleRate());
  // in this function I create the minim object, I start
  //communicating with Arduino,I load the song and I play it and
  // I start the Fast Fourier Transofrm

void draw()
  stroke(127, 255, 0, 200); //I set the color of my lines
  for(int i = 10; i < fft.specSize(); i++){
    line(i, height-30, i, height - (30+fft.getFreq(i*10)));
    // I create lines that represent the amplitude
    // of each frequency.
    text("Min Freq", 10, height-10);
    text("Max Freq", fft.specSize(), height-10);
  ledcontrol(); //I call the function for the arduino

void ledcontrol(){
  //In this function I use arduino analogWrite function
  // to write in PWM pins the amplitude
  // of five general frequency
  arduino.analogWrite(3, int(fft.getFreq(500)));
  arduino.analogWrite(5, int(fft.getFreq(400)));
  arduino.analogWrite(6, int(fft.getFreq(250)));
  arduino.analogWrite(9, int(fft.getFreq(150)));
  arduino.analogWrite(10, int(fft.getFreq(80)));

The code is simple so I think it's not necessary to explain it.
You can download the project files (.zip) here: download.
Here is a test:

For more information about the project comment or send me an e-mail to

Bye, Dami

lunedì 3 ottobre 2011

Arduino and Processing serial communication

This a tutorial about serial communication between processing and arduino.
There are two way to communicate from processing to arduino:
-Using "arduino" library in processing and "Firmata" library in arduino
-Using serial library in processing and arduino
In this tutorial I will use the second way because so you haven't to use any extra library.
First of all connect a potentiometer to arduino like in this image:

Then open arduino software and write the following code:

int potPin = 0;

void setup(){

void loop(){
  int val = map(analogRead(potPin), 0, 1023, 0, 255);

Using Serial.begin(9600) we start a serial communication between arduino and pc,  then using  
int val = map(analogRead(potPin), 0, 1023, 0, 255);
we assign the analog input from pin 0 to the integer "val" and we modify its value using map() function; finally we write the value of  "val" in the serial port.
Now we have to communicate to arduino shield using processing (in this tutorial I use processing because it's more simple but you can do the same thing using other programming language), so open the processing sketch editor and write the following code:

import processing.serial.*;
Serial port;
float brightness = 0;

void setup(){
  size(500, 500);
  port = new Serial(this, "COM3", 9600);

void draw(){

void serialEvent (Serial port){
  brightness = float(port.readStringUntil('\n'));

Using import processing.serial.* we import the processing serial library that we will use to communicate with arduino, then we create a Serial object called "port" and a float called brightness.
In void setup we set the window size and we start communicating with arduino, that is connected to the serial port "COM3" (in this case the arduino port is COM3 but your arduino can be connected in another serial port so if you want to know it, go to start>control panel>devices and printers>device manager and then look for your arduino in the "ports" section) using  
port = new Serial(this, "COM3", 9600);

In void draw we set the background color and we assign the blu color to the "brightness" float.
Finally we create a function called serialEvent and we add the "Serial port"  to its arguments.
In this function we assign the serial port value to the brightness float.

Now if you upload the sketch to arduino and you run the processing code the window bakcgound will change if you move potenziometer.
You can download this project here.
I hope you like this post, please comment or send me an e-mail to

P.S.: I'm happy to say you that my blog has more than 8000 views :DD

Bye, Dami