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 |
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 |
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.
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 |
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 |
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 |
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 |
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)
|
March 10, 2011 by Ralphxyz |
AAAAH, delighfull. Ralph |
Please log in to post a reply.
Did you know that inductors try to keep their current constant over short periods of time? Learn more...
|