Info page · no simulation · Arduino and electronics
Transistor and Capacitor Basics for Arduino Builders
Learn how a transistor switches a load that an Arduino pin cannot drive, and how a resistor and capacitor make a time delay, with two calculators.
This topic cannot be run safely or meaningfully in the simulator, so this page explains it with diagrams, calculations and sample code instead. Everything here is original.
What you will learn
- How a transistor works as a switch
- How to choose the base resistor
- What the RC time constant means
In short
A transistor lets a small current at the base control a large current from collector to emitter. An Arduino pin can give only 20 mA, but a transistor lets it switch a 500 mA motor or buzzer. A capacitor stores charge. Through a resistor it charges in a curve, and the time is set by R times C.
Try this next
- Switch a 5 V relay or small motor with a 2N2222 or BC547.
- Build a delay circuit that keeps an LED on for 10 seconds.
- Use a 0.1 uF capacitor close to a sensor to remove noise.
Transistor as a switch
An NPN transistor (for example BC547 or 2N2222) has three legs: base, collector and emitter. With the emitter to GND, the load between the supply and the collector, and a resistor from the Arduino pin to the base, the load turns on when the pin is HIGH.
- Base current needed = load current / gain (hFE). Use ten times more so the transistor saturates (is fully ON) and stays cool.
- Base resistor = (control voltage - 0.7 V) / base current.
- Example: 100 mA load, gain 100, 5 V pin: base current 1 mA x 10 = 10 mA, so Rb = (5 - 0.7) / 0.010 = 430 ohm. Use 330 ohm.
- Always put a diode (1N4007) across a relay or motor coil, with the stripe towards the supply, to absorb the voltage spike when it switches off.
- For loads above 500 mA use a logic-level MOSFET (for example IRLZ44N or IRLB8721). It needs only a 1 k resistor in series and a 10 k pull-down resistor from the gate to GND.
| Part | Typical use |
|---|---|
| BC547 / 2N2222 (NPN) | 100 to 800 mA, hFE 100 to 300 |
| TIP120 (Darlington) | Up to 3 A, voltage loss about 1 V, runs warm |
| IRLZ44N (MOSFET) | Up to 30 A with a heat sink, logic level |
Capacitor and the RC time constant
When a capacitor charges through a resistor, the voltage rises as a curve. After 1 RC it has reached 63 percent of the supply. After 5 RC it is practically full.
- Time constant: tau = R x C (ohm x farad = seconds). 10 kilo-ohm and 100 microfarad make 1 second.
- Energy stored = 1/2 x C x V x V.
- Decoupling: a 0.1 uF ceramic capacitor close to each IC removes fast noise from the supply.
- Smoothing: a large electrolytic (470 uF to 2200 uF) after a rectifier holds the voltage between the peaks.
- Timing: a resistor and a capacitor give a simple delay for a button debounce (10 k and 100 nF gives about 5 ms).
- Polarity: electrolytic capacitors have a minus stripe. If you reverse them or exceed their voltage they can burst.
Try the calculators
Transistor switch: base resistor
Capacitor: charging time
Do and do not
- Do use a base resistor every time. A transistor with no base resistor draws a large current from the pin and may destroy it.
- Do join the Arduino GND and the load supply GND.
- Do choose a capacitor with a working voltage at least 1.5 times the circuit voltage.
- Do not connect a motor or relay straight to an Arduino pin.
- Do not charge or discharge a big capacitor through a short wire: the spark and current are large.