Band 6 · Things That Move
Swap A fan on a transistor
Speed instead of position, and one three-legged part doing the whole job of a driver board.
Sketches for this project
Every file opens with a plain-English header saying what it does and how to wire it, hole by hole.
Speed is not position
The servo gate answers the question where should this be? A motor answers a different one: how fast should this go?
They are not interchangeable, and choosing the wrong one is a common way to waste an afternoon. A motor cannot stop somewhere in particular, because nothing in it is counting or looking. A servo cannot spin. If your build needs a fan, a pump or a wheel, you want this. If it needs an arm, a lid or a pointer, you want the servo.
There is a second thing in this build and it is the more important one.
Why the motor cannot go straight on a pin
An Arduino pin can supply about 20 thousandths of an amp before it is damaged. A small DC motor wants ten to twenty times that at the moment it starts turning.
So wiring a motor to a pin is not driving a motor. It is damaging a chip, slowly, with no warning and no error message. It may even appear to work for a while, which is worse, because you will not know when it stopped being fine.
A transistor fixes it. Think of it as a switch that a tiny current operates and a big current passes through. The pin gives the order. The battery does the work. The two never meet.
Band 6 Part 7 taught you that two power sources need one shared ground. This is the same lesson going the other way: signal and power are different jobs, and most of the mistakes come from treating them as one.
Wire it
35 minj7 goes to the 5V pin on its own.j, the same pair Band 6 used. That includes the wires that start down in rows a and b: a12, a4 and a21 all run the long way round to the top − rail, past the end of the board. It is a longer wire and it is the right one.Get this wrong and the failure is silent. Put the battery's black wire on one strip and the knob's
j5 on the other, and the knob has no ground of its own, A0 reads nonsense, and this page's own troubleshooting will tell you the knob is broken when the knob is fine.Read the number on the transistor before you power anything
Your transistor is a small black semicircle with three legs and a number printed on the flat face: S8050, 2N2222, PN2222, BC547, BC548, or something not in that list. Read yours. The number decides two separate things, and each of them can end the build.
Hold it with the flat face towards you and the legs pointing down. The middle leg is the base on all of them. It is the two outer legs that change.
S8050 · 2N2222 · PN2222
Left to right these are emitter, base, collector. They carry six to seven hundred thousandths of an amp, which is plenty for a small motor. Use one of these if your kit has one.
→ Left leg into e12, middle leg into e13, right leg into e14.
BC547 · BC548
Left to right these are collector, base, emitter, which is the opposite way round. They also carry only one hundred thousandths of an amp, and a small motor wants two to four times that at the moment it starts. This is not really the part for this job.
→ Keep the flat face towards you and cross the two outer legs over: the right-hand leg into e12, the middle leg into e13, the left-hand leg into e14. Do not physically turn the part round; only the two outer legs swap holes. If it then runs warm or will not start the motor, that is the size limit and not your wiring, and the ULN2003 box below is your way out.
There is no way to tell the leg order by looking, and guessing costs you the part.
→ If you can get online, search for your number and the word pinout, find a picture, then put the emitter in e12, the base in e13 and the collector in e14. If you cannot look it up, do not guess. Put that transistor back in the box and use the ULN2003 route in the box below. It runs this sketch unchanged.
Whichever part you have, wire it so that e12 ends up the emitter, e13 the base and e14 the collector. Then the table below is the same for everybody.
Getting the two outer legs the wrong way round usually destroys the transistor. The battery pushes backwards through a junction that was never meant to take it, and the part does not recover. There is no smoke and no error message, so nothing will tell you. Check the number, check a picture, then wire it. This is one of the few places in this whole course where the mistake is not undoable.
| Part | Goes to | Why |
|---|---|---|
| Transistor | Three legs into e12, e13, e14, in the order the box above gave for your number | e12 emitter, e13 base, e14 collector, for everybody |
Base, e13 | A 220 ohm resistor from a13 to a11, then a wire from b11 to Arduino pin 5 | b11, not a11: a hole takes one leg |
Emitter, e12 | A wire from a12 to the − rail | Where the big current leaves |
Collector, e14 | One motor wire into a14 | Where the big current enters |
| Other motor wire | The + rail | Battery power, never Arduino power |
| Diode, across the motor | Striped end into the + rail, plain end into b14 | Required. Read the red box below before you switch on. |
| Battery pack | Red to + rail, black to − rail | |
| Ground link | One wire, − rail to an Arduino GND pin | The Part 7 rule. Not optional. |
| Knob | Legs f5, f6, f7. j5 to − rail, j6 to A0, j7 straight to the Arduino 5V pin | Never the + rail; that is six volts. |
| Light | Long b3, short b4. A second 220 ohm resistor a3 to a1, wire b1 to pin 7, a4 to − rail | You need two 220 ohm resistors on this build, one here and one on the base. b1, not a1: a hole takes one leg |
| Button | Legs e19, e21, f19, f21. j19 to pin 2, a21 to − rail |
The diode is not optional, and the stripe faces the + rail
A spinning motor that is switched off becomes a generator for an instant and pushes a high voltage back down the wire it came from. This is the part that catches people out: PWM switches the motor off about a thousand times every second. So that spike is not a rare event at the end of the session. It happens a thousand times a second, the whole time the fan is running, straight into the transistor.
Use a 1N4001, or any other 1N400x. Do not substitute a small glass 1N4148: it is a signal diode rated for about a fifth of an amp, and this motor works it at or past that the whole time it runs. There is a stripe painted round one end. The striped end goes into the + rail. The plain end goes into b14, which is the collector row, so the diode sits across the motor.
Fitted this way it does nothing at all while the motor is running, and only conducts for the instant the spike appears. That is why it looks pointless and is not.
A diode fitted the wrong way round carries the current that was supposed to go through the motor. So the first thing you notice is that the motor does not turn at all, and the second is that the diode and the transistor get hot within a few seconds of turning the knob up. If that happens, switch off immediately and look at the stripe. Check it twice before you switch on and you never meet this.
BC547 or BC548Do not run this build with a bare transistor. Use the ULN2003 driver board from the stepper kit instead: it holds seven transistors and all seven of their diodes already. Everything in the sketch stays exactly as it is.Wiring it. Arduino pin 5 to
IN1. The board's − pin to the − rail, and its + pin to the + rail; that second wire is not optional, because the diodes inside the board have nowhere to send the spike without it. Then the motor. Some of these boards have pins marked OUT1 to OUT4 along one edge: if yours does, one motor wire goes into OUT1. If yours has only the white five-pin socket, the outputs are inside that socket: push a jumper wire into the socket hole at the end furthest from the IN pins and use that as OUT1. The motor's other wire goes to the + rail.What you lose is wiring a transistor as three separate legs yourself, which is most of the point of this page. Read the boxes above even if you take this route.
~ next to it, and that is why it was chosenSpeed is PWM, exactly like brightness in Band 2. On a pin without a ~ you get off and flat out and nothing in between, which would make this whole build pointless.Why 220 ohms and not 1k. A transistor needs roughly a tenth of the current it is carrying pushed into its base before it switches all the way on. Once it is turning, this motor settles at something like 150 to 200 thousandths of an amp, so it wants about twenty in the base. Through 220 ohms a pin supplies about nineteen, which is also about as much as one pin can give. Through 1k it supplies about four, the switch never fully closes, and you get a hot transistor and a weak motor. The much bigger gulp at the instant the motor starts is short enough not to matter. This resistor is not setting a brightness. It is deciding whether the switch actually shuts.
Tape the motor down before you switch anything on, and tape a small paper flag to its shaft so you can see it turning. A loose motor walks off the table and pulls its own wires out.
Plug in the USB, switch the battery on, and upload b6_07_fan_transistor.ino. Open the serial monitor at 9600 and turn the knob slowly from one end to the other.
a14.~. Read the printing on the board rather than counting holes.BC547 does here, or the base resistor is too big and the switch is only half open. A S8050 or 2N2222 with a 220 ohm base resistor should stay cool at every speed. Change the part rather than living with it.The number you have to find yourself
25 minint minSpeed = 60; // below this the motor hums and does not turn
Below some value there is not enough push to overcome the friction in the bearings, so the motor sits there humming and getting warm. Above it, it turns. That threshold belongs to your motor and this page does not know it.
Measure yours
- Set
minSpeedto0and upload. - Turn the knob up very slowly from zero, watching the printed number and listening.
- Write down the number at which it starts turning, not the number at which it starts humming. Those are different and the gap between them is the interesting part.
- Put that number into
minSpeed, upload again, and turn the knob from zero.
This is Band 4's calibration lesson in a new place, and it is worth saying out loud: the number was not in the code, it was in the motor. The only way to get it was to go and measure. Write yours in your build log. If somebody near you built one too, write theirs beside it and notice that they differ. Working alone, measure your motor again after it has been running for five minutes and see whether the warm number matches the cold one, because it often does not, and that tells you the same thing about where numbers come from.
The line that stops the fan hunting
if (abs(speed - lastSpeed) >= deadband || speed == 0 || lastSpeed == 0) {
Take your hand right off the knob and analogRead still wobbles by a count or two. Without that line every wobble would be written to the motor, and the fan would hunt up and down for ever instead of holding a speed. deadband says: ignore any change smaller than six.
The two extra tests after it are there because off has to mean off. Set minSpeed to 0, as the measuring box above has you do, and a change from 4 down to 0 is smaller than the deadband. Without speed == 0 the fan would carry on creeping after you had turned the knob right down. A rule that is almost always right usually needs its one exception written out by hand, and this is what that looks like in code.
There is one more line just underneath, which the quote above leaves out:
if (speed == lastSpeed) return;
That one throws away any value identical to the last one, whatever the deadband says. Keep it in mind for the experiment below, because it is doing part of the same job.
Try it
Set deadband to 0, upload, then leave the knob alone at a middle setting for thirty seconds. Watch the serial monitor and listen to the motor. Put it back to 6 and do the same. Write down what changed.
Expect a twitch rather than a runaway: the speed == lastSpeed line is still there catching the repeats. To see the full effect, take that line out too, and then put both back. This is Band 4's threshold lesson wearing different clothes, and you will meet it a third time in Band 8.
Why the light is on a separate pin
It would be tempting to wire the light across the motor and save a pin. Do not. The motor's side of the circuit is battery voltage and much larger currents; the light belongs on the Arduino's side where the rules you know apply. Keep the two sides separate everywhere except the one ground wire that joins them.
Make it yours
30 min- Bronze
- Make the fan speed up and slow down gradually when the burst button is pressed, rather than jumping to full and back. Use the fade loop from Band 2, and say in one sentence why a motor prefers this.
- Silver
- Drive it from the temperature sensor instead of the knob, if you have the DHT11: hotter room, faster fan. You will need two thresholds rather than one, for the reason Band 4 gave you.
- Gold
- Give the button a second job. Instead of one three second burst, make a short press step through three fixed speeds, slow then medium then fast then back to the knob. You will need a variable that remembers which of the four states you are in. You will also have to decide what should happen if somebody turns the knob while a fixed speed is running, and there is no right answer to that. Write down the rule you chose and one sentence saying why.
You still owe Band 6 the same evidence
This Swap replaces the gate, not the band. To claim Band 6 you still need:
- A video of the fan responding to something, and a photo clear enough to show both the ground link and the diode's stripe
- Your notes from the loaded-card activity in Part 1
- Your measured
minSpeed, and how you found it - Your build log, including whether the transistor legs caught you out
- The deadband table from Part 9b and the three values from Part 9c, done on the servo sketch even though you built the fan
- Five or six correct answers in Part 12, the Check yourself questions, including question 2
Ship it
One video under sixty seconds, and one post of five lines. You built something different from the person next to you, so your video is the one nobody else in the room can post. Say in the post which build you chose and why — that choice is itself worth a line.
The four shots: three seconds of the thing still, fifteen of you doing something to it, fifteen of it responding all the way to the end, and ten of your measurement or the thing that went wrong first.
Show your work has the template and the three checks to
make before anything goes public. Tag it #BozomaBuilds.