"Magic Button" for LED on ATtiny4

SESAME


I once had a touchscreen wonder switch SESAME . I liked him very much. But times change, ceased to fit into the interior, and then turned out to be completely not designed to work with all sorts of fashionable energy-saving lamps. I liked in him the principle of control. A short touch of the sensor turned on / off the light, and a long-lasting brightness was adjusted. Who cares - the heart of the switch was the K145AP2 chip, an analogue of the Siemens S576B (the K145AP2 is still sold).


Under the cut is my version of the emulation of this chip.


Not so long ago, I built a backlight from the LED strip in an aluminum profile with a diffuser above the table and the question arose about the switch. Putting ready as something cumbersome. That on a wire dangling - not beautifully, the usual switch to put - spoils a look, and especially there is no place.


I decided to build a switch, and for one, and a brightness control, at the end of a 16 mm chipboard. Make it touch, cover with a sticker, which furniture masks the bolts.


Iron


Started with the sensor. I tried on the principle of charge transfer on ATtiny13A . Option work, but I was too lazy to bother with auto-tuning parameters and so on. He did not take the ready either.


Then I decided to try to implement the sensor on the QTouch library. As a sensor ATtiny10 . There is a ready-made utility that turns the ATtiny10 into a touch button with all the buns. But at the exit binary and add your code there is difficult. I thought about what to do, surfed the Internet, and then I came across the mention of the TTP223 controller of a single touch button. This option completely suited me.


As MK, the choice fell on ATtiny4 . Same small as TTP223 , 16-bit timer. And for a long time I wanted to do something useful on these tinkles.


As a key - P3055LD from the old motherboard.


Printed circuit board


When developing a printed circuit board, I proceeded from the fact that the minimum possible hole in the end face of the chipboard was needed, I decided that it would be quite enough with a diameter of 7 mm. The board turned out to be 7x28mm, two layers.


Later, when the board was soldered, it became clear that the board would not fit into the 7mm hole, at least 9mm - it did not take into account the height of the elements. The idea with a sticker somehow also ceased to like. And then came across a furniture stub! Designed for a 10mm hole, and the inside diameter is exactly 7mm! Everything coincided!


Himself pen of the sensor on a separate scarf which is soldered to the butt end of the main one. The pictures can be seen.


Pictures

cool image
cool image
cool image
cool image


Program


The control program is written in assembler. Every 32ms (Watchdog Timer) a sensor is polled. Depending on the current state and duration of pressing, certain actions are performed. The logic of the work is slightly different from the prototype K145AP2


If the light is off (state after power up):



If the light is on:



Too short keystrokes (interference) by the program are ignored. Brightness is given by a PWM factor (16 bits). PWM frequency of about 122 Hz (8000000 Hz / 2 16 ≈ 122 Hz)


To compensate for the psychophysiological perception of the brightness of the illumination from the real brightness, the latter changes over a portion of the cubic parabola . Usually, tables are used for this, but in my version the coefficient is calculated. The coefficient varies with the frequency of the PWM, that is, when the brightness changes, each pulse is obtained by its duration. The minimum PWM value is software limited.


Most of the time MK sleeps and together with TTP223 consumes about 16 microamps. That is, the scheme is quite suitable for devices with autonomous power.


ATtiny4 has six conclusions. Two powered, one default reset. I have already used two. Left one free. I thought how to use it. And then I remembered a new laptop of a friend with a Force Touch trackpad. As an experiment, I decided to do something similar. I don’t need special accuracy of the response, and there are a lot of vibromotors from old phones. As a result, I implemented a function in the program that a short impulse appears on the free output when the control limit is reached. In 1452, when the adjustment limit is reached, the direction of adjustment changes. Therefore, a certain skill was needed to remove the hand from the sensor at the maximum or minimum. In my implementation, when the limit is reached, the adjustment stops. The total adjustment time from one border to another is about 4 seconds.


Code available on github


TPI through Arduin


Separately, I note programming MK. My JTAGICE3 does not support the TPI programming interface. But, fortunately, good people wrote a sketch on Arduin to program this little thing. Not immediately, but everything worked out for me, the firmware was flooded and everything worked. In addition to the arduin you need 4 resistors. The whole process is painted in a sketch.


Total


The magic button is installed and works as intended. Current consumption and dimensions allow it to be built into devices with autonomous power.


I did not get the expected effect from the vibra. Here apparently need experiments with the installation site.


In the prototype K145AP2 and analog Siemens S576B is the conclusion "Sleep". This is a mode in which the brightness drops very slowly until it is completely turned off. As planned by the manufacturer, for this additional sensor is installed near the head of the bed. 16 bits of the PWM timer allow you to implement this mode.


This is from ideas for the future.


Button in place

cool image


Look like that's it.


Thanks to all!


UPD: As promised, raised the PWM frequency to almost 1kHz. GitHub Code

Source: https://habr.com/ru/post/411263/


All Articles