DC Motor + PIR Sensor

Hi

I am a MA Fine art student doing kinetic sculpture and still getting to grips using my Arduino Uno but slowly getting there.

I'm trying to get a DC motor to turn on with a PIR sensor when motion is sensed using a Arduino UNO. I've been able to get both the DC motor and the sensor to work separately but i'm struggling with the code.

Can anyone Help or link me to anything that may help ?

Many thanks
James

Post your best attempt at coding it and we'll help you sort it out. You'll need to say what it currently does and exactly what you want it to do.

Steve

I'll be honest the code i've gotten too muddled with.

Currently it does nothing but what i want it to do is when the sensor detects motion , i want the motor to stop then to start again when the sensor detects no motion.

James

Then post the two separate programs that read the PIR and run the motor and say what problems you're having trying to combine them. General instructions for combining two sketches can be found at Merging Code

(The way it works is we don't really like just writing programs for people. When you make an attempt and then you get help fixing it you'll learn something.)

Steve

I'll be honest i need to learn it myself and i'm still learning basics. Thanks for the link

James

int sensor = 7;              // the pin that the sensor is atteched to
int state = HIGH;             // by default, no motion detected
int val = 0;                 // variable to store the sensor status (value)
#define ENABLE 5
#define DIRA 3
#define DIRB 4

int i;

void setup() {
  // put your setup code here, to run once:
  
  pinMode(sensor, INPUT);    // initialize sensor as an input
  pinMode(ENABLE,OUTPUT);
  pinMode(DIRA,OUTPUT);
  pinMode(DIRB,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
     digitalWrite(ENABLE,HIGH); // enable on
    for (i=0;i<5;i++) {
    digitalWrite(DIRA, HIGH);   // one way
    delay(200);                // delay 500 milliseconds 
    
    if (state == LOW) {
      Serial.println("Motion detected!"); 
      state = HIGH;       // update variable state to HIGH
    }
  } 
 }  else {
      digitalWrite(DIRA,LOW); // fast stop
      delay(200);             // delay 500 milliseconds 
      
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
    }
  } 
}

I had a go and the code is above but when i uploaded it , all that happened was the led light (thingy) on the board acknowledged that there was movement from the sensor and the motor did nothing

Also is a picture (hopefully) of the board

I am now stuck , is it the code or the board or both

James

I can't see anything in your code that does anything to a "led light (thingy)". Which LED is it? Do you mean on the PIR itself or where?

Are you getting the outputs you expect from your Serial.println() commands? That's what will tell you if the PIR is getting through.

You have set up the 3 pins needed to control a motor but you're only ever writing anything to one of them. That's not going to do very much.

What is the purpose of the for loop? It seems to keep writing the same thing to "DIRA" and then switching "state" to LOW then HIGH then LOW... And nothing ever seems to use that "state"variable. What is it for?

I can't see much in your picture but there seems to be nothing connected to Pin7 and you don't seem to have any ground connection between the motor driver and the Arduino. A circuit diagram might be more helpful.

Steve

The LED lights up on the board itself when the PIR senses motion.

where in the code am i writing only to one of the 3 pins ?

the LOW & HIGH are most likely mixed up now as i tried to get it to work but ended up getting lost with how it orginally was.

i was following tutorials to how the motor and sensor were setup.

Connect to ground - motor ???
Serial.println ??? what do you mean

i'll do a circuit diagram

In the code you have Serial commands intended to print "Motion detected!" or "Motion stopped!". Do they?

If not, it might also be worth temporarily adding a print immediately after the sensor read to see if anything is getting to the Arduino. E.g.

  val = digitalRead(sensor);   // read sensor value
  Serial.print(val);

What do you see? That should tell you if the PIR information is getting into the Arduino.

As for ground, the question meant "Are all ground pins, on the PIR, the breadboard/motor driver and the Arduino itself connected together"? Missing a ground link is a common problem when things don't work and I can't tell from your photo if there is any ground connection between the Arduino and the breadboard.

Motor pins. Nothing is ever written to DIRB. And in your "else" you write only to DIRA. BTW I'm guessing that your motor driver is an L293. Is that correct?

Steve

Yes the Motor is L293

the PIR is connected to ground , but the breadboard/motor driver to arduino isn't. From Ground where would i connect to , breadboard ?

i take it that both DIRA & DIRB need to be both written too ?

Let me try again. In the code you have Serial commands intended to print "Motion detected!" or "Motion stopped!". Do they?. On the Serial Monitor? Did you try the extra Serial.print()? What results did you get?

I'm trying here but I can't see what you're doing or any results you're getting. You can't easily debug programs by just flailing around, it has to be done fairly methodically. If the Arduino is not detecting the PIR switching then nothing else is going to work so let's get that going first.

Steve

Been a while since i have posted back, but i have done it

the motor switches off when motion is detected :slight_smile:

Hey! I'm doing a similar project as you did but i'm using DC motors. Can you share the code that worked for you? I am stuck with the code and i have attached it below. I am simulating my circuit and the motor runs for 2-3 seconds and after that it just stops. I'll appreciate your help. :slight_smile:

brave_wolt1.ino (546 Bytes)

That code doesn't do ANYTHING. It doesn't even compile. Do you want to try again? And a few comments saying what is connected where might help as would some details of what components you're using.

Steve

How did this go? i am doing the same thing, but cant code:( so need to find some existens one

binvant:
Hey! I'm doing a similar project as you did but i'm using DC motors. Can you share the code that worked for you? I am stuck with the code and i have attached it below. I am simulating my circuit and the motor runs for 2-3 seconds and after that it just stops. I'll appreciate your help. :slight_smile:

What does this code do?? Nothing I guess...