DIY Arduino Spy Bug

by Splat in Circuits > Gadgets

29097 Views, 117 Favorites, 0 Comments

DIY Arduino Spy Bug

thumbnail.png
IMG_0285.JPG
IMG_0286.JPG
IMG_0292.JPG

This slim spy bug can fit in your hand and can record great audio whether it is hidden on a bookshelf or in your pocket. Powered by 3 AAA batteries, it can record over an hour of audio onto a micro SD card in a .WAV format. It is a great project even if you are have been programming, soldering and 3D printing for a long time or if you are just getting into it. I would love to see what you come up with if you change the code or redesign the case.

Supplies

IMG_0001.JPG
IMG_0002.JPG
IMG_0003.JPG
IMG_0004.JPG
IMG_0005.JPG
IMG_0011.JPG
IMG_0007.JPG
IMG_0013.JPG
IMG_0014.JPG

Here is a complete list of all the supplies you need.


Parts required:

  1. Arduino Pro Micro (without header pins)
  2. SD card adapter (without header pins. I had to de-solder mine)
  3. MAX9814 microphone module (without header pins)
  4. 2x SPDT switch (3.7mm X 3.7mm X 8.7mm)
  5. AAA battery terminals (one with a bump, one with a spring, and two with both. I got mine from a AAA battery pack.)
  6. 4x screws 4mm tall and 1.5mm in diameter not counting the head
  7. 1x screws 4mm tall and 2mm in diameter not counting the head
  8. 3x AAA batteries
  9. Wire (22 awg or less)
  10. 3D printed parts
  11. Electrical tape or anything that won't let light through
  12. Micro SD card (32gb or less)


Tools required:

  1. 3D printer (I used an Anycubic Kobra 2 Pro)
  2. Soldering iron
  3. Solder
  4. Screwdriver
  5. Hot glue or super glue


Software required:

  1. Arduino IDE
  2. UltilMaker Cura

Printing the Parts

IMG_0284.JPG

The first thing we need to do is print the case and the lid for the spy bug. Here are the print settings I used in Cura. If you don't have Cura installed, you can download it here.


Spy bug case:

  1. Support: Yes
  2. Buildplate adhesion: Skirt
  3. Layer height: 0.2mm
  4. Wall thickness: 0.8mm
  5. Infill: 20%
  6. Top layers: 4
  7. Bottom layers: 4
  8. Print speed: 200 mm/s
  9. Material: PLA+

Spy bug lid:

  1. Support: No
  2. Buildplate adhesion: Skirt
  3. Layer height: 0.2mm
  4. Wall thickness: 0.8mm
  5. Infill: 20%
  6. Top layers: 4
  7. Bottom layers: 4
  8. Print speed: 200 mm/s
  9. Material: PLA+

Attached below are the STL files for the case and the lid.

Attach the Battery Terminals

IMG_0005.JPG
IMG_0016.JPG
IMG_0020.JPG
IMG_0021.JPG

We will start by attaching the battery terminals to the case. As you can see, I had two of the same terminals with a bump and a spring, so I had to flip one upside down. For the terminals with a single bump or spring, you will need to cut the metal tab off. You may have to use a hammer or shave off some metal on the sides to get them to fit.

Add the Switches

IMG_0022.JPG
IMG_0023.JPG
IMG_0024.JPG
IMG_0025.JPG
IMG_0026.JPG

Now we will add the switches the will turn the device on and off and also start and stop the recording.


I used a screwdriver to push the switches into place and then cut off the pins on the right. After that, I soldered the pins in the middle together and then used a wire to attach it to the negative pin of the battery pack.

Wiring the Micro SD Card Module

IMG_0049.JPG
IMG_0051.JPG
IMG_0052.JPG
IMG_0053.JPG
IMG_0054.JPG
IMG_0055.JPG
IMG_0057.JPG

Now we will solder the SD card adapter to the Arduino pro micro.


Make sure that the wires are not too long, otherwise you won't be able to fit it in the case. The wiring is listed below.

  1. GND to GND
  2. VCC to VCC
  3. MISO to GPIO 14
  4. MOSI to GPIO 16
  5. SCK to GPIO 15
  6. CS to GPIO 10

After that is done I suggest trying one of the SD libraries of the Arduino and micro SD card adapter setup to make sure that it is working.

More Wires

IMG_0069.JPG
IMG_0070.JPG
IMG_0071.JPG
IMG_0072.JPG
IMG_0073.JPG
IMG_0074.JPG
IMG_0076.JPG
IMG_0075.JPG

Now we will solder more wires onto the microcontroller. These will attach to the microphone and the switches so make sure that they are long enough. I did this by sticking the Arduino and micro SD card adapter setup in their place and then measuring the length of wire and soldering it onto the microcontroler.


After you are done with that, stick all of it in the case. It should fit into place in the indented rectangle and the USB C slot on the Arduino should fit in the hole in the wall. If the micro SD card adapter is too big for the rectangle, use something to shave off the edge that is too long and try again. Don't worry; shaving some off the board won't damage it at all.


Now you can solder the wires onto the switches. Solder GND onto the bottom switch and GPIO 2 onto the top switch.

Wiring the Microphone

IMG_0067.JPG
IMG_0077.JPG
IMG_0078.JPG
IMG_0079.JPG
IMG_0080.JPG
IMG_0082.JPG

Now we will solder the microphone onto the wires that we soldered onto the microcontroller in step 4. First, screw the microphone into place with the screw 4mm tall and 2mm in diameter. Then solder the wires to the mictophone according the wiring listed below.

  1. GND to GND
  2. Vdd to VCC
  3. Out to A0

Now that that is done, solder a wire from the positive output of the battery pack to the VDD pin on the microphone.

Finishing Up

IMG_0083.JPG
IMG_0085.JPG
IMG_0086.JPG

Now we will glue the microcontroller to the case to prevent it from moving and also cover up the lights on the Arduino Pro Micro so they don't shine through the holes. I used electrical tape to cover up the lights but you can use anything that won't let light through. I did have to glue it onto the microcontroller so it would stay on.


After that, get 3 AAA batteries and put them in the battery pack.

Programming

sketchbook_location.png
TMRpcm_folder.png
uncomment.png

Before we put the top on, lets program the spy bug. First, Open Arduino IDE make sure you have these libraries installed. If you don't have Arduino IDE installed, you can download it here.

  1. SD
  2. TMRpcm

After those are installed, find your sketchbook location. If you don't know where it is, open Arduino IDE and go to File > Preferences. The path to the sketchbook will be listed as seen in the first picture. After you have found the location, go there and find the libraries folder and open it. That will show all the libraries you have installed. Look for the TMRpcm folder and open it and you will see a list of files. Open the one called pcmConfig.h; you can do this with any text editor. Scroll down until you see the //#define ENABLE_RECORDING. Then remove the two /s and save the file. If we don't uncomment that line, the code won't compile.


Now we can upload the code to the Arduino board. Connect it to your computer with a USB C cable and then select the Arduino micro board from the list of boards. Now open the file attached below in Arduino IDE and upload it to the board. Now you can test your spy bug. Insert the micro SD card into the slot and reset the board. Now, turn on the top switch and open the serial monitor. It should print Recording and then the file name. Once you turn off the top switch, it should print Stopped recording and then the file name.


Now you can take out the micro SD card and insert it into a computer. You should see a .wav file in the micro SD card. If you do, congratulations! You have successfully made a spy bug. If not, make sure that the micro SD card was formatted as FAT32 and that all the wires are correct. Attached below is the code for the spy bug.


Code Explanation

It is always good to know how your code works. It makes it easier to change and add things to it.

#include <SD.h>
#include <SPI.h>
#include <TMRpcm.h>

These lines include the libraries needed for the program. The SD library to read and write to the micro SD card, the SPI library to communicate via the MOSI and MISO, and the TMRpcm to record analog audio signals onto the micro SD card.


#define CS 10
TMRpcm audio;

int fileNumber = 0;
int switchPin = 2;
bool isRecording = false;

The first line here defines the chip select pin on the micro SD card to GPIO 10. The second line creates an instance of the TMRpcm class. We use this to call functions from the library. The last 3 lines initialize variables for the file number, the pin for the switch and weather the bug is recording or not.


int findNextFileNum() {
int currNum = 1;
char currFileName[13];
snprintf(currFileName, sizeof(currFileName), "REC%03d.wav", currNum);
while (SD.exists(currFileName)) {
currNum++;
snprintf(currFileName, sizeof(currFileName), "REC%03d.wav", currNum);
}
return currNum;
}

This function prevents the bug from overwriting file that are already on the micro SD card. This way, if you already have REC001 on the micro SD card when you power on the device and start recording, it won't overwrite it. It works by defining a variable (currNum) and a string for the file name (currFileName). Then it adds the currNum to the currFileName with the snprintf function. The snprintf function also adds three digits to the end of the name so if the file number is one, the file name will be REC001 and if the file number is 10, the file name will be REC010. Then it uses a while loop that runs until it can't find the file name in the micro SD card. After it does that, it returns the file number that it will start recording at.


void setup() {
Serial.begin(9600);
delay(100);
pinMode(A0, INPUT);
pinMode(switchPin, INPUT_PULLUP);
Serial.println("Mounting micro SD card...");
if (!SD.begin(CS)) {
Serial.println("Failed to mount micro SD card.");
Serial.println("");
while (true);
}

Serial.println("Successfully mounted microSD card.");
Serial.println("");
audio.CSPin = CS;
fileNumber = findNextFileNum();
}

This function sets everything up. It starts the serial at 9600 baud and adds a small delay to connect. Then it sets the pinMode of the analog pin 0 to input and the switch pin to INPUT_PULLUP. The reason I am using INPUT_PULLUP instead of INPUT is because INPUT_PULLUP adds a pullup resistor inside the microcontroller so I can directly attach the switch without soldering a resistor onto the it. Then it mounts the micro SD card and sends an error to the serial monitor if it failed and also stops the code from running any further. Then if the mounting of the micro SD card was successful, it sets the Chip Select pin of the micro SD card and uses the findNextFileNum function to set the starting file number.


void loop() {
int switchState = digitalRead(switchPin);

char fileName[13];
if (switchState == LOW && !isRecording) {
isRecording = true;

snprintf(fileName, sizeof(fileName), "REC%03d.wav", fileNumber);
audio.startRecording(fileName, 16000, A0);
Serial.print("Recording ");
Serial.println(fileName);
} else if (switchState == HIGH && isRecording) {
isRecording = false;
snprintf(fileName, sizeof(fileName), "REC%03d.wav", fileNumber);
audio.stopRecording(fileName);
Serial.print("Stopped recording ");
Serial.println(fileName);
Serial.println("");
fileNumber++;
}
}

Now we get to the part of the code that runs constantly. It starts by reading the value of the switch pin. If the switch is on, the value will be 0 and if it is off the value will be 1. Then it defines the file name as a 13 byte variable. This is enough for 12 characters plus 1 null terminator '\0'. Then there is an if statement. It only runs the code inside of it if the switch is on (LOW) and it isn't recording. If both of those are true, it sets the recording variable to true and creates the file name using the snprintf function. Then it uses the startRecording function from the TMRpcm class to record the analog audio values from the analog pin 0 (A0) to the file name at a 16000 HZ sampling rate onto the micro SD card. Then is prints that is started recording plus the file name to the serial monitor. Because we set isRecording to true, this code won't run again until the switch is turned off and then back on. There is another part of the if function that runs if the first part didn't run and if the switch is off and you are recording. All it does is set isRecording to false and create the file name with the snprintf function and then stop the recording with the audio.stopRecording function from the TMRpcm class. Then it prints that it stopped recording to the serial monitor and increases the file number by 1. Because we set isRecording to false, none of the parts of the if statement will run until the switch is turned on again.

Downloads

Putting the Top On

IMG_0084.JPG
IMG_0090.JPG

Once everything is working, screw the lid onto the case. Take the four matching screws and stick them in each of the corner holes on the lid. the line the lid up with the case (making sure the microphone is under the big hole) and screw the screws in. Now the spy bug is complete! To turn it on, slide the switch on the bottom to the right. To record, slide the switch on the top to the right to start recording and then to the left to stop recording.


If you have any problems or questions, feel free to tell me in the comments.