I've designed the schematic below:
Basically, I want to detect direction of motion at a door frame from the top to turn on or off a relay which is probably connected to lights. The phototransistors will be 6 centimeters away from each other and there will be two IR LEDs in the middle.
I have four concerns in mind:
The current at IR_A and IR_B has a great range from 1 mA to 100 nA and the voltage on these diodes become nearly equal to VCC when low light is present. I compare the voltage signal on the photodiodes with "Sensitivity pot" voltage, but I would have hard time when photodiode voltage is close to VCC because only a fraction of a degree turn would make a difference. How can I make a better comparator setup? I thought of implementing a logarithmic reading in the code so that the pot readings would be interpreted exponentially instead of linear. Is there a better yet simple solution? Maybe I can switch to different resistors with MOSFETs depending on the range (similar to an auto-range multimeter).
I pulse the IR transmitter LEDs with high current for maximum brightness and sensitivity. The code is like:
I learned single conversion lasts around 100us for 10 bit measurement thus 0.325 amps will pass for around 200-250us from the LEDs and then there will be a 32ms sleep time. Even though the duty cycle is really low will I damage the LEDs? Can I go for even higher currents? Datasheet says current surge can be 1A and pulse can be 200mA for 100us, doesn't say anything about higher durations. Also, to not disturb the microcontroller, I've arranged a totem pole which costs a few cents only.
To not disturb the power rail as I turn on the LEDs, I've added two decoupling capacitors (C6 and C5) but how can I make sure I've selected the right capacitance? It must be about the formulas that are used to choose the output capacitor when designing a power supply (ripple voltage).
I've burned a few relays back then when I was switching power supplies. This time, I will connect the relay to 220V LED bulbs directly. I hope there will be no inrush current of the internal power supplies. The relay is relatively small, so I wanted to make sure.relay datasheet
Note: I am open to any suggestions, I don't know if I can pull this project off because there is light, reflections that have different angles depending on the person's height, scatter, distance, complexity of human motion. I hope I've chosen the correct way to determine direction of motion.
In physical placement , Blue dots are phototransistors, Red dots are IR LEDs:
After both sensors are triggered, MCU will look which sensor looses the signal first and decide which way the person is going, will count the people inside, if counter > 0 lights lit, else lights off.