Oddball arduino digitalRead() analogRead() inputs

So im messing with a PIR sensor that outputs a DC of 3V if it detects motion. My problem is im trying to get the arduino to read the input and light up an LED, and possibly later turn on a speaker. With no wires going into the inputs, the serial monitor jumps between 0's and 1's and the light goes on and off. same thing with the analog. My question is how am i supposed to know if the input coming in is actually driving the light, or if it is just whatever oddball thing is going on with my arduino?
David

alright well i read that if you dont put anything in the inputs your outputs will be random. so now i actually have a wire going into the board and trying to read it. at the same time im staring right at the bench meter and can tell when the PIR sensor notices im there. 3V when i move, and 0V when i stay still. Yet while its at 0V the led is still blinking. Anythoughts?

When nothing is connected to your input it is "floating" and will give you random values whenever it's read. The proper way to set an empty input is to tie it to either ground or VCC through a resistor.

The atmega has built in 20k pullup resistors that you can enable by writing a HIGH to the digital pin when it's set as an INPUT. In this case though you probably want to tie to ground rather than VCC, then you'll read 3V when the sensor is on, and when it's off the resistor will pull your input down to 0. For this just use an external 10k or so resistor directly from your input pin to ground.

im not sure what you mean, here is my code:

  int pin2 = 2;
  int val = 0;



void setup() {                

  pinMode(9, OUTPUT);
  pinMode(2,INPUT);
  Serial.begin(9600);

}

void loop() {
  

  val = digitalRead(2);    // read the input pin
  Serial.println(val);
  if (val)
  {
  digitalWrite(9, HIGH);
  delay(5000);
  digitalWrite(9,LOW);
  }
  delay(1000);



}

i have the sensor outputting to a breadboard. one wire from that goes to the pin 2, and the other wire i have a 10k resistor running to ground but nothing is happening. Do i have to ground all the unused pins?

i attached a rough sketch. the 10k has the same effect whether its there or not.

That is the correct setup for a pulldown, perhaps the 3.0 volts isn't enough to trigger the 5v digital pin (I know that 3.3v logic levels work fine, but I can't remember the input hysteresis of the Atmega off the top of my head. There is a "no mans land" right in the middle of the voltage range where the state of an input pin is undefined, and it will act just like it does when it's floating.

A simple way to change things would be to hook the sensor up to an analog input instead of a digital one. If you do an analog read you should see a number around 614 when the sensor is on, so you could just test for values above and below that for your on and off.

val = analogRead(A0);    // read the input pin
  Serial.println(val);
  if (val>500)
  {
  digitalWrite(9, HIGH);
  delay(5000);
  digitalWrite(9,LOW);
  }
  delay(1000);

That is the correct setup for a pulldown, perhaps the 3.0 volts isn't enough to trigger the 5v digital pin (I know that 3.3v logic levels work fine, but I can't remember the input hysteresis of the Atmega off the top of my head. There is a "no mans land" right in the middle of the voltage range where the state of an input pin is undefined, and it will act just like it does when it's floating.

I think the legal HIGH minimum voltage is .6 X Vcc, so if running at 5vdc then 3vdc is right at the hairy edge and would not be reliable.

Lefty

im running an Uno by the way, sorry if i didnt mention that earlier but i dont know if it makes a difference :/. So lefty your saying i should increase the voltage on my Vcc going to my sensor? I cant understand what changing any voltages would do anyway since its still reading from nothing. I just hooked up to the A0 pin and its reading 1023 on the serial when there is no output from the sensor

So lefty your saying i should increase the voltage on my Vcc going to my sensor? I cant understand what changing any voltages would do anyway since its still reading from nothing.

No, I didn't suggest you do anything. I just stated to have a digitalRead command return a valid HIGH value, the voltage on the pin must be between .6 x Vcc and Vcc, so 3 to 5vdc for a standard arduino running at 5 volts.

I just hooked up to the A0 pin and its reading 1023 on the serial when there is no output from the sensor

I can't make much sense of your attached drawing. Also a link to the sensor might be more helpful then anything else we might guess at suggesting for you to try.

Lefty

I attached a .jpg for the sensor. DC_SS015

DC_SS015.jpg.pdf (271 KB)

dedshaw1612:
So lefty your saying i should increase the voltage on my Vcc going to my sensor?

To read from a digital pin you don't need to increase the voltage to the sensor, you need to increase the voltage of the output pin. This is doable with a transistor wired up similar to the diagram under "Typical applications" on the datasheet, except with the arduino digital input pin instead of the relay. It's not an exact circuit as it lacks resistors for the transistor (need one resistor on the base, and one on the collector to control current)

dedshaw1612:
I just hooked up to the A0 pin and its reading 1023 on the serial when there is no output from the sensor

That means that the Analog pin is seeing +5V, either you've got it hooked up wrong, or you've damaged the sensor. What value do you see when the sensor is triggered? I'm assuming that you haven't done anything to change the analog reference voltage from it's default of 5v.

the sensor is fine. im staring at the benchmeter and its hooked up to the output. everytime i move it flicks to 3V and then back to 0 when i stand still. I dont think i changed anything to do with the analog reference.

And you're still watching the input while it's plugged into A0? and A0 is setup as an input?

If you accidentally had the pullup resistor turned on for A0 I would expect you to see 5v on your multimeter.

If you unplug the sensor, and put a resistor directly from A0 to ground you should read about 0 from that pin, this is working correct?

yup. if i had it my way i would just hook the sensor up to the LED :confused: unfortuantly my professor wont let me do that

Well if your meter is reading 3v and the Analog input is reading 5 (at the same time) then I'm out of ideas for troubleshooting the analog pin. You could go back to boosting the sensor output voltage to a level that the digital pin will read, but there is something very fundamental here that's wrong and I can't put my finger on it if everything is setup exactly the way you explain it.

Thanks all for your help. I ended up doing a clever fix in the programming. Because the input jumps around so much i made a count variable to basically figure out how long the input was high. The sensor when activated lasts about 3 seconds or so, long enough time to enter in to an if statement. I admit its not a total fix, it still bounces long enough for it to goof up, but you can tell the difference between the bugs and the actual activation. My code is below if anyone is interested, and i took out the 10k resistor. Turns out i didnt need it.

David

/*
David Shaw
dedshaw1612@gmail.com
Annoying Fine-Tuning of the Arduino/PIR setup
9/17/2011

Components Used:
Small White LED
50 Ohm resistor
DC_SS015 PIR Sensor
Arduino Uno


*/
  int pin2 = 2;
  int val = 0;
  int count = 0;



void setup() {                

  
  pinMode(9, OUTPUT);
  pinMode(3,INPUT);
  //setting up the Serial to print the results
 
  Serial.begin(9600);

}

void loop() {
  




//Basically the point of this setup was to investigate the pins b/c i was interested in the values  and so i have them read to the serial monitor.
//Because the input pin 3 jumps around so much (still dont know why), i initialize a varible to count how long the value of the input has been
//high. because it jumps around it wont be that way for long. Only when i activate the sensor does the DC current stay high long enough to exceed the 
//count varible to send it into the if statement which acknowledges motion has been sensed. 8500 was an arbitrary guess, but it works great.
//But i based it upon the fact the analog reads 10,000 times a second so i just picked a high number assuming the digital pins read roughly the same
//rate. Although, im still tweaking it as i type this and its still going off, but ive reduced the error dramatically.
  
   for (int digiChannel = 0; digiChannel < 13; digiChannel++) {
            val = digitalRead(3);
            
              while (val)
              {
                count++;
                if(count >= 8500)
                {
              Serial.print("MOTION SENSED ");
              Serial.println("");
              digitalWrite(9, HIGH);
              delay(3000);
              count = 0;
                }
              val = digitalRead(3);


            }
             digitalWrite(9,LOW);

            
            
            Serial.print("binary at pin ");
            Serial.print(digiChannel);
            Serial.print(" is ");
            Serial.print(digitalRead(digiChannel));
            Serial.println("");
            delay(700);
          
        }



}

DC_SS015.jpg.pdf (271 KB)

You do have a common ground don't you??