Taking an IR signal and outputting a stable crisp square wave for an atmel328p. - Page 1
Forum section: electronics / beginners

author: SirAlucard
author_rank: Regular Contributor
timestamp: July 25, 2025, 06:42:43 pm
content: I currently have a project that I'm trying to shrink down the PCB in size (not my original PCB, but creating something new and smaller from it) and there's a component that I've been unable to correctly identify. I'm under the assumption that it's an opamp however I'm a little unfamiliar with using opamps to their full capabilities. I understand the basics but unsure if this is correct.In the schematic you have 3 connections, P15, P16, and P27. P15 and P16 are connected to an ir phototransistor, and P27 is connected to the microcontroller.The reading from the oscilloscope is from the original PCB and what is expected as an outcome.Yellow is P15, blue is P16, and Pink is P27 respectively. Regardless of the voltage on P15 I want a crisp square wave output, and as you can see there isn't much of a delay in that.I tried using a MAX40007ANT+T in a wlp-6 package as space is a premium. So the replacement new to be as small.

author: Buriedcode
author_rank: Super Contributor
timestamp: July 25, 2025, 08:04:51 pm
content: Phototransistors are pretty slow, I assume you can't change that?What is the application here? A little context helps, because for things like IR comms, the encoding matters - PWM, PPM, NRZ, as well as the datarate. For things like, proximity sensing, IR signals are modulated so that other sources of IR can be filtered out, and often that frequency isn't particularly important, nor is the duty cycle.

author: BrianHG
author_rank: Super Contributor
timestamp: July 25, 2025, 08:52:03 pm
content: Use a Pin photodiode instead. They are cheap and have sub 6ns rise and fall times for the cheap sub 1$ ones.If you want to pay 5$, you can get a transceiver pair with built in driver and receiver amps. Though, those are designed for 115kbaud where you can get 1 to 10 megabit going the custom route.Even Digikey charges less than 50cents for an IR-pin photodiode with 5ns rise and fall times.See here:Digikey IR Photodiodes

author: SirAlucard
author_rank: Regular Contributor
timestamp: July 26, 2025, 01:45:42 pm
content: Quote from: Buriedcode on July 25, 2025, 08:04:51 pmI'm assuming it's a photo transistor, not really 100% on that. As I'm coming into this project kinda blind about the components used. Everything is 0603 or smaller in my new redesign, With most everything on the PCB being 0402 so I can keep the PCB as small as I can. Use case is moveable objects that communicate with each other and stations via IR. Each device puts out an ir signal and receives that signal from others as well.I don't have access to the microcontrollers programming, so I can't tell you much about the or codes other than what I've collected on the oscilloscope.I need the signal to come in as a square wave which is why I originally thought rail to rail opamp, however the max40007ant isn't outputting what I was hoping. In further research I've been pointed at a comparator, however I couldn't find an exact answer. So wanted to try asking here.

author: Peabody
author_rank: Super Contributor
timestamp: July 26, 2025, 02:27:00 pm
content: I'm confused about what you are trying to do. Does the original device use a 328P MCU? If not, how will you port to a 328P. What information is conveyed in the transmissions?It looks like there are two distinct frequencies, and the opamp or comparator just converts that to a square wave, although I have to say that the circuit is above my pay grade. It would need to include some kind of AGC and possibly some tuning to specific frequencies. Maybe someone can explain how it works. It does appear to be a comparator - the pullup resistor on the output.

author: SirAlucard
author_rank: Regular Contributor
timestamp: July 26, 2025, 08:35:26 pm
content: Quote from: Peabody on July 26, 2025, 02:27:00 pmYa it is, like I said I don't have access to the programming, I'm taking the original chip off of the original board and placing it onto a custom board for my application. However I'm trying to keep the original boards functionality while expanding on it. Everything else functions correctly, however this is the only thing that doesn't and arguably the most important. Which means I didn't get the original component correct. Looks like I may need a high speed comparitor with an open drain output and rail to rail input.Both of the yellow signals in both those images are the same code just with direct line of sight obfuscated to show that even at low voltage it's triggering correctly.

author: Buriedcode
author_rank: Super Contributor
timestamp: July 26, 2025, 08:58:14 pm
content: Ok, in your first post, you mention blue is P27 - thats the output of the opamp (or more likely comparator?) and you say you want a nice crisp square wave output - but if the blue trace is P27, then the "opamp" is a flat DC. Surely the pink trace - the nice square wave - is the output of the opamp/comparator? Meaning blue = P16, yellow = P15, and pink = P27?When I saw the tace first, I initially thought of a data slicer - something that takes an incoming waveform of varying amplitude, tracks the average, and uses that as a threshold for deciding if the incoming waveform is a 0 or 1. Generally the way it does this is to use an RC filter to low pass the signal - effectiviely averaging, getting the DC component (or at least the low frequency component) to get the comparators negative input, then a much higher frequency filter for the positive input. This way it can adjust the threshold on the fly, allowing for slow changes in amplitude for long runs of 1's or 0's, whilst detecting sudden changes as transitions.If thats the case then that device is most likely a comparator, and the passives are there to set the time constants, thresholds, and gains.Another clue is the strong pullup on the output (1k?). Many comparators have outputs that are open collector, for connecting multiple outputs in parallel, and require a relatively strong pullup for half decent rising edges. Opamps always have a push-pull output with no need for pullup (unless its to improve driving capability).

author: Buriedcode
author_rank: Super Contributor
timestamp: July 26, 2025, 09:42:13 pm
content: Well, its a Saturday night and (sadly) plans were cancelled. So 10 minutes with LTspice and I got this.Using the part numbers in my schem (not yours).. looks like R1/C1 are a filter for the power supply to give a clean supply for the photodiode and comparator reference. R2, R3, and C2 form a divider, setting the comparators threshold at slightly less than the supply voltage (comparator looks to be rail-to-rail input, or at least accepts inputs up to the supply voltage).C3 blocks the DC offset so the output from the phototransistor sits at 0V. R4 biases this signal to the threshold, so the signal wiggles around this threshold and the comparator chops this to high/low.R5 biases the photodiode.I made a lot of guesses in this:1) assumed a photodiode. Could be a phototransistor, but I have no model for that. Given the slow rise/fall times in your yellow trace, looks to be a phototransistor since they're pretty slow. Photodiodes tend to be much faster when photoconductive mode.. but I have no idea what your input signal (the source of illumination) is.2) Driving the photodiode mode here, the DC offset is the ambient illumination of the sensor (daylight), signal is the amplitude. I assumed reasonable 16:1 ratio of ambient to signal. Things like TV remote snesors this ratio is mich higher, given the greater variation in ambient lighting, and high variation in input signal.3) Used a generic LM393, since its open collector, and it looks like your signals aren't that fast (4kHz?). It's old but simulates easily. The device you have is most likely a more modern comparator.4) Used 5V as the supply, but should work with 3V5) Assumed all your part values were correctI'll post the LTspice netlist here as well as the photodiode model if you want.

author: John Coloccia
author_rank: Super Contributor
timestamp: July 27, 2025, 04:06:51 pm
content: If I understand what you're trying to do, I would thinkAC couple the signalFeed into a Schmidt triggerTune bias/gain to match triggers hysteresisAm I thinking about that wrong? Don't think I've ever actually had to do this.

author: SirAlucard
author_rank: Regular Contributor
timestamp: July 28, 2025, 02:39:58 am
content: Quote from: Buriedcode on July 26, 2025, 08:58:14 pmYou're absolutely correct, sorry about that. Was writing my original note from my phone without the photos right in front of me.Quote from: Buriedcode on July 26, 2025, 08:58:14 pmI'm also realizing as I've been going over everything, this may or may not be the original values. I'll need to go back and grab the original values but I'm away from home and my notes. Cause the values were bothering me myself as they were changed to go with the Max40007.Edit: I stand corrected, just checked my original notes and the values are correct.Quote from: Buriedcode on July 26, 2025, 09:42:13 pmI've been racking my brain on this all weekend despite my GF wanting to spend time. This is amazing btw, thank you! I've gotta play with LTSpice, I've been using Falstad for my visuals.. 😅 I'm definitely looking at comparators now. Looking at the MAX40009 instead of the 40007, hoping that'll test out well since I already have boards made using that WLP-6 package.

author: SirAlucard
author_rank: Regular Contributor
timestamp: September 10, 2025, 02:54:20 pm
content: So update on this, I've changed the component to a Max40009 comparitor, as that's the size of component I want to work with. It's a push-pull as opposed to an open drain, I'm assuming I can just remove the pull up resistor to get this to work correctly, however it's still not doing as I expect it to. It's pegging or at vcc voltage and not responding to the down swings. You can see the signal from the phototransistor and what the output is doing. What am I missing? I've tried with and without R17 and same output.

author: Buriedcode
author_rank: Super Contributor
timestamp: September 10, 2025, 03:36:42 pm
content: What do the signals are A1 and B1 - the comparators inputs look like? That'll determine whether the problem is the comparator, or the peripheral circuitry, since we can see the difference between the signals on a scope ourselves.I'm not sure whats going on with that yellow trace at the end - looks like it decreases exponentially before shooting back up again - like its discharging a resistance. Unless its driving something with significant capacitance, it *should* have nice fast edges.The node where R20 and R21 join should be a nice fixed DC that sets the threshold - thats the inverting input to the comparator.Btw, that comparator looks like a cool part, pretty good specs for low power consumption!

author: SirAlucard
author_rank: Regular Contributor
timestamp: September 11, 2025, 01:09:38 am
content: Hey Burriedcode, sorry for the late response. So I checked it out, and as far as I'm understanding it, My voltage for A1 is not high enough to trigger correctly? So in this capture I've got Blue as my A1, Yellow as my B1, and pink as my output. From what I understand my blue should be half way up or just under the max of my yellow in order to allow it to compare correctly and give me a nice square wave out. After going over my board I did notice find that my R19 was a 1k resistor instead of the 330 that I was supposed to have. However in ltspice it still shows the circuit as working.As for what's going on with the yellow trace at the end, I think it's just picking up some noise from being probed. It does go away if I just probe the output by itself.That's exactly why I wanted to use this component, was the low power consumption so it can last on battery for a long time and quick response time to give crisp square wave outputs with the push-pull.

author: Buriedcode
author_rank: Super Contributor
timestamp: September 11, 2025, 05:32:19 pm
content: A1 and B1 should sit at the same DC level, because A1 is biased by that 470k resistor, so the singal coming through the 100pF cap causes the signal to go above and below that DC bias.Also, I think you have switched the traces again: blue looks like B1 - the inverting input, the DC reference, and yellow looks like the input to the circuit rather than the non-inverting input to the comparator.The reason is, looking at that yellow trace, it looks exactly like the original post where that was the output from your photosensor - input to the circuit. It starts low, and when it reiceves an IR signal, it very quickly rises, oscillating at a higher voltage, then, dorpping back down to low. The input to the comparator is DC biased - it sohuld sit at a DC level that is static, with an input signal only causing it to modulate around that DC bias.The LTspice sim has limitations - most notably that I/we are guessing as to the input signal - is it a current? a voltage? it shouldn't really matter since I assuming it was a photodiode given the circuits DC bias.

author: DavidAlfa
author_rank: Super Contributor
timestamp: September 11, 2025, 06:31:20 pm
content: Use a proper IR receiver/demodulator and the signal will be perfect