SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By StaticDet5
#138225
Image

I'm trying to build an airsoft gun shot counter.
The first step was getting an Arduino to control the motor. Airsoft motors draw high loads from 6v-18v batteries (I want to use an 11.1v LiPo). I picked up a Sparkfun MOSFET breakout board http://www.sparkfun.com/products/10256 to make my life easier (I'm not an electrical engineer, not even close) during the prototyping phase. I swapped out their MOSFET for a higher rated MOSFET. I know the circuit limitations are probably on the circuit board traces, not the MOSFET, but I'm only prototyping this on a bench right now. Eventually I'm going to have to tackle the problem of massive electrical traces (again, I'm trying to go in small, easy steps).

I rigged everything together today on my workbench. I was stunned when everything worked. I push the button, and the gearbox fires. I let go of the button, and it stops.

I'm trying to figure out how to actually count the number of shots. Before I even tried to trigger the gun with a microprocessor, I tried to measure the number of shots with a photo-interrupter attached to the barrel.... I had issues with that. Eventually I want to get back to that, but right now I'm trying to approach it from a different angle.

An airsoft motor works cyclically. It's an electric motor (similar to the brushed motors in RC cars). However, it fires the airsoft pellet by compressing and then releasing a spring. It has periods where it has to work (very hard) to compress the spring, and then "free periods" where the spring has been released.

Is there a way to measure those periods using the Arduino's analog inputs? Can I measure the amps drawn by the motor? I'm thinking that this would allow me to monitor the electrical situation of the airsoft gun.

Where would I measure it from? How do I protect the Arduino from electrical spikes inherent to this project?
Last edited by StaticDet5 on Tue Feb 14, 2012 10:04 am, edited 1 time in total.
By Philba
#138226
This is pretty easy to do. You can measure the current pretty easily. I assume the airsoft gun runs off a battery so you can use one of this devices http://www.sparkfun.com/products/8883 in line with a battery lead. You can fiddle with the gain to measure 5A or less.

You run the output in an ADC and sample it frequently. You should be able to tell when a shooting cycle completes - the current draw between compress and release is probably pretty large. You might need to filter out some noise but that's pretty easy to do. I'd suggest using an interrupt driven approach for sampling the current flow. I think you are pretty protected with this device.
By StaticDet5
#138231
I'm pretty sure the system draws more than 5 at peak.
I should have mentioned the possible loads on these things. Some of them blow 20amp fuses.

What happens if the load is over the 5amp limit? Are we looking at a damaging condition, or is it just going to read a "max" value and nothing higher? If it's the latter case, then I'm going to attempt it. I can get a fair amount of information just from the peak timings. Heck, at the core, I'm really just interested in counting the peaks (shots).
By StaticDet5
#138321
I spent the morning studying.
What is the disadvantage to building a series of voltage dividers? I'm looking at the equations (I=V/R). I should be able to derive current (amps) from the voltage, right? I'm having trouble figuring out what resistance I'm actually measuring, to fill out the equation, but this is doable, isn't it?
By Philba
#138325
Yes, it would have helped to say the current range. Look at this - http://www.sparkfun.com/products/11005
It's not quite plug-n-play but the energy monitor project linked from that page will give you some ideas.

yes, dividers are possible and quite straightforward. Ohms law is your friend but, when you are talking high power, isolation is a very good thing. Also, if you meant dividers after the ACS712 sensor board, I have to discourage that idea. I'd be reluctant to shove that much current through those little legs on the IC. The chip can actually handle a fair amount but the risk isn't something I'd be comfortable with. I certainly doubt the board has been tested anywhere near that.
By StaticDet5
#138335
I'm thinking that instead of using a breakout board, I build a voltage divider to bring the voltage of the motor driving portion of the circuit down to 5v (A voltage divider using a 30k and 10k ohm resistor will bring the voltage down to 5v using a 20v input. This would "design" the circuit to use the Arduino maximum voltages, and still give me decent resolution on the ADC, about 0.02 volts per increment on the ADC).

From that, I'll get the voltage. Then I need to figure out the resistance. That's where I'm stumped, now. Resistance of what?
I think I'm going to build in a voltage sensor, anyway. That will give me some way to check the LiPo, and make sure I don't over-discharge it.
By Philba
#138347
I'm not sure where you are putting the divider. Across the motor terminals? that won't work.

The way to figure out motor current is to put a resistor in series with the motor (between one terminal and ground). The voltage drop across that resistor is proportional to the current via Ohm's Law (V=IR). But this requires that you use very low value resistors. These are often called current sense resistors. For example a 100 mOhm (.1 ohm) resistor will develop 100 mV of drop per amp. You will need to determine the right power rating. If the motor can pull 2 Amps then it would dissipate .4 Watts (voltage drop*current = .1*2*2). a half watt resistor would be reasonable.

Note that the drop voltage takes away from the motor. If the motor voltage is fairly low, it could be an issue. Using the 100 mOhm resistor with a 6 V motor that pull 10A, the motor would see only 5V. You can get 1% sense resistors down to 10 mOhm or less. DigiKey and Mouser have great selections. You need to determine the V/A by Ohms Law. For example 50 mOhm will develop 50 mV per amp (1*.05).

Now, you feed the top of the sense resistor into the ADC. You need to know the voltage reference for the ADC to determine what the input voltage is and from there you get current. You can fiddle with the ADC voltage reference to make the numbers easier to use. I like references that are related to the number of bits the ADC has. a 12 bit ADC with a 4.096V reference means that each count is 1 mV.
By Mee_n_Mac
#138353
StaticDet5 wrote:I'm trying to build an airsoft gun shot counter.
...
I'm trying to figure out how to actually count the number of shots. Before I even tried to trigger the gun with a microprocessor, I tried to measure the number of shots with a photo-interrupter attached to the barrel.... I had issues with that. Eventually I want to get back to that, but right now I'm trying to approach it from a different angle.
Just my $0.02 but I'd think it might be easier to go back to your first approach, modified a bit. Use your interrupter or a simple switch or something like this to sense the position of the piston or something on the gearbox. This would count the cycles using just one digital input line.

I'm surprised you had some problem with the interrupter type counter (which would actually count the BBs). That's basically what's done in chronographs. A 6mm BB going even 500 FPS (ouch !) would cast a shadow for ~40 usec. If you used an interrupt (vs a polling) technique, that should be sufficiently long to be detected. Perhaps if you told us what you did along this avenue, we could be of some help in getting that working.

FWIW I have in a box someplace a cheap CO2 NBB pistol that I intend to make into a hybrid gas/AEP (akin to how a paintball marker works I guess). One of my (probably silly) goals was to incorporate a chrony into the "barrel" to effect some feedback into the gas valve operation to get a constant controlled velocity. So I'm curious/motivated to see your original idea work.
By StaticDet5
#138360
I have already drilled the barrel with two sets of two holes. I mounted photo-interrupters in various configurations, but could not get a decent/consistent signal. I need to give it a better shot, I guess. My confidence is a little higher, now that I got the digital trigger working.
I'm thinking about grabbing two of these (http://www.sparkfun.com/products/9299), and starting on it fresh.

I have to be honest with you. My project, initially, is almost exactly what you're going for. I wanted to build a digital shot counter and chrony. I want the smartgun from Aliens.

I looked at electrically controlling a valve, but grew tired of trying to find a decent valve. Pneumatics is another field that I'm just not well versed in.

AEG (Airsoft Electric Gun) internals are something that I'm incredibly familiar with.
At some point I'm going to wind up incorporating a voltage sensor into the system. Battery condition is a bigger deal than people consider, especially when I'm using LiPos. I may reconsider the motor cycle detection, but I love the idea of getting multiple pieces of information from the same sensor.
By Mee_n_Mac
#138363
StaticDet5 wrote:I have already drilled the barrel with two sets of two holes. I mounted photo-interrupters in various configurations, but could not get a decent/consistent signal.
I won't ask how you keep the pressure from leaking out the holes or how you keep them from messing with the BB as it travels down the barrel and instead concentrate of the inconsistent signals. What were you measuring before and how were you doing it ? I think the interrupters you linked to should work. I think you could even physically cut them into 2 pieces at the "U" if needed to package them into the AEG better. Initially I'd just stick one at the end of the barrel to get everything working (for the counter).

In order for this to work you'll need to run the output into an external interrupt pin. The interrupt routine can then count the pulses received (perhaps with a little "smarts" to avoid any double counts). The main loop would then report the count and reset and enable/disable the interrupt as desired.

Making a built-in chrony requires a bit more work but if the first step works (a counter) then I don't see why it couldn't be done. Controlling the gas pressure/volume is a bit more problematical. :( With an AEG perhaps you could control the stroke of the piston, winding it up a little more or a little less, but that would be a mechanical gearbox change ... something I'm not familiar with. First things first I guess ...
By StaticDet5
#138368
Controlling the velocity on an AEG is a nightmare, with existing gearboxes. I'm not even going to approach it until I get all the other problems fixed. Heck, it would be easier to build a 600+ fps gearbox with servo controlled bleed valves. Inefficient as hell, but easier.

My test barrel suffered almost no velocity launch. The original photo-interrupters were held in place with hot glue, which sealed the barrels. The system that I'm using is pretty low-velocity (for an airsoft gun), as it's my indoor gun. In real life, I'm the medical guy, and I really don't want to ever hurt people.

I may look at the test barrel tomorrow. I need to find my notes on the photointerrupters, but I really think I'm going to go with the breakout board I listed above. I'm broke and not really pulling in play money right now, but I think the simplicity of the breakout board is worth my time and blood pressure.
By StaticDet5
#138407
I put the above referenced voltage divider (10k and 30k) on the voltage inputs.
The voltage dropped when I triggered the gun. My normal variance was only one to two levels on the ADC (A variance of 637 to 635). When I triggered the gun, the voltage appeared to drop significantly (Half a volt, or 10 levels on the ADC).
Why is that? I thought voltage was constant. I'm not reading enough values to measure how many shots are coming out, but it really has my curiosity piqued.

I'm also getting long periods of no reading from the ADC pin. Not a "pin floating" random value, but a true "0". Then the pin comes back and reports (an apparently) correct value.

The bad news: during the testing, one of the gears in the gearbox broke. It's going to be a bit before I can fix it.
By Mee_n_Mac
#138409
Where did you put the divider ? If it's across the motor then I'd expect this. When drawing current there will be some voltage loss due to the battery's internal resistance and the resistances of the wiring and switch (FET). The more current the more voltage drop there will be across those items and the less voltage "left" for the motor. Google Ohms law. Then when the FET is off there will be no voltage delivered to the motor and so none measured there.
By StaticDet5
#138410
Image

I've updated the graphic.
I just repaired the gearbox. Before I call this project quits, I'm going to have to get a new gearbox. That thing is old and crusty.

The MOSFET is getting hotter n' hell. How hot is too hot? I pulled out my thermometer and got around 150 Fahrenheit. I've got a heatsink on it (I'm aware that it is going to get hot), but do I need to consider a thermal check on the system?
By Mee_n_Mac
#138419
StaticDet5 wrote:I've updated the graphic.
Hmmm, not sure what to tell you about the zero readings now. And I'd have guessed there would be less voltage drop in the battery than what you're seeing with the Arduino. I am curious though, do you have a voltmeter that you can measure the battery voltage with ? If so then it would be interesting to measure the battery voltage with the motor off (and running) and measure the voltage across the 10k resistor with it off (and running, why not!).

Also is your Arduino a 3.3V or 5V version ?
StaticDet5 wrote:The MOSFET is getting hotter n' hell. How hot is too hot? I pulled out my thermometer and got around 150 Fahrenheit. I've got a heatsink on it (I'm aware that it is going to get hot), but do I need to consider a thermal check on the system?
If the hotest it ever gets is 150F then you're OK. That's about 65C, which will certainly feel hot to the touch, but enough below the 85C where I'd be worried. Can I assume you have a think layer of thermal paste between the FET and the heatsink ? If not then do this, it will help. The datasheet for the FET should have some curves for max current vs case temp.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 32