# Measure position of gear with arduino

So, my plan is to be able to turn a motor, with a gear on the end, for a certain amount of gear teeth then stop it. I can’t find a data sheet for the motor but it is very fast, brushed and DC. I was thinking putting something like this below the gear so when a tooth goes past, it turns on and off. Like I said before the motor is very fast (faster then being able to see the teeth as it spins) and I don’t think the arduino will be able to read that kind of speed with digital read, I may be wrong. Any suggestions would be appreciated. Thanks.

EDIT: The motor runs off 2 cell or three cell lipo which is 7.4-11.1v, it’s maximum current is 20 amps.

Edit: For anyone wondering how to use the sensor, the datasheet is here and it shows the positions of the collector, emitter, anode and cathode.

• I know, it’s not helpful at all but I couldn’t find a data sheet and I wanted to say it’s going much too fast to see Jun 13, 2019 at 22:11
• you could certainly try that sensor .... paint one of the gear teeth white Jun 14, 2019 at 2:17
• I am sure Arduino is fast enough to read the frequency of a gear
– Juraj
Jun 14, 2019 at 4:38
• You can try to use an encoder attached to the shaft of the motor in this way you can measure the amount of steps per turn and thus you can calculate when and how you turn the motor. Or just get a stepper motor. And for the concern that the Arduino is not fast enough you can use interrupts which are very fast. Jun 14, 2019 at 8:56
• unfortunately, for the application in airsoft, it needs to be very compact and sit below the gear, hence why I thought using some sort of IR sensor would be appropriate. Jun 14, 2019 at 9:14

If memory serves, the clock speed of an arduino Uno is 16Mhz. Some are faster, some much faster and a few are slower.

Most assembler instructions take around 1-3 clock cycles. A few take more. Assuming an average of 3 ticks per instruction, that translates to executing over 5 million instructions per second.

So, unless your motor is approaching supersonic speeds, the arduino should be able to "keep up" with polling your motor's sensor in your `loop()` function.

If you are doing a few other things in your loop, you might consider using an interrupt to trigger some code when the motor "activates" the sensor. In this case you will unlikely need to worry about the speed of the CPU at all. When using an interrupt, you do not need to worry about polling the state of your sensor inside the `loop()` function.

Always remember to keep the code in your interrupt service routine as short and fast as possible. In this case, probably you would just turn off the motor and return when the interrupt is triggered.

Having said all of that, you will still need to do some work to get the most appropriate implementation as only you know what other things you need to take into account at this time.

One other thing to note is that a single high level instruction will normally equate to more than one assembler instruction. Even a simple statement like `int x = 1000;` will be more than one assembler instruction and thus take more than 1 clock tick. However, if memory serves, the individual instructions generate to execute `x = 1000` will all take just one clock tick.

Others will vary the amount of instructions that are generated to execute them. For example ``` float x = 1.0; x = x + 2.0; ```

will likely take more instructions to execute than it's integer counterpart: ``` int x = 1; x = x + 2 ```

I hope this helps and enables you to try controlling your motor via Arduino. If not, you can always ask a more specific question later.

• Thank you very much, this is unbelievably helpful. I’ll have to try and condense my code down. Do you know if that sensor will pick up the gear even if it’s not painted white? Thanks Jun 15, 2019 at 14:00
• Sorry, i am not familiar with that particular sensor. I suggest scrutinising the data sheet on the web page; Asking the manufacturer; And/or posting a new question either here or on the Electronics site.
– GMc
Jun 16, 2019 at 1:07
• Alright, I’ll take a look at the datasheet first. Thanks Jun 16, 2019 at 8:22