Proximity Light Switch With Sound Effect on Arduino
by CyberLab in Circuits > Arduino
240 Views, 2 Favorites, 0 Comments
Proximity Light Switch With Sound Effect on Arduino
Hello everyone!
Today I will tell you about a proximity switch with a sound effect, which was made by me 9 years ago, or to be precise, in January 2012. I posted a link to the 2012 video in the comments below this video. Since then, the switch has been working with me around the clock for 9 years. What is most interesting, he During all this time, he did not fail and never even hung up, and he also never had false positives. In general, it has proven itself well and I can confidently recommend it to you for self-assembly. If you are interested in all the details, then please watch this video to the end and rate it by subscription and like.
Let's Start
I have 7 lamps mounted in the corridor.
Wires
And to achieve a beautiful visual effect, I used sequential switching of lamps, for this I needed to stretch a separate wire from each lighting point to the controller board.
Hid
I hid the board itself in the space between the drywall and the ceiling, since there is more than enough space there.
Sensor
I placed the infra red receiver and the LED in the socket. In order to avoid false alarms, they need to be isolated from each other, for this I used a heat-shrinkable cambric. To connect this optical sensor to the controller board, you can use the wires embedded in the wall.
Design
So that the design of the switch does not differ from other installed decorative overlays in the interior, I used a TV outlet from the same series, from which I threw out all the insides, and glued a round window cut out of purple acrylic into the hole.
PCB
All the components were placed by me on one board, on which screw connectors are installed for connecting wires from the lamps.
Charger
I powered this board with a regular phone charger. Which to this day works, you see at that time the charges were made according to more reliable schemes than now.
Arduino
The basis of the whole device is the arduino Nano V.3 controller, but you can also use any other boards with the Atmega328 microcontroller
An IR LED with a phototransistor can be taken from the obstacle sensor, but it is not necessary to solder them, it is enough to cut the extra tracks and solder 3 wires to them. If you have from somewhere these previously soldered parts, then before using, it is better to first check them for operability. Connect the infrared LED to a voltage of 5 V, through a 120 Ohm current-limiting resistor and look at it through the phone camera, it should glow purple. To check the phototransistor, you will need any tester with a continuity test function. We transfer the tester to the dialing mode, and connect the phototransistor leads to the tester probes. After that, you need to bring any remote control from household appliances to it tightly and press any button. An intermittent squeaking sound will be heard in response.
Relay
9 years ago I did not find suitable solid state relays and I had to assemble them myself from loose materials. But at the moment it is easier to buy an 8 channel solid state relay module like in the picture than to waste time looking for these components.
The Switch Works As Follows
Arduino from the D5 output constantly outputs a PWM signal with a frequency of approximately 977 Hz. An LED is connected to this output through an 82 Ohm current-limiting resistor, which emits a signal in the infrared range. The phototransistor connected to the D2 input detects the IR signal reflected from the hand and checks it for plausibility, and if a signal of 20 or more consecutive pulses corresponds to a frequency of 977 Hz, then the controller turns on all 7 lamps in turn and starts playing a sound effect through the PWM output D11. All the same happens when you turn off.
Playing Sounds
The uncompressed WAV format is used to reproduce sound effects, with a frequency of 16000 Hz and a depth of 8 bits, but when playing this format using PWM, an unpleasant hiss and hiss are observed in the audio path. Therefore, to improve the playback quality, I used linear interpolation in the code. At which, the sampling of samples occurs at a frequency of 62.5 kHz and between the original samples 3 additional samples are inserted, calculated by the method of linear interpolation. Thus, quantization noise is reduced at the output, whistling disappears, sound quality is improved and additional RC filters are not required for playback.
Instead of a speaker, I used an old, small computer speaker with no built-in amplifier.
Convert
To convert Wave files to C code, you can use the online converter
Circuit On the diagram, solid-state relays are marked with gray rectangles, and for those who want to get confused, they can assemble the circuit completely, just like I did in the distant past.
Download the sketch in one archive
This time I decided to add all the libraries used to the folder with the sketch, and in the sketch itself I wrote down their local use. Now I hope newbies will have fewer questions for me about compilation errors. The code contains several constants that can be changed before flashing. The power_ir constant is responsible for the breaker operation distance; it can take values from a minimum of 20 to a maximum of 200. The value you require can be determined experimentally. lamp_num - Determines the number of lamps you are using. The minimum number of lamps cannot be less than 1, and the maximum is not more than 7. If you correct the code, you can increase it to 15. lamp_delay is the delay between consecutive switching on of lamps, which is expressed in milliseconds and can start from 0 to 4,294,967,295 ms. Although I do not think that such huge delays will be needed by someone
Conclusion
In conclusion, I would like to add that I am very surprised that a microcontroller without WDT has never frozen in 9 years. For the same reason, I did not edit the code and add WDT to it, since Arduinos with old bootloader do not know how to work with it and there are still quite a lot of them in the hands of the DIY community Thank you for reading to the end! If you liked my article, then support it by liking and subscribing. If you have questions, you can ask them in the comments.