Playing with Music and Lights

Yesterday I was experimenting with my Music and Lights system and trying out different algorithms to sync lights to Music. For this purpose I had integrated a ATmega328p microcontroller (MCU) when I designed the Music and Lights PCB.

Three of the MCU analog input pins are connected to the outputs of the bass, mid and treble filters. This allows the MCU to read the bass, mid and treble levels of the audio signal at any instance of time. The MCU can then process the inputs and drive the RGB strip according to a pre-programmed algorithm.

To program the MCU in-system, I have included a 5-pin header on the PCB, which connects to the respective programming pins of the MCU.  If you want to know the details this is a good tutorial that explains how to upload a program to a stand-alone ATmega328p.

The ATmega328p on board the Music and Lights PCB can be programmed via the 5-pin programming header, which connects to an Arduino board

The ATmega328p on board the Music and Lights PCB can be programmed via the 5-pin programming header, which connects to an Arduino board

Calibrating the bass, mid and treble filter outputs

Before experimenting with algorithms, I calibrated the bass, mid and treble filters to the same level. There are three pots on the PCB that can be used to adjust the input audio level to the three filters. The filters are of type multiple feedback active bandpass filter, which has a very narrow pass band. The center frequencies are 224 Hz, 1057 Hz and 3202 Hz for bass, mid and treble respectively. Please read my previous post if you want to know how these types of filters are constructed.

I used this handy online tone generator tool to generate an audio signal with the center frequency for each of the filters. Then with the pots turned to maximum input signal, I read the output value of each of the filters using the RGB strip with the number of lit LEDs proportional to the signal level. The number of lit LEDs was 15, 13 and 11 for bass, mid and treble filters respectively. Since the treble filter had the smallest output signal level I adjusted the pots for the bass and mid filters to the level of the treble filter.

Now with all the filters calibrated, I set out to experiment! So far I came up with three algorithms that I liked. Let’s see them in action first and then I’ll explain the algorithms.

Algorithm 1

This algorithm is very similar to the one I had originally, but with only three colours instead of the full RGB spectrum. The audio levels are sampled every 50 ms. It then compares the bass, mid and treble levels and selects the LED colour depending on the largest value; Red if bass, green if mid or blue if treble. The number of LEDs lit is proportional to the signal level.

I like this better than having the full RGB spectrum since it is easy to see whether the bass, mid or the treble is the largest.

Algorithm 2

Very similar to Algorithm 1 except the zeroth LED is at the center of the strip. The bass level is indicated on the right side (red) while the treble level is indicated on the left side (blue). The mid level is indicated on the center (green). Again the sample rate is set to 50 ms, and for each sample only the largest level is displayed.

Algorithm 3

This one is my favourite so far. It is more smoother and eye pleasing than the previous two. To make this happen I created two virtual shift registers of size 16 bits (or an int) each; one for the LEDs on the right side of the strip and the other for the LEDs on the left side of the strip. The bits in the shift registers determine whether a particular LED is supposed to be on or off in each cycle.

How this works is better explained with the actual code, and here it is

 Algorithm3

These are a few examples that showcase the capabilities of the Music and Lights system. My idea with this project is to develop a system that can sync lights to music in real time, but one that performs better than a simple light organ.  I will continue to experiment  and improve it with new and more complex algorithms until I run out of memory of the MCU, and an upgrade becomes necessary.

Thanks for reading!

Music and Lights – the PCB!

After that fun-but-this-is-too-much-work making of the PCB, and over 200 solder joints later, I am finally done with the construction phase of the Music and Lights system.

Music and Lights PCB assembled.

Music and Lights PCB assembled.

One difficulty I had with soldering components on to this board is that certain components like the screw terminals and the audio connector could not be made flush with the PCB surface. This is because in order to solder the pins I had to reach in between the component and the board surface with the soldering iron. So when you look at the board from certain angles you can see the gaps, and it doesn’t look that neat. Next time I will make sure to put the traces for these components on the bottom side so they can be easily soldered.

I put all of the components on the top side of the PCB except for the LM1875 audio amplifiers, which were placed on the bottom side, so that I can heat sink them directly to the ground plane and avoid those bulky heat sinks.

The LM1875 audio amplifiers were placed on the bottom side of the PCB so that they can be heat sunk directly to the ground plane.

The LM1875 audio amplifiers were placed on the bottom side of the PCB so that they can be heat sunk directly to the ground plane.

The next step is to test it and see if all that hard work in designing and making the PCB has payed off. First I decided to try just the Speakers with no lights. The first time you power up a circuit board is always both exciting and dreadful. So it turned out that one of the speakers worked, and one of them didn’t.

Now it’s time to troubleshoot. First thing I did is to check if the amplifier for the speaker that didn’t work has power. Turned out that it didn’t, and this is good news. Close inspection of the solder joints of the amplifier found that I had forgotten to solder the power pin. Thankfully it is an easy fix. After soldering the power pin, both speakers are now happily working.

So far so good. What about the lights? After connecting the RGB strip to the PCB I turned on the lights switch and there was…. no flashing lights. Time to troubleshoot again. As before, I checked to make sure that power is available to all the components of the RGB driver portion of the circuit. This includes the RGB strip, the ATmega328p and the active filters. All the components had power. This means I have to dig a bit deeper to find the problem.

I decided to re-program the ATmega328p first. Surprise surprise! After re-programming the MCU the lights started flashing. Most likely what happened was that sometime in between removing the MCU from the breadboard and putting it on the PCB and soldering it, the program on the chip got corrupted. This could be due to ESD since I didn’t take any protective measures for ESD.

The best part however, is that now there is none of the interference that I observed before when the setup was on the breadboard. The audio is crystal clear even when the RGB strip is running. This means that the interference observed before is due to imperfections of the breadboard. Good layout technique and having a ground plane eliminated the interference problem.

Finally, let’s enjoy a show to celebrate this

So what’s next? This project is not done yet. One of the reasons I build this is to have a platform where I can test and implement different ways of syncing lights to music. So I will be trying out different algorithms and doing a bit of programming for the next little while to get the maximum use out of the Music and Lights system. And I will keep you up to date on that as well. Stay tuned and Thanks for reading!

PCB making at home – Etching

This post continues from my previous post, where I developed the Music and Lights PCB using the UV exposure method. Once an etch resistant image of the layout is successfully placed on the Copper board it is time to do the etching. Etching is the process of removing unwanted Copper from a Copper clad board.

Ferric Chloride used for etching

Ferric Chloride used for etching

 

The etching solution I am using is called Ferric Chloride, a corrosive chemical that will dissolve exposed Cu and pretty much any metal. It also leaves a stain on pretty much anything it touches. When working with Ferric Chloride, it is very important to be in an area with good ventilation as it tends to produce a strong fume (specially when you just open the bottle or container used to store it)

Compared to the photoresist developing process, the etching process is slow. The amount of time taken to etch the board depends on the board size, temperature, amount of etchant and circulation. I finished etching my board in about 40 minutes.

And these are the steps, continuing from the previous post

Warning: Ferric Chloride is corrosive. Wear safety gloves when handling. Work in a well ventilated area.

Step 20: Pick a plastic container and place the Cu board with the etch resistant layout image inside.

Step 21: Pour Ferric chloride into the container until the board is completely covered. Do not dilute with water.

Fill the plastic container with Ferric Chloride until the board inside is completely covered

Fill the plastic container with Ferric Chloride until the board inside is completely covered

Step 22: To increase the etching rate agitate the solution every few minutes by tilting the container back and forth.  Examine the board and flip the board every 5 minutes for the first 20 minutes or so.

The exposed Cu will start to disappear from the edges first. When the exposed Cu has disappeared from about half of the board start monitoring the process more frequently. If left for too long in the etching solution it will start eating through the etch resist as well.

Step 23: When exposed Cu from both sides of the boards is etched away remove the board from the container and rinse with plenty of cold running tap water. Dry the board with tissue or soft cloth.

 

The back side of the PCB after etching

The back side of the PCB after etching. Notice on the right side how the etchant has started to eat away through the etch resist. This side was slightly overexposed during the development process.

As you may notice in the image above, the right side of the bottom PCB layout got a bit eaten away by the etchant. This is because this side was over exposed during the photo development process. Fortunately it is not too bad as the affected area is mostly the ground plane. However, I will have to take extra care when soldering components to ensure that good connections are made.

Top side of the board after etching

Top side of the board after etching. Notice how the top side component pads line up perfectly with the bottom side component pads

Step 24: Use tissue paper and nail polish remover to wipe off photoresist from the board and expose the Copper.

Use nail polish remover to wipe away photo resist to expose the Cu after etching

Use nail polish remover to wipe away photo resist to expose the Cu after etching

Step 25: If necessary, trim the edges of the board with a fretsaw.

If necessary, trim the edges of the board with a fretsaw

If necessary, trim the edges of the board with a fretsaw

Step 26: If you have through-hole components, drill the holes with a PCB drill.

Tip: I used a 1 mm drill bit for larger components like switches and power connector, and a 0.8 mm drill bit for other components.

Drill component holes with a PCB drill. I used 1mm and 0.8mm drill bits

Drill component holes with a PCB drill. I used 1mm and 0.8mm drill bits

That’s it! Now in my hands I have a home made PCB for the Music and Lights system.

Now you might be wondering why in the world go through all that trouble to make your own PCB at home when you can get a better one manufactured professionally. I totally agree that it makes more sense nowadays to get your PCB manufactured, plus you will have a solder mask and a nice silk screen too. But making your PCB at home is fun, and you get to control and see the whole process. It is a path with many challenges and finding ways to overcome them is the best part.

Now to the final note about making PCBs at home,

What to do with all the chemicals when you are done with them

You shouldn’t dump used chemicals down the drain, nor flush it down the toilet nor throw in the dumpster. They will destroy your plumbing and could do terrible damage to the ground water and environment.

Safe and easy way to dispose of them is to contact your local hazardous waste disposal company. I store my used Ferric Chloride and photo developer solution in plastic containers, usually the ones they came in, until a sizable portion is ready for disposal.

Music and Lights – the layout

I have finally finished designing the layout of the PCB, and I am very happy with the way it turned out. Everything fitted nicely on a 100 x 150 mm double-sided board. Here are some of my design guidelines before I started:

  • Keep the AC signals separate from the DC signals. Specially the speaker amplifiers.
  • Have a large ground plane and connect GND pins directly to it.
  • Place decoupling capacitors close to the respective device pins of the speaker amplifier and MCU.
  • Place the speaker amps symmetrically on the board to evenly distribute the heat.

And here is the result,

Front side of the Music and Lights PCB layout.

Front side of the Music and Lights PCB layout.

The smallest traces are 0.7 mm wide. The power rails are 1.5 mm wide. All the components are on the front side of the PCB except the speaker amps. The speaker amps will be heat sunk to the ground plane, so I placed them on the bottom side of the PCB.

I tried to line up similar components the best I could. It gives an aesthetic look to the board and actually makes it easier to mount the components and solder them.

Bottom side of the Music and Lights PCB layout

Bottom side of the Music and Lights PCB layout

The bottom side mostly consists of the GND plane. I had to put a few traces on the bottom side because they didn’t fit on the top side. Also this is the first time I am incorporating a filled zone into my PCB, and I am curious what challenges this will bring when I am going to etch the actual PCB.

One advantage of having filled zones, as far as etching is concerned, is that it reduces the amount of Cu needed to etch away. Also it reduces the amount of time needed for etching. But, because there are no filled zones on the front side I don’t think it will save me significant etching time.

One potential problem with having to etch filled zone is that it increases the chance of having unintended connections due to inadequate etching, since all the pads and traces are very close to the filled zone edges. I might increase the clearance a little bit more to reduce the chance of this happening.

Also here’s a 3D view of what the board will look like once its made and populated,

3D rendition of populated Music and Lights PCB. Note that some of the custom footprints do not show up in the 3D view

3D rendition of populated Music and Lights PCB. Note that some of the custom footprints do not show up in the 3D view

So I am onto the next step, making the PCB! I will keep you posted on that as well. Stay tuned and Thanks for reading!

The first Music and Lights system

Currently I am in the process of designing the PCB layout for the Music and Lights system. I am using KiCAD for designing the layout. I want to do a really good job on the layout because as observed on the bread board prototype, this circuit is very susceptible to interference.

In the meantime, I thought I should write a blog post about the first Music and Lights system I built. Yup, that’s the one on the image at the top of this page. I called it the MSYNC.

It has two 32ohm speakers driven by classic LM386  audio amplifiers. Each speaker has 20 LEDs around them; 10 green and 10 red leds. These LEDs are driven by LM3914 led drivers. The red lights show the bass level and the green lights show the treble level of the input audio.

On the front side, there are 10 RGB LEDs showing the bass and treble levels. At maximum bass (when all 10 red LEDs are lit) a blue lights goes across the front LED bar. I will explain how it works in detail in just a bit, but first lets see it in action!

Unlike the Music and Lights system I am building right now, MSYNC is completely analog. It has no microcontrollers. Also MSYNC uses passive filters made from resistors and capacitors unlike the new system, which has active filters made from op-amps.

It is made with two double sided PCBs stacked together separated by an insulated layer (I used a sheet of plexiglass). The top PCB has the speakers, speaker amplifiers, filters and the LEDs around the speaker (see schematic below). In order to conserve space I decided to place the speakers on top of the LM3914 LED driver chips with a bit of insulation.

Schematic of the top PCB with the speakers, speaker amplifiers, filters and the LEDs around the speakers (click image for a larger view).

Schematic of the top PCB with the speakers, speaker amplifiers, filters and the LEDs around the speakers (click image for a larger view).

 

The second PCB controls the 10 RGB LEDs on the front. With the help of a LM555 timer IC, a set of shift registers poll the state of the LM3914 output pin that corresponds to maximum bass level at regular intervals. When the maximum bass is registered, the blue LED connected to the first output pin of the shift register is turned on. This value then gets shifted out of the shift register on subsequent readings, which results in a blue light moving across all ten RGBs.

The schematic below shows how the components in the second PCB are connected.

Schematic for the second PCB showing how the RGB leds are driven by the LED drivers and the shift registers (click image to see a larger view).

Schematic for the second PCB showing how the RGB leds are driven by the LED drivers and the shift registers (click image to see a larger view).

Now I could get into how I made the PCBs at home and how I assembled everything in a plexiglass box, but it would take many more blog posts to explain everything. There will surely be a post about how I make PCBs at home in the future.

Thanks for reading! If you have any questions please comment.

Music and Lights – Reducing noise

In my last post I talked about two interference noises in the speaker output. In this post I will explain my understanding of where the interference is occurring, and what I did to reduce them.

1. The low frequency interference (~25 Hz)

This noise is there whenever I have the Arduino board connected to the system. The amplitude of this noise increases significantly when the Arduino is sending data to the RGB strip or sending data over to the computer. When the Arduino board is taken out from the system the noise disappears.

The Arduino board is powered by a computer USB port. Any variation in power levels of the USB port will also affect the speaker amplifier system. To test this I removed the USB port connection and powered the Arduino board with a linear voltage regulator outputting +5V. This reduced the noise notably, but not completely.

In the Arduino board, in addition to the microcontroller there are other systems that use power even when the microcontroller is not doing anything. For example, the on board regulator and the FTDI chip that facilitates USB communication. These devices could affect how much current is flowing in the power rails leading to variations in the power rail voltages. The amplifier is very sensitive to changes in the power rail voltages.

So I decided it is better to have a stand-alone Arduino microcontroller (ATmega328p) on the breadboard. Moving the microcontroller from the Arduino board to a breadboard involves a few additional components. I found this tutorial that explains how it is done very well. Schematic below shows how I implemented the ATmega328p on my breadboard.

BreadboardArduino

How to integrate a stand-alone Arduino microcontroller in a circuit

The stand-alone breadboard microcontroller reduced the noise even further. However not completely. Then I ran into another problem; The microcontroller kept crashing at random times. Then I realized that I haven’t put any decoupling capacitors on the microcontroller power pins. Adding 10uF decoupling capacitors to the power pins stopped the microcontroller from crashing. A good lesson of the importance of decoupling capacitors in stabilizing the power to a device.

2. The high frequency interference 

This high frequency noise is present whenever the LEDs on the RGB strip are lit. The noise is proportional to the number of LEDs lit.

If this noise is entering the speaker amplifier through the input signal, then its intensity should increase when the volume of the input signal is increased. However, increasing the volume of the input signal had no noticeable effect on the noise.

After doing a bit more research into amplifier noise sources on the web, I noticed a problem with the way I have wired the components on my breadboard. On the breadboard I have the amplifier ground, signal input ground, the speaker ground, the microcontroller ground and the RGB strip ground all wired to different locations within the breadboard. They have long leads that connect them to the terminal of the power supply ground as in the diagram below. This way of wiring leads to an unstable ground rail as the voltage in different sections of this rail will be different due to different amounts of current flowing through them.

One solution to this is to use a ‘star’ grounding system as shown in the diagram below. In a ‘star’ grounding system all the ground leads from different devices in the circuit connects directly to a single point (hence the name ‘star’) close to the power supply ground connection.

StarGrounding

Improper grounding of the devices can lead to interference. A ‘star’ grounding system minimizes interference by having a single reference point to measure signals for all devices.

 

After rewiring the amplifiers, speaker and the RGB strip in a ‘star’  grounding method, the high frequency noise was greatly reduced. This method also helped to reduce the low frequency noise even further.

I am now satisfied with the Music and Lights system on the breadboard to move it to the next step, which is to design the PCB layout! Proper layout (including a large ground plane) and solid connections on a PCB should take care of the remaining interference. That will be another blog post.

Music and Lights – RGB strip driver

First of all, I made some changes to my project objectives. The music and lights system I am building will not be attached to my bicycle. This is because I don’t think it will be used that often if it is just on my bicycle. So instead I am gonna set it up on my room, so that I can make the most out of it.

So far I have build an amplifier for the speakers and a triple channel audio filter for the lights (see previous posts). The next step is to read the output values of the triple channel audio filter and drive the TM1809 RGB strip to represent those values. For this I am using an Arduino. I have several Arduino boards hanging around and they are great for when you need to write a simple program to read sensor data and do something with it. Also there is a fantastic library for Arduino called FastLED that makes driving LED strips a breeze.

So I wrote a simple program to read the output values of the triple channel filter and drive the RGB strip with the red, green and blue values representing bass, mid and treble levels.

/**
   RGBdriver.ino
   Purpose: Read three analog values and drive a TM1809 RGB strip with the red,
   green and blue levels representing the analog values. 

   @author Kasun Somaratne
   @version 1 06/07/14
 */

#include <stdio.h>
#include <FastLED.h>

#define NUM_LEDS      30
#define DATA_PIN       7
#define BASS_PIN      A0
#define MID_PIN       A1
#define TREBLE_PIN    A2

//create an array of RGB values for the number of leds on the strip.
CRGB leds[NUM_LEDS];

void setup()
{
  //Specify the led strip type and the data pin used to send data to the led strip
  FastLED.addLeds<TM1809, DATA_PIN>(leds, NUM_LEDS);

  //Initially turn all leds off
  for(int i = 0; i < sizeof(CRGB); i++)
  {
     leds[i] = CRGB(0,0,0);
  }
  FastLED.show();
}

void loop()
{
  // read the bass, mid and treble values. These values will be between 0-1023.
  int bass = analogRead(BASS_PIN);
  int mid = analogRead(MID_PIN);
  int treble = analogRead(TREBLE_PIN);

  //calculate the number of LEDs to be lit using the bass value
  int litLEDCount = map(bass, 0, 1023, 0, NUM_LEDS);

  //map the bass, mid and treble values to red, green and blue values. The rgb values can only be from 0-255
  int rVal = map(bass, 0, 1023, 0, 255);
  int gVal = map(mid, 0, 1023, 0, 255);
  int bVal = map(treble, 0, 1023, 0, 255);

  //turn off all the LEDs on the array
  FastLED.clear();
  //update the LED strip with calculated red, green and blue values
  for(int i = 0; i < litLEDCount; i++)
  {
    leds[i] = CRGB(bVal,rVal,gVal);
  }
  //display the LEDs
  FastLED.show();

  delay(50);
}

Finally, It is time to test the entire setup. Here’s a short video showing how it looks at the moment:

Not bad for the first try, right? However, it has several issues:

  1. There is a high frequency noise that seems to be proportional to how many LEDs are lit on the strip.
  2. There is a low frequency noise (~25Hz) whenever the Arduino board is connected to the system.

Next step is to identify the sources of these noises and eliminate them. But so far I am satisfied with the progress.