A DC motor is operated using an L298n driver and Arduino UNO. How can I get to know whether the Motor is ON or OFF? Using a code in Arduino.
Unfortunately, the L298N does not return a status, so you are going to have to add sensors to detect motor movement.
Here are a few options you can try:
- Current sense. Add a small resistor to the output of the L298N and measure the voltage differential across that resistor. If there is current being supplied to the motor then you will have a voltage differential.
- Rotation sensor. This can be in the form of a quadrature encoder wheel, tooth sensor or Hall effect sensor detecting a magnet on the shaft. If you see pulses from the sensor then you know the motor is spinning.
- Optical sensor. A camera system or other optical sensor to detect motor movement. Probably too complex for a simple application. There are however off the shelf sensors that will turn on an output if they see movement in their field of view. Alternatively you could use a PIR sensor from a home alarm system if the movement is course enough.
This may be too simple, but for completeness: You can read back the state of the output bit, in case your code doesn't save its current state. That will tell you if the motor is literally "on" or "off". Not rotating despite being "on" is another matter; an error.
If you need error detection then you need additional hardware support. Some possibilities:
- Sense rotation with a cam and a micro-switch or an optical sensor and a slotted disk;
- Sense the motor current - a stalled motor will draw a high current;
- Sense vibration in the motor platform;
- (apply imagination here... ;-)