Go Down

Topic: DC Motor + PIR Sensor  (Read 2165 times) previous topic - next topic

Glover0603

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
 

slipstick

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

Glover0603

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 

slipstick

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 http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html


(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

Glover0603

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


James 

Glover0603

Code: [Select]
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
    }
  }






Glover0603

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

slipstick

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



Glover0603

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
 

slipstick

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.
Code: [Select]

  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

Glover0603

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 ?

slipstick

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

Glover0603

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

the motor switches off when motion is detected :)

Go Up