Amplified digital output

Theory

Microcontroller pins are usually not intended to switch large loads. Atmel microcontroller pins allow only 30 mA per pin, and on many controllers the number is even smaller (ARM controllers only a few mA). This means that a microcontroller pin can control only low-current devices (e.g., LEDs, ICs).

N-FET for switching a motor

To control higher-current devices, the output signal must be amplified. This is usually done with a transistor or an integrated circuit. When using a transistor circuit, ensure that the gain is sufficient and that maximum pin and transistor currents are not exceeded.

One of the simplest solutions is to use a field-effect transistor (MOSFET). A field-effect transistor is a semiconductor device where the conductivity of the current channel is controlled by an electric field. Unlike a bipolar transistor, it is voltage-driven. FETs are divided into P-channel and N-channel types (similar to PNP and NPN), abbreviated P-FET and N-FET.

A MOSFET typically has 3 pins: Drain(D), Source(S), Gate(G). The Drain pin is always on the side of the device being switched, the Source pin is on the supply side for a P-FET and on the GND side for an N-FET, and the Gate pin is used to control the MOSFET. To turn the MOSFET on or off, a voltage difference between G and S is needed. Thus an N-FET turns on when the gate is logic 1 (the minimum voltage depends on the MOSFET, typically 3-14 V), and a P-FET turns on when the gate is logic 0. To turn off, the MOSFET gate must be brought to the same potential as the source: 0 V for N-FET and supply voltage for P-FET.

Therefore, if you need to switch devices whose operating voltage is higher than the control electronics (e.g., the microcontroller) voltage, you should use an N-FET, or add another amplification stage between the switching circuit and the P-FET, otherwise the P-FET cannot be turned off properly.

Advantages of MOSFETs compared to bipolar transistors:

  • High input resistance - very low control current
  • Low on-resistance - low heating between S and D
  • Low temperature dependence - switched current does not depend on temperature
  • High switching frequency - fast enough for PWM signals
Flyback diode

Sometimes you need to switch devices that must (or should) be electrically fully isolated from the switching circuit. For example, when switching AC devices or very high-power devices. In such cases use a relay. Depending on the relay coil current, you must also amplify the microcontroller pin signal using a bipolar transistor or a MOSFET.

When switching inductive devices (e.g., DC motors, relay coils, etc.), you must also use a protection diode that can protect the switching circuit and microcontroller pin from reverse voltage when the inductive load is switched off. The negative voltage generated by the inductive coil can damage the switching element (transistor, MOSFET) or the microcontroller pin.

In addition to discrete components, transistors or MOSFETs are sometimes integrated into a single IC. One example is ULN2004, which contains 7 Darlington pair channels. Such chips are ideal when multiple channels must be controlled (e.g., a unipolar stepper motor), simplifying PCB design and reducing component count. These ICs often include flyback diodes.

Practice

Below is a simple code snippet for switching a motor on and off. The motor is connected to a unipolar stepper motor connector. One motor wire is connected to supply (Vbat) and the other goes through an ULN chip via pin Stp1.

//
// Amplified output test code
// DC motor is connected to a unipolar motor connector
// Motor first pin Vbat
// Motor second pin Stp1
//
#include <homelab/pin.h>
 
pin motor = PIN(J,3);
 
//
// Main program
//
int main(void)
{
	// Set LEDs and motor pins as outputs
	pin_setup_output(motor);
	pin_setup_output(led_green);
	pin_setup_output(led_red);
 
	// Green LED off, red on
	led_on(led_red);
	led_off(led_green);
 
	// Infinite loop
	while (true)
	{
		// If S1 is pressed, motor on
		if(button_read(S1))
		{
			pin_set(motor);
			led_on(led_green);
			led_off(led_red);
		}
		// If S3 is pressed, motor off
		if(button_read(S3))
		{
			pin_clear(motor);
			led_off(led_green);
			led_on(led_red);
		}
 
	}
}
en/examples/digi/amp_out.txt · Last modified: by 127.0.0.1
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0