Monday, January 27, 2025

Update: GPIO Protection

Followers of this blog may have noticed that I confess my ignorance about circuit design, and especially solid state circuits. I understand enough to build, test and repair well-designed circuits but without deep understanding or the ability to design my own. There is a wide gulf of expertise between design and repair, be it transceivers, cars or any technology product. I can design software, antennas and other technology devices, but not electronics.

In that light I will admit that I spectacularly failed at a circuit design attempt last year. I needed a way to protect the GPIO pins of an Arduino microcomputer since an out-of-range voltage will destroy it. Something is needed between the voltage source and the Arduino to reliably protect it from damage, and it must do so without disturbing the linear signal.

If you look at that earlier article you will see that I made the effort to characterize the linearity and under- and over-voltage clamping of the circuit. It's pretty simple, a diode to block negative voltage and a Zener diode to shunt current to ground when the voltage is too high. Within its operating range it is linear.

Despite all that, it does not work in its intended application. The application is the digital prop pitch motor controller that I designed and is under construction. You can see from the article date that it's been under construction for a long time. I set it aside when I ran into difficulties with the combined circuit of the op-amp differential amplifier and the GPIO protection circuit. Each works well in isolation but not when connected together.

I was reluctant to pick it up again until recently. For months it seemed to taunt me, demanding my attention every time I looked at it. Finally I accepted the challenge and snipped the connection between the circuits. Once again both halves of the circuit worked well: the op-amps linearly tracked the inputs and the protection circuit behaved the same as the prototype.

Those of you with greater knowledge can probably guess what I did wrong. For me it was a journey of exploration. The key was realizing that op amp linearity depended on the network of resistors surrounding it. The protection circuit disturbed that network so that the linear range became unacceptably narrow; that is, it was a non-constant load.

Diodes are not on-off devices. The transition zone across reverse and forward bias (zero crossing) is quite complex. Zener diodes add a further wrinkle in that their reverse polarity behaviour mimics a variable resistor that adjusts its value so that the current increases as the applied voltage rises. The result is a variable voltage divider with the tap point at the Zener's specified voltage (5.1 volts in my circuit). The load seen by the op-amp is therefore voltage dependent and that degrades its linearity.

The solution is obvious: a buffer amplifier. All I had to do was pick one from a long list of available options. These ranged from unity gain op amp to single transistor amplifiers. The criteria include:

  • Unity gain
  • Linear
  • Negligible loading of the differential amplifier
  • Reverse polarity protection (negative voltage)
  • Over-voltage protection (greater than 5 volts)

The last two are what my first protection circuit accomplished. I thought I might have to keep that circuit in addition to the buffer amplifier. That posed a problem because the tiny proto board (PCB) I selected has no more room for two of these circuits, and I really didn't want to redo the entire job.

The picture on the right shows the main PCB for the direction pot interfaces after the old Zener-based protection circuits were removed. The areas are at the upper left and right between the op amps and the 10 kΩ level control trimmers. The 2.2 μF capacitors smooth short duration direction pot glitches. 

There are several resistors and wires on the underside of PCB since there wasn't enough space on top. That said, I'll leave a description of the PCB and circuit for a future article where I discuss the completed controller. I hope that will not be too far in the future.

After hunting around the internet for alternatives I was attracted by an emitter follower circuit utilizing an NPN transistor. It has unity gain, is linear and has a high input impedance. Further, since the interior junction acts as a diode between base and emitter it offered the possibility of negative voltage protection. That would eliminate the diode. So far we've satisfied 4 of my 5 criteria.

That was enough to send me to my workshop to breadboard the circuit. I tapped into the PCB to carry the output from the 741 op amp to the breadboard so that I was using the actual circuit rather than a proxy that might not behave identically. The only other connections from the controller were ground and power for Vcc. The linear 10 kΩ 10:1 pot at the lower left is identical to those on the towers. It serves to emulate the tower pot.


Yes, it's messy! However, 90% of what you see on the breadboard is left over from a previous project and not used for this circuit; it was easier to leave them than clean the board. Messy though it was, it was good enough to test the circuit. The forward voltage drop across the internal transistor junction measured between 0.62 and 0.64 volts. That agrees with the approximate 0.63 volt drop in the circuit model (see below) within the DMM measurement uncertainty.

Now we need to address the fifth criterion: over-voltage protection. I could use the original Zener diode circuit, which would require two more parts per circuit. I wondered what would happen if I set Vcc to 5 volts. Well, what do I know, so I tried it. To my surprise it seemed to work. However, appearances can be deceiving, as I've learned to my dismay many times in the past -- my intuition about electronic circuitry is unreliable.


I downloaded and installed LTspice. Just as there are great benefits from modelling antennas there are similar benefits from modelling circuits. This was new for me. Luckily it was quite easy to set up the circuit needed to validate what I measured with my breadboard circuit. The full range of input voltage, from -15 to +15 volts (op amp power supplies), can be conveniently explored without risk.

Rather than design the complete circuit, I substituted a voltage source (V1) for the op amp output. In practice this works because the emitter follower has a high impedance input, much higher than the 10 kΩ (trim pot) loading the op amp. Measurements confirmed that. It was interesting that using a 5 volt regulated supply for Vcc (V2) limited the amplifier's output to 5 volts. However, there is a catch.

R2 is needed in the unlikely case that V1 soars toward the +15 volt rail and the tap on the trip pot is set close to the op amp output side of the pot. In this case, the model shows high current flowing between V1 and V2. That destroys linearity and is likely to damage the op amp and Q1 due to power dissipation. Setting R2 to 10 kΩ works well but I varied it in the model to better characterize the circuit. R1's value seems to work well and is typically shown in emitter followers utilizing a 2N2222A. A Darlington transistor (2 × NPN) might be more elegant but, as already said, a simple circuit fits better on the PCB. The final design uses a 3.3 kΩ resistor, as shown in the circuit above.

R3 represents the approximate analogue GPIO pin input resistance. Its value isn't critical since the Arduino GPIO input resistance is much higher than R1. A pull-up resistor should not be selected when declaring the GPIO pin as an analogue input: pinMode(pin, INPUT);

Once the circuit was designed, modelled and tested it was time to implement it; that is, fit the components for both direction indicators, hook them up to the Arduino and do a full system test. The picture shows the completed PCB. I ran out of pads for the transistor collector pins so I wired them above the board to a new connector for the 5 VDC power supply. The connector is for possible future use since there's already 5 VDC on the PCB for the LCD.

I began with a linearity test. That is more accurate than reading the ADC values in software (see below). The software algorithm on the Arduino calculates the antenna bearing from the GPIO pin's ADC output. Due to the different linear range the software must be updated.

Above is the complete circuit. There are two of them on the PCB, one for each rotator. Op amp gain is fixed so the gain level is set via a trim pot. Once again the Bourns 10 kΩ 10:1 pot emulates the direction pot on the tower. Power for the 741 differential amplifier is ±15 VDC. It is important to keep the tower pot near its centre so that the input voltages do not get close to the supply voltage and disturb op amp linearity.

The linearity test was a success: the voltage to the Arduino GPIO (blue) is linear and tracks the direction pot (green) in the range 1 to not quite 5 volts. I adjusted the direction pot in 0.5 volt steps from 0 to 5.5, measured with a DMM at the transistor emitter. I then made the other 4 measurements. The direction pot voltage ranged over a wide positive and negative range so I applied the noted arithmetic to ease visual comparisons.

The direction pot wiper voltage was measured with it in circuit  (yellow) and with no connection (N/C green) since op amp feedback alters the measured voltage. The green plot is the important one. I am not certain why the yellow plot wiggles as it does, but it is repeatable and not a measurement artifact. My guesses aren't worth mentioning. For completeness I included the voltage measured at the 741 output pin (red).

Over the linear output range, the measured Vbe across the 2N2222A varies between 0.66 and 0.69 volts. That is slightly higher than the 0.63 to 0.65 volt range in the LTspice simulation. Likely reasons include device variation and DMM counting error in the least significant digit. It isn't relevant to the application since the response is linear.

The 360° rotation range will be set to use the most linear part of the curve from 1.5 to 4.5 volts. Small excursions outside the range will be supported since the prop pitch motor has no stops and it is occasionally useful on the air. The centering pot sets the north (0°) on the display (3 volts on the GPIO ADC input). It can be easily adjusted if the tower pot slips. The level pot is set to accommodate the ratio of mast to pot rotation. In my case, one is 1:1 and the other is a little over 2:1.

The next step is to complete the Arduino software to drive the direction display. Sampling, smoothing and glitch algorithms are needed. For example, the ADC output (values from 0 to 1023) regularly vary by a count while the direction pot is static. Flickering numbers on a digital bearing display is annoying.

After that I move the tower pots to the new controller and use it instead of the old breadboard op amp circuits that drive a panel meter. The final step, a big one, is relay activation of the motor power supply using direction controls buttons. With luck I'll have it completed before spring. It isn't difficult work but I have other projects and obligations and this project isn't the highest priority.

No comments:

Post a Comment

All comments are moderated, and should appear within one day of submission.