Strip Led Dimmer
In this tutorial, you will learn how to create a light dimmer for an LED strip using a light sensor.
We drew our circuit diagrams using the open source software Fritzing.
What do you need :
In this tutorial, we will use a light sensor to change the intensity of a unicolor strip LED. You can update the code to use a RGB strip led with 3 PWM outputs, using range, if luminosity is between X1 & Y1 switch on color A, if luminosity is between X2 & Y2 switch on color B, if luminosity is between X3 & Y3 switch on color C.
- Electronic circuit
The sensor will send the luminosity information directly to the board on analog pin A0, the command will then be sent from the PWM pin 9. You have to correctly choose your transistor, it needs to be a “level logic gate drive”. This is usually indicated with an “L” in the name, for example IRL520, IRL540, IRL640. You also have to check the maximum current that it can support. This can be measured, simply connect your strip LED directly to the 12V DC and with an ammeter in serial, check the value of the current. For example, since we draw about 0.2Amps per meter, if you have a 5 meter strip you will need to pass up to 1 Ampere. If you use a RGB strip LED, it will be 0.2Amps per meter per channel. You can add a 100Ω-220Ω resistor on the transistor’s gate pin.
We chose an IRL540N because it allows 36A, so we will have a margin to do whatever we want.
Circuit for unicolor strip LED
Circuit for RGB strip LED
- Arduino Code
In your code, be sure to specify “analogReference(EXTERNAL);”. When using an external analog reference, it is important to specify analogReference(EXTERNAL) in your code before performing any analogRead() operations. Otherwise, your external reference will be shorted to the default internal 5v reference. This can damage your Arduino.
The code is very simple, it reads the sensor value, constrains it between 2 values, then write the value in the PWM output pin. The light intensity changes every 100ms thanks to the delay, you can change it as you want.
You can setup the 2 variables (ValMinCalibration & ValMaxCalibration) to constrains the sensor. The higher the range between the two values the more the light intensity will increase smoothly. Do take care not to make your value to high as the LED will not be able to reach the value due to the voltage limit. The values in the code are already well calibrated to have a good luminosity in terms of the light sensor.
Below a impression of the final system, on the first picture the strip LED works with the current luminosity. On the second one, we just reduce the luminosity seen by the sensor by putting a little piece of cardboard on it. As you can see the luminosity is decreased.
You can also try to change the code to make the intensity increase as the value from the sensor decreases.