0

I am trying to run a DC motor for 100ms and then turn it off completely. Here is my code any suggestions on why it might not be working?

My Code

const int pirPin = 9;
const int motorPin = 2;
int switchState = 0;
// the setup routine runs once when you press reset:
void setup() {
  // initialize the digital pin as an output.
  pinMode(motorPin , OUTPUT);
  pinMode(pirPin, INPUT);
}
void loop() {
  while (digitalRead(pirPin) == HIGH) {
    digitalWrite(motorPin, HIGH);
    delay(100);
    digitalRead(pirPin) == LOW;
    break;
  }
}
  • What is the voltage of the DC motor are you using any driver IC or transistor becasue if you are not using any then it may damage your arduino also. Can you please share the connections also – Maaz Sk Oct 28 at 4:18
  • So what u exactly want is that the motor runs when the pirpin is high and it should continue to run for extra 100 ms after the pin goes low – xbox gamer Oct 28 at 4:34
  • Please share a proper code and not a picture. Dose the motor run? – xbox gamer Oct 28 at 4:37
  • Please explain how the motor is supposed to react to pirPin. Also, have a look at digitalRead(pirPin) == LOW; which will evaluate to true or false and isn't used anywhere. – ocrdu Oct 28 at 11:32
  • So what you want is to be explained how the motor will react to pir pin and you want to run the motor when the pir pin is high and run for 100ms and then go off when the pir pin is low – xbox gamer Oct 28 at 13:20
1
#include <Arduino.h>

const int pirPin = 9;
const int motorPin = 2;
int switchState = 0;
// the setup routine runs once when you press reset:
void setup()
{
 // initialize the digital pin as an output.
 pinMode(motorPin, OUTPUT);
 pinMode(pirPin, INPUT);
}
void loop()
{
 while (digitalRead(pirPin) == HIGH)//this part of the code gets executed if the pir 
 //pin is read high
 {
   digitalWrite(motorPin, HIGH);//the motor pin is set high so if a motor is connected 
   //the motor will start turning. 
 }
 while (digitalRead(pirPin) == LOW)//this part of the code gets executed if the pir 
 //pin is read low
 {
   delay(100);// so your motor stop after running for extra 0.1 seconds 
   digitalWrite(motorPin, LOW);//the pin gets low and the motor should stop turning
 }
}

I have commented all the lines

Hope this helps.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.