# Encoder level-shifting

#### cmartinez

Joined Jan 17, 2007
7,062
I want to convert an optical encoder's signal from a 5V to a 12V output. This is because the wire connecting the encoder to the MCU is a very long one (about 20m long) and I do not trust a clean 5V signal traveling that far without being affected by important voltage drop and ambient noise (EMI, for instance, even though I'll be using shielded cable).

So I simulated a simple, single channel, 2 transistor level shifter working at 60 kHz in LTSpice and the results seem encouraging. What I did was add an inductor between the level shifter's output and the voltage divider that I'll be placing at the PCB to connect it to my MCU, so as to simulate those 20m of #24 wire.

What I did was use the following pages to calculate the wire's inductance and resistance:

There are a couple of fields that I don't understand in LTSpice's inductor model, and those are the parallel resistance, and the parallel capacitance. How can I calculate those values?

#### Attachments

• 2.5 KB Views: 1

#### GopherT

Joined Nov 23, 2012
8,012
@cmartinez

Voltage drop is from current x resistance. The input of the microcontroller is high-impedance and current flow is nil, therefore, voltage drop is nil. I think you are making this into a big problem before checking that it is a big problem. it is a simple encoder, Add a Schmitt trigger (or adjust hysteresis on your micro if available) if necessary to make sure you don't get a second pulse from a reflection but I doubt your signal or microcontroller is fast enough to catch a reflection. I don't see a problem if you are using good quality (and shielded) cable. You have a scope and an encoder, check how 20m of cable behaves with a 5 volt digital signal.

Finally, assume you are using 30 gauge wire at 0.338 ohms/meter, at 20 meters you have 7.7 ohms of resistance. Double that for round trip and you have 15 ohms. A 10 mA of current flow yields a voltage drop of 0.15 volt. Hopefully your micro wont catch that. Even if you are worried about other losses, use a coaxial cable loss calculator to see how much trouble you will have. Here is one that starts at 5MHz (way above an encoder) and shows less than 0.5dB attenuation for all common coaxial cables at 20 meters.
http://www.net-comber.com/cable-loss.html

#### cmartinez

Joined Jan 17, 2007
7,062
Ok... I've just realized that I needed to add a second and third inductor to simulate the ground and power wires traveling to the encoder. And the result is a big mess at the enconder's output channel! Funny enough, the signal arriving at the MCU is unaffected. And to make things a little more realistic, I've also added an LM7805 powering the encoder, with appropriate input and output decoupling caps.

#### Attachments

• 4.1 KB Views: 1

#### cmartinez

Joined Jan 17, 2007
7,062
You have a scope and an encoder, check how 20m of cable behaves with a 5 volt digital signal.
I've done that already with a previous encoder, and I kept reading glitches (up to 10V) here and there on the MCU's side.
To be fair, I found that the glitches were almost eliminated when I added a 100K resistor from the encoder's output to ground. Maybe some sort of capacitive charges were building up in the cable, and the 100K resistor prevents that.

#### cmartinez

Joined Jan 17, 2007
7,062
Silly me... I've just measured the encoder's output with the probe referenced to ground on the encoder's side, and the glitches are gone

Gopher, maybe you're right.... maybe I'm complicating things here... I just wanted to play a little with this sim .... see what happens

#### GopherT

Joined Nov 23, 2012
8,012
Silly me... I've just measured the encoder's output with the probe referenced to ground on the encoder's side, and the glitches are gone

View attachment 104061

Gopher, maybe you're right.... maybe I'm complicating things here... I just wanted to play a little with this sim .... see what happens
Simulations is so much easier than disassembling and putting away all of those probes, components and wires on the work bench. Just hit "safe as..." or, my favorite, "close without saving?" - Ok!

#### OBW0549

Joined Mar 2, 2015
3,510
When it comes to sending moderate-speed digital signals over moderate distances in the face of significant noise, don't overlook the good old tried-and-true MAX232 (and its derivatives). Though it was designed as an RS-232 transceiver chip, it's good for all kinds of digital transmitting/receiving tasks where noise immunity is a desired quality.

#### cmartinez

Joined Jan 17, 2007
7,062
When it comes to sending moderate-speed digital signals over moderate distances in the face of significant noise, don't overlook the good old tried-and-true MAX232 (and its derivatives). Though it was designed as an RS-232 transceiver chip, it's good for all kinds of digital transmitting/receiving tasks where noise immunity is a desired quality.
right... I hadn't thought of that... in that case, I have a few single 5V supply MAX488 (RS485) laying around... maybe they're even better suited for the task than the MAX232? they can work at up to 250 kbps after all...

#### OBW0549

Joined Mar 2, 2015
3,510
right... I hadn't thought of that... in that case, I have a few single 5V supply MAX488 (RS485) laying around... maybe they're even better suited for the task than the MAX232? they can work at up to 250 kbps after all...
I've never used them, but it looks like they might be worth a try.

#### cmartinez

Joined Jan 17, 2007
7,062
Just out of curiosity, I put in specs for Q1 and Q2 as 2n3904 and 2n3906, and the result was a dramatic effect on the output's duty cycle!

#### cmartinez

Joined Jan 17, 2007
7,062
Alright, just for the heck of it, I decided to ditch the two-transistor level shifter approach, and use simple filtering techniques instead.
Here's the simple circuit, with C1 and C2 tuned according to a frequency of 60Khz

And here's what happened when I got rid of C1 and C2

Funny enough, the signal seems far cleaner if I use 10 nF for C1 and C2, but it almost looks as a sinusoid now... I wonder if it's useful as an input for my 8051 MCU, since it overshoots by almost 0.4V and undershoots by about -30mV