Motion and Light Sensors with Arduino (and Without)

by Natalia

I have recently received the following question from a reader:

I’m looking for a circuit board design that will need to turn on an array of LEDs when motion is detected during the day time, and also stay on continuously during the night time; using the Arduino would be nice. The project that I am working on is just a picture frame with my artwork in it. The art is actually an embossed piece. The light that I am placing within the frame will shine across the embossed art, and reflect off the raised areas of paper and make the picture appear more three-dimensional. So, the picture acts as a night light when it’s dark, and then turns on for a moment during the day time when some approaches the picture.

I suspected there had to be a simple circuit to accomplish this without having to program a microcontroller to take care of triggering the light. I could see that was overkill; after all, it is just a way to switch lights on/off. Still, I had no idea how to do it, if not from a software point of view.

Well, he mentioned the Arduino in his email, and I wanted to give him a quick response, so I put together a prototype to achieve the effect he desired. I properly warned him that might not be the best solution, but since he wanted to tinker with the Arduino, that would be a simple sketch.

I suggested the use of a compact Arduino clone like the Ardweeny (check out “Testing the Ardweeny” to see how tiny it is!)

Then, soldering everything onto a stripboard should result in a small footprint that would not detract from his art piece.

Anyway, here’s a Fritzing diagram that shows how to wire the circuit:

Fritzing diagram for motion-triggered light using Arduino

You can see the Arduino, LDR (light sensor, the component with the “squiggly” line), LED and PIR (motion sensor, the black “mystery” component to the right — the Fritzing version I’m using has no PIR component and I still don’t know how to add a custom component the proper way).

Here’s an excellent tutorial on the use of PIRs, from Ladyada who did create her own component on Fritzing.

And here’s a picture of the setup:

Circuit for the motion-triggered light with Arduino

Bill of Materials (include parts used for both Arduino and non-Arduino* versions):

Here’s a video showing the concept:

(On a side note I have now acquired a HD flip camera so I don’t have to use my phone to record videos anymore).

OK, here’s the Arduino sketch:

/* www.TheElectronicsHobbyist.com/blog
 * Natalia Fargasch Norman
 * Motion detection using Arduino
 */

#define LDR 0
#define PIR 2
#define LED 3

int pirState;
int ldrValue;

void setup() {
  //Serial.begin(9600);
  pinMode(LED, OUTPUT);
  pinMode(PIR, INPUT);
  digitalWrite(LED, LOW);
}

void loop(){
  ldrValue = analogRead(LDR);
  //Serial.print("Analog reading = ");
  //Serial.println(ldrValue);

  if (ldrValue <= 512) { // dark
    digitalWrite(LED, HIGH);
  } 
  else { // ldrValue > 512
    pirState = digitalRead(PIR);
    if (pirState == HIGH) {
      digitalWrite(LED, HIGH);
      delay(5000);
      digitalWrite(LED, LOW);
      delay(1000);
    } 
    else { // pirState == LOW
      digitalWrite(LED, LOW);
    }
  }
  // The processing in the Arduino occurs faster
  // than the response from the PIR, and adding this delay
  // eliminated a flickering on the LED
  delay(1000);
}

The idea is to trigger the light when it’s dark; otherwise, trigger it for a short duration if motion is detected. This simple Arduino sketch does just that. A light dependent resistor is connected to an analog pin on the Arduino, and reading from it will either trigger the light (if it’s dark) or have the Arduino check for motion by reading from the motion sensor connected to a digital pin (if it’s not dark).

And finally… a non-Arduino version!

A few days and a couple of aha! moments later, I finally figured out how to accomplish the same behavior without using a microcontroller.

I knew an OR gate could be used to trigger the LED (output) on either (or both, doesn’t matter) condition: darkness or motion detected. The PIR outputs either 0 (no motion detected) or 1 (motion detected). But what about the light detection piece of the circuit? The LDR gives me an analog reading, and I needed a 0 or a 1 as inputs to the OR gate.

When later reading about transistors in Forrest Mims’ “Getting Started in Electronics” it occurred to me that I could use one as a switch on the LDR part of the circuit to generate the second input to the OR gate.

So I excitedly went to Marvac (my trusty local electronics shop) to buy OR gates; unfortunately, they were out of stock… :-(

But then, again courtesy of Forrest Mims, I learned how to build my own OR gate using two rectifier diodes. (And I had the necessary parts!)

Here’s how it’s wired:

Diode OR Gate

Now on to the complete circuit…

First I hooked up the LED to the OR circuit to test that it worked. Check.

Then I connected the PIR to the LED to make sure that it woks, i.e. LED lights up when there is motion detected. Check.

Then I connected the PIR to be the first input of the OR gate… and it didn’t work.

I hooked up the LDR as the second input to the OR gate and that part… “kind of” worked. (LED off when dark and on when light; should be the other way around). I had used an NPN transistor, so I just switched to a PNP instead and it worked. Makes sense!

This is how it was hooked up: Emitter to ground, Collector to load (the LED) and Base to LDR. The LDR was connected to a 10K pull-up resistor and a potentiometer (to fine tune the amount of darkness it takes to trigger the LED).

But the PIR part of the circuit was still “kaputt”… I checked with the logic probe and found out that there was a signal coming to the LED. I replaced it with a diffused LED and could see that it was actually on, just veeeery faint.

Using a multimeter I measured the voltage between the LED leads and it was 2.3V. My power source was 4.92V. It seems that the PIR causes a voltage drop…

Then I looked at the datasheet for the Parallax PIR I have (note to self: that should definitely NOT be an afterthought), and learned that there are two versions, and that mine requires a transistor or a MOSFET to drive external loads.

I hooked up a random (I’m no EE!) transistor (a 2N2222) and now the LED is brighter. But still not as bright as when I cover the LDR.

Looking at the current values, my multimeter read:

Current through the LED when the LDR portion of the circuit is active: 4.2mA
Current through the LED when the PIR portion of the circuit is active: 0.8mA

The only other transistors I had were 2N3904, so I decided to try that one. The LED was much brighter and the current read 2.4mA. With a base current of 0.02mA I was getting a gain of 40 with the 2N2222 and 120 with the 2N3904.

I checked the datasheets for these two transistors, and these gain values were consistent. After some research it seems the BC548B is a better amplifier transistor for this application, and has a minimum gain of 200.

Here’s what the circuit looks like on Fritzing:

Fritzing diagram for motion-triggered light

And here’s a picture of the real thing:

Circuit for the motion-triggered light

By the way, I heard back from my reader; he bought the Boarduino from Adafruit, not the Ardweeny, and successfully built his “darkness-or-motion-triggered artwork illumination” project.

Keep sending me your questions; I love to see what you are working on, and it helps me on my learning journey as well. (Explaining something to somebody else is the best way to learn). I will also from time to time publish some of your questions here. (If you have an electronics blog let me know and I will include it here).

No related posts.

{ 3 comments… read them below or add one }

Annie Andre January 12, 2012 at 3:05 pm

Natalia, i see an online group session class in our future starring you as professor electronic. Seriously this is amazing. To me, a non electronic hobbyist, this seems far more advanced than a hobbyist. Is this your day job too?
I’ve always wanted to take apart one of catherines dolls that speak. it says something silly like “i love you”. I wanted to reprogram it so that i could put my own messages in it something more meaningful like “brush your teeth” and ” don’t talk back to mommy” and “be kind to your elders” . is that even posssible to take a store bought toy and do that? or does that require a slew of equipment and stuff i don’t have?

Reply

Natalia January 12, 2012 at 8:34 pm

Annie, I used to work with software, but never with electronics. It would be possible to do something like that with a doll, but the vague idea I have on how to do this involves the doll having to wear a “purse” to carry her hardware with her… I don’t have experience with modding yet, but it is on my list of things to explore.

Reply

Annie Andre January 12, 2012 at 11:31 pm

When we lived in California, the MakeFaire would come by once a year and we would always go there as a family. This seems like something they would have available there.

Reply

Leave a Comment

Previous post:

Next post: