electrical_engineering:transistors_as_switches
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| electrical_engineering:transistors_as_switches [2026/01/09 11:01] – removed - external edit (Unknown date) 127.0.0.1 | electrical_engineering:transistors_as_switches [2026/01/12 11:26] (current) – [Further information] dbui | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Transistors as Switches ====== | ||
| + | |||
| + | {{:: | ||
| + | |||
| + | When we talk about " | ||
| + | |||
| + | {{:: | ||
| + | |||
| + | You can imagine the transistor like a switch. When the switch is pressed, the current flows. If the switch is released, the current no longer flows. The transistor has three connections: | ||
| + | |||
| + | Here is a drawing from the __datasheet__ of the TIP 122, which shows where base, emitter and collector pins can be found. | ||
| + | |||
| + | {{:: | ||
| + | |||
| + | The following diagram visualizes the current flow (I/red) and the voltage flow (U/blue) which are applied to the transistor. Important: A base resistor (RB) and a load are always required for correct operation. | ||
| + | |||
| + | {{:: | ||
| + | |||
| + | UCE = Collektor-Emitter-Voltage\\ UE = Driving Voltage\\ UBE = Base-Emitter-Voltaege (Threshold value)\\ IC = Collector Current \\ IB = Base current\\ RB = Base resistor | ||
| + | |||
| + | ==== The Transistor as a Switch ==== | ||
| + | |||
| + | A common use of a transistor is in the switching of higher loads - since most microcontrollers are limited in their output power on digital pins. For example, the Arduino can switch a load of 5V at 20mA per output. This is enough for LED's and maybe a small vibration motor. However, if a fan, a high power LED, a solenoid, a motor, etc. is connected you definitely need a transistor circuit. In this case, the transistor works similar to a relay: it switches higher loads with smaller loads. A distinction must also be made between switching non-inductive loads (e.g. LED) and inductive loads (e.g. fan, relay, motor). An inductive load will probably need a __Fly Back Diode__. | ||
| + | |||
| + | ==== Switching a non-inductive Load ==== | ||
| + | |||
| + | For switching a non-inductive load, the transistor circuit is very simple to set up. You only need an NPN transistor (TIP 122) and a series resistor in the path IB (1 kOhm). The circuit diagram looks as follows: | ||
| + | |||
| + | R1 = 1 kOhm (approximate value)\\ Transistor = TIP 122 | ||
| + | |||
| + | {{:: | ||
| + | |||
| + | ==== Example ==== | ||
| + | |||
| + | In this example, we want to light up the RGB LED strip and control its brightness. According to the labelling and datasheet, the strip needs 12V supply voltage - corresponding resistors are already present and soldered on the strip. To realize this circuit we need a 12V power supply, one transistor (TIP 122 NPN) per channel, one resistor (1kOhm) per channel and the Arduino. | ||
| + | |||
| + | The following setup is needed to control a single color (here red)... | ||
| + | |||
| + | {{:: | ||
| + | |||
| + | We program the Arduino with the following example code, which fades the red channel back and forth. | ||
| + | |||
| + | <code cpp> | ||
| + | #define RED_LED 10 // PWM PIN | ||
| + | void setup() | ||
| + | { | ||
| + | pinMode(RED_LED, | ||
| + | } | ||
| + | void loop() | ||
| + | { | ||
| + | for (int i = 0; i < 255; i++) | ||
| + | { | ||
| + | analogWrite(RED_LED, | ||
| + | delay(10); | ||
| + | } | ||
| + | for (int i = 255; i > 0; i--) | ||
| + | { | ||
| + | analogWrite(RED_LED, | ||
| + | delay(10); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Exercise 1 ===== | ||
| + | |||
| + | - Extend the previously developed circuit so that all three channels of the RGB LED strip can be controlled differently. | ||
| + | - Program a colour change that fades between red, green and blue (logic: off> | ||
| + | |||
| + | ++++ Solution Exercise 1.2 | | ||
| + | |||
| + | <code cpp> | ||
| + | const int RED_LED = 11; // PWM Pin | ||
| + | const int GRN_LED = 10; // PWM PIN | ||
| + | const int BLU_LED = 9; // PWM PIN | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | pinMode(RED_LED, | ||
| + | pinMode(GRN_LED, | ||
| + | pinMode(BLU_LED, | ||
| + | } | ||
| + | void loop() | ||
| + | { | ||
| + | for (int i = 0; i < 255; i++) | ||
| + | { | ||
| + | analogWrite(RED_LED, | ||
| + | delay(10); | ||
| + | } | ||
| + | for (int i = 255; i > 0; i--) | ||
| + | { | ||
| + | analogWrite(RED_LED, | ||
| + | analogWrite(GRN_LED, | ||
| + | delay(10); | ||
| + | } | ||
| + | for (int i = 255; i > 0; i--) | ||
| + | { | ||
| + | analogWrite(GRN_LED, | ||
| + | analogWrite(BLU_LED, | ||
| + | delay(10); | ||
| + | } | ||
| + | for (int i = 255; i > 0; i--) | ||
| + | { | ||
| + | analogWrite(BLU_LED, | ||
| + | delay(10); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ++++ | ||
| + | |||
| + | ===== Exercise 2 ===== | ||
| + | |||
| + | Develop the circuit as shown.\\ Add in a potentiometer and write new code to control the motor speed by rotating a potentiometer. | ||
| + | |||
| + | {{:: | ||
| + | |||
| + | The diode in the picture is called a **flyback diode** (also known as a freewheeling diode) and it's crucial when controlling motors or any inductive load with a transistor. | ||
| + | |||
| + | **Any time you switch a motor or relay with a transistor, add a flyback diode across it.** | ||
| + | |||
| + | {{:: | ||
| + | |||
| + | When you suddenly turn off a motor (by turning the transistor off), the current through the motor’s coil is interrupted. Because the coil stores energy in a magnetic field, that field collapses and generates a high voltage spike across the motor that flows back to the circuit and can potentially damage the Arduino. | ||
| + | |||
| + | The flyback diode is connected in reverse across the motor (cathode to positive, anode to ground). During normal operation, it doesn' | ||
| + | |||
| + | You can see the diode placed across the motor terminals with a specific orientation - the stripe (cathode) points toward the positive voltage supply. This is critical, if you put it backward, it will short circuit your power supply when the motor is on. | ||
| + | |||
| + | ===== Further information ===== | ||
| + | [[https:// | ||
| + | |||
