Hi,
I took a look at the working principle of the entire thing we discussed yesterday. However I don't understand one thing.
Note that without the datasheet describing the encoder precisely, we do not know if it has open collector/open drain outputs or not.
Like I explained, open collector/drain output is common, because it is so simple for optical encoders: the axis has a disk with radial opaque and transparent "spokes" of the same width, and the only electronics are a LED and two phototransistors. With a wide input voltage like 5V-20V, there probably is a transistor as a constant current source for the LED, or perhaps a zener diode as a crude voltage regulator and a resistor as a current limiter. (Infrared LEDs have as low as 1.2V forward voltage, so the voltage drop over a simple current limiting resistor would vary between 3.8V and 19.8V, or by a factor of 5.2.)
If there is any kind of output filtering, for example a Schmitt trigger to clean up noise from the output (making the outputs "decisive"), the circuit inside the encoder can be much more complicated, and there is no guarantee that the outputs are open collector. It could very well be a push-pull output (but based on your experiment, it does not sound like one). Only the datasheet will tell – or careful measurements using a multimeter or an oscilloscope. If you can read all the markings on the side of the encoder, it may be possible to find the datasheet.
To my knowledge, I understood that the phase signals' outputs that come out of the encoder's receiver is connected to the base of the npn transistor and the pullup resistor is connected to the open collector that in turn sends the electrons of the 3.3v source through emitter if the logic state is one.
No, the other way around. The transistor (be it NPN BJT or N-channel MOSFET) only pulls the output to ground. It does this by connecting the base/gate of the transistor to the signal source, and source/emitter to ground. When the signal source (base or gate) is high enough (passes enough current for NPN, exceeds Vgs(th) voltage for N-MOSFETs), the transistor conducts, pulling the output to ground. When the signal source is low enough (no current through base, or gate voltage is at or below ground), the transistor has very high resistance, so does not conduct, and the output floats.
To make sure the output goes high, the open collector output must be pulled up to some voltage; anything above the saturation voltage of that transistor (typically around half a volt or so, depends on the transistor type) will work. Because the transistor connects the output to ground when it conducts, the pull-up must be weak enough (high enough resistance) that the transistor can pull it to ground. If the pull-up is too weak, then the transistor leakage current may cause the output never to reach the high voltage.
This is also why a single NPN/N-MOSEFET used like this inverts the logic: a high on the base/gate causes the drain/collector to go to ground.
I tried the same encoder in arduino without actually pulling up just by connecting the phase signals to the interrupt pins and it worked completely fine.
There are many reasons why this is. Perhaps the outputs are not open collector, but have internal pull-ups to the resistor. Or perhaps the encoder has push-pull outputs, like microcontrollers, and will pull the outputs to high or low, instead of just one.
On top of that, I measured around 15 readings of the phase signal's output (changing the position of the encoder) with a multimeter and the output didn't even go beyond 1v.
One possibility is that your Arduino inputs had their internal pull-ups enabled. They are typically rather weak, equivalent to something like 50k or higher pull-up resistors (but actually use transistor logic).
That, combined with the resistance of your wires to the encoder (pretty thin, so probably a couple of ohms of resistance), would explain why the voltage never rises very high.
Does it mean that the arduino threshold for logic state high is too low (maybe arduino considers logic state 1 as soon as the signal reaches 0.4 volts for instance and it need not reach around 5 volts?)
Digital logic is not exactly ground or Vcc; they are known ranges, with the voltage inbetween indeterminate, with typically 0 to 0.3×Vcc considered low, 0.7×Vcc to Vcc considered high, anything between indeterminate, and any voltages possibly damaging.
For example, Teensy 4.x considers any I/O inputs below 1V or so as low, anything above 2.3V high, with voltages 1V to 2.3V indeterminate; i.e. either one. Voltages below 0V or above 3.3V can damage the Teensy, although there are small protection diodes that can handle small over or undervoltages (say, 0.15V) if the current is low enough too; they're normally intended for electrostatic discharge safety, or very short static spikes, and not for input protection per se.
The given limits are basically "guarantees", and the real world limits for any particular microcontroller are likely slightly lower (or even significantly lower for some individual chips), so a particular Arduino board giving logic high even though the input voltage only varies between 0 and 1 volts is not strange. You just cannot assume it does so reliably –– and you already found out it is actually not reliable for you either.
My question is why shouldn't we plug in directly the phase signal into io pins of teensy?
My reason for using the shown circuit with open collector encoders and fan tachometer outputs is
safety and
reliability.
As you probably know, there is a design/construction method called "throwing sh.. uh, spaghetti at the wall, and picking the one that sticks". It means you simply try stuff at random, until something seems to work. I do not do that, because it yields truly crappy results. Instead, I look at what datasheets and manufacturers promise, and use circuits that work with that.
I also do defensive stuff, like using extra components even when there is over 90% chance of the circuit working, just to make that 100% of all properly working units. And I pick my capacitors and transistors with lots of headroom: for example, even though I know that the NX138AKR/NX138BKR gate voltage varies between 0V and 12V (fan supply voltage), I picked one that can handle ±20V. It just makes my circuits more robust. (For example, if the fan supply was inverted, nothing except the fan itself would be damaged, because the -12V on the gate of NX138AKR/BKR does nothing to the transistor, and it does not 'leak' across the transistor enough to hurt the rest of my circuit.)
You see, another option for the outputs would be to pull them to the encoder supply voltage, but use a voltage divider – so a total of three resistors per output – to drop that down to 3.3V for the Teensy pins. The downside is that you then have three resistors in series from supply to ground, wasting power; so picking the "best" resistor values is a bit of a magic act.
I know it doesn't make any sense as the collector is still open but I wonder how it works completely fine with an arduino.
If the encoder outputs are true open collector, with no connection to the supply voltage –– and many, but not all, encoders are like this! –– then connecting the two phase outputs to Teensy,
and adding two say 10kOhm resistors from the outputs to 3.3V, would make it just as robust as it would be with my circuit above.
Thing is, we don't know if the encoder outputs are open collector or not.
Connecting a multimeter in voltage or continuity mode between one output and the encoder supply voltage, when the encoder is powered, and then very slowly rotating the encoder, would tell if it is not: If the voltage does not stay at encoder ground, then it is not open collector output. Both outputs need to be tested like this.
I would also repeat the test with the multimeter in ohms mode, and check if the resistance (between encoder output and encoder supply) ever drops below a megaohm (although "overload", 0L, is even better, as it indicates no connection at all the multimeter can detect). If the resistance between one of the outputs and the encoder supply voltage is ever less than a megaohm or so, it is not an open collector output.
Finally, connecting the multimeter between one output and encoder ground, and slowly rotating the encoder, should show a good connection about half the time. There is a voltage drop, so it is not purely resistive, and the resistance can be significant even then (hundreds of ohms, perhaps a couple of kilo-ohms). This would be the phototransistor collector-emitter connection, and should be "not connected" half the time, "connected" half the time, with the "connected" state looking like it was a diode and a resistor.