Saturday, April 4, 2015

Switching devices off of different supplies, flyback, and realities of the electron

The microcontroller can only drive about 5mA per pin; however, most non-digital circuits take much more power. Let's say you have a controller that runs off of 3 volts and you want to switch something that requires 5V or 12V. How is this done? The easiest way in a shared GND system is to use a NPN transistor so that when the input line goes "high", the NPN pulls hard to low.
Consider the following schematic:


For this example, let us assume that the power supply is 12v, and you have a BJT that you know very little about except that it's a NPN. The first order of business is to protect the MCU. You want to be sure to not zap your MCU. So we will assign our first value:
R0 1kOhm

R0 will limit the current drawn by the BJT to MCU Vdd/R. If you decrease R, the driving strength of the BJT increases (until saturation); however, you risk over driving your MCU output pin. If you need more driving strength use a Darlington Transistor or a motor driver IC.

R1 and LED0 exist purely to signal that your circuit works. You do not need these in reality. A good value for R1 would also be 1kOhm, and be sure that your LED is connected in the right direction

R2, LED1 and IND0 are where things get interesting. Firstly, let's talk about IND0, the inductor. A relay and a motor are basically inductors, and inductors resist a change in current, ie: they store current in their field. This means that if you change the state of an inductor, there will be a change in voltage based upon the internal resistance of the coil. This is where R2 and LED1 come in. R2 should be about 10ohms, only to increase the resistance for this experiment. R2 should be 0ohms otherwise. LED2 responds as something called a "flyback" diode, so that when the inductor changes state from on to off, it does not cause a voltage spike back into VDD. You dissipate this voltage into LED2 and you can see it flash for an instant. The larger the inductor, the larger this flyback voltage that is produced when the inductor is switched off. This flyback current is the number one cause of stress in college lab classes because it will cause something called "latchup" if the spike makes it back to the MCU.

No comments:

Post a Comment