NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Sensors, Actuators, and Robotics » Radio Shack Motion sensor - how do I connect this to the 168?

March 09, 2011
by Bigforky
Bigforky's Avatar

Fellow Nerds, I’ll preface that I am a complete rookie with microcontrollers, electronics and C programming.

I have a project where I am making a controller to manage two sets of lights activated by a motion sensor.

The lights themselves are 120v LED walk-don’t walk semaphores found from a local surplus shop, and I am using a solid state relay to make them mimic what happens at a cross walk. The blinky part I have figured out including long ‘on’ and the blinking of both lights.

Where I am stuck is the trigger from the motion sensor.

I looked at the code in the NK manual from page 63 about digital I/O : DDRC &= ~(1<<PC3); // set PC3 as input while(1) { if(PINC & (1<<PC3)) { // do something if PC3 is high } else { // do something else if PC3 is low } }

I read this that PC3 (pin26) if active high meaning has voltage, would trigger the event in the brackets. The motion sensor (Radio shack acquired) has three leads +,-, signal. I have connected the power and measure 3.8v with motion and 0v without. When I tested my program, I was not able to get pin 26 to fire off the rest of the script…

Am I off thinking 3.8v is enough? Was it way too much and I fried the chip? Am I way off on my logic understanding of PC3?

Any advice is greatly appreciated…

March 09, 2011
by bretm
bretm's Avatar

It doesn't really "trigger" the code, it runs it over and over again each time the main loop executes, as long as PC3 is high. So if you only want to trigger something when it transitions from low to high you need to keep track of whether it was low or high the previous time through the loop, and then only do the "trigger" action when it changes.

uint8_t previousPinC = 0;

while (1)
{
    uint8_t newPinC = PINC;
    uint8_t pinsThatJustWentHigh = newPinC & ~previousPinC;

    if (pinsThatJustWentHigh & (1<<PC3))
    {
        // triggered
    }

    previousPinC = newPinC;
}

Does it run the code when you force PC3 high by connecting it directly to +5V? If not, then it's a software problem.

Is it 3.8V even when connected to the MCU? That's getting a bit close to the 3V limit that the MCU considers to be "high" for a regular digital input. You might consider using the analog comparator instead.

March 09, 2011
by Bigforky
Bigforky's Avatar

Bretm,

I could not get it to activate the rest of my script - even when connecting it to +5v...or to ground (thinking my logic may be whacked I tried both).

The sensor seems fairly consistent at 3.8v when triggered - I didn't think to measure it when connected to the MCU.

It sounds like it wont kill the MCU by applying +5v, I was a bit worried I wrecked the chip thinking that may be why it didn't work. Whew! (well, I guess they are $6 so its not so painful)

I will give your code example a try later tonight when I get some more time in the man-chamber.

March 10, 2011
by Bigforky
Bigforky's Avatar

I figured out my pitfall.

For whatever reason, the loop starts sequencing on it's own as soon as I power it on, and at the end of the loop I had the line return 0 - which ended the looping.

So all this time, I basically didn't give the processor a chance to determine if the motion sensor was activating.

So....it all works now. Its a pretty good feeling to complete my first project!

Thanks Bretm!

March 10, 2011
by Ralphxyz
Ralphxyz's Avatar

Bigforky, would you post your working code?

I have one of those motion sensors setting in a drawer that I ned to setup one of these days.

It would be nice to have some working code to start with, to save a few steps.

Ralph

March 10, 2011
by Bigforky
Bigforky's Avatar

Sure Ralph.

I'm sure it doesnt look like pretty code, but it works. If you replace this code in the ledblink code it should load right up.

I used pin 28 and 27 to drive two solid state relays to switch the 120v LED light signs. Pin 26 was my input.

Everything else on the breadboard is wired the same as the LED blink (less that LED)

//This is the WALK-DONT Walk blinky from the ledblink project

// led_blink.c
// for NerdKits with ATmega168
// hevans@nerdkits.edu

#define F_CPU 14745600

#include <avr/io.h>
#include <inttypes.h>

#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"

// PIN DEFINITIONS:
//
// PC4 -- LED anode

int main() {

 // Pin27 for output to walk sign
  DDRC |= (1<<PC4);

 // pin28 for output to dont walk sigh (temp red LED)
  DDRC |= (1<<PC5);

 // set PC3 as input
 DDRC &= ~(1<<PC3);

//The Checker to see if pin 26 is high (motion)
while(1) {

//if it is hign start the sequence
if(PINC & (1<<PC3)) {

////////////////
//The Sequence//
////////////////

//turn WALK for 20 seconds
    PORTC |= (1<<PC4);

//delay for 8000 milliseconds to let the WALK light stay on
    delay_ms(8000);

// turn off WALK
    PORTC &= ~(1<<PC4);

/////////////////
//dont walk /////
/////////////////

//set number of times to flash dont walk
int count=10;

//dont walk routine
while (count>0) {
    // turn on DONT WALK
    PORTC |= (1<<PC5);

    //delay for 700 ms to let the DONT WALK light stay on
    delay_ms(700);

    // turn off LED
    PORTC &= ~(1<<PC5);

    //delay for 700 milliseconds to let the light stay off
    delay_ms(700);

count = count-1;

  }

// turn on LED
    PORTC |= (1<<PC5);

//delay for 8000 ms to let the DONT WALK light stay on
    delay_ms(8000);

//turn off DONT WALK light
PORTC &= ~(1<<PC5);

//delay from starting again for 1.5 seconds
    delay_ms(1500);

//////////////////////////////////////////////
//The Routine now goes back to the beginning//
//until the motion sensor is triggered again//
//////////////////////////////////////////////

//Open a beer

}
}
}
March 10, 2011
by Ralphxyz
Ralphxyz's Avatar

AAAAH, delighfull.

Ralph

Post a Reply

Please log in to post a reply.

Did you know that many systems in nature can be described by a first order response? Learn more...