15-bit binary counter

I need to count pulses from a shaft encoder attached to a motor that is neither a stepper nor a servo. The maximum number of pulses to be counted will be less than 15 binary bits (2**15 = 32768)... most likely about 21,000. An event will be triggered (shut off the motor) when a specified number of pulses has been counted. I'll need to upload that number of pulses to the Arduino board via the serial/usb connection; issue commands to start the motor; and shut the motor off when that number of pulses has been counted. I'll also want to count the number of pulses of "overshoot" (that is, additional shaft revolutions while the motor is spinning down to a stop) and download that number to the computer via the usb port.

Will the Arduino Uno R3 record a not-quite-15-bit binary count? If so, how should I approach this project? Or should I use a different board?

Thanks... Dave.

The Arduino can easily count to over 4 billion so a 15-bit count is trivial.

How fast are the pulses arriving?

Is it a simple interruptor or a quadrature encoder that gives you direction as well as count?

Does the motor only go forward?

Is the motor powered by DC or AC?

Assuming a simple interrupter, a DC motor going in one direction, and pulse rate less than about 500,000 per second:

byte MotorPin = 4; //  Motor control pin: HIGH = run, LOW = stop
unsigned int counter;  // 16-bit counter
unsigned int limit;

// Interrupt Service Routine
void pulseInterrupt() {
  if (limit != 0) {
    if (counter >= limit)
      digitalWrite(MotorPin, LOW);

void setup() {
  pinMode(MotorPin, OUTPUT);
  digitalWrite(MotorPin, LOW);
  limit = 0;
  attachInterrupt(0, pulseInterrupt, RISING);

void loop() {
  if (limit != 0 && digitalRead(MotorPin) == LOW) {
    // Motor has reached the limit
    delay(5000);  // Wait five seconds for motor to wind down
    limit = 0;  // Stop counting and look for another input

  if (limit == 0) {  // Nothing happening
    limit = Serial.parseInt();
    if (limit != 0) {  // We got a new value from Serial
      counter = 0;  // Reset the counter
      digitalWrite(MotorPin, HIGH);  // Start the motor

The pulse rate is about 360Hz from a simple led/sensor device on the shaft... output is TTL-level. Motor controls exist in the form of two TTL-level binary inputs for direction and activation: cw is logic level 0 and ccw is logic level 1; for activation, off is logic level 0 and on is logic level 1. These are easily controlled by the Uno. But since I'm new to Arduino boards, I'm not quite sure how to accomplish the pulse counting, uploading the target pulse count to the board, or downloading the "overshoot" pulse count to the computer.

What is the application?

Tom... :slight_smile:

I need to start the motor, run it until a specified (variable) number of pulses is counted, then shut the motor off. I need to set the required number of pulses from the computer over the usb connection. I also need to read the number of pulses of "overshoot" after the motor is shut off but is still turning. The motor is neither a stepper nor a servo and is controlled by an existing circuit with TTL inputs, as previously described. So I just need to know how to set the pulse number, trigger the start and stop events, and read out the number of overshoot pulses. I also need to send a signal to another board pin to control motor direction. Looks like John Wasser's code gives me a good starting point (thanks, John), but any further advice is most welcome.


Why have you started a second Thread for the same projet? If you continued with your other Thread all the info would be in one place.

Please ask the moderator to merge the Threads.


This thread is about a different question.
But I don't care if the threads are merged as long as it complies with forum rules and preferences. I'll let the moderator decide that for himself/herself.