LCD acting up when used with motion sensor

Hi fellow Arduinians,

I set up a PIR motion sensor and an LCD (20x4) to UNO. PIR is ground, positive and also goes out to PIN 2. LCD is connected using Ladyadas tut.

My code is written to display whenever there is motion detected or not. Simple.

However, when I try to run this, the lcd becomes very static and unable to read. With some letters faded out and so on. Does anyone know why? I am guessing this is due to voltage (either too much or too low) with PIR sensor messing it up somehow.

However, while not changing my connection in anyway, if I just run a sketch to display Hello World on the LCD it works perfectly, once I include the code to use PIR sensor (pretty much exact copy of code posted in ladyadas tut the LCD screen goes haywire.

So here is my code:

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
 
void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
 
  Serial.begin(9600);
}
 
void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
    digitalWrite(ledPin, LOW); // turn LED OFF
    if (pirState == HIGH){
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
(somewhere here I indicate this is 20x4)
if ( val == HIGH )
lcd.setcursor(o,1);      
lcd.print(“Motion”);
if ( val == LOW )
      lcd.print(“No Motion”);

}

Thanks for any help…

Also, does anyone know if its possible to do a faster refrash rate on the PIR sensor. It seems like it detects motion and displays Motion detected for 6 seconds. Is there a way to make it instant? If there is motion display msg, and if the next second there is none display accordingly?

Thanks guys

Also, there may some mistakes in the code such as syntax, i am doing this by memory i have correctly written code at home. I am just looking for advice why LCD is acting up only when PIR sensor code is included in the sketch being run.

Thanks

What PIR sensor are you using and how it is connected? Seems like these tend to be open collector designs, so that you can tie multiple sensors into the same pin. If this is the case, you need to use a pull-up (or pull-down) resistor.

this is the one i am using http://www.adafruit.com/index.php?main_page=product_info&cPath=35&products_id=189

I just need something to sense motion. Are there any others?

The page doesn't do a very good job of describing the exact sensor you would have received. So I would try to find a datasheet from the original manufacturer.

As for your original problem, how have you wired everything up maybe you have a short somewhere?

Since you didn't post your actual code used, it is difficult to see if you have some kind of syntax bug (e.g. if-statement not enclosed as expected.) This snippet is a good example:

if ( val == HIGH )
lcd.setcursor(o,1);
lcd.print(“Motion”);
if ( val == LOW )
      lcd.print(“No Motion”);

It doesn't look like the if-statements are setup how you expected. However, since it isn't the real code there isn't much of a reason for anyone to put more than a few seconds of effort in reading it.

fair and valid. i will post up exact code and video tonight

thanks all :)

This is almost certainly a case of lack of power supply decoupling.
http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html

There is an outside chance that your power supply is not up to providing the current but you don’t say what one you use.