# PIR to Arduino to Transistor and LEDs

#### AllenPitts

##### Member
Hello etech and the ETO forum,

Nike. (Greek victory, success)
Pheidippides ran 26 miles from the plains of marathon to the city Athens,
spoke one word and died. The word was nike.

Finally got it to work.

And I am quite red-faced as to the problem. I went back and looked and I
ordered 430k ohm resistors not 430 ohm. Thanks for sticking w me thru
my dumb mistake.

1. Because 430k were ordered instead of 430, I have no 430 ohm resistors.
So, for now the PIR to opto-coupler interface is working with 470
ohm resistors that are on hand. Have ordered 430 ohm from Mouser and
will substitute when they arrive tomorrow.

2. PIR duty is a term I made up. I stole it from literature on 555 timer
that describes the time when the timer is sending a positive signal
as it's 'duty cycle'. In future will indicate PIR_out=1 or PIR_out=0 for
PIR high (3.3v) or PIR high (<0.1v), respectively.

3. The next step is to connect the optocoupler output
to the Arduino. The Arduino sketch, attached herewith, is simple and
has three elements. The first element declares two variables, one for the PIR
and a second for the value of the sensor. The second element sets
the mode that the two pins are being used should have: one input and one output.
Finally, the third element says: 'Read the value sent by the PIR and write that
value to the out put pin'. More values\pins will be added as more

4. On the schematic marked 'PODACAL 200803', attached,
it shows 'DC IN Jack' and 'VREG'. For simplicity, was thinking
to connect the Arduino barrel jack to the +12v rail and the
ground on the breadboard. The Arduino site indicates that
Arduino specs
the Uno Input Voltage (recommended) is 7-12V and the
Input Voltage (limit) is 6-20V. Would the Arduino not run ok
on 12V? Or should a voltage regulator like L4971 be included?

5. Speaking of power, the power supply being used at 'VSUPPLY'
on the schematic is a CalRad Part number 45-752
which is 120v input to 12v 1.2 amp output. The plan
is for two LED modules of type LM7 (at top and bottom stair treads)
and five of type LM1-LM6.

LM-7 have eighteen LEDs and LM1-6 have a dozen.
That is (2 x 18) + (5 x 12) = 96 LEDs
It would be unusual for all of them to be on at the same time
but it is possible.
96 LEDs x 20 mA (max current draw/LED) = 1920 mA or 1.9 amps.
So thinking may need to go to a two or three amp power supply.
What do you think?

Thanks.

Allen in Dallas

#### Attachments

• 490 bytes Views: 2

#### eTech

##### Well-Known Member
Hello etech and the ETO forum,

Nike. (Greek victory, success)
Pheidippides ran 26 miles from the plains of marathon to the city Athens,
spoke one word and died. The word was nike.

Finally got it to work.

And I am quite red-faced as to the problem. I went back and looked and I
ordered 430k ohm resistors not 430 ohm. Thanks for sticking w me thru
my dumb mistake.

1. Because 430k were ordered instead of 430, I have no 430 ohm resistors.
So, for now the PIR to opto-coupler interface is working with 470
ohm resistors that are on hand. Have ordered 430 ohm from Mouser and
will substitute when they arrive tomorrow.
Ok...we got it working.

2. PIR duty is a term I made up. I stole it from literature on 555 timer
that describes the time when the timer is sending a positive signal
as it's 'duty cycle'. In future will indicate PIR_out=1 or PIR_out=0 for
PIR high (3.3v) or PIR high (<0.1v), respectively.
OK...but PIR low = <0.1v

3. The next step is to connect the optocoupler output
to the Arduino. The Arduino sketch, attached herewith, is simple and
has three elements. The first element declares two variables, one for the PIR
and a second for the value of the sensor. The second element sets
the mode that the two pins are being used should have: one input and one output.
Finally, the third element says: 'Read the value sent by the PIR and write that
value to the out put pin'. More values\pins will be added as more
Understood. I will get back on this

4. On the schematic marked 'PODACAL 200803', attached,
it shows 'DC IN Jack' and 'VREG'. For simplicity, was thinking
to connect the Arduino barrel jack to the +12v rail and the
ground on the breadboard. The Arduino site indicates that
Arduino specs
the Uno Input Voltage (recommended) is 7-12V and the
Input Voltage (limit) is 6-20V. Would the Arduino not run ok
on 12V? Or should a voltage regulator like L4971 be included?
The Arduino Uno Rev3 has an on-board regulator...an NCP1117ST50T3G. Its input is the barrel jack.
The specs on this regulator states a Max voltage input of 20v. 12v is well below 20v, so no external regulator is needed.

5. Speaking of power, the power supply being used at 'VSUPPLY'
on the schematic is a CalRad Part number 45-752
which is 120v input to 12v 1.2 amp output. The plan
is for two LED modules of type LM7 (at top and bottom stair treads)
and five of type LM1-LM6.

LM-7 have eighteen LEDs and LM1-6 have a dozen.
That is (2 x 18) + (5 x 12) = 96 LEDs
It would be unusual for all of them to be on at the same time
but it is possible.
96 LEDs x 20 mA (max current draw/LED) = 1920 mA or 1.9 amps.
So thinking may need to go to a two or three amp power supply.
What do you think?
Ok...calculate LED current:
LM1-6 = 80mA each module = 6 x 80mA = 480mA
LM7 = 120mA
120mA + 480mA = 600mA for total for LED modules (all LEDS on)

That leaves 600mA Max for the remainder of the system.

Calculate System subtotal:
PIR = 60uA + 5mA (ISO in) = 5.06mA x 7 = 35.42 mA
Interface = 3mA (ISO out) = 3mA x 7 = 21 mA
Arduino Uno = 20mA/io pin x 14 = 280 mA <---hard to believe...but that's what the spec says.
LED Driver = 1mA/input x 7 = 7 mA

So...
035.42 mA + 021.00 mA + 280.00 mA + 007.00 mA= 343.42 mA subtotal (MAX)

And with all LEDS on:

600 mA + 343.42 mA = 943.42 mA Total Max 12v supply current.

943.42 mA is about 78% of 1.2 Amp MAX VSupply with all LEDS on.

I think the 1.2 Amp supply will be OK. I'd feel more comfortable with 1.5A-2A supply though.

Last edited:

#### Pommie

##### Well-Known Member
With hindsight, you can see the yellow orange yellow bands in your picture but unfortunately no one noticed. Glad you got it working.

Mike.

#### ChrisP58

##### Well-Known Member
I doubt that the Arduino input pin fed from the opto handle the 12 Volts that you're feeding it. Or do you have a voltage divider that I don't see?

If that is a problem I suggest splitting R8 into two resistors and tapping the Arduino feed from the center.

If you put 3.6K on top and 2.4K on the bottom, the Arduino voltage will be 4.8 Volts when the opto is on at 12 volts.

#### eTech

##### Well-Known Member
I doubt that the Arduino input pin fed from the opto handle the 12 Volts that you're feeding it. Or do you have a voltage divider that I don't see?

If that is a problem I suggest splitting R8 into two resistors and tapping the Arduino feed from the center.

If you put 3.6K on top and 2.4K on the bottom, the Arduino voltage will be 4.8 Volts when the opto is on at 12 volts.
why do you think the input pin won’t handle 12v?
It seems to work ok.
I'm using an external pulldown resistor (R8), weak pullup off, and 12v directly from the power supply.
I do think, however, should increase R8 to 47k.

Feel free to comment. your suggestions are welcome.

Last edited:

#### eTech

##### Well-Known Member
3. The next step is to connect the optocoupler output
to the Arduino. The Arduino sketch, attached herewith, is simple and
has three elements. The first element declares two variables, one for the PIR
and a second for the value of the sensor. The second element sets
the mode that the two pins are being used should have: one input and one output.
Finally, the third element says: 'Read the value sent by the PIR and write that
value to the out put pin'. More values\pins will be added as more
Hi

I've come up with a very rough draft of arduino code.
I focused more on structure than routines. Some might be able to be simplified by using subroutines.

There is a code block I reserved for "Control logic". I'm thinking that you would read the inputs, set variables
based on those inputs, then set output variables that would be used to set the output pins. I'm assuming you would code this logic.
Feel free to change anything you want, its your system.

Code:
//
// constants - map PIR's to input pins
// inputs
const int PIR1in = 1;     // PIR1 input pin
const int PIR2in = 2;     // PIR2 input pin
const int PIR3in = 3;     // PIR3 input pin
const int PIR4in = 4;     // PIR4 input pin
const int PIR5in = 5;     // PIR5 input pin
const int PIR6in = 6;     // PIR6 input pin
const int PIR7in = 7;     // PIR7 input pin

// constants - map LED Controls to output pins
// outputs
const int LC1out =  1;     // LED Control 1 output pin
const int LC2out =  2;     // LED Control 2 output pin
const int LC3out =  3;     // LED Control 3 output pin
const int LC4out =  4;     // LED Control 4 output pin
const int LC5out =  5;     // LED Control 5 output pin
const int LC6out =  6;     // LED Control 6 output pin
const int LC7out =  7;     // LED Control 7 output pin

// create/initialize PIR status variables
int PIR1State = 0;         // PIR1 status
int PIR2State = 0;         // PIR2 status
int PIR3State = 0;         // PIR3 status
int PIR4State = 0;         // PIR4 status
int PIR5State = 0;         // PIR5 status
int PIR6State = 0;         // PIR6 status
int PIR7State = 0;         // PIR7 status

int LC1State = 0;         // LC1 status
int LC2State = 0;         // LC2 status
int LC3State = 0;         // LC3 status
int LC4State = 0;         // LC4 status
int LC5State = 0;         // LC5 status
int LC6State = 0;         // LC6 status
int LC7State = 0;         // LC7 status

void setup() {
// initialize PIR input pins (no weak pullups)
pinMode(PIR1in, INPUT);
pinMode(PIR2in, INPUT);
pinMode(PIR3in, INPUT);
pinMode(PIR4in, INPUT);
pinMode(PIR5in, INPUT);
pinMode(PIR6in, INPUT);
pinMode(PIR7in, INPUT);
// initialize control output pins
pinMode(LC1out, OUTPUT);
pinMode(LC2out, OUTPUT);
pinMode(LC3out, OUTPUT);
pinMode(LC4out, OUTPUT);
pinMode(LC5out, OUTPUT);
pinMode(LC6out, OUTPUT);
pinMode(LC7out, OUTPUT);
}

void loop() {
// read the state of PIR1

// LED control logic block here. using
// Do some logic and set output variables
// using PIRnState and LCnState variables

// Check LC1 status (repeat this as needed)
// Maybe could be a subroutine.
if (LC1State == LOW) {
// turn LC1 on
digitalWrite(LC1out, HIGH);
} else {
// turn LC1 off
digitalWrite(LC1out, LOW);
}
}

#### Attachments

• 2.4 KB Views: 1
Last edited:

#### Nigel Goodwin

##### Super Moderator
why do you think the input pin won’t handle 12v?
The fact that the pins are 5V only (assuming it's fed from 5V and not just 3.3V - in which case they would be 3.3V only).

You're very likely to fry the chip.

Either connect the collector of the PC817 to the 5V (or 3.3V) pin on the Arduino, or better still connect the emitter of the PC817 to gnd of the Arduino, the collector to the input pin, and a pull-up to 5V (3.3V) - this means the PC817 works more as a switch, rather than an analogue device.

However, is there some reason you're not simply connecting the HC-SR505 directly to the Arduino?, and powering it from there as well?.

#### eTech

##### Well-Known Member
The fact that the pins are 5V only (assuming it's fed from 5V and not just 3.3V - in which case they would be 3.3V only).

You're very likely to fry the chip.

Either connect the collector of the PC817 to the 5V (or 3.3V) pin on the Arduino, or better still connect the emitter of the PC817 to gnd of the Arduino, the collector to the input pin, and a pull-up to 5V (3.3V) - this means the PC817 works more as a switch, rather than an analogue device.
After re-checking the ATmega328 datasheet, I see your (and ChrisP58) point. Should be 5v input.
So I'll re-arrange the input for active low, and use 5v from the Arduino 5v pin.

However, is there some reason you're not simply connecting the HC-SR505 directly to the Arduino?, and powering it from there as well?.
The HC-SR505 doesn't actually output 3.3v, the sensor has an onboard regulator and the output is actually ~1.96v. So I used a bjt to raise the output voltage/current level. The reason for the opto is long wires (and possible spikes from a rug/carpet) from the sensor to the arduino.

#### eTech

##### Well-Known Member
Schematic update.

I've updated the schematic to use the arduino V5 pin as a supply for the arduino inputs.

The opto's now show use of the arduino on-board V5 pin to supply 5vdc to the arduino inputs and comply with the input pin voltage limits.
The opto output limiter resistor has been changed to 22k and has been moved to the collector side of the output. This inverts the logic state seen at the arduino input. The code uploaded on post #26 has been modified to reflect this change.

#### ChrisP58

##### Well-Known Member
why do you think the input pin won’t handle 12v?
It seems to work ok.
I'm using an external pulldown resistor (R8), weak pullup off, and 12v directly from the power supply.
I do think, however, should increase R8 to 47k.

Feel free to comment. your suggestions are welcome.
I don't know either way. I've never used an Arduino. My experience is mostly with PIC chips.
My comment is based on the assumption that the Arduino board pins are connected directly to the microcontroller pins without any on-board dividers,
If there are on-board dividers, or if the microcontroller pins can handle 12 Volts directly, then my concern is not founded.

#### Nigel Goodwin

##### Super Moderator
I don't know either way. I've never used an Arduino. My experience is mostly with PIC chips.
My comment is based on the assumption that the Arduino board pins are connected directly to the microcontroller pins without any on-board dividers,
If there are on-board dividers, or if the microcontroller pins can handle 12 Volts directly, then my concern is not founded.
The Arduino pins are 5V (or 3.3V) only, just as with a PIC.

'Presumably' the pins also include protection diodes (just like a PIC - Atmel basically tried to make a direct competitor for the PIC), so sticking 12V on an I/O pin means the diode passes it through to the Vdd pins - sticking 12V on it's power rail

'Hopefully' there was too little current capability on the input through the opto-coupler, so that didn't happen.

#### eTech

##### Well-Known Member
The Arduino pins are 5V (or 3.3V) only, just as with a PIC.

'Presumably' the pins also include protection diodes (just like a PIC - Atmel basically tried to make a direct competitor for the PIC), so sticking 12V on an I/O pin means the diode passes it through to the Vdd pins - sticking 12V on it's power rail

'Hopefully' there was too little current capability on the input through the opto-coupler, so that didn't happen.
Nope....no damage.

I mistakenly saw +13.0v max on the I/O pin. But missed that....that was for the Reset pin!

Should be -0.5 to Vcc+0.5 on any pin except Reset..

#### AllenPitts

##### Member
Hello Etech, Nigel Goodwin, ChrisP58, Pommie, and the ETO forum,

Added the Arduino, DMOS array and LEDS and got the system working from Sensor 1 thru to the LED module 1 using the schematic marked 'PODACAL... 200803' shown in post #21 of this thread

Have read all the posts carefully. Not sure I totally get all of them but the system is working using 12 volts, the PIR, optocouple, Arduino, DMOS array and the LEDs. Unless there is a compelling reason, if it ain't broke don't fix it.

1. Have often done prototype boards between breadboard and PCBs in development but I am going straight to PCBs because
2a. I seem to commit the most errors between breadboard and prototype soldering
2b. Its faster
2c. PCBs have gotten so fast and cheap that the pain of a PCB mistake is greatly mitigated

2. Construction elements are becoming more real. For instance, power, V+ and ground, is needed at eight sensors, the Arduino,
and V+ at seven LED modules. I figure sixteen power feeds.
The first thought was to use barrel jacks

But is seems a bit overkill so thinking about going to fixed block terminals

3. Have started on 3D printer designs for the LED modules. This is the prelim on LED modules 2 thru 6

The PCB would fit in the rectangle in the bottom of the
module.

The sensor modules would be similar except smaller and with a single
hole at the top and the optocoupler at the bottom on the PCB.

LED modules 1 and 7

(I realize there may be some difference between these designs , the LEDs on the schematic and the original design
[ please see 'Staircase Application PIR...' at post # 7 of thread at Link to Original Design].)

4. In soldering ICs to PCBs have always used sockets. The optocoupler, PC817, wants a four position socket. Having a little
trouble finding a four position socket. May have to take an eight position socket and use a razor saw to split it in half unless
one of you double E studs have a better idea.

5. Thinking about doing a shield PCB on the Arduino for the DMOS but haven't gotten that far in PCB design yet.

6. Don't know if I am getting off the electronics track w some of these construction details, but because it would be such
a pain to fish the wires through the wall behind the drywall a surface mount chase is contemplated. The LED
modules shown need to be modified to accept a surface mount wire chase:

A more complete demonstration of the completed design was desired, especially the PCBs
but wanted to post today because it looks like the thread is evolving and
getting feedback from you guys reduces my 'Oh [email protected]#%!' factor.

Thanks.

Allen Pitts, Dallas Texas

#### eTech

##### Well-Known Member
Hello Etech, Nigel Goodwin, ChrisP58, Pommie, and the ETO forum,

Added the Arduino, DMOS array and LEDS and got the system working from Sensor 1 thru to the LED module 1 using the schematic marked 'PODACAL... 200803' shown in post #21 of this thread

Have read all the posts carefully. Not sure I totally get all of them but the system is working using 12 volts, the PIR, optocouple, Arduino, DMOS array and the LEDs. Unless there is a compelling reason, if it ain't broke don't fix it.
HI

I strongly recommend you use the new schematic in post #29 and use an input voltage of 5v. Only the input circuits have changed.
As mentioned in previous posts, the arduino GPIOs are not designed to operated at 12 volts. I did confirm this with a support call to Microchip. Doing so can damage the arduino (if not quickly, eventually). If you want to keep 12v inputs, then use voltage dividers as mentioned in post #24, to drop the input voltage to 5v.

BTW-
I think you did benefit from bread boarding at least some of the sub circuits in that you now somewhat understand how the design works.

Last edited:

#### AllenPitts

##### Member
Hello eTech and the ETO forum,

This the POCADAL working at the top of the stairs.

So I am quite reticent to change it. Kinda like that saying about parachuting is jumping out of a perfectly
good plane. But you have stuck w me thru all my dumb ass mistakes so I am adopting the
schematic in post #29.

This the schematic from post #21 which is the breadboard at the top of the stairs in the photo.

The breadboard altered to adopt changes in schematic from post #29 looks like

If there is 12 volts coming into the Arduino from the barrel jack and 5 volts coming into the 5 volt
pin from R8 would we not have 17 volts going into the microcontroller?

Thanks.

Allen in Dallas

PS Yes, I did get a lot insight by doing the breadboard.

#### Pommie

##### Well-Known Member
The 12V going into the barrel jack goes to a 5V regulator to supply the micro. Note the 5V is not "coming in" - it's going out.

Mike.

#### eTech

##### Well-Known Member
So I am quite reticent to change it. Kinda like that saying about parachuting is jumping out of a perfectly
good plane. But you have stuck w me thru all my dumb ass mistakes so I am adopting the
schematic in post #29.
Heh...except we know the plane will crash at any moment.

This the schematic from post #21 which is the breadboard at the top of the stairs in the photo.
Nice....didn't know you were that far along...

The breadboard altered to adopt changes in schematic from post #29 looks like
Glad you decided to do the rework.

If there is 12 volts coming into the Arduino from the barrel jack and 5 volts coming into the 5 volt
pin from R8 would we not have 17 volts going into the microcontroller?
No. The voltage supplied at the barrel jack feeds the onboard 5V regulator. So...while we are using an external 12v power supply, the arduino internal circuits still use 5Vdc. The output of the internal regulator is brought out to the V5 pin on the arduino. This make it convenient to use for things like GPIO inputs that don't require a lot of current.

PS Yes, I did get a lot insight by doing the breadboard.
Yes...its always useful to breadboard at least some of the circuits to get comfortable with how they behave and know what they do.

#### AllenPitts

##### Member
Hello eTech, Pommie and the ETO forum,

Been jammed at work and have not had time to make adjustments until today.

Thanks for hanging with me during extended periods between updates..

As mentioned in post #35 have bread boarded the system following fritz marked
'PODACAL 12 Volt Complete 200808' The system works as expected. That is,
when 12v power is applied to the breadboard and the Arduino barrel jack,
simultaneously, the green LED on the Arduino marked 'On' lights and the yellow
LED labeled ' L' lights. and the nine LED module light and the indicator LED lights comes on.
All of these things happen immediately when my hand is waved at the PIR.
The module lights stay on for
about ten seconds and then goes off. The Arduino 'On' light stays on.
After about six seconds the PIR rearms and the cycle can be repeated.
This would be the behavior required to make the Staircase system work.

Altered the PODACAL according to fritz 'PODCAL 12 Volt Complete 200812' also
in post #35.
When 12v power is applied to the breadboard and the Arduino barrel jack,
simultaneously, the green LED on the Arduino marked 'On' lights and the yellow
LED labeled ' L' come on..
The nine LED module lights, after motion at PIR, come on after about a nine second delay.
At first, because there was no motion. the LEDs stayed on for over three minutes
and it was thought that the LEDs were on permanently. But a second movement
caused the LEDs to go off immediately. Then after about twelve seconds the
the lights came on again.

Still trying to get the exact sequence on the 'PODACAL...200812' because the lights
come on in a delayed pattern.

The closest thing to a pattern that can be discerned is the LED module takes about
ten seconds to come on after PIR motion and stays on until a second wave turns
them off. A third wave turns the lights on again after ten seconds and the cycle
is repeating. That is, motion causes the nine LEDs to light after nine or ten second delay
and stay on a second wave st the PIR makes them go off.

Not sure what causes the initial delay or why the second wave causes the LEDs
to extinguish.

Have begun PCB based on 'PODACAL...200812'
but will probably concentrate on getting the breadboard to work before going back to the PCB.

Thanks.

Allen in Dallas

PS The same Arduino sketch id used in both tests.

Code:
int led = A0;               // the pin that the sensor is attached to
int sensor = 2;         // the pin that the sensor is attached to

void setup() {
pinMode(led, OUTPUT);
pinMode(sensor, INPUT);
Serial.begin(9600);
}

void loop() {
Serial.println(sensorval);

if (sensorval == HIGH) {
digitalWrite(led, HIGH);
}else{
digitalWrite(led, LOW);
}

}

Last edited:

#### eTech

##### Well-Known Member
Altered the PODACAL according to fritz 'PODCAL 12 Volt Complete 200812' also
in post #35.
When 12v power is applied to the breadboard and the Arduino barrel jack,
simultaneously, the green LED on the Arduino marked 'On' lights and the yellow
LED labeled ' L' come on..
The nine LED module lights, after motion at PIR, come on after about a nine second delay.
At first, because there was no motion. the LEDs stayed on for over three minutes
and it was thought that the LEDs were on permanently. But a second movement
caused the LEDs to go off immediately. Then after about twelve seconds the
the lights came on again.
This sounds like the software input logic is reversed. The post #29 HW change inverts the input signal so that when motion is detected, the signal at the gpio input changes from HIGH (5v) to LOW(0v). When you made the hardware change, did you also change the input logic states in the software? It needs to be changed to match the HW change.

In addition, with the external 22k pullup resistor being used, you don't need the internal "weak pullups" enabled in the arduino. You can disable the "weak pullups" if they aren't already.

Still trying to get the exact sequence on the 'PODACAL...200812' because the lights
come on in a delayed pattern.

The closest thing to a pattern that can be discerned is the LED module takes about
ten seconds to come on after PIR motion and stays on until a second wave turns
them off. A third wave turns the lights on again after ten seconds and the cycle
is repeating. That is, motion causes the nine LEDs to light after nine or ten second delay
and stay on a second wave st the PIR makes them go off.

Not sure what causes the initial delay or why the second wave causes the LEDs
to extinguish.
This sounds like a bad/loose connection(s) on the breadboard. Maybe something became loose when you made the changes. Check the connections.