1 Where Lab 4 Sits
In Lab 3 a GPIO pin drove an LED directly. A few milliamps, no moving parts, and the pin was the entire power supply. This week the load is a DC motor that wants hundreds of milliamps, stores energy in its windings, and generates its own voltage when it spins. The pin can no longer be the power supply; it can only give the command.
That separation is the central idea of this lab and of most real embedded hardware:
Separate the signal path from the power path. A GPIO pin decides when; a transistor and its own supply decide how much. The two paths meet only at the transistor's gate and at a shared ground.
You already have every idea you need. PWM (§8 of the Lecture 03 notes) sets the average power. The photodiode detector from Lab 1 becomes a tachometer in Task 4. What is new is the MOSFET in between, and the discipline of a shared ground.
1.1 Task map
| Lab 4 task | What you build | Concept & section |
|---|---|---|
| 1 | Generate a range of PWM signals on BCM 12; find the range over which the output is valid; capture scope screenshots | Software-PWM limits at extreme frequency and duty (§11) |
| 2 | Read the IRL510 datasheet, document the pinout, build the motor circuit on a +5 V bench supply, not the Pi | MOSFET as a switch; flyback diode; grounding (§7–10) |
| 3 | Move the gate drive from the bench supply to BCM 12 and vary the speed | Logic-level gate drive (§5.10) |
| 4 | Build a rotation-rate detector; tabulate duty vs RPM (≥ 10 points); plot; derive a formula; analyze accuracy | Optical tachometry, the RPM–duty relation (§12–13) |
2 From Silicon to a Transistor
Lab 4 hands you an IRL510 and expects you to know why it is the right part. That answer starts two steps back, with what a semiconductor is and what the older kind of transistor costs you.
2.1 Doping: making carriers on purpose
Silicon has four valence electrons and shares all four with its neighbors in the crystal. In a perfect crystal nothing is free to move, so pure (intrinsic) silicon is a poor conductor and a poor insulator at the same time. Thermal energy frees the occasional electron and leaves a hole where it used to be, and those two are the only carriers available.
Useful devices begin by spoiling that crystal deliberately:
| Dopant | Valence electrons | What it leaves behind | Result |
|---|---|---|---|
| Phosphorus | five | one spare electron per atom | n-type: majority carriers are electrons |
| Boron | three | one missing bond per atom | p-type: majority carriers are holes |
Both materials are still electrically neutral overall. What has changed is how many carriers are free to move, and of which sign. Everything else in this lecture is built by putting n-type and p-type regions next to each other in different arrangements.
2.2 The PN junction
Join a p region to an n region and electrons from the n side cross over and fill holes on the p side. That leaves a region with no free carriers at all, called the depletion layer, with a built-in potential barrier across it.

Forward bias pushes carriers toward the junction and collapses the barrier at roughly 0.7 V for silicon; current flows. Reverse bias pulls carriers away, so the depletion layer widens and essentially nothing flows. That is a diode, and you already used one as the photodiode in Lab 1.

The width of a depletion layer is set by the voltage across the junction. The JFET in §3.2 exists entirely to exploit that, and the flyback diode in §9.4 is the same device used as a one-way valve.
2.3 The BJT: two junctions, three regions
Stack the doping as n–p–n and you have a bipolar junction transistor: an emitter that is heavily doped, a base that is more lightly doped and physically thin, and a collector that is the most lightly doped of the three (and the largest, because it has to absorb the carriers and the heat). There are two junctions, and in normal operation the base–emitter junction is forward biased while the base–collector junction is reverse biased.
Carriers injected from the emitter into the thin base mostly fail to recombine there, and are swept onward into the collector. A small base current therefore permits a much larger collector current. “Bipolar” is literal: both electrons and holes take part. Counting the currents at the three terminals, IE = IC + IB.

2.4 What the BJT costs you
Equation (1) looks like a bargain until you notice what it demands. The base current is not a one-off charge; it flows for the entire time the device is on. And β is not a constant you can design around confidently: it varies between parts of the same type, and with temperature and collector current.
Suppose you wanted a BJT to carry 1 A to a motor, and the part offers β = 50 at that current. The base needs
continuously, from whatever drives it. A Raspberry Pi pin is rated at 16 mA, and about 50 mA across the whole header. One motor would consume more than the pin can give, and the pin would be supplying it for as long as the motor ran.
(a) A transistor has β = 120 at 200 mA and β = 40 at 2 A. What base current does each case need, and what does the change tell you about designing for a worst case?
(b) Why does the base of a BJT have to be physically thin? Answer in terms of what happens to carriers that linger there.
(c) Name the two junctions in an NPN transistor and state how each is biased in normal operation.
3 The Field Effect
The field-effect transistor takes a different route to the same goal. Rather than injecting carriers into a region, it uses the electric field from a nearby electrode to change how many carriers an existing path already has. Establishing a field takes charge; holding it takes none. That single fact is why a 3.3 V pin with a 16 mA budget can command amperes.
3.1 The family, and where the IRL510 sits
Every FET has the same three terminals. The channel is the path from drain to source; the gate sets how much of it conducts. Current flows drain to source, and the gate only ever sets the valve position.
| Type | Gate is | Modes available | Used for |
|---|---|---|---|
| JFET | a reverse-biased PN junction | depletion only | amplifiers, mixers, voltage-controlled resistors |
| MOSFET | metal over an insulating oxide | depletion or enhancement | switching, logic, power control |
Each of those is available as N-channel or P-channel. The IRL510 is an N-channel enhancement-mode MOSFET, and every word of that matters for how you wire it.

3.2 The JFET: normally on, squeezed shut
In a JFET the channel is physically present from the start, so the device conducts when the gate is at the same potential as the source. Reverse biasing the gate widens its depletion layer (§2.2) into the channel, narrowing the path until it pinches off entirely.

That gate is one side of a PN junction. Forward biasing it turns the control terminal into a conducting diode, and the device stops behaving like an amplifier at all.
3.3 The MOSFET: put an insulator under the gate
The MOSFET replaces that junction with a thin layer of silicon dioxide. The gate is now a metal plate over an insulator, which makes it one plate of a capacitor rather than one side of a diode. No DC path to the channel exists at all.

3.4 Enhancement and depletion
The channel in Figure 6 did not exist until the gate created it. A device that works that way is called enhancement mode: it is off with the gate at zero, and the gate voltage that just brings it on is the threshold voltage VGS(th). A depletion mode device is built with a channel already in place, and the gate is used to take it away. The right half of Figure 6 plots both, as shapes rather than datasheet values. What matters is where each curve leaves the horizontal axis: the enhancement device is off until the threshold, and the depletion device is already conducting with its gate at zero.
| VGS < 0 | VGS = 0 | VGS > 0 | |
|---|---|---|---|
| Depletion mode, n-channel | channel off | channel on | channel on |
| Enhancement mode, n-channel | channel off | channel off | channel on |
An enhancement device is normally off. If the gate wire falls out, or the Pi has not finished booting, the motor stays stopped. A depletion device would do the opposite, and a loose wire would start the motor. That is the whole argument, and it is also why §9.3 insists on a pull-down resistor: it guarantees the normally-off state even when nothing is driving the gate.
4 FET Against BJT
Both devices let a small signal control a large current. They charge very different prices for it, and the difference is exactly why Lab 4 specifies a MOSFET.
| BJT | FET | |
|---|---|---|
| Controlled by | base current | gate voltage |
| Control terminal draws | current, continuously | nothing, once the switching edge is over |
| Input impedance | moderate | extremely high: the gate sits on an insulator |
| Carriers involved | electrons and holes | one kind only, hence unipolar |
| Fully on looks like | a residual VCE(sat) ≈ 0.2 V | a resistance RDS(on) |
| Conduction loss at 1 A | ≈ 0.2 W, fixed by that voltage | I²RDS(on), and RDS(on) keeps falling with each part generation |
| Driving it from logic | a base resistor, and current for the whole on-time | connect the pin through a gate resistor |
| Typically destroyed by | heat and overcurrent | static discharge and gate over-voltage |
4.1 Why the comparison settles Lab 4
Line them up against the numbers from §8. A BJT carrying the motor current would want tens of milliamps of base current for as long as the motor ran, against a per-pin budget of 16 mA and a header budget of about 50 mA. A MOSFET wants a brief pulse of current to charge its gate capacitance at each switching edge, and nothing at all in between. The pin can afford the second and cannot afford the first.
The loss row matters too. A saturated BJT drops a roughly fixed voltage, so its dissipation rises in proportion to current and you cannot design it away. A MOSFET's loss is I²RDS(on), and RDS(on) is a number you can buy down by choosing a better part.
4.2 What you give up
The oxide that makes the gate free to hold is extremely thin, and it is the part that fails. Static discharge from a fingertip can puncture it, and so can exceeding the actual part's VGS(max). The Vishay IRL510 specifies ±10 V, so a generic ±20 V assumption is inappropriate. A MOSFET that has died usually died at the gate rather than the drain.
Practically: handle the part by its package rather than its leads, do not leave the gate floating (§9.3), and check VGS(max) before connecting anything other than a logic output to it.
(a) A BJT saturates at VCE(sat) = 0.25 V and a MOSFET has RDS(on) = 0.15 Ω. Compute the power each dissipates at 0.3 A and at 3 A. At which current does the MOSFET overtake the BJT, and why does that crossover exist at all?
(b) State two independent reasons a 16 mA GPIO pin cannot drive a BJT that is carrying 1 A.
(c) The gate draws no steady current, yet a gate resistor is still specified. What is it limiting, and when?
5 Inside an Enhancement N-Channel MOSFET
§3 placed the IRL510 in its family. This section opens it up and follows the gate voltage from zero to fully on, one state at a time, then reads the same story off the IRL510 datasheet.
5.1 The structure
Start from a slab of p-type silicon and diffuse two heavily doped n+ islands into its surface: one is the source, the other the drain. Between them, on top of the silicon, grow a thin layer of silicon dioxide and put the gate metal on it. The body, which is the p-type substrate, is wired to the source inside the package.
(A real power MOSFET such as the IRL510 is built vertically, with the drain on the back of the die, which is why the metal tab is the drain. The physics is the same, so these notes use the simpler flat picture.)
Nothing yet connects the islands. There is p-type silicon between them, and its mobile carriers are holes, which cannot carry current from one n+ island to the other.

5.2 Gate at 0 V: two diodes back to back
With the gate at 0 V, each n+ island forms a PN junction with the p substrate, so between drain and source there are two diodes back to back. Whatever the sign of VDS, one of them is reverse biased and blocks. The device is normally off.

One refinement for a discrete part like the IRL510. Because the body is tied to the source, the source junction is shorted out and only the drain–body junction is left. It blocks whenever the drain is positive, which is always the case in a low-side switch. If the drain were pulled below the source it would conduct. That is the body diode drawn inside the IRL510 symbol (§5.9), and it is why §9.4 notes that the body diode cannot absorb a positive drain spike.
5.3 A small gate voltage pushes the holes away
Put a small positive voltage on the gate. The gate and the silicon beneath the oxide form a capacitor, so positive charge on the gate metal must be matched by negative charge in the silicon. At first the silicon supplies that charge by losing holes: the gate field pushes them away from the surface and leaves behind the fixed, negatively charged boron ions they were neutralizing. This is a depletion layer, like the one in a PN junction, and like that one it contains no free carriers. The islands are still isolated.

5.4 Above threshold, an electron channel forms
Raise the gate further and the depletion layer stops growing. Instead, the gate starts to pull electrons (minority carriers in p-type silicon, but plentiful in the n+ islands next door) into a thin layer right at the surface under the oxide. Once there are more electrons than holes at the surface, that layer behaves as n-type silicon: the surface has been inverted. The inversion layer is an n-type channel joining source to drain, and electrons flow from source to drain along it. The gate voltage at which this happens is the threshold, VGS(th); for the IRL510 it is 1.0–2.0 V.

5.5 The gate is a capacitor
The whole mechanism runs on capacitor charge. The positive charge on the gate is balanced by an equal negative charge below the oxide, and above threshold that negative charge is channel electrons. More VGS means more gate charge, more electrons in the channel, and a lower channel resistance.

Holding a voltage costs no current; changing it does. Once the gate is charged, only the ≤ 100 nA leakage flows. Each switching edge moves a charge of at least CissV = 250 pF × 3.3 V ≈ 0.8 nC. The datasheet’s total gate charge, Qg ≤ 6.1 nC to 5 V, is larger, because it is measured while the drain voltage swings and the gate–drain capacitance has to be charged as well. Even the larger figure averages to only Qgf = 6.1 nC × 1 kHz ≈ 6 µA at 1 kHz PWM. The peak current at each edge is a different matter (§9.3).
5.6 Threshold is where it starts, not where it is fully on
The datasheet defines VGS(th) = 1.0–2.0 V at a drain current of only 250 µA. That is the gate voltage at which conduction becomes measurable, not the voltage at which the device is a good switch. Above threshold the current rises steeply. The typical transfer curve (datasheet Fig. 3) gives about 2 A at 3.3 V of gate drive, at 25 °C with short pulses.

Two cautions. The curve is typical, not guaranteed, and a part at the high end of the threshold range delivers less. It was also measured with 50 V across the device, so it shows the most the channel can pass at that gate voltage (saturation, §5.8), not how small the voltage drop is when it switches a motor. About 2 A is enough for a small motor, but it is not a guarantee.
5.7 More gate voltage, lower resistance
In a switch the number that matters is the on-resistance, and the datasheet guarantees it at two gate voltages only: RDS(on) ≤ 0.76 Ω at 4 V and ≤ 0.54 Ω at 5 V. Nothing is specified at 3.3 V.
A simple model shows the trend. In the ohmic region the channel conductance grows in proportion to the gate overdrive, so
Fitting it through the two datasheet maxima gives Vt ≈ 1.54 V and k ≈ 0.54 A/V². (Divide the two conditions: 0.54/0.76 = (4 − Vt)/(5 − Vt).) Here Vt is the model’s own threshold. It falls inside the datasheet’s 1.0–2.0 V range for VGS(th), but it is a fitted number, not a measured one. At the Pi’s 3.3 V the model then gives
This is roughly twice the 5 V value. It is a model extrapolation, not a datasheet number, so measure VDS and ID on the bench and compute the real value. At 0.5 A the model predicts (0.5 A)² × 1.1 Ω ≈ 0.27 W in the transistor. That warms the package by roughly 17 °C with no heatsink (§5.9), which you can feel but which does no harm.
![Sketch of IRL510 on-resistance against gate voltage, R = 1/[k(V_GS - V_t)], passing through the datasheet maxima of 0.76 ohm at 4 V and 0.54 ohm at 5 V. The threshold band is 1 to 2 V. At the Pi's 3.3 V the model gives about 1.1 ohm, a value the datasheet does not specify.](/_astro/rdson-vs-vgs.D1ZzlWXt_1ENkPn.webp)
5.8 Raise the drain voltage: ohmic and saturation regions
So far the drain voltage has been small. Raise it and the channel changes shape. The gate-to-channel voltage is VGS at the source end but only VGS − VDS at the drain end, so the channel is thinner there. When VGS − VDS falls to Vth, the channel pinches off at the drain. Raising VDS further moves the pinch point toward the source, and the current stops increasing.

With the device on, its behavior still depends on the drain-source voltage. At very low VDS the channel behaves as a resistor, and drain current rises in proportion: this is the ohmic or linear region, and the slope is the on resistance RDS(on). As VDS rises the channel pinches near the drain and the current flattens out, set almost entirely by VGS: the saturation region.
In the square-law model, with the overdrive Vov = VGS − Vth (Vth here is the same model threshold as Vt in Equation (2)), the two regions are
The boundary between them is the pinch-off condition VDS = Vov. The ohmic slope, ID/VDS = kVov, is the conductance whose inverse is Equation (2).
Do not push this model too far. With the k and Vt fitted in §5.7, it predicts a saturation current of only about 0.8 A at 3.3 V, well below the roughly 2 A of the typical curve in Figure 12. The square law is a rough description of a power MOSFET, and the fit used worst-case resistances, so treat both numbers as estimates and trust your bench measurement.

Whether a given gate voltage is “enough” therefore depends on the load: the channel must be able to carry more current than the load will draw, or the operating point slides into saturation. §9.6 works this check with numbers.
A switch lives hard in the ohmic region, where the device is a small resistance and dissipates little. An amplifier or mixer lives in saturation, where drain current depends on the gate voltage and barely on the load. Lab 4 is a switch, so every calculation in §7 assumes the ohmic region, and §9.6 shows how to check that the assumption holds.
(a) A MOSFET has VGS(th) = 2.0 V. Is it conducting with its gate tied to its source? What mode is it?
(b) Explain, in terms of carriers at the surface of the substrate, why the channel in Figure 10 is called an inversion layer.
(c) You measure 0.18 V across a switched-on MOSFET carrying 0.40 A. What is RDS(on), and which region is the device in?
(d) Why does a JFET have no enhancement mode?
5.9 The IRL510: pinout and the datasheet numbers that matter
The IRL510 comes in a TO-220 package. Hold it with the printed face towards you and the legs down: from left to right the legs are G, D, S (gate, drain, source). The metal tab is connected to the drain, so keep it clear of other conductors. The pinout is not self-explanatory, so mark the legs before you wire it.


These are the numbers from the Vishay datasheet that the rest of this lecture uses:
| Quantity | IRL510 | Why it matters here |
|---|---|---|
| VDS max | 100 V | Far above the 5 V supply, but an unclamped inductive spike can exceed it (§9.4) |
| VGS max | ±10 V | Not the ±20 V of many parts (§4.2) |
| VGS(th) | 1.0–2.0 V | Defined at ID = 250 µA only (§5.6) |
| RDS(on) | ≤ 0.54 Ω at 5 V, ≤ 0.76 Ω at 4 V | Unspecified at 3.3 V (§5.7) |
| ID continuous | 5.6 A (case at 25 °C) | A case held at 25 °C needs a large heatsink; the bench limit is heat, not this |
| Ciss, IGSS | 250 pF, ≤ 100 nA | The gate is a small capacitor with negligible leakage (§5.5) |
| Gate charge Qg | ≤ 6.1 nC to 5 V | Charge moved at each edge (§5.5) |
| Rise, fall time | 47 ns, 18 ns (typical) | Negligible against a PWM period of milliseconds |
| RθJA, no heatsink | 62 °C per watt | Temperature rise = dissipated power × 62 °C/W |
5.10 “Logic-level” gates, and why the IRL510 was chosen
The Raspberry Pi can only put 3.3 V on a gate. Many power MOSFETs need 10 V to turn on properly; driven at 3.3 V they only partly conduct, sit in a high-resistance state, dissipate serious power and get hot. A logic-level MOSFET is specified to be fully on at 4–5 V of gate drive, and the “L” in IRL510 signals exactly that (compare the IRF510, which is not logic level).
Task 2 asks you to review the datasheet. When you do, find and record: the pin assignment, VGS(th), the RDS(on) and the gate voltage it is quoted at, the continuous drain-current rating, and the maximum VDS. Then state explicitly whether 3.3 V of gate drive is enough, and what the consequence is if it is marginal.
The Vishay IRL510 specifies on-resistance at 4 V and 5 V gate drive. Threshold marks the onset of conduction at a small test current. Neither threshold nor the label “logic-level” guarantees low resistance at 3.3 V. Measure the on-state drain voltage and current under the actual load, then use P = IDVDS to estimate conduction loss. If the drive is inadequate, consult the instructor about a suitable driver or device.
6 CMOS, and the Pin You Have Been Using
This section is a short detour, and it is worth it twice over: it explains the logic family every processor you will ever use is built from, and it explains what is physically behind the GPIO pin you have been driving since Lab 3.
6.1 The complementary pair
Put a p-channel MOSFET above an n-channel MOSFET, tie both gates to the same input and take the output from the join. The two devices have opposite polarities, so any input turns exactly one of them on.
6.2 Why static power is nearly zero
In either steady state one of the two devices is off, so there is no conducting path from the supply to ground. A CMOS gate that is sitting still draws almost nothing; it burns power only while it is changing state, charging and discharging the capacitance it drives.
Equation (4) is worth more than it looks. It says a processor's power scales with its clock frequency and with the square of its supply voltage, which is why chips have spent thirty years moving to lower supply voltages, why raising a clock costs power linearly, and why your laptop throttles rather than melting.
6.3 Your GPIO output driver is one of these
Lecture 03 described a GPIO output as “a pair of transistors acting as a two-way switch” and called it push-pull. That pair is a CMOS pair on the chip that drives the pin (the RP1 I/O chip on the Pi 5), drawn exactly as Figure 18. Writing a 1 turns on the upper device and connects the pin to 3.3 V; writing a 0 turns on the lower device and connects it to ground.
It is roughly what those two small on-chip transistors can carry while still holding the pin close to 3.3 V or 0 V, and without damage. Ask for more and the output voltage sags out of its logic range, and the chip heats. The IRL510 you are about to wire up is the same device physics with a far larger die, which is why it can do what the pin cannot.
7 The MOSFET as a Switch
7.1 What the device does
An n-channel MOSFET has three terminals: gate, drain and source. The voltage from gate to source, VGS, controls how freely current flows from drain to source. Used as a switch it has just two states:
- Off: VGS below the threshold VGS(th). Essentially no drain current.
- On: VGS well above threshold. The device behaves as a small resistance RDS(on) between drain and source.
The gate is insulated from the channel by a thin oxide layer, so in the steady state it draws essentially no current, only the small transient needed to charge its capacitance at each switching edge. That is exactly what makes it a good match for a GPIO pin: the pin supplies almost nothing, yet it controls amperes.

7.2 Why the power dissipation is small
In the on state the MOSFET dissipates
and in the off state essentially nothing, because ID ≈ 0. A switch that is only ever fully on or fully off wastes very little power in either state. It is the same efficiency argument that made PWM dimming attractive in Lab 3, now with real current.
A MOSFET with RDS(on) = 0.54 Ω carries 500 mA:
This is the conduction loss for the assumed resistance. Temperature rise depends on the package and cooling, and PWM transitions add switching loss. Compare this with a linear approach: dropping the same 500 mA across a resistor to halve the motor voltage would burn 0.5 A × 2.5 V = 1.25 W, roughly ten times as much, as heat. Measure your IRL510's actual RDS(on) (§5.7) and repeat this calculation with your measured current.
The transistor itself is where the half-on case hurts. Without a heatsink the IRL510 rises RθJA = 62 °C for every watt it dissipates. Fully on at 0.5 A, 0.135 W × 62 °C/W ≈ 8 °C: barely warm. Half on at the same current, 1.25 W × 62 °C/W ≈ 78 °C above room temperature: too hot to touch, and on its way to failure. Fully on or fully off, it wastes almost nothing; half on, it burns the difference. That is why a switch must be driven hard into the ohmic region and why the gate voltage in §5.7 matters.
8 Why a GPIO Pin Cannot Drive a Motor
Three independent reasons, any one of which is sufficient.
8.1 Current
A GPIO pin is rated at 16 mA, with about 50 mA total across the whole header. A small brushed DC motor typically draws hundreds of milliamps while running freely, and several times that at stall, the moment of starting or any time the shaft is held. Stall current is the number that destroys things, and it is the one to look up.
With no rotation there is no back-EMF (§8.3), so the only thing limiting the current is the winding's DC resistance, often only an ohm or two. Measure Rwinding with the DMM and compute Istall before you connect anything. It is a striking number, and it is worth putting in your report.

A motor on a 5 V supply has a winding resistance of 2.5 Ω. At rest, Equation (6) gives
That is 125 times the 16 mA a GPIO pin is rated for. As the motor accelerates, back-EMF (§8.3) opposes the supply and the current falls toward its running value. But the switch sees the start-up peak every time the motor starts or stalls. The GPIO pin can only supply the control signal to a power switch.
8.2 Inductance
A motor winding is an inductor. Current through an inductor cannot change instantaneously, and interrupting it produces a voltage spike
Switching at PWM rates means interrupting that current hundreds or thousands of times a second. With a fast switch, dI/dt is enormous and the spike can reach tens or hundreds of volts. That is far beyond anything a 3.3 V pin can survive, and it can exceed even the IRL510’s 100 V rating. §9.4 deals with this.
8.3 Back-EMF
A spinning motor is also a generator. The rotating windings cut flux and produce a voltage opposing the supply, proportional to angular velocity:
This is useful. It is why a motor draws far less current at speed than at stall, and it is the reason the speed settles rather than running away. But it also means a coasting motor feeds voltage back into your circuit after you switch off, which is another reason the power path must be isolated from the Pi.
9 Wiring the Switch
9.1 The Pi sends the signal; the supply sends the power
The circuit has two paths, and it helps to trace them separately. The power path carries amperes at 5 V: from the bench supply through the motor, into the drain, out of the source, and back to the supply. The signal path carries milliamps at 3.3 V, mostly at the switching edges: from BCM 12 through the gate resistor into the gate capacitance.
The two paths meet in only two places: at the gate, and at one shared ground. Without that shared ground, VGS is undefined, because it is measured from the source, and the source sits on the supply’s ground (§10).

9.2 Low-side switching
Put the MOSFET between the load and ground: supply, then motor, then drain, with source to ground. This is low-side switching, and it is the arrangement to use here.
The reason is physics, not style: the gate voltage is measured with respect to the source. With the source bolted to ground, VGS is simply the GPIO pin's voltage, and the switch behaves predictably. If you instead put the MOSFET above the load (high-side), the source sits at the motor terminal and rises as the device turns on. VGS shrinks until the transistor is barely conducting, so it never turns fully on and runs hot. That is why high-side switching needs a P-channel device or a dedicated gate driver.

9.3 The gate resistor and the pull-down
Two small components make the circuit behave:
- A series gate resistor of a few hundred ohms limits the current the GPIO pin must supply while charging the gate capacitance at each edge. Without it, the momentary current at a switching edge can exceed the pin rating even though the steady-state gate current is zero.
- A pull-down resistor of around 10 kΩ from gate to source holds the gate at 0 V whenever the pin is not actively driving it: while the Pi boots, after
GPIO.cleanup(), or if a wire falls out. Without it the gate floats, and a floating gate on a high-impedance input can drift up and switch the motor on by itself.
The numbers for the lab values, 470 Ω and 10 kΩ, with the IRL510’s Ciss ≈ 250 pF (§5.5):
- Peak gate current. At the instant the pin goes high the gate is still at 0 V, so the whole 3.3 V is across the resistor: 3.3 V/470 Ω ≈ 7 mA, inside the pin’s 16 mA rating.
- Edge time. The gate charges with time constant RGCiss = 470 Ω × 250 pF ≈ 118 ns, so the gate current has decayed within about half a microsecond. With the motor connected, the gate must also supply the gate–drain charge while the drain swings (up to 3.3 nC, the “Miller plateau”), and only about a milliamp is left to do it, so the real switching edge stretches to a few microseconds. That is still negligible against a PWM period of milliseconds.
- Steady state. The two resistors form a divider, so the gate settles at 3.3 V × 10 kΩ/10.47 kΩ ≈ 3.15 V rather than the full 3.3 V, and the pin supplies a steady 3.3 V/10.47 kΩ ≈ 0.3 mA while it is high. Both are small prices for a gate that can never float.

9.4 The flyback diode
This is the component most often left out, and leaving it out destroys MOSFETs.
When the MOSFET switches off, the motor's winding current has nowhere to go. By Equation (7) the inductor develops whatever voltage it takes to keep that current flowing. With the switch open, that voltage appears across the MOSFET's drain and can far exceed its VDS rating.
Connect a diode across the motor terminals, oriented so that it is reverse-biased by the normal supply voltage: cathode (the banded end) to the positive supply, anode to the drain.
In normal operation it does nothing. At switch-off, the inductor's reversed voltage forward-biases it, giving the winding current a path to circulate and decay gently instead of spiking. The spike is clamped to roughly one diode drop above the supply.
Fit it backwards and it short-circuits your supply through a forward-biased diode, so check the band before powering up. Take this one seriously: it is the one orientation error in this lab that produces smoke rather than a wrong reading.
Keep the flyback diode fitted at all times. To see it working, put a suitably rated scope probe on the drain, with its ground clip on the common ground, and check that the voltage at switch-off is clamped near the supply. If you want to see what happens without the diode, do that in a simulation, not on the bench. And do not count on the MOSFET’s body diode instead: it points from source to drain, so it offers no path for a positive spike on the drain in this low-side circuit.

9.5 Example: an IRL510 LED driver
The same low-side switch works for any load too big for a pin. In this example from the lecture, an LED and a 100 Ω resistor sit between a 5 V supply and the drain, the source is grounded, the gate receives a 0–5 V square wave at 2 Hz, and a 20 kΩ pull-down holds the gate low when nothing drives it.
That is nearly twice a GPIO pin’s 16 mA rating, but trivial for the FET. (It is also at or above the rating of many LEDs, which is often 20 mA; check yours, and use 150 Ω if it is.) At 5 V of gate drive its RDS(on) ≤ 0.54 Ω drops at most 30 mA × 0.54 Ω ≈ 16 mV and dissipates under half a milliwatt. The 2 Hz drive makes the switching visible by eye; at PWM rates the same circuit dims the LED.

9.6 Example: how much current reaches the motor?
A second worked example from the lecture uses a different power MOSFET, the MTP4N80E. A 30 Ω motor sits on a 30 V supply, and the gate is driven from 0 to 10 V. At VGS = 10 V this part has RDS(on) = 1.95 Ω, and it can carry ID(on) = 2 A before its channel saturates.
Which region is it in? The most current the load itself would allow, with the transistor a perfect short, is
The load limits the current before the channel can, so the transistor is in the ohmic region and behaves as a small resistor in series with the motor:
This is the method for any switch: check that the load, not the transistor, sets the current, and then treat the transistor as RDS(on). Here 1.7 W is a real amount of heat. In a TO-220 package with no heatsink, at about 62 °C/W, it would mean a rise of roughly 100 °C, so this driver needs a heatsink.

10 The Complete Motor-Drive Circuit

The Pi's ground and the bench supply's negative terminal must be connected together. VGS is a voltage difference between the gate and the source, and the source sits on the bench supply's ground. If the two grounds are not tied, that difference is undefined: the circuit will behave erratically, work intermittently, or not at all. Worse, the fault is invisible in a schematic that draws two separate ground symbols.
This is the single most common wiring error in Lab 4. If the motor does nothing when you switch to Pi control in Task 3, check the ground connection first.
-
Task 2, with a bench supply on the gate. Build the circuit and drive the gate from a fixed +5 V, so the motor either runs or does not. This isolates the power path: if it works here, the power circuit is sound.
-
Have an instructor or TA check the circuit before powering the motor; Task 2 requires this.
-
Task 3. Disconnect the +5 V gate supply, connect BCM 12 (physical pin 32) to the gate resistor, and confirm the common ground: header pin 34 (GND) to the supply’s negative terminal. Now the signal path is under test, and only one thing has changed.
-
PWM the gate and vary the speed (§11–13).
Changing one thing at a time is why Lab 4 is structured this way. Resist the temptation to build it all at once.
11 PWM Revisited: the Range of Validity
A quick rewind from Lecture 03. A PWM signal is HIGH for ton of every period T. With the duty fraction d = ton/T, the average voltage is ⟨V⟩ = dVhigh. In RPi.GPIO:
p = GPIO.PWM(12, f)creates a PWM generator on BCM 12 at f hertz;p.start(D)starts it, with the duty D in percent;p.ChangeDutyCycle(D)changes the duty while it runs.
It is software PWM, so check what it actually produces on the scope.

On the gate, PWM sets the motor’s average voltage. The MOSFET switches the motor between 0 and the full 5 V, and the motor averages the pulses twice over: the coil’s inductance smooths the current (when the PWM period is short compared with the coil’s L/R time), and the rotor’s inertia smooths the speed.

Task 1 asks for something more searching than “make a PWM signal”: it asks for which frequency and duty-cycle values it functions as expected. That is a characterization task, and the answer is a region on a two-dimensional map, not a single number.
11.1 What “as expected” means
Define the criterion before you measure, and state it in your report. A reasonable set:
- The measured frequency is within a stated tolerance of the commanded frequency (say 5%).
- The measured duty cycle is within a stated tolerance of the commanded duty cycle.
- The waveform still has clean, full-amplitude transitions between 0 V and 3.3 V.
- The jitter is small compared with the period.
11.2 Where it breaks down, and why
Recall from Lecture 03 that GPIO.PWM() is software PWM: a background thread toggles the pin, so every edge is placed by the CPU under a non-real-time operating system. Four distinct failure modes follow, and you should look for all four:
| Region | What you will see, and the reason |
|---|---|
| Very low frequency below a few Hz | Works fine electrically. The motor will not run smoothly, though: it receives discrete kicks rather than a steady average, because the PWM period is longer than the motor's mechanical time constant (§13.3). |
| Useful mid-band | Frequency and duty both track the commands. This is the region you will use for the motor. |
| High frequency | The commanded period approaches the time the CPU needs to service an edge. The measured frequency falls below the command, the duty cycle drifts from its setting, and jitter grows as a fraction of the period. Find this ceiling and record it. |
| Extreme duty cycles near 0% and near 100% | The on-time (or off-time) becomes shorter than the software can reliably produce, so the narrow pulses become erratic, get stretched, or disappear entirely. This failure depends on frequency, because ton = d/f: 1% duty is 10 ms at 1 Hz and 10 µs at 1 kHz. Note the distinction; it makes a good observation for the report. |
Sweep a grid rather than a line: pick five or six frequencies spanning several decades, and at each one measure duty cycles of roughly 1, 5, 25, 50, 75, 95 and 99%. At every point record commanded and measured frequency, commanded and measured duty, and a jitter figure from the scope's measurement statistics.
Present the result as a table plus a simple map showing where the criteria hold and where they fail. Capture scope screenshots at a few representative points (one clean, one at the frequency ceiling, one at an extreme duty cycle) rather than one per grid point.
(a) At 1 kHz, what on-time does a commanded duty cycle of 2% correspond to? At 20 Hz?
(b) If the software can only place an edge to within about 150 µs, estimate the lowest duty cycle that is reliable at 1 kHz, and at 50 Hz.
(c) A student reports “PWM works from 1 Hz to 10 kHz.” What is missing from that statement?
12 Measuring Rotation Rate
Task 4 asks you to design a detection system, and hints at an LED and a photodiode. This is a satisfying moment in the course: the light detector you built in Lab 1 becomes the sensor for the motor you built this week.
12.1 The principle
First, a rewind from Lecture 01. A photodiode is a light-controlled current source: the photocurrent Iphoto is proportional to the light falling on it. The transimpedance amplifier (TIA) turns that current into a voltage, Vout = −IphotoRf. Block the light and the output steps; that step is a tachometer pulse.


Put an LED on one side of the spinning shaft and a photodiode on the other, and attach something to the shaft that interrupts the beam once (or N times) per revolution: a card with a slot, a strip of tape, a paper disc with holes. Each interruption produces a pulse. Count pulses over a known time and you have the rotation rate:
with tgate the counting interval in seconds and N the number of interruptions per revolution. Alternatively, measure the period between successive pulses on the oscilloscope and invert it. That is often the quicker route, and it needs no code at all. Count the pulses on the scope first, then with a GPIO input, but only after reading the warning below.

The TIA from Lab 1 runs on ±15 V, and its output swings negative, down to about −14 V. A GPIO input accepts only 0 to 3.3 V, so a direct connection can destroy the Pi. The scope can take any of these voltages, so do your first measurements there. To count pulses with a GPIO input, first turn them into clean 0–3.3 V logic, for example with an open-collector comparator pulled up to the Pi’s 3.3 V. Check the result on the scope, and have a TA look at the circuit, before it goes anywhere near the header.
In terms of the pulse frequency the scope reports,
With N = 2 and pulses arriving at fpulse = 80 Hz, the shaft turns 80/2 = 40 times per second, which is 60 × 40 = 2400 RPM. Assume N = 1 by mistake and you would report 4800 RPM.

A single strip of tape gives one pulse per revolution. A disc with four slots gives four. A two-bladed interrupter gives two. If you get N wrong, every RPM in your table is wrong by that factor and your derived formula is wrong with it, while still looking perfectly self-consistent. State N explicitly in your report and justify it from the geometry you built.
12.2 Making the pulses clean
A raw photodiode signal is a small, slow, rounded bump, not a crisp pulse. Three things help, in increasing order of effort:
- Use the TIA from Lab 1 rather than a bare resistor. It gives real gain and holds the diode at a fixed bias, so the pulse is bigger and better shaped.
- Shield from room light. The overhead fluorescents flicker at 120 Hz, which lands squarely in the range of rotation rates you are trying to measure. It is an excellent way to record a completely fictitious RPM. Shroud the detector, and check what it reads with the motor stopped.
- Measure on the scope first. Confirm you have a clean, repeating pulse train before you write any counting code. If the waveform is ambiguous on screen, no amount of software will rescue it.
Double counting. If the signal is noisy or the edges are slow, one interruption can register as several. On the scope this shows as extra small transitions at each edge. Cleaning up the optics or adding hysteresis is the real fix.
Counting the PWM. The motor is being switched at your PWM frequency. If that switching couples into the detector, electrically through a shared supply or optically if the motor has an indicator LED, you may count PWM edges instead of revolutions. A giveaway is a measured “RPM” that tracks the PWM frequency rather than the motor speed. Test for it by changing the PWM frequency at a fixed duty cycle: the true rotation rate should barely move.
13 What the RPM-vs-Duty Curve Looks Like
Task 4 asks for at least ten data points, a plot, a derived formula, and an analysis of accuracy. Knowing the shape in advance tells you where to put your points.

13.1 Three regions
- Dead zone. Below some duty cycle D0 the motor does not turn at all. The average torque is less than the static friction holding it still. This region is real, reproducible, and must appear in your data, so do not skip it.
- Roughly linear region. Above D0, the average applied voltage is (D/100) Vsupply, with D the duty cycle in percent, and from Equation (8) the steady-state speed is set by the balance between applied voltage and back-EMF. Speed therefore rises approximately linearly with duty cycle: RPM ≈ k(D − D0) (11)with D and D0 in percent, so k is in RPM per percentage point.
- Saturation. Approaching 100% duty the curve flattens as the motor reaches its no-load speed for that supply voltage.
Do not space them evenly from 0 to 100%. Put several points close together around the dead-zone edge, where the interesting behavior is and where D0 is determined; spread the rest across the linear region; and take two or three near the top to show the flattening. Ten well-placed points say far more than twenty evenly spaced ones.
Fit Equation (11) to the linear region only, and say in your report which points you included and why. Quote k in RPM per percentage point of duty, with units, and D0 as a percentage.
13.2 Hysteresis: a result worth looking for
Take your data twice: once increasing the duty cycle from zero, once decreasing it from 100%. The motor typically keeps turning down to a lower duty cycle than the one needed to start it, because sliding friction is less than static friction and the spinning rotor has angular momentum.
If you find this, you have measured a real physical effect rather than just filling in a table, and that kind of observation lifts a lab report. If you do not find it, say so. A null result, honestly reported, is still a result.
13.3 Choosing the PWM frequency for the motor
The motor's mechanical time constant is the rotor's inertia divided by the damping, typically tens of milliseconds. If the PWM period is much shorter than that, the rotor cannot follow individual pulses and responds only to the average, which is what you want. If the period is comparable or longer, the motor jerks once per cycle.
Practically: a few hundred hertz to a few kilohertz works well. The instructor's reference code uses 50 Hz, which is a reasonable starting point and low enough that you can see the switching on the scope alongside the motor's response. Its 20 ms period is not much shorter than the mechanical time constant, though, so at low duty you may hear or feel the individual pulses. Try a few hundred hertz as well and compare. Above roughly 20 kHz the switching becomes inaudible, which is why commercial motor drives run there, but software PWM on the Pi will not get you that far reliably (§11.2). Report which frequency you used and why.
(a) A motor reaches 2400 RPM at 100% duty and has a dead zone up to 15%. Estimate k in Equation (11) and predict the RPM at 50% duty.
(b) Your tachometer counts 250 pulses in 5.0 s with a two-slot disc. What is the RPM? What would you have reported had you assumed one slot?
(c) The motor draws 0.45 A running and its winding measures 2.2 Ω on a 5 V supply. Compute the stall current and state the ratio to the running current.
(d) Name two distinct sources of error in your RPM measurement and estimate the size of each.
14 Measurement Technique
| What you are measuring | How | Watch out for |
|---|---|---|
| PWM gate signal (Task 1) | Scope probe on BCM 12, ground clip to a header ground pin. DC coupling; measure Period, +Width, Duty with statistics on. | AC coupling makes the duty-cycle reading meaningless. |
| Drain waveform | Probe the MOSFET drain, ground clip to the common ground. | Inductive spikes can exceed the probe's rating if the flyback diode is missing. Fit the diode first. |
| Motor current | DMM in series on the supply side, or the voltage across a small sense resistor. | The reading is a time-average of a switched current; say so when quoting it. |
| Winding resistance | DMM on ohms, motor disconnected from everything. | Brushed motors read differently at different shaft positions, so take several readings and quote the range. |
| Rotation rate | Tachometer pulse train on the scope; read Frequency directly, or count in software. | Confirm N; shield from room light; verify the reading is zero with the motor stopped. |
15 Common Mistakes and Bench Safety
15.1 Things that destroy hardware
- No flyback diode, or the diode fitted backwards. The first kills the MOSFET; the second shorts the supply.
- Motor powered from the Pi. Lab 4 states this explicitly: use a separate +5 V supply. The Pi's 5 V pin can brown out the board, or worse.
- Missing common ground. Undefined gate voltage, unpredictable switching.
- Motor terminals touching the Pi header. Back-EMF from a coasting motor can appear at the terminals after switch-off.
- Bench supply current limit set high. Set it to somewhat above the expected running current before first power-up. A current limit is a fuse you can reset, and it converts a wiring error into a lesson rather than a replacement part.
15.2 Things that waste your afternoon
- Confusing the MOSFET's pins. Identify D, G and S from the datasheet and mark them; the package outline is not self-explanatory.
- Using a non-logic-level part. An IRF510 will half-turn-on at 3.3 V, run hot and give a weak, nonlinear response.
- Forgetting the gate pull-down, then wondering why the motor twitches during boot.
- Room light contaminating the tachometer (§12.2).
- Taking RPM data before the motor has settled. Give it a second or two at each duty cycle.
- A loose interrupter flag that shifts position mid-run, silently changing N.
15.3 Mechanical safety
A motor shaft spins fast enough to catch hair, sleeves and probe leads. Secure the motor to the bench; do not hold it in your hand. Keep the interrupter flag light and well attached; a stiff card thrown off a shaft at speed is a real eye hazard. Wear eye protection while the motor runs, and switch off before adjusting anything mechanical.
15.4 Before you leave
Transfer all programs, data and screenshots to your laptop, power down the Pi cleanly, turn off the bench supply, and return components. Verify the copy opens on your machine before deleting anything from the Pi.
16 Self-Check
- What does adding phosphorus to silicon produce, and what does adding boron produce? Name the majority carrier in each case.
- Describe a depletion layer, and say what widens it.
- State the equation relating collector and base current in a BJT. Why does that relationship make a BJT a poor match for a 16 mA GPIO pin?
- What physically separates the gate of a MOSFET from its channel, and what does that make the gate look like electrically?
- Explain how raising VGS creates a channel in an enhancement-mode device. Why is the result called an inversion layer?
- Which mode is normally on, depletion or enhancement? Which do you want for a motor switch, and what is the safety argument?
- Give three differences between a BJT and a FET, and state which device each difference favors.
- A switch should operate in which region, ohmic or saturation? What would going into the other region cost you?
- In a CMOS inverter, why is the static power nearly zero, and what does the gate burn power on instead?
- Give three independent reasons a GPIO pin cannot drive a DC motor directly. Which one is quantitative, and what number would you cite?
- A motor winding measures 2.5 Ω on a 5 V supply. Compute the stall current and compare it with the 16 mA pin rating.
- What does the “L” in IRL510 signify, and why does it matter for a 3.3 V controller?
- Draw the flyback diode across a motor and state its orientation relative to the supply. What happens if it is reversed?
- Explain why the MOSFET goes between the motor and ground rather than between the supply and the motor.
- Why must the Pi's ground and the bench supply's ground be connected? Answer in terms of VGS.
- What two roles do the gate series resistor and the gate pull-down resistor play?
- Define the dead zone in the RPM-vs-duty curve and explain its physical origin.
- Your tachometer gives 180 pulses in 6.0 s from a three-slot disc. Find the RPM.
- Why might the motor keep running at a lower duty cycle than the one needed to start it?
When a load needs more current than a pin can supply, the pin commands and a transistor does the work. Give the load its own supply, tie the grounds together, put a flyback diode across anything inductive, and switch fully on or fully off so the transistor stays cool. Then measure what you actually built: characterize the PWM before you trust it, and calibrate the tachometer before you believe an RPM.
17 Practical Engineering Connections
- Doping is the whole industry. Nearly every chip, sensor and solar cell you will use begins with deliberately contaminated silicon. The photodiode of Lab 1, the MOSFET of Lab 4 and the ADC of Lab 8 are three arrangements of the same two materials.
- Bipolar is not obsolete. BJTs still win where a low, predictable saturation voltage or good analogue matching matters: precision current mirrors, bandgap references and many audio output stages.
- CMOS. Every processor, memory and FPGA is the inverter of Figure 18 repeated billions of times. The C V² f term in Equation (4) is why clock speed, supply voltage and battery life are one design conversation.
- Gate charge and switching loss. At megahertz switching rates, charging the gate capacitance stops being free and becomes a real power term. It is why switching supplies use dedicated gate-driver chips rather than a logic pin.
- Electrostatic discharge. The fragile oxide of §4.2 is why boards ship in conductive bags, why assembly benches are grounded, and why most chip input pins carry small protection diodes.
- Low-side switching. The workhorse arrangement for solenoids, relays, valves, heaters, fans and lamps in cars, appliances and industrial controllers.
- PWM power control. Electric-vehicle traction drives, cordless tool controllers, variable-speed fans, induction cooktops and switching power supplies all control power exactly this way, and for the efficiency reason in Equation (5).
- Flyback protection. Almost every relay and solenoid driver includes this diode. It is also the operating principle of the boost converter, which harvests the inductive spike instead of clamping it.
- Logic-level gate drive. The 3.3 V-versus-10 V gate question appears whenever a modern microcontroller drives legacy power hardware, and is the reason gate-driver ICs exist.
- Back-EMF. Sensorless motor controllers measure back-EMF to infer rotor position, and regenerative braking recovers it as useful energy.
- Optical tachometry. Optical encoders on printer carriages, robot joints, CNC axes and computer mice all count interruptions of a light beam, exactly as your Task 4 detector does.
- Characterizing before trusting. Task 1's range-of-validity exercise is what an engineer does with any new subsystem: find the region where it behaves, and document the edges, before designing anything on top of it.
- Isolation. Where the power side is dangerous or noisy, the signal and power paths are separated further still, by optocouplers or transformers. Your common-ground circuit is the simplest member of that family.