I have an airsoft rifle which uses an 8.4V battery to drive a motor when the trigger is pulled. The circuit is that simple, just a battery, motor and switch - there's also a 15A fuse.

I want my microcontroller to detect when the trigger is pulled, but I don't want to have to dismantle or extensively modify the replica gun. The only wiring I can get to is the battery terminals.

My first thought was to just put in a relay on one side of the battery, but I'd prefer to avoid the noise and reliability issues. (I'm willing to be persuaded otherwise)

Now I'm considering using opto-isolator. I haven't picked a particular model yet.

I can't risk the microcontroller being damaged. Its digital input is pulled up to 3.3V by a 22k resistor.

I apologise for my total lack of skill at electronics, but I don't know how to put the opto-isolator in circuit, at the right voltage etc. (Other than just putting it in series, with a resistor, which would presumably slow down the motor - no good.)

I'd appreciate any relevant example you can provide - or completely different approaches.


  • 1
    \$\begingroup\$ A hall effect dc current sensor adjacent to a motor lead will cause no extra losses. |The battery voltage almost certainly dips enough when the motor starts to be easily detected by meassuring battery voltage. \$\endgroup\$ – Russell McMahon Jul 4 '14 at 14:49
  • \$\begingroup\$ Are you trying to measure how many times the motor is started, how long the motor is operated or how many times the gun cycles? \$\endgroup\$ – KalleMP May 2 '17 at 20:37

First you need to measure the "running current" of the motor. Do this by putting an ammeter (Digital MultiMeter set to the "amps" mode) in series with either terminal of the battery.

I am assuming the "motor" you are referring to is a "permanent magnet DC motor" - the most likely type of DC motor used in this type of simple application you describe. In this type of motor there is also the "starting current". This is a brief high-current pulse which occurs when the motor first starts up from a resting position. You may see a brief display of this current when you first pull the trigger. The "running current" occurs when the motor achieves its final speed, usually after a fraction of a second.

It would also help if you can measure the resistance of the motor using the "ohms" setting of your DMM. It is probably in the range of 20 to 100 Ohms. This resistance along with the battery voltage can be used to calculate the "starting current" using Ohms Law ( in this case I = V/R, where V is the battery voltage and R is the measured motor resistance). Do this by removing the battery, connect the ohmmeter across the battery terminals and pull the trigger.

Once you have those parameters you can determine whether the series opto-isolator approach you propose will work. The main limitation with this approach is that the series voltage drop imposed by the opto's LED will starve the motor of starting current and maybe thwart operation of its intended mechanical function.

The topology (component connections) you will want to use here is a first resistor in series with the opto's LED and a second resistor in parallel with the first resistor and LED. Choosing the correct resistor values is the key to getting this scheme to work reliably. It may be possible that there is NOT a workable solution. It depends on the current measurements you come up with as described above.

The idea is that the second resistor will be a low enough value (Ohms) to pass the "starting current" with minimal voltage drop, but large enough that it won't shunt too much of the current away from the opto's LED during the "running current" period of operation. It's a delicate balance, but easily calculated once you know the currents involved.

Once you get enough current to flow thru the opto's LED, it's a simple matter to connect the opto's output transistor to your 3.3 volt micro.

There will definitely be certain opto-isolators better suited for this task than others. You'll want to choose one with as low an operational LED current as possible.

There is another way to do this. You would use a current sensing resistor in series with the battery. But this approach would require a much more complex circuit.

I'd suggest you make the measurements I described, then post the question again with these values and my proposed two-resistor & opto-isolator topology shown in schematic form, along with the battery voltage and type (AA, AAA, etc). You may also want to ask for opto-isolator selection suggestions.

| improve this answer | |

This works in simulation and the LTspice circuit file is here, in case you want to play with the circuit.

enter image description here


U1 is a voltage comparator with its inverting (-) input biased to half of V2 and stiffened up with C2.

U1's noninverting (+) input is biased to just slightly higher than U1's - input by adjusting R9 until the base of Q1 just goes high or, if the MCU is connected, until the collector of Q1 just goes low, plus a tiny bit more just to get the switching action away from random noise triggering.

R1 is V2's [assumed] internal impedance, and the way the circuit works is that when the trigger is pulled and the switch, SW, is made, the current into the motor will cause a voltage drop across R1 which will result in charge being sucked out of C1, forcing U1+ to momentarily go more negative than U1-.

When that happens, U1's output will go low, turning off Q1, allowing its collector to be pulled up to +3.3V through R8, sending a positive-going pulse to the MCU which will signal that the gun has been fired.

| improve this answer | |

Use a current transformer.

Salvage a toroid from a dead fluorescent bulb. Let one of the battery terminals go through the toroid. Wind a few turns for the secondary. Connect the secondary of the transformer to the MCU.

Protect you MCU's GPIO from the -ve spike when the battery current drops.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.