Detect when 5V motor is being turned on

Hi guys,

I'm completely new to this and I wanted to ask if someone has some basic ideas about the following:

I want to detect when a 5V motor is being turned on and a counter variable should be decreased by one. Starting count is 400. When the count is down to a specific number (i.e. 50) a LED should turn on.

I used the search function but couldn't find anything. Your help, thoughts and ideas are much appreciated.

Thank you!

If the motor always turns in one direction you can connect the + side of the motor to a digital input pin to see if the motor is getting power or not.

int counter = 400;
boolean motorRunning = false; 
const int motorSensePin = 2;
const int LEDPin = 13;

void setup()
 {}

void loop()
   {
   if (digitalRead(motorSensePin))
      {
     if (!motorRunning)
        {
        counter--;
        motorRunning = true;
        }
      }
   else
      motorRunning = false;

    if (counter < 50)
       digitalWrite(LEDPin, HIGH);
    }

Hello John,

yes the motor always turns in one direction. Thank you for your response including the sample code.

Why do you check the pin which is connected to the motor's + and then in addition if motor is running = true?

Can I also control 4 motors independently with the Arduino?

Why do you check the pin which is connected to the motor's + and then in addition if motor is running = true?

If the counter decremented every time the motor was running it would drop very quickly. By checking to see that the motor is on noe but wasn't already on we can decrement the counter only each time the motor is turned on (which is what you specified)

Can I also control 4 motors independently with the Arduino?

Yes. You can check as many 5v motors as you have spare data input pins. If you want one LED for each then you can still easily do 5 motors + 5 LEDs + serial I/O (pins 0 and 1) + 6 analog inputs.

That should do it for right now. The end result would be to go as many as 128 motors. I guess I would need something else later to connect all these motors. The LED is only there for testing purposes. Later on instead of the LED a signal to a telemetry module needs to be sent which sends out a text message.