Issue using a 5V encoder with Teensy 4.1

Nish

Member
I just recently began using Teensy, and I'm happy to say that I'm a Teensy 4.1 user. However, I am having a lot of problems, which I am gradually resolving with the assistance of the knowledgeable experts present in the forum. All of you are appreciated.
In order to explore the terrain, I'm currently building a skid steer rover that interfaces with ROS. The system uses the Odometry information to locate my robot. For each side of my rover, I therefore initially intended to use two inexpensive Rotary Incremental Encoders with a resolution of 600 PPR.
I attempted to use an Arduino, but there were many restrictions, such as its clock speed, interrupt pins, RAM and many more I'm giving Teensy 4.1 a try. I'm testing out using a Teensy to control a motor like in the image below.
I first tried publishing my data and interacting with Arduino. Given how computationally intensive my code was, it didn't work all that well. The encoder has a 5 to 24-volt rating. Motors can run at a maximum speed of 24 RPM, which is not very fast. I discovered that the teensy's pinout can withstand 3.3 volts. I tried to connect a TXS0108E Level Shifter that I had purchased, but I was unsuccessful because there were no tutorials available specifically for encoders. I want to know if I can use a 5V encoder on a teensy (specifically the 4.1 board).
Also, if I use a logic level shifter, will I miss any pulses between them?

Thank you very much, everyone. I would appreciate all your help.
 

Attachments

  • Issue.jpg
    Issue.jpg
    98.6 KB · Views: 103
How to connect the outputs of that encoder to a Teensy, depends on what kind of outputs the encoder has, e.g. open-collector or push-pull?
See this thread for more info.
Do you happen to have a partnumber of the encoder on the photo?

Paul
 
Can you provide a link to the datasheet for your specific encoders? Most encoders are open-collector specifically to make it easy to use, which may explain your troubles with the level shifter.
 
https://forum.pjrc.com/threads/70595-5v-encoder-(600PPR)-for-JOGWHEEL
I think this thread should work. The part number can be seen from the attachment in this message (c38s6g5-600b-g24n). Since, its not produced a very renowned manufacturer, I dont know if the datasheet is accurate. However I have attached the one I found online. You can take a look at that as well.
I need some help setting up my level shifter too. I dont know if the level shifter I bought (TXS0108E) should work in my case. Let me know if I need to buy another level shifter of a similar type or a different one.
Encoder Closeup.jpg
Amazon Link for the Encoder: https://a.co/d/31UQkXz
Datasheet: https://drive.google.com/file/d/1NpaKxo0ttTe46B03Aze6b3us8Ulb8psq/view
 

Attachments

  • Photoelectric Incremental Rotary Encoder.pdf
    318.4 KB · Views: 94
I'm 99% sure that this encoder will work when hooked up as indicated in that thread. No need for a level-shifter, just use two 1K pullup resistors to 3V3 and the two pins on the Teensy 4.1 you are planning to use.

Paul
 
Before you connect the wires to Teensy, use a voltmeter to test. If you just turn the encoder manually, both signals should have a 50-50 chance of ending up high or low when the motion stops. In the 25% chance they both are low, just turn again until either is logic high. The voltmeter can confirm whether the logic high signal really is 5V or 3.3V with the way you've connected the resistors.
 
I recently got involved with a surprisingly similar issue, namely interfacing 12V PWM-controlled 4-pin fans, where (Intel 4-pin PWM fan spec) the PWM is an open collector output, and the tachometer signal from the fan (two pulses per rotation) is open collector output, but requiring a pull-up to 12V.

Assuming the encoder output is an open collector one, it means we need to add a suitable pull-up resistor to the specified voltage level; here, anything between 5V and 20V. (The 4-pin 12V PC fans' tachometer output requires a pull-up to 12V; so we're talking about a very similar situation here as what I've investigated recently.)

Because Teensy 4.x cannot handle such voltages in its I/O pins, we add that single MOSFET as a level shifter. You could also use an unidirectional level shifter here, as long as its input side can handle the voltage you pulled up to with the resistor, and the output side the 3.3V voltage level as needed by Teensy. (I didn't found any suitable for 12V, so I had to use a MOSFET here.)
The N-channel MOSFET gate is connected to the encoder output and pull-up resistor, and source is connected to Teensy ground.
This essentially inverts the signal, and leaves us with an open collector output, but if we choose the MOSFET correctly, we can just pull it to the 3.3V Teensy wants, and it'll work perfectly.

So, we need a small N-channel MOSFET, that can handle the target voltage levels between drain and source, and gate and source. I needed Vds of 15V and Vgs of ±15V, or more; your needs are somewhat lesser, since the encoder works down to 5V. Looking at what e.g. Mouser has available, and what hobbyists like myself can solder by hand, I arrived at Nexperia NXP NX138AKR or NX138BKR (the NX138AKR or NX138BKR variants are the SOT-23-3 packages). There are lots of others similarly suitable, but as NXP also provides Spice models I could adapt to use with KiCAD and check my circuits work, I chose these.

Because the gate has capacitance –– MOSFETs are voltage-controlled; i.e. you charge or discharge the gate to control them, much like a capacitor –– we often add an inrush current-limiting resistor between the signal and the MOSFET gate. This is because when we change the MOSFET state, there is only a quick (microseconds or less!) pulse of current (until the gate voltage reaches the given level); a resistor slows this down, smoothing out the spike, lowering the maximum current. This also slows down the MOSFET a bit, but my simulations indicate 1 kOhm is absolutely fine at 25 kHz (which is the desired PWM frequency for 4-pin fans). With an encoder, it is possible such a resistor is not needed at all, and in any case a much smaller value makes more sense anyway; perhaps 10 to 100 Ohms. (At 5 V and 100 Ohms, the spike will be on the order of microsecond or less, but stay below 50mA.)

Let's look at what I suggest, then:
encoder-to-teensy.png
A natural choice for the encoder voltage is obviously 5V. Because the encoder has two output signals, both need the above circuit; ie. four 10 kOhm resistors, two 100 Ohm resistors, and two aforementioned MOSFETs.
The resistor values aren't precise at all, though; they just determine the currents in this circuit, 10 kOhm resistors limiting the current to below 1mA.
A 600 PPR encoder rotating at 2500 RPM produces a 25 kHz pulse train. With just a 100 Ohm gate inrush current limiting resistor, simulation at 500 kHz worked just fine. That corresponds to 50,000 RPM of a 600 PPR encoder, and I don't think typical 600 PPR optical encoders can even survive such RPMs!

If one knew the exact encoder, i.e. had its datasheet, it would be pretty simple to implement the above in e.g. EasyEDA, and manufacture at say JLCPCB or PCBWay. The parts for each two-channel/one-encoder board cost perhaps USD $1 (fraction if anyone were to produce a large batch of them), and are definitely hand-solderable.
_ _ _ _ _

Another option would be to use just the 10k pull-up to 5V, and then an unidirectional level shifter like TXU0102. Connect OE and VCCB to Teensy 3.3V, B1Y and B2Y to suitable Teensy pins, VCCA to 5V, and encoder outputs to A1 and A2. (This ensures the outputs are not enabled unless the Teensy 3.3V is present.) The pull-ups are between A1 and 5V, and A2 and 5V. Fewer parts, but at least the same price as the component version. Since this is only available in surface-mount versions, with TXU0102QDCURQ1 and TXU0102DCUR in VSSOP-8, you could use VSSOP-8 to DIP adapters, and solder the two pull-up resistors on the other side. Making your own VSSOP board or TXU0102 DCUR package with footprints for the pull-up resistors and OE already connected to VCCB in EasyEDA or KiCAD would be quite easy, too, if the encoder datasheet is available.
 
Not all encoders will work with open-drain/open-collector outputs to pulled to 3.3V, if they're specified to work with 5-20V.

In the simplest optical encoder designs, the outputs are from phototransistors with emitters grounded and collectors directly as outputs. These will work down to saturation voltage (on the order of half a volt).
Other circuits are possible, especially if they have filtering etc., and such circuits may require the output to be pulled to specific voltage ranges.

Only the datasheet will tell. My circuit above is the safe way, assuming open-collector encoder outputs. For push-pull encoder outputs, one only omits the pull-ups from the encoder outputs; then it is again safe.
 
Not all encoders will work with open-drain/open-collector outputs to pulled to 3.3V, if they're specified to work with 5-20V.

In the simplest optical encoder designs, the outputs are from phototransistors with emitters grounded and collectors directly as outputs. These will work down to saturation voltage (on the order of half a volt).
Other circuits are possible, especially if they have filtering etc., and such circuits may require the output to be pulled to specific voltage ranges.

Only the datasheet will tell. My circuit above is the safe way, assuming open-collector encoder outputs. For push-pull encoder outputs, one only omits the pull-ups from the encoder outputs; then it is again safe.

Hi,
I took a look at the working principle of the entire thing we discussed yesterday. However I don't understand one thing.

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.

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.

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.

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?)

My question is why shouldn't we plug in directly the phase signal into io pins of teensy? 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 I'm completely wrong, please let me know what's wrong with my understanding so that It will benefit speeding up my project.
 
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.
 
Last edited:
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.


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.


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.


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.


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 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.


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.

Thank you for the insight.
I actually forgot to mention something. I used another Teensy 4.1 and I just connected my encoder outputs directly to the interrupt pins of the Teensy as I was not really aware of this before. I did that because it seemed to work well with Arduino. I ended up burning the board I guess as I noticed the board just started heating up (LED Pin heating up) and it was not recognized by my IDE. I assumed that the pulses were 5v as the encoder was powered by 5v and gnd. This is the only contradiction I am still unclear about. Did my teensy stop working because of this or it stopped due to any other connection error? I know you really cannot comment about it but still I was wondering how it burnt the board when the pulses were less than 1v in all my 15 readings I tried to take with a multimeter.
 
5V can destroy Teensy 4's (either immediately or by cooking in CMOS latch-up). But that encoder says its NPN open-collector and you only measured 1V max on the output... Bit of a mystery.

Did you double check all the connections before powering up?

If I wasn't sure I'd have added 1k in series with the encoder outputs to limit the current, as that may spare the input protection diodes and will prevent latch-up.
 
the board when the pulses were less than 1v in all my 15 readings I tried to take with a multimeter.

Sounds like something is clearly wrong with your hardware. The signal should be close to 0 volts when low, and something like 3.3V or 5V when logic high. Getting 1V is clearly not right. Maybe it is damaged or defective. Maybe it is connected incorrectly, either by a mistake or misunderstanding. Or perhaps both, maybe even damaged by a prior mistake and now not working when connected correctly?

Please understand diagnosing hardware problems remotely over the internet is difficult at best. But so far, this conversation is far from a best case. We can't see how you've actually connected the parts (eg, photos or clear diagrams). Written descriptions of what's been tried and how results were measured lack detail. So far most of the answers written here are detailed descriptions about how to use encoders, rather than directly troubleshooting your specific problem, because we just don't have much specific info. We're all trying to help, but given so little to go on, this is the best anyone can do.

I would highly recommend showing photos. Try to capture multiple angles, with a couple wide shots so we can see the whole system with all wires, and close ups from a few angles so we can see how the wires really connect. Show all the details, without over-thinking whether they are important or mundane. Often the real problem is something you've overlooked or not anticipated. The better you can show us everything, the higher the chance you'll end up capturing in a photo some key detail you may have overlooked... which we might be able to notice.

Over and over on this forum we've helped solve electronics wiring problems. Misunderstandings are usually the hardest without photos, because if you explain what you've done with only words, usually those words will sound correct. But if there is a misunderstanding about the meaning of those words, the wires and parts might be connected differently than everyone imagines based on their understanding. Only photos really help in the case of a misunderstanding.

So at this point, I really have 2 suggestions. #1 - Continue testing with only a voltmeter and don't risk destroying a Teensy until the signals are confirmed correct, and #2 - Pour your effort into trying to actually show us what you've really done and the results you're actually observing. We can help better when we actually see the problem.
 
For a good example, look at msg #6 from PaulS on that other thread.

His photo shows the encoder with part number visible. Some of the wire is outside the view, but we can clearly see the end of the encoder cable with 4 colored wires. We can see the solderless breadboard from a good angle where the place all 4 wires really connect is easily visible. Two resistor are present and the place they connect is easy to see. This is a good example of how to shoot a photo showing how a part really is connected. Now compare with the photo in msg #1 above, where we can't even see the end of the encoder cable or how any of the wires really connect.

Please let us help you. Shoot good photos like this example, so we can actually see how every wire really connects. If using a voltmeter, get it in the photo. If using power source other than USB, let us see what it really is in the photo. Details matter. Remember, the thing that is actually wrong may well be something you haven't anticipated or understood, so try to actually show us everything.

We'll try to help by bling guessing, but the help you can get here will be so much better if you actually show us the all the wires and let us actually see the results.
 
For a good example, look at msg #6 from PaulS on that other thread.

His photo shows the encoder with part number visible. Some of the wire is outside the view, but we can clearly see the end of the encoder cable with 4 colored wires. We can see the solderless breadboard from a good angle where the place all 4 wires really connect is easily visible. Two resistor are present and the place they connect is easy to see. This is a good example of how to shoot a photo showing how a part really is connected. Now compare with the photo in msg #1 above, where we can't even see the end of the encoder cable or how any of the wires really connect.

Please let us help you. Shoot good photos like this example, so we can actually see how every wire really connects. If using a voltmeter, get it in the photo. If using power source other than USB, let us see what it really is in the photo. Details matter. Remember, the thing that is actually wrong may well be something you haven't anticipated or understood, so try to actually show us everything.

We'll try to help by bling guessing, but the help you can get here will be so much better if you actually show us the all the wires and let us actually see the results.

Connection:
Connection.jpeg

5v Pullup:
5V Pullup.jpg

3.3V Pullup:
3V3 Pullup.jpg

No Pullup:
No Pullup.jpg

These experiments completely make sense now. The problem was due to the current that sunk into my gpio pins. The current measured with no pullup was the highest which was around 15mA in 15 readings and with a 1k Pullup resistors it did not go above 5mA. Please let me know if you want to know any additional details. If the above data is okay, I will proceed to connect my teensy and start my project with a 1k Pullup resistors. I was wondering if I use a 10k Pullup resistor, would that delay my signal or miss some pulses by any chance?
 
If I checked your picture right, you have a 38S6G5-B-G24N optical rotary encoder.
You should have mentioned the model number in your original post, because that would have saved everyone a lot of confusion.

38S6G5-B-G24N are optical rotary encoders made by various Chinese manufacturers, but they all do have open collector outputs.
With such an encoder, you only need a pull-up resistor from each encoder output to 3.3V. There is no need to use my circuit with these at all.
Note that if you had say 38S6G5-B-G24X encoder instead, then the situation would be different!

These experiments completely make sense now. The problem was due to the current that sunk into my gpio pins. The current measured with no pullup was the highest which was around 15mA in 15 readings and with a 1k Pullup resistors it did not go above 5mA.
Yes. I suspect it was because the voltage was neither decisively "low" nor decisively "high", and floated somewhere in between. I don't know exactly what kind of circuit NXP used in i.MX RT1062 as an input, though, so it is just a guess.

If the above data is okay
If your encoder says 38S6G5-B-G24N on the side, then yes, you only need a pull-up resistor to 3.3V per output, and the circuit I showed would not bring any additional benefits.

If it says something else, let us know, and we'll find out together.

I was wondering if I use a 10k Pullup resistor, would that delay my signal or miss some pulses by any chance?
The pull-up resistor determines the current needed to pull the signal down; the "strength".
To me, 10k is just my chosen starting point, not any kind of rule of thumb or anything.

1k pullup to 3.3V means the current needed is 3.3V/1000Ohm = 3.3mA
2.2k pullup to 3.3V means the current needed is 3.3V/2200Ohm = 1.5mA
4.7k pullup to 3.3V means the current needed is 3.3V/4700Ohm = 0.7mA
10k pullup to 3.3V means the current needed is 3.3V/10000Ohm = 0.33mA

Note that it is not the Teensy pin that sinks or sources the current: the source is the 3.3V line, and the sink is the encoder NPN transistor; and for that, 3.3mA is one tenth of the maximum specified, and thus a good choice. Teensy input pin is only supposed to look at the voltage; so make sure you set the pin as an input setPin(pin,INPUT) and not as an input-with-pullup or input-with-pulldown.

If you have the Teensy and the Encoder connected with very short wires inside a metal enclosure, all of the above pull-up resistor value examples will work.

If you have longer wires, and/or electrically noisy environment (say, a bit of capacitive coupling, some electromagnetic interference from a motor), then the 10k pullup may be too weak and "overcome" by the noise.

If the signal rate is very high, a too-weak pull-up resistor can slow down the edges (because the current limits the rate of change in the voltage). So, for higher-frequency signals, you also want a bit stronger (lower resistance) pull-ups. For example, for I2C, a 2.2k pull-up tends to work much better than 4.7k or 10k (the I2C SDA and SCK signals can be considered an "open collector" signal, because host and devices only ever pull them down like open collector outputs, and pull-up resistors are needed to keep it normally at the logic high level).

In your case, you have max. 24 RPM with 600 pulses per revolution, which is 24×600/60 = 240 pulses per second maximum, or a pulse train at a maximum of 240 Hz. The pull-up resistor effect on edges (change rate) does not really show up until tens of thousands of pulses per second, or tens of kHz (fast I2C is 400 kHz), so you don't need to worry about that.

However, you do have an electrical motor nearby, and you probably have the signal wires "flying about" across your robot electronics to reach Teensy, so to be safe, I'd use stronger pull-ups than 10k, for better noise immunity. Thinking about this, if I were you, I would use 1 kOhm = 1000 Ohm pull-up resistors.

The only downside in using a stronger pull-up (lower resistance) for an open collector output is the little bit of "waste" current turned to heat in the NPN transistor; in the encoder in this case. However, because the encoder maximum recommended current is ten times larger (35 mA), the 3.3mA current set by the 1k pull-up resistor is absolutely fine.
 
If I checked your picture right, you have a 38S6G5-B-G24N optical rotary encoder.
You should have mentioned the model number in your original post, because that would have saved everyone a lot of confusion.

38S6G5-B-G24N are optical rotary encoders made by various Chinese manufacturers, but they all do have open collector outputs.
With such an encoder, you only need a pull-up resistor from each encoder output to 3.3V. There is no need to use my circuit with these at all.
Note that if you had say 38S6G5-B-G24X encoder instead, then the situation would be different!


Yes. I suspect it was because the voltage was neither decisively "low" nor decisively "high", and floated somewhere in between. I don't know exactly what kind of circuit NXP used in i.MX RT1062 as an input, though, so it is just a guess.


If your encoder says 38S6G5-B-G24N on the side, then yes, you only need a pull-up resistor to 3.3V per output, and the circuit I showed would not bring any additional benefits.

If it says something else, let us know, and we'll find out together.


The pull-up resistor determines the current needed to pull the signal down; the "strength".
To me, 10k is just my chosen starting point, not any kind of rule of thumb or anything.

1k pullup to 3.3V means the current needed is 3.3V/1000Ohm = 3.3mA
2.2k pullup to 3.3V means the current needed is 3.3V/2200Ohm = 1.5mA
4.7k pullup to 3.3V means the current needed is 3.3V/4700Ohm = 0.7mA
10k pullup to 3.3V means the current needed is 3.3V/10000Ohm = 0.33mA

Note that it is not the Teensy pin that sinks or sources the current: the source is the 3.3V line, and the sink is the encoder NPN transistor; and for that, 3.3mA is one tenth of the maximum specified, and thus a good choice. Teensy input pin is only supposed to look at the voltage; so make sure you set the pin as an input setPin(pin,INPUT) and not as an input-with-pullup or input-with-pulldown.

If you have the Teensy and the Encoder connected with very short wires inside a metal enclosure, all of the above pull-up resistor value examples will work.

If you have longer wires, and/or electrically noisy environment (say, a bit of capacitive coupling, some electromagnetic interference from a motor), then the 10k pullup may be too weak and "overcome" by the noise.

If the signal rate is very high, a too-weak pull-up resistor can slow down the edges (because the current limits the rate of change in the voltage). So, for higher-frequency signals, you also want a bit stronger (lower resistance) pull-ups. For example, for I2C, a 2.2k pull-up tends to work much better than 4.7k or 10k (the I2C SDA and SCK signals can be considered an "open collector" signal, because host and devices only ever pull them down like open collector outputs, and pull-up resistors are needed to keep it normally at the logic high level).

In your case, you have max. 24 RPM with 600 pulses per revolution, which is 24×600/60 = 240 pulses per second maximum, or a pulse train at a maximum of 240 Hz. The pull-up resistor effect on edges (change rate) does not really show up until tens of thousands of pulses per second, or tens of kHz (fast I2C is 400 kHz), so you don't need to worry about that.

However, you do have an electrical motor nearby, and you probably have the signal wires "flying about" across your robot electronics to reach Teensy, so to be safe, I'd use stronger pull-ups than 10k, for better noise immunity. Thinking about this, if I were you, I would use 1 kOhm = 1000 Ohm pull-up resistors.

The only downside in using a stronger pull-up (lower resistance) for an open collector output is the little bit of "waste" current turned to heat in the NPN transistor; in the encoder in this case. However, because the encoder maximum recommended current is ten times larger (35 mA), the 3.3mA current set by the 1k pull-up resistor is absolutely fine.

Great insight, thanks. I'll proceed with this tonight and let you know how my control tuning goes. I hope I don't burn my teensy this time.
I actually was new to the forum and haven't asked any kind of questions before. I remember that I mentioned the model number in my second reply. However I'm happy that I understood every single concept that was introduced in this discussion.
Thanks once again.
 
Back
Top