Reflecting on a gift for a girl on March 8, I remembered the project of an RGB LED lamp, which was decided to be implemented. Especially since everything needed for this was at hand. But to make the gift more interesting, I added control using an optical IR sensor, which made the original handling of the lamp and also did not spoil the design with the buttons.
')
The RGB LED is essentially 3 LEDs of different colors in a single package. In my case, a LED with a common anode and with 3 cathodes, one for each diode, although the designations on the diode are reversed.
To get the different colors of the glow of our LED, each diode must be turned on using PWM 'a (pulse-width modulation), otherwise the LEDs will be lit to the maximum, which ultimately will give us a white color.
PWM is a pulse signal of constant frequency and variable duty cycle. By setting the duty cycle, you can change the average voltage at the PWM output, as a result, the brightness of a specific LED will change. In our case, the PWM frequency will be 1kHz. To generate a PWM signal of variable duty cycle, we need a microcontroller.
Optical sensor - consists of an IR LED and an IR receiver. The IR LED and the receiver will be directed upwards, when you hold your hand over the sensor, the light will be reflected by hand and will be accepted by the receiver. Since the RGB LED has a strong parasitic infrared radiation, it is necessary to protect the sensor from false alarms. To solve this problem, use the receiver TSOP1738. It provides for triggering only on a modulated signal at a carrier frequency of 38 kHz, but if the modulation pulses go continuously for more than 1 second, the receiver recognizes them as interference and will not react to them. It was experimentally revealed that in order for the receiver not to cut the signal, it must be interrupted every 100ms for 20 ms.
The LED should be about the same signal.
A microcontroller (mk) is a chip designed to control electronic devices. To implement the control, it is necessary to write a program and flash it in a micron with the help of a programmer. In the final version of the device, I used the ATMEGA8 micron, and for the first experiments I took the ATMEGA32, since for him I had a debug board on which everything could be quickly assembled.
In ATMEGA8 - there are 3 timer / counters, their essence is that they can count clock cycles with a selected frequency, which depends on the clocking frequency u. When a certain value is reached, an interrupt occurs, according to which the interrupt code will be executed.
Timer / Counter 0 This timer is 8 bit, which means that it can count up to 255 cycles, after which it will be reset. This timer does not have a hardware PWM generator, it will be used for program modulation for the IR LED.
Timer / Counter 1 Timer / counter 1 - 16 bit, it counts up to 65,535 cycles. He has 2 hardware PWM generator, which will be used to control 2 of 3 diodes.
Timer / Counter 2 It is also 8 bit and has 1 PWM generator that will drive the 3rd diode.
As a programmer, I used USB programmer USBAsp To assemble it you need to flash one micron, so there are simpler options that really require LPT or COM ports.
I wrote the program in C using CodeVisionAVR and ImageCraft7. First of all, you need to set the timers 1 and 2 to PWM mode at 1 kHz. Then set the timer interrupt 0, with sufficient frequency to process the interrupt code, in my case the interrupt call frequency was 148 kHz. It is also necessary to establish an interrupt from an external source, it is necessary to respond to the receiver signal.
Upon interruption from the receiver to indicate the operation, the LED will flash with white light for 100 ms, and add 1 to the mode variable, in the main loop a certain mode will turn on depending on the variable. The mode change is implemented in a cycle by changing the duty ratio with a delay at each iteration.
Assembly
First of all, the schematic diagram.
Power is supplied from the power supply, the output voltage is 5 volts, the current is 1A.
I spread a fee in the program Sprint Layout 5
The board is divided into the main and optical sensor board. This is done so that the sensor can be conveniently positioned, the blue line on the main board is a jumper, the other blue and black lines are wires.
Made by laser iron . I note that it is better to use an iron with a steam generator, thanks to a couple of paper it is easier to move away from the board.
The board in the center in the final device was not used.
Preliminary assembly.
Lamp
In the collection
Since the glass itself turned out to be almost mirror inside, we had to isolate the receiver and the infrared LED with emery paper and foil, but it would be necessary to isolate it anyway, otherwise the illumination will be. The radiator is obligatory for installation, the diode will warm up very much and without a radiator just overheat, besides, in my firmware the diode works at 70% of maximum power. In the white light mode, the temperature rose to 53 degrees for an hour and did not rise any more, which is quite acceptable.
Final result
To implement the plan it took the following:
RGB LED, I took a power of 3 watts.
ATMEGA 8 microcontroller - for others you will have to modify the firmware.
ATMEGA 8 socket
IR receiver - for example TSOP1736
IR LED, for example FYL 3014ir
3 transistors BC337
0.1 μF capacitor
Resistors:
10 ohm - 2 pieces
3 ohms - 1 piece
1 Kom - 1 piece
10Kom - 1 pieces
330 ohms - 1 piece
4,7Kom - 3 pieces
AVR programmer
The power supply unit is pulse - the output voltage is 5 volts, the current is 1A.
Upd : If you want to build a lamp for this post, then do not forget to look at the sources, fuses micron are stitched at 8 MHz. Since the micron is clocked from the internal resonator, and its error is quite high and can be 10%, you will most likely need to adjust the modulation of the IR LED in the TCNT0 = 0xCA line; that its frequency coincides with the frequency of the receiver. Archive layout file for Sprint Layout 5.0.