Sound grenade for Airsoft [Part 2: Electronics]

Airsoft sonic grenade - Open

Let’s get started on the second part of the sound grenade build, this part focusing on soldering, programming and assembling the electronics into the enclosure. For instructions on how to make the 3D-printed enclosure, check out Sound grenade for Airsoft [Part 1: Enclosure].

I decided to use an ATtiny85 microcontroller and the Arduino environment because 1) It’s relatively easy for people who know little about electronics and C++, and 2) It allows me to change the behavior at any time by editing the code (such as increasing the countdown duration or disabling warning beeps). Could it be done with a simpler and more robust solution? Sure.

If you remember the previous entry, there was a section talking about how to protect the sound grenade against impacts against the floor. Electronics too have ways to protect themselves:

  • A pushbutton has moving parts inside which may break after a good hit. After having trouble with others I tried the one listed below, which gave good results while still being cheap. They’re not really heavy duty but for now will do.
  • Notice that, in the perfboard, there is a coin cell battery fitted inside a battery holder? The instant the grenade hits the floor, contact between battery and battery holder is lost multiple times due to vibrations, even if it’s just for a fraction of a second. This causes the microcontroller to lose power and reset, so the explosion never happens. I solved this by soldering a 220 µF capacitor between positive and negative, which acts as a power buffer during those interruptions (a filter, basically).
  • Notice that the board is dangling inside the enclosure, with the cables acting as dampeners. Usually this is a very bad design choice, because cables are not made to dangle or be under stress, but for an application like this it’s reliable enough.

 

Required components

You can make a trip to a local electronics store, but I recommend buying on eBay from Hong Kong sellers (usually the cheapest in low quantities) for most parts.

  • Pushbutton: must be the one in the picture (with a 16 mm diameter thread), or else it will be difficult to attach to the upper hole. You can find it on eBay by searching for ‘Pushbutton 3A/125V’.
  • Buzzer: must have a diameter of 23 mm and (Important!) be rated at 3V or less. Otherwise the coin cell can’t provide enough voltage to make any noise. Look on ebay for ‘Buzzer 3V 23mm’.
  • ATtiny85: Make sure it’s through-hole. An ATtiny45 is fine too. Using a socket is optional, but by not using it you cannot reprogram easily the firmware plus you risk damaging the chip when soldering.
  • Perfboard: Any you’re comfortable with. You will have to cut it to about 30 x 50 mm, so make sure it’s bigger than that.
  • 2 x Terminal block connectors (optional, you can also solder cables directly to the board).
  • 220 uF capacitor
  • Coin cell holder
  • Coin cell: A CR3032 does the job, although any will work as long as it supplies 3V and fits in the holder.

 

 

Sound Grenade code

If you’ve dabbled in Arduino or know either C or C++, you can skimp through the code and even change the behavior of the sound grenade. The code has the following features:

  • Pressing the button starts a 10 second countdown. There are discreet alerts at the start and the last 3 seconds, and a loud beep when the countdown reaches 0.
  • When not doing anything, the microcontroller sleeps to save power. It only wakes up to perform the countdown sequence and then goes right back to sleep. You can even store it away and use it a year later (although eventually the coin cell will discharge itself).

Download Sound Grenade code

Click here to expand the full code
///////////////////////////////////////////////////////////
////CONSTANTS//////////////////////////////////////////////
///////////////////////////////////////////////////////////
#define COUNTDOWNTIME 10

//Button pin is 1 -> This is selected in hardware (interrupt)
const int BUZZERpin = 2;


///////////////////////////////////////////////////////////
////SLEEPING SETUP////////////////////////////////////////
///////////////////////////////////////////////////////////
#include <avr/sleep.h>
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif


///////////////////////////////////////////////////////////
///////SETUP///////////////////////////////////////////////
///////////////////////////////////////////////////////////
void setup()
{
  pinMode(BUZZERpin,OUTPUT); 

  //Prepare interrupt pin so that it is triggered when connected low
  pinMode(1,INPUT_PULLUP);
  sbi(GIMSK,PCIE); //Turn on Pin Change interrupt
  sbi(PCMSK,PCINT1); //Sets pin 1 as interrupt
  
  BatteryInsertedSignal();
}


///////////////////////////////////////////////////////////
///////LOOP////////////////////////////////////////////////
///////////////////////////////////////////////////////////
void loop()
{
  system_sleep();
  
  CountDown();
  Explode();
}


///////////////////////////////////////////////////////////
////FUNCTIONS//////////////////////////////////////////////
///////////////////////////////////////////////////////////

//Produces short beep whenever the microcontroller is powered up
void BatteryInsertedSignal()
{
  digitalWrite(BUZZERpin,HIGH);
  delay(20);
  digitalWrite(BUZZERpin,LOW);
}


//Executes countdown. On each second decides what to do:
//  Seconds 9-4  -->  Do nothing
//  Seconds 3-1  -->  Short beep. Explosion is imminent
//  Second   0     -->  KABOOM
void CountDown()
{
  CountdownStartSignal();
  
  for(int i=COUNTDOWNTIME; i>0; i--){
    
    switch(i){
      
      case 4 ... (COUNTDOWNTIME-1):
        NormalSecondSignal();
        break;
      
      case 0 ... 3:
        LastSecondsSignal();
        break;
        
      default:
        delay(1000);
        break;
    }
  }
}


//Discreet sound signal to indicate succesful start of countdown
void CountdownStartSignal(){
  for(int i=0;i<3;i++){
    digitalWrite(BUZZERpin,HIGH);
    delay(30);
    digitalWrite(BUZZERpin,LOW);
    delay(30);
  }
}



void NormalSecondSignal(){
  delay(1000);
}

void LastSecondsSignal()
{
 digitalWrite(BUZZERpin,HIGH);
 delay(15);
 digitalWrite(BUZZERpin,LOW);
 delay(985);
}

void Explode()
{ 
  for(int i=0;i<15;i++){
    digitalWrite(BUZZERpin,HIGH);
    delay(250);
    digitalWrite(BUZZERpin,LOW);
    delay(50);
  }
}


//Puts microcontroller to sleep (shuts down pretty much all hardware
//except the ability to wake up through interrupts)
void system_sleep() {
  cbi(ADCSRA,ADEN); // Switch Analog to Digital converter OFF
  set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Set sleep mode
  sleep_mode(); // System sleeps here
  sbi(ADCSRA,ADEN);  // Switch Analog to Digital converter ON
}


//When an interrupt happens, code execution jumps to this function and
//afterwards resumes normal code from where it left off.
//Since we just wanted to resume normal code, this function can be empty.
//ISR stands for Interrupt Service Routione.
ISR(PCINT0_vect)
{
  
}

 

Uploading the firmware

I could make a guide on how to program ATtiny85/45/84/44 microcontrollers with the code above, but the internet already offers countless sources. Usually you’d need an ISP programmer, but for starters I recommend using an Arduino UNO board to emulate an ISP programmer. You’ll need to burn the bootloader at the lowest frequency (1 MHz) and then upload the code provided. The process has become much easier over the past few years. Find everything you need at:

Programming ATtiny85 with Arduino Uno as ISP programmer

 

Soldering the circuit

The circuit to solder is this one:

sound grenade schematic

Note that the pin names (Pin 1, Pin 2…) reference the physical pins and not their equivalent names in software. Check out this diagram if you want to get the idea.

Cut the perfboard to about 30 x 50 mm. Then solder the components in a disposition like this:

sound-grenade-circuitIf you solder the microcontroller without a socket (not recommended) make sure it has the firmware already uploaded. I recommend you make all the connections on the opposite side of the components, and instead of making solder paths use either short wires or discarded component legs (in my case, I’ve got a small box full of cut legs from capacitors and resistors). If you cut the board too small solder the capacitor on the opposite side as well, like this:

Airsoft sound grenade - Perfboard backside

 

Assembling everything

Fit both the pushbutton and the buzzer into their respective holes. Use glue if they’re not tight enough.

Either plug the pushbutton and buzzer cables into the terminal connectors or solder them directly to the perfboard.

Finally, close the grenade making sure the cables fit nicely and screw the remaining two nuts at the base.

Congratulations, you’re finished your very own semi-realistic looking sound grenade. Now be careful and don’t break it on your first match. Always ask your referees if impactless grenades are allowed to prevent confusion in the middle of a match.

And don’t forget to leave a comment if you build one for yourself.

Add a Comment

Your email address will not be published. Required fields are marked *