Sunday, May 29, 2011

Controlling The RGB LED


Now that we've covered a simple method for reading the line level audio signal, we can talk a bit about the other side of things; the LED. These things are very bright and look great if you put something around them like a cylinder of paper to diffuse the light.
RGB LEDs have four pins; 3 for each colour, and one for ground. I calculated the resistor values here based on the 5V PWM pin voltage, the forward current of each colour, and the forward voltage of each colour. That is, the resistor values for each colour are approximately equal to (5V - forward voltage of colour)/forward current. We need to include current limiting resistors because the forward current of each colour (approximately 20mA) is less than the maximum rated current of the PWM pins (40 mA), which would otherwise flow and likely burn out the LEDs in the absence of any resistance. I have included the basic schematic below.

The code below will simply switch between each colour. Note that 0 corresponds to LED off, and 255 corresponds to the maximum brightness for the PWM signal - something we will be including in the actual algorithm to control brightness with signal level and colour with each beat.

//Simple code toggles colour of LED at maximum brightness

const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;

int thisPin = redPin;
int other1 = greenPin;
int other2 = bluePin;

void setup() {

pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(greenPin, OUTPUT);

}

void loop() {


analogWrite(thisPin,255); //maximum brightness
analogWrite(other1,0); //turn off
analogWrite(other2,0); //turn off

//switches colour every second
switch (thisPin) {
case redPin:
thisPin = bluePin;
other1 = redPin;
other2 = greenPin;
break;
case bluePin:
thisPin = greenPin;
other1 = redPin;
other2 = bluePin;
break;
case greenPin:
thisPin = redPin;
other1 = greenPin;
other2 = bluePin;

}

delay(1000);

}

Sunday, May 15, 2011

Reading Sound Level


I have included a very simple circuit below along with the required code to read in line level audio. No special considerations were given to the sampling rate or anti-aliasing filters; these things are not required for this project as was found experimentally.
For this circuit a simple voltage divider is formed with a blocking capacitor (using something other than 100uF is fine - feel free to experiment) to maintain that DC bias at analog pin A0. This is to ensure that the negative portion of the line level audio does not clip. A DC offset in code is used to compensate for any differences in the voltage divider resistor values due to inherent tolerances, and the signal is then mapped to the -256:256 range (convenient for the algorithm, as you will see). Values are sent to the serial monitor to allow you to make sure that things are working.

Regarding the 3.5mm jacks, the diagram shows only one. Typically there is one pin that is GND, and two other pins that are the left and right channels of stereo audio. For our purposes you can simply tie the left and right channel together. For a line out signal, connect the corresponding pins of the second 3.5mm jack to the first 3.5mm jack. It's just that simple!

Note that you will need to ensure that the volume is high enough at the input to get some real values. Don't expect to see any clipping. Experiment! Every input source will be a little bit different.

//Simple read of line-level audio, write to serial monitor
//Uses simple voltage divider input circuit with blocking capacitor
//Basic approach doesn't consider using anti-aliasing filter

//or sampling rate

const int audioPin = A0;
int x=0;
int sensorValue = 0;
int offset = 0; //choose offset based on the reading you get
//in the serial monitor when there is no input signal
//this value may be nonzero if resistors aren't exactly
//equivalent in voltage divider circuit; simpler software

//hack than to use a potentiometer

void setup() {

Serial.begin(9600);

}

void loop() {

sensorValue = analogRead(audioPin);
x = (sensorValue - 512)/2; //maps 0:1024 to -256:256
x = x - offset; //subtract offset so we see zero for no input

Serial.println(x);
delay(1);

}

Monday, May 2, 2011

The Arduino


Arduino is an open-source single-board microcontroller, descendant of the open-source Wiring platform, designed to make the process of using electronics in multidisciplinary projects more accessible. The hardware consists of a simple open hardware design for the Arduino board with an Atmel AVR processor and on-board I/O support. The software consists of a standard programming language compiler and the boot loader that runs on the board.
Arduino hardware is programmed using a Wiring-based language (syntax + libraries), similar to C++ with some simplifications and modifications, and a Processing-based IDE.Currently shipping versions can be purchased pre-assembled; hardware design information is available for those who would like to assemble an Arduino by hand. Additionally, variations of the Italian-made Arduino—with varying levels of compatibility—have been released by third parties.The Arduino project received an honorary mention in the Digital Communities category at the 2006 Prix Ars Electronica.The name is an Italian masculine first name, meaning "strong friend". The English pronunciation is "Hardwin", a namesake of Arduino of Ivrea.

Wikipedia


Microcontrollers can be thought of as very small, simple and inexpensive computers-on-a-chip, found in everything from microwaves to robots. They have memory, digital and analog I/O pins, and can execute the instructions that you program them with

The platform. I have included a few pictures of the Arduino UNO Board below. The Arduino is an open source microcontroller platform that was initially developed in Italy, based on the Atmel AVR microprocessor. It's simple to connect to your computer to power and program over USB. There are essentially a number of pins that one can access including analog inputs (for ADC), PWM output pins for driving motors, servos, or in our case LEDs, power pins, communication pins, and digital I/O pins for reading and writing high or low voltages. The intent of this blog is not to duplicate all of this information, so I would direct you here for . Our focus will be on making the thing work for our music visualizer.