Author: lmu34
Source: DF Maker Community
Summer is here, and it’s the season of buzzing mosquitoes. There is no difference between domestic and overseas in this regard.
Today, we introduce a project that upgrades our weapon — the electric mosquito swatter, making our battle against mosquitoes a bit more fun!
The upgraded electric mosquito swatter features:
-
Add electronic devices to detect lethality -
Add a 4-digit counter to tally the “kills” -
Support USB charging -
Add “kill sounds” for a better experience
▼ Click to see the effect
Creative Thinking
The author’s idea is that when a mosquito hits the metal mesh of the electric swatter and generates an arc, there must be some change in current on the circuit of the electric swatter. If we can find a place in the circuit that can be read by Arduino or ATtiny (i.e., between 0 to 5 volts), then we can easily count and display the score and play sounds.
The sound function can be easily added to the ATtiny using the DFPlayer module.
The score will be displayed using a 4-digit 7-segment display, which can be driven by the onboard TM1637 using just two wires (two IO pins of the ATtiny).
Since these electronic modules operate at 5V, and the electric mosquito swatter usually uses two 1.5V batteries, we can upgrade the power supply of the swatter with a USB rechargeable battery.
In other words, this “weapon upgrade” is mainly done using relatively easy-to-find off-the-shelf electronic modules, and the only complex part is in signal processing, which is handled by the ATtiny through programming.
In the tutorial shared below, it is assumed that everyone has some understanding of Arduino and how to program; if not, please learn the basics of Arduino first.
Before we start, a safety issue needs to be mentioned:
The electric mosquito swatter uses high voltage (there are hundreds of volts on the mesh when the button is pressed), and even though the current is low, be careful not to touch it or its internal circuit when connecting the battery.
Note: Do not let children play with this!
Materials
For this “ultimate” electric mosquito swatter, we need to prepare the following materials:
-
An electric mosquito swatter -
Digispark pro (with ATtiny, easily found online) -
DFPlayer (purchased from DFRobot) -
Resistors: 1Ω, 1kΩ -
8Ω speaker -
SD card (a small capacity card is sufficient for storing some sound files) -
Lithium-ion rechargeable battery: used battery -
Battery charger -
Potentiometer (I used a 1MΩ one; it’s not very important, but a high value will limit the current consumption through the potentiometer) -
4-digit LED display with TM1637 (make sure it’s a 4-digit display + TM1637, not just a 4-digit display) -
Capacitor: 470μF -
On/off toggle switch -
Button -
Wires (reuse old phone lines or network cables)
For tools, we need a soldering iron, a hot glue gun, and a 3D printer (some creativity to modify the handle of the swatter…)
Step 1: How to Upgrade Our Electric Mosquito Swatter?
If you don’t care how it works and just want to make the “ultimate” electric mosquito swatter, you can skip this step.
Resources explaining how electric mosquito swatters work can be found online.
https://www.homemade-circuits.com/mosquito-swatter-bat-circuit/

It consists of an oscillating circuit and a circuit that boosts the voltage to several hundred volts, plus the metal mesh connected to the swatter.
The first problem encountered is that nothing can be hung on the ATtiny here (due to the 5-volt limit of the ATtiny, the high-voltage side cannot be used directly).
To solve this problem, the second idea is to measure the current consumption.
When an arc occurs on the metal mesh, and the mosquito is electrocuted, there must be some energy consumption that can be read by the ATtiny.
The best way to measure this consumption is to measure the voltage across a resistor, which the ATtiny can do.
Therefore, the method of counting mosquitoes is to insert a small resistor between the battery and the electric mosquito swatter circuit and monitor the voltage across this resistor.
In addition, we use ready-made electronic modules (each function has one: sound, display, charger, etc.), so overall, this is a relatively simple electronic project.
Step 2: Prepare the Circuit
The main components are:
-
ATtiny (Digispark pro) -
DFPlayer -
USB charger and battery -
Display -
Reset button -
On/off switch -
Potentiometer for volume adjustment -
Two resistors and one capacitor
We should first arrange them to ensure they fit inside the handle, keeping Digispark and DFPlayer close together, and leave appropriate wire lengths for other components based on their positions in the final module (i.e., the USB charger has a USB port that needs to be used for charging).
Step 3: Disassemble the Electric Mosquito Swatter
You can first see how much space is available for us to place additional components.
You can also check if your swatter is the same as the one used in this tutorial.
The main difference to note is the position of the swatter’s push button. In the picture, it is located between the battery’s+
and the swatter’s circuit. If that’s the case, you’re good (see the photo).
In some swatters, this button is located between the ground-
and the circuit. In this case, the modifications and code below will not work. Adjustments will need to be made in both hardware and software.
Step 4: Modifying the Electric Mosquito Swatter
-
Make some space for the battery in the compartment designed for two AAA batteries (and reuse the battery connectors from the rechargeable battery).
Step 5: Build the Circuit and Solder the Components
In the previous step, ground and VCC (5V) wires were not drawn for clarity, but all grounds need to be interconnected, and all VCCs need to be interconnected.
For VCC, Digispark has 3 pins labeled 5V that are connected together and can be redistributed to other components. Do not use the VIN of the Digispark (VIN is a voltage input that needs to be above 6V, we do not use it, but instead use the 5V output from the battery charger).
It is recommended to start with the Digispark:
-
Solder the Digispark to the DFPlayer -
Solder the Digispark to the display, button, and potentiometer -
Use heat shrink tubing to avoid contact and short circuits (for example, on the resistor between Digispark and DFPlayer)
Then, continue connecting the electric mosquito swatter circuit, battery charger, and switch:
-
Battery -
to charger Battery-
Input -
Battery +
to charger Battery+
Input -
1-ohm resistor on the electric mosquito swatter circuit +
-
Capacitor on the electric mosquito swatter circuit’s +
and-
, pay attention to the capacitor’s polarity! -
Digispark’s A12 pin connected to the button (the button has two terminals, one connected to the battery +
and A12 connected to the other) -
Use heat shrink tubing to avoid contact and short circuits (on capacitors/resistors etc.).
Step 6: Isolate the Display from the Electric Mosquito Swatter Circuit
It is suspected that electronic interference caused by high voltage changes affected the display circuit and connections.
The fix is simple: stick some tape or aluminum foil (electromagnetic shielding) on the circuit and then tape it again.
Of course, do not place the aluminum foil directly on the circuit…
Step 7: Load Sounds onto the SD Card
The sounds played by the DFPlayer are stored on the SD card.
The method is simple: insert the card and copy the selected sounds:
-
Sound played at startup (“Get ready for the next fight”) -
A “monster killed” sound -
A “level up” sound -
Some sounds that play randomly when a kill is detected
The DFPlayer will play sounds based on the “track number.”
According to observations, files are not sorted by their names but by their inode numbers on the card (which can be viewed in Linux using ls -id
).
ls -id * | more
647 1_Monster_kill.mp3
648 2_mixkit-final-level-bonus-2061.wav
649 3_get_ready_to_the_newt_fight.mp3
650 mixkit-arcade-retro-scoring-counter-273.wav
651 mixkit-arcade-video-game-bonus-2044.wav
652 mixkit-arcade-video-game-scoring-presentation-274.wav
653 mixkit-game-bonus-reached-2065.wav
654 mixkit-game-experience-level-increased-2062.wav
655 mixkit-winning-an-extra-bonus-2060.wav
If you start with a new formatted card, the track number/inode order will be the order in which the sound files were copied (i.e., the first file copied onto the card is track 1).
In the code below, the selection of files/tracks is done throughsetTrack()
.
// 0 is random between tracks 4 to 9
// 1 is monster kill = track 1 (listed by inode on card (ls -id))
// 2 is level up
// 3 is power on
if (sound_type==0) setTrack(int(random(4,9))); //SD card contains 9 files
if (sound_type==1) setTrack(1); // monster kill
if (sound_type==2) setTrack(2); //level up
if (sound_type==3) setTrack(3); //power on
You can modify this part to match the actionsound_type
with the audio played.
Audio files can be downloaded as a package at the end of the article.
Step 8: Print a New Handle
If it’s different, you may need to design one yourself.
However, some swatters have very large handles and almost nothing inside, allowing the additional circuit to be directly installed. Remember, we still need to find a position for the display and speaker.
The 3D printing files can be downloaded as a package at the end of the article.
Step 9: Assemble It Together
The speaker is also directly glued to the swatter.
Step 10: Where Are the Mosquitoes?
Once again, if you don’t care how it works and just want to get your “ultimate” electric mosquito swatter player, you can skip this step… or come back to it later.
Once we have soldered all the components together, if we load a simple program onto the ATtiny that only performs a simulated read (A12), we can look at the raw data:
The raw data looks very noisy, and we cannot tell where/when there are mosquitoes (or other things, I didn’t wait for a mosquito to hit my swatter to debug the code 🙂 It itself hit the mesh of the electric mosquito swatter (see image 1).
Averaging is a good way to clean up some noise, my idea was to compare the final average with the “long-term” average, but the results were a bit disappointing (see image 2).
https://www.iese.fraunhofer.de/blog/change-point-detection/
This seems to be exactly what we are looking for; calculating the standard deviation of the signal should allow us to detect when a mosquito is killed (see the “Animation of change point detection via sliding window” section in the above link).
In the program, we calculate the square of the standard deviation, and we can see that when the button is pressed or released, it reaches a very high value (this is expected, the signal goes from 0V to close to 5V), but once these stages are excluded, we can monitor this standard deviation and assume that if it reaches some high value, we have an interference in the circuit, which must be a mosquito that has been “killed” (image 4)!
To be more accurate, we added a few things:
-
A 470uF capacitor (which itself provides some more noise filtering and acts as a “power bank” when large current is needed); -
We have a two-stage averaging mechanism (as can be seen in the code, we measured the voltage of pin A12 five times in its raw state, took the average, and stored this average in a sliding window for standard deviation calculation).
Step 11: Load the Program onto Digispark Pro
This is relatively simple; you can program the ATtiny using the Arduino IDE.
Wiki:http://digistump.com/wiki/Digispark/tutorials/connectingpro
Some explanations of the code:
In thesetup()
function, we initialize the serial communication needed for the DFPlayer, read the potentiometer value to adjust the volume (only read once during setup, meaning the swatter needs to be powered off/on to consider volume adjustment).
For resetting the counter, if you want to reset the score to zero, you need to press the reset button, power on the swatter, and wait for the display to show “0”.
Then it will read the score stored in EEPROM.
It sets the brightness of the display (otherwise it will remain off).
In theloop()
function, we basically process the average power of the swatterprocess_average()
and the square of the standard deviation, which tells us whether there is lethalityprocess_std_dev()
.
Related code can be downloaded at the end of the article.
Step 12: Update Digispark Micronucleus (Optional)
Digispark Micronucleus is a piece of code responsible for “booting” the Digispark.
It checks whether we have uploaded a new program, and if not, it starts the already loaded program.
The problem is that it needs to wait 6 seconds, which is a long time for us to want to start our weapon to swat mosquitoes.
However, Micronucleus has some variants with different checking mechanisms. If you follow these instructions to update Micronucleus (using the “recommended” configuration), the swatter will be ready in a second or two.
Reference:https://github.com/ArminJo/micronucleus-firmware
After updating the Micronucleus firmware, reload your program.
Step 13: Troubleshooting
Hopefully, you won’t need to read this,
Here are some tips just in case……
1. Not working much…… check the wires and soldering;
2. If the electric mosquito swatter restarts by itself (you hear the startup sound, but it doesn’t turn on/off), charge the battery;
3. The electric mosquito swatter will automatically turn off after about 30 seconds.
-
Some USB charger circuits have an automatic standby mode (mine is like this, using the IP5306 chip), if the current consumption is below a certain amount (IP5306 is 45mA), it will enter standby mode. -
The first possible solution is to press the swatter button periodically… like every 20 seconds. The circuit board (containing the IP5306) has a “key” function that can turn the power on/off, marked with a “K” on the board. The watchdog()
function suggested in the above code will keep the power working properly.
4. If the swatter’s detection capability is really poor or frequently detects errors… then some code adjustments may be needed.
If you want to use the USB port to display some variables, you must modify the code to use the DigiCDC library and remove SoftSerial (used for the 4-digit display). But more importantly, when doing this, we will be powering from the USB port instead of the battery charger port, which makes a big difference… the quality of VCC has a significant impact on our calculated average and standard deviation…
In other words, any adjustments made while connected via USB may fail when running on battery.
One way to obtain some small information is to use the display itself (i.e., display the last standard deviation when pressing the reset button).
Knowing this, you can try adjusting the following values in the code, which have a significant impact on our detection.
Number of samples for single readings:
int samples=10;
Size of the averaging sliding window:
int nbr_slot=15;
int value[16]; // array of (nbr_slot + 1)
Threshold for standard deviation:
int threshold=110;
int monster_threshold=250;
Step 14: Continue to Improve the Electric Mosquito Swatter, Share Your Improvements
If you have found a simpler way to modify the electric mosquito swatter or a design, feel free to share!
Copyright statement:This article is sourced from the internet, free to convey knowledge, and the copyright belongs to the original author. If there are any copyright issues, please contact me for deletion.
‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧ END ‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧
Follow my public account, reply "join group" to join the technical exchange group according to the rules.
Click "Read the original text" for more sharing, welcome to share, collect, like, and view.