RGB firefly on Attiny13

I welcome you, colleagues, comrades, companions and just interested!
The other day I made a device that can be used as the basis for an original gift, a souvenir, or simply made for your own pleasure, if any of you are fussed with modern microelectronics. Immediately video demonstration of the device:
Unfortunately, the camera cannot convey the shades and overflows of the glow of the RGB LED, it looks much nicer live. However, if you are nevertheless interested, then there are many letters and pictures.

The prerequisites for creating the device were an irresistible craving for anything and everything filled with microcontrollers, an article by comrade Ariman ( We create an original gift using the chemistry of physics and electronics ) - for which he once again has a separate respect and respect, and the desire to do something interesting with his own hands.
First, I bring to your attention a quick assembly manual, and then, for the curious, the details of the internal structure and performance characteristics.
So, we need:
1. Atmel Attiny13 microcontroller - 1 pc.
2. RGB LED - 1 pc.
3. Development board type CRS 044 - 1 pc.
4. Resistor 1 MΩ, smd - 1 pc.
5. Copper wire (twisted pair core) - 30 cm.
6. CR2032 battery - 1 pc.
7. Paper clip - 1 pc.
image
It also assumes the presence of a soldering iron, a simple tool for working with electronics and straight arms.
First of all, cut a piece of about 3x3 cm from the breadboard. The size is determined by the dimensions of the battery, which should fit on the designated piece, taking into account the clearance for the wire sensor on the sides.
image
After sawing out a piece of the board, we expand the paper clip, which we will use as a battery contact holder. We bend the paper clip as needed and fix it in the holes of the board. We insert the battery and bend, so that it would hold tightly. Also from the copper wire we make the second contact for the negative pole of the battery.
image
Solder both pins to the breadboard. Once again we stick in the battery and check the reliability of its fastening. It is better to tighten the paperclip more tightly, try until the battery holds well.
image
And now we will sew. But not a microcontroller, but recall the skills of sewing with a thread and a needle. The stitch behind the stitch, around the perimeter, bypasses our shawl with copper wire, and put the ends in the designated place. This will be our sensor. One moment, at the corners of the main diagonal, we bite the wire and fix it. Therefore, as it turned out later ("... experience, the son of difficult mistakes ..."), such a large sensor is poorly calibrated and leads to poor-quality touch detection. So the role of the sensor remains only on two sides of the square of our board, the rest is decorative (in the photo the sensor is not bitten, because all this became clear after trial tests. But the cut sensor is visible on the last photo of the finished device).
image
Well, we got to our microcontroller. What a wonderful thing this is! On a tiny site with only 8 paws, you also have an ADC, a timer, a hardware PWM, and an oscillator built-in and you just need to show him electricity, but it already works. What else is needed for happiness, especially since the price of this pleasure is only 75 kopecks. Of course, there are some drawbacks, but - well, the world is not perfect. Alright, enough lyrics, let's continue.
So, solder our tinka to the prepared site. STOP! .. I almost forgot. Now we will sew.
Download ready-made kosher firmwareand fill it in any convenient way, I personally used the simplest LPT programmer, though I did it a little for myself, but this is not the point - the circuit is simple like a stool, it works reliably like a hammer. We don’t touch the fuses.
Now we solder our tinka to the prepared site: the 4th output (ground, minus the source) to the wire contact from the center of the battery, the 8th output (power, plus the source) to the clip, the 2nd output to the wire from the sensor. On the side we hook an smd resistor (1 MΩ), between pins 2 and 3. If the smd resistor is not at hand, the usual one can be shoved.
image
The thing remains for small - RGB-LED. We need an RGB-LED that has four pins (there are, however, oddly enough, and with 2 pins - do not take them). Three of them, these are the anodes of diodes of red, green and blue colors, one conclusion is general. The LED lens must first be fused with transparent silicone hot melt adhesive. If this is not done, then it will not shine very beautifully, because emitters produce beams that hardly mix and generally do not look ice. So we warm the thermogun and densely apply molten silicone to the LED head, here you can show your sculptural abilities by trying to depict a flower, a bug or whatever happens.
image
Then we solder the LED to mikruha, common to the 4th leg (ground), red to the 5th, blue to the 6th, green to the 7th (the color arrangement matters, because the programmed brightness correction is used for green) .
image
That's it! We boldly and deftly insert the battery - 3 colors should light up in turn, each for 1 second. During this time, you should remove your hands from the device, because then the sensor calibration immediately starts, which we are signaled by a fervent flashing for about 4 seconds. During this period, the state of the sensor is recorded when it is not touched. After the illumination is over, you can take the firefly in your hands and watch the mesmerizing overflows of colors, the play of light and halftones.

If you have not yet run to the nearest electronics store for details, then I offer you a clue to the focus, i.e. how it all works. Also, perhaps, something will seem interesting to those who are in the subject. As I already wrote, the design was based on the above article. Therefore, for a description of the basics and the operation of the sensor, I send you to it, everything is very well chewed there. My improvement is connecting to the RGB tinke and refactoring the code. I’ll explain in more detail: the 13th tinka, of course, is beautiful, but it only has 2 PWM hardware channels, and to control the RGB LED, as Cap tells us, you need at least 3 channels. This begs the question: where to get another PWM channel? To the question, the simple one has the answer - we won’t look for another PWM, and we won’t use a hardware PWM at all, but we’ll fill in our own PWM - Orthodox, how many one channel! We will pass from loud slogans to real practice. I hope that such a PWM, duty cycle, do not need to be explained? (Rhetorical question).
So, it is given:
1. The frequency of the PWM channel is not less than 100 Hz, actually the eye and from 50 flicker practically does not notice, but real boys do not look at less than 100. T.O. the pulse duration with a duty cycle of 100% should be 10 ms. In these 10 ms we have to push the brightness control, at least 100 levels. Then we get that the minimum pulse duration is 0.1 ms (at a duty cycle of 1%).
2. The frequency of the internal timer of the microcontroller - depends on a) the frequency of the clock generator, which, since we use internal, maybe 4.8 or 9.6 MHz; b) the divider of the clock generator; c) the divisor of the timer itself. Using a timer, we should be able to count 0.1 ms intervals, i.e. process events at a frequency of at least 10 kHz.
In short, as a result of theoretical and empirical research, such results were obtained. The controller clock frequency is 9.6 MHz, the timer works with a division of 1/256 = at a frequency of 37.5 KHz. In order to be able to process events at this frequency, we set the timer operation mode by triggering by coincidence. Those. as soon as the timer counter matches the value set in the ORCA0 register, an interrupt occurs - and here we are. At the same time, the set value of the ORCA0 register, as some of you have already guessed, should be 0. The brightness will be regulated not by 100, but by 256 levels (the controller is 8-bit, so it is more convenient, and there are more shades), then we get the frequency PWM 37500/256 = 146 Hz, which is more than satisfactory.
Let’s take a closer look at interrupt handling:

//глобальные переменные
unsigned char wave_counter=0;//счётчик ШИМ цикла

unsigned char red_bs=0;//яркость светодиода1 (0-255)
unsigned char green_bs=0;//яркость светодиода2 (0-255)
unsigned char blue_bs=0;//яркость светодиода3 (0-255)

interrupt [TIM0_COMPA] void timer0_compa_isr(void)
{
PORTB.RED=(wave_counter PORTB.GREEN=(wave_counter/2 PORTB.BLUE=(wave_counter wave_counter++;
}


the brightness of each LED is set by the global variables red_bs, green_bs, blue_bs and is changed by the main program according to the special effects plan. The wave_counter counter is spinning in a circle. When the brightness of the LED is greater than the counter value, it glows, otherwise it is off. T.O. if the brightness is 255, then this corresponds to 100% duty cycle. I made a division by 2 in the green LED in order to increase its brightness, because its visual luminosity turned out to be less than the others, apparently the specificity of the spectral characteristic. In general, food lovers and those interested can appreciate the whole source .
Much more could have been written about adjusting the overall brightness, and about the processing of the sensor, and the size of the program and the amount of memory (oh, dumb, here's another sore spot), setting the watchdog timer and low power consumption mode (by the way, as the meter showed , in hibernation, the circuit eats 0.06 mA) ... It would be possible to attach waveforms to increase the visibility and clarity for beginners, discuss the use of the rand () function and discuss the juxtaposition of the charms of assembly language to the convenience and power of optim tion CodeVisionAVR compiler code. But, as the unforgettable Kozma Prutkov said: “One cannot embrace the immense,” and popular wisdom says “Little by little good.” I hope that I will still have the opportunity to share with you the delights in this area, which I also wish you, also professional,
Ladies present - with the coming!

Also popular now: