Sunday, July 3, 2016

BONUS! Driving a High power RGB LED from Arduino only.

While I was working on the hardware side of the Sound Reactive LED lights tutorial, my friend decided to make one for his room as well. So hopefully I'll manage some better photos of the build process for that.

In the meantime, my Linux work laptop decided that I was being too productive in my vacations and decided that I should go on a break. Hence, I am struck with just one laptop now. I know, I sound crazy, but once you have used 2 computers simultaneously, having just one makes you feel restricted. Same goes for those insanely cool multi-monitor rigs.

Anyhow.

Along with all those components needed for the sound reactive lights, I had also ordered one small little thing. A 10 Watt RGB LED Module. These are classified as high power LEDs and it is required to use a proper heat sink with them as they get very hot. I used to wonder how hot can a 90% efficient device can be, so I tried to power it without a heat sink.
At 300mA for 3-4 seconds, the back plate of the LED got so hot that it burned my finger when I touched it. WOW!
Not to mention that these things are INSANELY BRIGHT! You cannot really directly look at them without hurting your eyes and going blind for a second afterward. And that is for a 10 watt LED. 100 watt LED's are also available and I can only imagine how bright those are.

Why did I buy one? I was curious.

The main issue with these high power LED's is that they require special methods to receive power, And are very sensitive to the power input. That is why you cannot simply attach them to a battery to make them work. Nope, you need a Constant current power supply to get the most from them.

Theory Time!

LED stands for Light Emitting Diode. It's a DIODE.
Remember Ohm's law? Stating that "For a given voltage, the current flowing in the circuit is dependent on the resistance" or mathematically V=I*R. Basic electrical law.

Also, remember that Diodes and other semiconductor devices do not obey the Ohm's law. What that means is that for a material obeying ohm's law, the V-I graph is linear. For something like a semiconductor, it's non-linear.

Values are not important. The nature of the curve is, which is linear here. This is for  normal resistance, following Ohm's Law


See the graphs on the right. Non-linear, and different for different colors.
The Dashed line represents the max efficiency current. Hence, constant current power supplies are required.



So what's the big deal? Well, for diodes, a small voltage change can cause a huge increase in the current. And as that current increases a certain limit, The diode junction is bound to get damaged.

That is the reason there always is a resistance in series with the LED, usually of a value of around 150 ohms, whenever an LED is to be used with Arduino or a Raspberry Pi. It limits the current flowing via the LED and protects both the LED and the board, by protecting from over-current.

Simple resistor, used to limit current to the LED


Neat solution. But this works only for low power LED's, typically those 3-5mm LED bulbs. What if we use a resistor to regulate current for a high power, say 3 watt LED? You need a big resistor. And I don't mean a big value resistor... I mean a resistor that can dissipate quite a lot of heat, without getting destroyed. Normal electronics resistance are 1/4 of a watt. For a 3 Watt LED, you need roughly a 5 Watt resistor. So to drive 3 watt LED, you end up wasting another 5 watts as heat. That's not very efficient, is it?

RGB LED strips have small resistance in the strips itself. hence we can simply connect it to a battery and it will work fine. That is not the case for high-power LED modules. They just have the diodes in them and no resistance is included, which makes it necessary to have a high power constant current source to power them. And you cannot simply connect them to a battery. They will burn out within a couple of seconds. Even more importantly, for RGB LED's, there are different forward voltages for different colors. For example, the LED I got had 4V for Red, 7V for blue and 9V for green. Those are the forward voltages. The forward currents were 350mA for each color. At such high current, there is no way that I can simply put a resistance in there.

Another solution is to use something like an LM317 as a constant current power source. That's a neat idea, but again it gets limited to about 100mA of constant current. Anything over that, and you will need a heatsink on the LM317 and again a high watt resistance. So that's also not a viable solution.

Yet another solution, one that is used commercially in LED light bulbs on mains is to use a DC Buck converter with an Op-Amp to get a constant current source. Such breakout boards are easily available on Amazon and EBay and have a precision potentiometer to tune the voltage and the current output of the buck. They can also supply around 3 amperes of current without the need of a heat sink. All this makes them ideal for constant lighting application, where there is just one color LED and that too is maintained at a constant brightness, just like a normal light bulb. For RGB LEDs, you need 3 of them, and there is no way to control the brightness other than manually changing the resistance values, and even then, most of the RGB LEDs won't work because they are the common anode type, that is single +ve and 3 -ve lines per color.

Commercial solution for Driving LEDs
Notice that Voltage is variable, but the current is fixed at 300mA. That means this supply can work with 3-5 watt LEDs


So it seems that if you want to drive an RGB LED, on a budget (I AM ALWAYS ON A BUDGET), there is no easy way to do so....  or is there?

There is. A very simple way. So simple that I did not even think of it.
The answer yet again was PWM.

So I loaded up the Arduino, connected the MOSFETs and the potentiometers, wrote a simple code to change the duty cycle of the PWM pulse as per the value from the potentiometer, and hooked up everything to my 12V 300W ATX power supply.

I used the following:-

  • Arduino Nano
  • 3X IRFZ44N MOSFETs
  • 3X 10kOhm Variable Resistance
  • 10 Watt RGB LED Common Anode (+ve)
Here is the final circuit diagram. P.S. I'm new to Fritzing. Yellow Wire is +12VDC. Black is ground.


The LED module is at the top. I could not find something similar to what I have, but the connections are similar..


You might think that connecting a 7V LED to a 12V line capable of pushing nearly 20A of current will surely blow out the LED. You are correct, but what you are missing is that it's not connected directly. There is a MOSFET in between that will regulate the power delivered to the LED. Of course, make a direct connection if you wish to see smoke and stuff.

Since I had no idea that how much of the duty cycle will result in how much current, I added a serial monitor output that gave me the duty cycle of the PWM on the screen of my laptop. That way, I could see the duty cycle of the PWM.

Here is the sample code, with the fixed values for my case. Yours may vary. Check accordingly.

int redled = 9;     //red channel to D9 of arduino
int greenled = 10;    //green to D10
int blueled = 11;    //blue to D11

int redPin = 0;        //red pot to A0
int greenPin = 1;       //green pot to A1
int bluePin = 2;        //blue pot to A3
int pred,pgreen,pblue,lred,lgreen,lblue;         // variable to store the read value

void setup()
{

  Serial.begin(9600);
  
  pinMode(redled, OUTPUT);   // sets the pin as output
  pinMode(greenled, OUTPUT);
  pinMode(blueled, OUTPUT);
}
void loop()
{

  pred = analogRead(redPin);    //reads the ADC values coming via the pots
  pgreen = analogRead(greenPin);
  pblue = analogRead(bluePin);

  lred = map(pred,0,1023,0,90);     //maps the ADC to pwm duty cycle
  lgreen = map(pgreen,0,1023,0,190);   //0,1023 is the adc values
  lblue = map(pblue,0,1023,0,145);      //0,90/190/145 are the pwm values

  analogWrite(redled, lred);      //output to the Digital pins for PWM
  analogWrite(greenled, lgreen);
  analogWrite(blueled, lblue);

  Serial.println(lred);      //prints the PWM duty cycle in serial monitor
  //Serial.println(".");
  Serial.println(lgreen);
  //Serial.println(".");
  Serial.println(lblue);
  Serial.println("  ");

delay(100);
}



Next, I simply connected my Multimeter in series with every color channel one by one and started to increase the duty cycle by rotating the pots. As soon as the current reached 320mA, I recorded the PWM Duty cycle for that color, via the serial monitor of Arduino.

Completed circuit. A bit of mess, but minimalist.
Proper heatsink with thermal conduction paste in between is a MUST!

The net result

The front of the LED is perpendicular from me, Its daytime, and you can still see lens flares here.
And this is 1/3rd the brightness.


Repeating this for every color, I found the maximum duty cycles for red, green and blue. Next, I simply replaced the maximum duty cycle of 255 in the Arduino code to the values I found above.

Upload the updated code, and it's done! Since the PWM signal is now limiting the maximum current using the software, I can directly power the RGB LED without any complex circuit, and still, operate the LED at full intensity. There is no heat loss, and the whole system works on 12V, and I can even select what color I want, by rotating the pots. Simple!

Did you enjoy reading this? Thinking of trying it out? Some mistakes here and there? Do let me know! Feedback is much appreciated, as always.

Have fun!

Monday, June 6, 2016

Making the party room (Theory Post)

Remember when I said, "sooner than you expect?"

Well, this post here is a tutorial, like my RasPi lighting system. Actually, its more of an extension to that project really, since I am using the same LED Strips I got back then. Only now, they are properly mounted.

So what is different now? 

Have you ever been fascinated by those disco lights? Ones that change their color and intensity according to the song that is being played? I bet you have. I bet some of you even wanted those in your room. I know because I wanted one. But those system, don't come cheap. And I'm already on a shoestring budget, pretty much always. The cheaper solution is (pretty much) always to make your own. And hence, I turned to Arduino, the nifty little microprocessor.


The Arduino Nano. There are many versions available. The Uno being the most popular. You can use the Uno or the Nano. They have the same processors, but the Nano is easier on the breadboard.



Basically, What I needed to do was to split the audio into 2 signals, one going to my main stereo system, and one going to the analysis chip to break down the sound into diff parts, identifying the lows, mids, and highs of the sound. and somehow drive / light up the LED strips according to those processing values,

The first part is easy, splitting the audio signal. I just bought the 3.5 mm audio jack splitter. DUH! You can also use a microphone placed near the speaker, in-case you do not want to tamper much. But, that will also pick up the ambient noise, so will be good only in silent environment. Only thing that is required is getting a clean audio signal.


Simple Audio Splitter. You can use a microphone board as well, if you like.


Now for the second part, the signal analysis. It's a bit tricky. Especially if you plan to use an arduino only. That is because the Arduino itself is not meant for direct audio. We can however, turn it into a spectrum analyser using some pretty complex maths. Remember Fourier transforms? That's what the Arduino will have to do, hundreds of time per second, to separate the audio into different frequency bands.

Okay, a little bit about Fourier transforms...  

"The Fourier transform decomposes a function of time (a signal) into the frequencies that make it up, in a way similar to how a musical chord can be expressed as the amplitude (or loudness) of its constituent notes. The Fourier transform of a function of time itself is a complex-valued function of frequency, whose absolute value represents the amount of that frequency present in the original function, and whose complex argument is the phase offset of the basic sinusoid in that frequency."

Didn't got any of that? Me neither. In simplest terms, any sound you hear, in music, is a mashed mixture of sounds of various frequencies and amplitudes together hitting your ear. What this transform does is that it takes this sound, and breaks it into individual components of the sounds that make the original sound. For example, a drum kit will have bass, mid and treble, all together. Fourier will separate out these different frequencies so that they become a simple sinusoidal function, which can be easily processed by a computer.


Here, the Red line is the sound pattern you hear. 

The blue lines in the background are what the fourier transform does.
Breaking the main sound  into its components, so that computers can process it.




The problem however is that these transformation equations are complex. Humans, like me take forever to solve them, and even small microprocessors like the arduino may not be fast enough to calculate them for a proper song. The obvious solution is to use a faster CPU, which is done in providing visualisations of music in music players on PC and mobiles. But, we are on a budget, right?

Thankfully, there is a simpler solution. Enter the MSGEQ7 IC.


It's TINY!


What this IC (integrated circuit) does, is that it takes away all the maths that's needed and stuffs it into a tiny space, one that we do not even need to go into. 

"The seven band graphic equalizer IC divides the incoming audio signal into seven bands of 63Hz, 160Hz, 400Hz, 1kHz, 2.5kHz, 6.25kHz and 16kHz. The seven frequencies are peak detected and multiplexed to the output to provide a DC representation of the amplitude of each band."



The frequency splitting graph of the MSGEQ7 IC


So, this chip solves 2 major problems for us. It splits the audio signal into 7 bands of the mentioned frequencies, and also gives a voltage output for these frequencies according to their respective amplitude. That is great, since we can use that output as a signal to drive the LED lights. And since every frequency will have a different voltage level, We can make the LEDs light up in different colours as per the frequency. Example, Red glows for bass or the low end, green for the mids, and blue for the high end.






The green chip on the breadboard is the MSGEQ7 IC
The 3 black parts on the left are the MOSFET's which are driving a simple single RGB LED. 
Here is a basic circuit diagram, if you want to start right away. Obviously, there is Google as well, so you might as well just search for this thing. There are plenty of guides on how to make one. Ofc, I'll be posting a proper hardware guide later..


Here it is in working. (What I made)







I think this might have piqued your interest,after the boring part of theory. But that was a bit necessary. Without knowing how all this is actually working, there is no point in making it. You might as well buy something off the shelf. The advantage here is that once you know why you are doing what you are doing, you can customize it however you want.

So ultimately, what we are going to do, is to feed the audio signal into the MSGEQ7 IC, let it do it's thing, Take the Voltage output of the analysed signal from the IC, and feed it into one of the Analog pins of the Arduino. Once the signal is in the Arduino, we simply take the signal values for the different frequencies and map them to the PWM output, which with the help of some MOSFET's light up the LEDs according to the beats of the song. SIMPLE!

That's probably it for the theory part. I'll be updating this with the parctical guide in the coming time as well. Stay tuned!

And as always, Feedback and suggestions are very much appreciated. Thanks for reading!

Wednesday, June 1, 2016

The 10-Month Hiatus

Yeah, I know.
10 months.

I cannot stick to a schedule. I know that as well. But, I keep on working. I may not document what all I've been doing, even though I want to, but I just don't end up having that much time to do it.

You might ask then, What have I been doing since the last post here.

It's been a roller coaster, frankly. If you guys follow me on Google+, you would have some idea as to what all has happened.

First, let's talk about the update to my RasPi Lights. (Previous post)



The Main "Brain"
This took more than a year, from start to finish, and back then, I knew nothing about anything in this pic.


As you can see, It's now more or less in its final form. Here are the basics.


  • RasPi connected to
  • BMP180 for ambient temperature and pressure
  • DHT11 for ambient temperature and humidity
  • DHT11 for room temperature and humidity



IP address at top
Readings from BMP180 (outside room)
Readings from DHT11 (Blue sensor just below the screen)
Readings frm DHT11 Outside the room, alomgside the BMP180


All the sensor reading are now recorded and stored in a web server database at intervals of 10 minutes.
Graphs of all the readings plotted in real time on the web page hosted by the Pi itself.


BMP180 readings graphs, Opened on Chrome.
Blue is Temperature, Red is Pressure
I can actually predict the weather with these!


And obviously a much neater RGB LED Driver system.
Prepare to be dazzled!



My favourite setting. There's a red Laptop missing from the table here tho.

I saw somewhere speakers from LG that had LED Lights in them. That right then was a "Challenge Accepted" moment.
Turned out to be pretty good, I think

Obviously, It's an RGB LED Strip

Not sure if this is gaming mode, or the red one is...


Usually in this setting. The LED's are very bright and I do not end up needing any additional lighting in this config.
Photos look dark because they are underexposed to -2EV



Next, There's now an Arduino Nano controlling the power to my Pi, Speakers, Desk-Lamp, which is controlled by Bluetooth.
And it also monitors the Battery Charge, current drawn and the voltage of the Solar power system.


From left to right:- Buck converter, ACS712 Current sensor, Nano, Relay board (hidden), Screen.


Yeah, Everything I've mentioned above is running off solar energy. 12 Watts panel connected to a total of 14Ah of Lead Acid battery.


Salvaged PWM Solar charge controller I had lying around. Gives a total of 150W of backup.

And no more breadboards here. Its all Permanent, Although I think I could have made it a little better with more planning. But it works fine ATM.

That's what I have done for my room. But that's not the only thing. Me and my pals were also working on a project for college.
Here it is.





Basically, what we tried is to make a robot that mimics the movements of the human arm and replicates it in a servo driven arm with at least 1 degree precision.
Its close, but calibration is quite hard, considering all the parts we are using are cheap ripoffs and no original stuff can be found here. Tough luck.








For the curious, we are again using a Nano, with 6 Potentiometers as the angle sensors, and then feeding the angles measured to servos via a PCA9685 PWM driver board.
Servos are Tower Pro MG996R (Again, not originals)

The electronics on this were easy. The manufacturing and assembly of the parts were not even close to easy. It took a lot of time (about 3-4 months), and a little blood, to get to this point here.
And the best part was, when I got to present my project, the teacher just scoffed and said that we got it made from the market.

And then people ask why India is not doing so well in R&D. Short answer, because of people like him, no one actually bothers to work.



But that's not the worst.
Along with all this, I also got involved in what has to be one of the most challenging and amazing projects I have ever heard about. The CanSat competition organized by the American Astronomical Society and NASA.

We had to design a can shaped payload, which would be  launched and ejected into the atmosphere at approximate 10km from the surface. The payload then would descent, using parachute, and at a height of 400m, a fixed wing glider should be deployed, which is recording the temperature, pressure, humidity, air velocity, GPS data every second and transmitting it back to the ground station using XBee radios, whose graphs were to plotted using MATLAB in real time. Not to mention, there was also a TTL Camera to be there in the Glider.

That sounds AMAZING, right? That's because it is. We had to make everything from scratch, test everything, pack everything in a space of nothing more than 300mm by 120mm diameter, having a weight of less than 500gm.

And I'm pretty sure we would have managed to make the satellite, possibly even be in the top 10, if it were not for some really, REALLY big IDIOTS in the team. And sadly, those idiots were my seniors, and I couldn't really do much. It was out of my control.

The work got delayed, the parts were late, there were only discussions, and no actual work had started until April, and were were scheduled to go to Texas, USA for the launch in June.

Now fortunately / Unfortunately, we were kicked out of the competition in the final round of presenting the Critical Design Review to the judges, Mostly because the guy making the presentation had english grammar skills worse than that of a 10 year old. Also, we had not actually made a single thing, and we were presenting a CRITICAL DESIGN REVIEW of that.

I say fortunately, because thankfully I do not have to deal with such level of insane and imbecility anymore, let alone stay with them as a team for 2 weeks in US during the competition.
Unfortunately, because this was once in a lifetime kind of opportunity for me, and because of them, we lost it. Not to mention the visa fees and other stuff. Those guys were just looking for a visa and I don't think that they were really interested in making cool stuff.

I should have been packing my bags for the trip, but I'm just typing away at my keyboard. Feels bad, but what are my options anyways?

One option is, to make Sound Reactive LED lights, the ones that respond and change color and brightness according to the beats of the song. It looks good!  I've already started working on it, and as of now, after spending about 5-6 hours on that, at least the basic circuit is working. 

I also got a 10 watt RGB LED module, just because I was curious, and I am working on that as well. 



It's a MONSTER! At full power, you cannot even look at it, and heats up the Intel 775 Heatsink fairly quickly.
Just  pure insane, but very fun!

And that is what I'm gonna be doing in my Vacations now. Apart from maybe a few other small things. I'll try to write some small guides here... some sort of documentation, but I can only hope.

If you guys like what you see here, Follow me on Google+! There's my Photography Collection and a basic IoT Collection as well. And obviously, its not updated once in 10 months. :P


Questions, requests and feedbacks is much appreciated.  Until next time, and it will be sooner than you think. :)