> > I am a beginner at PIC programming and built a test circuit using the > 16C84. The circuit uses a magnetic switch for an input and when the switch > closes, and LED and piezo buzzer turn on. Simple right? Well, not for me! > > Here is the problem: when I physically move closer to the breadboard (with > the mag switch open), the LED turns on (but not the buzzer), getting > brighter as I get closer to the breadboard. Once this happens, the > operation of the circuit is all messed up. If I close the switch, > sometimes the LED goes on, sometimes the buzzer sounds, sometimes both, and > sometimes the LED works opposite, i.e. when the switch is open, the LED is > ON, and vice-versa. > > Brief description on the circuit: 10 MHz Crystal (ECS) w/ 33 pF caps > directly to ground; PORTA<0> switch input, PORTA<1> led output, PORTA<2> > piezo output. > LED output feeds an 74HC04 inverter; output of inverter to LED cathode; LED > anode through 100 ohm resistor to +5V. All unused 74HC04 inputs direct to > ground. All unused PIC I/O are configured as outputs and left unconnected > to anything. !MCLR pulled up to +5V. > > As for hardware, I configure the I/O ports on reset, and basically run a > tight loop scanning the inputs, and changing the outputs based on the > input. > > Any ideas? Hardware or software? I can't do much else until I figure out > what is going on here. Hardware. Specifically the piezo. What's the current rating on it? The voltage? PIC output lines can only source/sink 25ma. I'm pretty sure the buzzer takes more than that. Quick test: remove the buzzer and replace it with another LED and current limiting resistor. Retest and see if the same erratic behavior occurs. One last thing: switches bounce. You have to have some sort of hysteresis to counteract the bounce. This is either checking the switch state for stability for 50 milliseconds or so, or acting instantly to the switch change but delaying more than 50 milliseconds before checking the switch again. I have a basement light project that is quite similar to yours. The opening of the magnetic door switch turns on the lights. My hysteresis delays checking the switch again for 90 seconds. Works like a champ. BTW on this project I use an optoisolated output to drive the relay that turns the light on. That's why I suggest testing with another LED. If that works you can then use an optoisolator with a higher current drive to drive the buzzer. Hope that helps, BAJ