Arduino & PIR sensor

I'm having some troubles getting my Arduino to work with my PIR sensor.....

I'm trying to get the sensor to turn on one LED when it gets motion, and turn off the LED and turn on another one when there isn't any motion.....

Is it my code?

int sensorValue = 0;
int ledPin = 13;
int ledPin2 = 12;

void setup() {
Serial.begin(9600); //MIDI baud rate
pinMode(sensorValue, INPUT); //Sensor 1
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT); //blinking LED indicates that midi data is send

}

void loop() {
sensorValue = analogRead(0);
Serial.println(sensorValue, DEC);
delay(5);

if(digitalRead(sensorValue) == 0){
pinMode(0, INPUT);
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, LOW);
delay(5);
}

else if(digitalRead(sensorValue) > 1){
pinMode(0, INPUT);
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, HIGH);
delay(5);
}
}

Can you read the PIR sensor by itself, without trying to do anything with the output?

yeah, the readings are perfect.....

no motion I get a reading of 0

with motion, it jumps to 676 & 678

no motion, back to 0

so the sensor is working fine, I just can't get the outputs to work correctly....

You appear to be reading the analog value on analog pin 0, which is fine:

sensorValue = analogRead(0);

but then you do a digital read on that returned value:

if(digitalRead(sensorValue) == 0){

which is probably not fine...

Did you mean:

if(sensorValue < 2){ // 2 is an arbitrary lower threshold
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, LOW);
...
...

Andrew

That's exactly what it needed! It now works perfectly.....Thank you so much!

Just curious, what PIR do you use?

the PIR sensor from parallax.com

Thank you.

I was looking at this one from Seeedstudio, it's a bit cheaper at US$ 5.90: Seeed Studio Bazaar, The IoT Hardware enabler.

I have several of those, they work great. Right now I have one watching the office and logging motion events to a web server using an Xport, and sending me emails if there's motion during odd hours.

Macegr,

This PIR needs about 50mAh. Do you power it directly from Arduino or do you think 50mAh is just a little too much. I think pins can only provide 40mAh, right?

You're getting mixed-up with units: mA is milliamps, a measure of current. mAh is milliamp-hours, a measure of the charge stored in a battery. If you mean mA, then your queastion makes more sense. The I/O pins can only supply 40mA, maximum. But we don't power devices like the PIR from the I/O pins, we connect them to the power output pins, that is, directly to the Arduino's power supply. Those pins will be able to supply 50mA with no trouble.

Ok, thank you. I think I should have had more coffee before asking this :slight_smile:

How much mA can the power pins supply?

One limitation on the power available is the maximum power that we can get from USB. The USB maximum is 500mA, or half an Amp, but this limit can only be reached if the USB device is "intelligent" enough to make a request for more power to the host or hub. Without making a special request, the USB limit is 100mA, or one-tenth of an Amp.

When the Arduino is powered from an external source such as a "wall-wart", then the power available is slightly different. But I'm not sure what the exact limit is in that case, so I'll let someone else answer!

ok, thank you very much!

You should easily be able to get 500ma out of the arduino's power pin when it's plugged into a wallwart. That is, so long as your wall wart is rated that high. Also, the LM7805 regulator can handle putting out 1A but only with a heatsink. I'd recommend a heatsink at 500ma too.

It's really best to keep anything over 500ma (total) away from the arduino board. If you find yourself needing a whole bunch of power then it's probably better to create a power board with heftier traces, a dc-dc buck converter, filter caps, and all that jazz. Then power the arduino off of that board either to it's VCC pin (if you are providing more than 5V) or it's 5V pin (if you are providing regulated 5V).

Without making a special request, the USB limit is 100mA, or one-tenth of an Amp.

This is a good point. Does the FTDI FT232RL USB serial converter chip request more then the default 100ma from the PC's USB port? Does anyone know for sure?

Lefty