Controlling a Fan Using PWM and Arduino

Here is how to give speed control to a regular 12V computer fan using an Arduino and PWM (pulse width modulation). This will work for other motors as well.

This is not intended to be a deep tutorial on the subject, but if you need more information you can check out the various links provided. This is mainly a quick write-up to show a friend how to do speed control on a fan that will eventually be hooked to a temperature sensor so that the fan can regulate the temperature inside an enclosed box.

Items Needed

  • Arduino (can be done with a Raspberry Pi using Python)
  • TIP122 (or adjusted for just about any Darlington transistor)
  • 1N4001 diode
  • 12V computer fan
  • 12V power supply
  • 270Ω resistor

The above items are not special or magical. If you don’t have a TIP122 transistor, you can probably make this work with just about any large transistor or MOSFET (large as in handling power as opposed to just physical size, though they often correspond). The same applies for the diode: as long as it is sufficiently large, it will probably work. The resistor size was chosen simply based on it being the one closest to my Arduino when I started the project.

Hookup

I leaned heavily on an Instructables article for getting my wiring set up. Certainly check out his article for more details about the wiring if you need more than a schematic to figure this out.

Or, an even more detailed description of what we are doing (at least on a partial level) is the Adafruit article on motor control. They have lots of pictures and a great explanation. But what their article is lacking is how to power a motor that needs much more voltage than what the Arduino can provide. That is why you are reading this article.

Here is my schematic of the build.

Electrical schematic of the project.

The Build Logic

I find it helps to try to understand what is happening in the electrical path so that I can complete the build and adjust the hardware or the code. Let me try to logically walk you through what is happening. Check out the video below for a rough verbal description.

The two connections to the Arduino are ground and digital pin 9 which is what sends the PWM signal to the transistor. PWM signal goes out of the Arduino into a resistor. I’m not entirely sure how much resistance is needed for this build. It works with no resistor and I have seen builds that call for up to a 2.2 kΩ. I don’t know that the resistor value is super critical, but you should probably include one.

So far we have PWM → resistor.

Now you go from the resistor to the base of the Darlington transistor. I am using a TIP122 because that is what I had handy. This should work identically with a TIP120 (and probably dozens of other transistors).

When the base of the transistor gets power from the PWM signal, it will allow power to pass through from the collector to the emitter. Check out Adafruit’s explanation of transistors for more details.

When the signal on the base pin triggers the collector/emitter connection, the transistor begins passing 12V from the power supply to the fan. The signal on the base is making the transistor act like a switch to turn on and off the collector/emitter connection.

Therefore, we kinda have 2 separate halves to this project:

  • The low power side (Arduino side) is PWM → resistor → base → emitter → ground.
  • The high power side is 12V positive → fan positive → through the motor → fan negative → collector → emitter → ground.

Between the collector and emitter we also insert a diode. This prevents the spinning of the fan (or DC motor) from pushing power back through the transistor and blowing up the Arduino. A DC motor (which a fan is) generates electricity when spinning but does not have outside power pushing it. So when the Arduino stops sending a signal to the fan the fan will spin down and in the process create a small DC voltage. In this case it is probably not enough to matter, but it is a good habit to put a diode in place to stop the newly created voltage from traveling backwards into the Arduino. As you probably know, a diode only allows the electricity to travel one direction. We want power to travel from the Arduino to the fan but not the other direction.

Both the ground coming from the Arduino and the ground of the 12V power supply need to be tied together. You should have only 1 ground in a circuit even if you have multiple power supplies and voltages.

In my video below you also see an LED and another resistor. This is just an indicator that power is being supplied. The second resistor is 100 kΩ. Also carefully chosen because of its proximity to my workspace.

Note that the code in the video turns the fan/LED full on and full off. However, the code I provide here is full on and only about 40% off. Being able to adjust the speed of the fan using PWM is what this project is about. Therefore you can adjust the provided code to run the fan at any speed you would like.

Code

This sets pin 9 as the control pin. Then makes its pinMode to be an output. The loop turns on pin 9 at 100% speed (255) for 3 seconds and then drops to 40% speed (100) for 6 seconds.

And that’s it!

The ultimate project is to control the fan based on temperature. Getting temperature values from a digital or analog temperature sensor on an Arduino is fairly simple. It then becomes a matter of telling the fan PWM to raise or lower the speed based on the temperature.

My Homemade Book Light

I can’t actually remember when I started my book light project, but a recent trip to the Atlanta Maker Faire motivated me to get back to some projects that I had worked on in the past but never finished. The book light was one that benefited from this renewed interest.

LED strip lights and 1/8" acrylic sheet.
LED strip lights and 1/8″ acrylic sheet.

My book light project wasn’t really as much about making a usable lamp as much as it was about learning how to work with certain materials. After buying a 5 meter roll of white LED lights for a bigger project, I wanted some simple lighting task to learn how to use them. I also had a partial sheet of 1/8″ acrylic (Plexiglas) that I wanted to do something with.

The project proposal was inspired by something I saw on Instructables. (At least I think it was there. I did a quick search and could not find it). I needed to hollow out a book then install the lights and acrylic sheet. That was the basics of the project. I also wanted to make a switch so that when the book opened it would automatically turn on the light. That switch mechanism is what delayed the project every time I got into it again.

Oh, and, as always for my projects, I wanted to spend as little money as necessary.

I got two books from a used book store that gives away books that they don’t feel have any resale value. I got two because my original plan was that I would practice on one and then I would make my final project from the nicer book. At this point, the second book light will never be made. I learned what I wanted from the project and don’t really plan to make the second one.

Preparing the Book

When gluing the pages together put wax paper between the cover and pages.
When gluing the pages together put wax paper between the cover and pages.

I closed the book with wax paper between the front cover and the rest of the book so I could glue all the pages together. This kept the front cover free from the glue I was about to apply. I painted the edges of the book with Elmer’s glue to hold them together. I think I may have fanned the pages a bit to make sure I got glue in between the pages as much as was practical and not just on the edges (I really don’t remember since it was well over a year ago that I started the project). While the book dried from the gluing, I pressed it down with other books on top. I wanted to make sure the pages didn’t wrinkle too much.

Cutting out the Pages

I figured out how much margin I wanted to leave in the book and cut everything inside that line using an array of X-Acto knives and box cutters. The variety in blades was because I kept thinking that something would make the process easier and quicker. It is just a tedious process. You should not try to cut more than a few pages at a time.

After the book was hollowed out I painted the inside of the pages with Elmer’s glue, put my wax paper back in the front cover and then weighted the book down again to dry. I don’t remember how long I let it all dry, but as you can tell, I was not in a hurry.

I painted the acrylic sheet with a frosted spray paint. I did this on both the inside and the outside of the acrylic. This helps to diffuse the lights and make a more even glow instead of seeing the harsh individual LEDs.

Lights!

Cut out pages, LEDs and frosted acrylic sheet. You can see the strips of wood I added later.
Cut out pages, LEDs and frosted acrylic sheet. You can see the strips of wood I added.

I installed a little wooden strip on the inside of the hollowed out area to act as a platform for the Plexiglas. I used contact cement for this. Hot glue would have probably been sufficient. The wood needed to be about 1/8″ below the top of the book pages so that the acrylic sheet could sit flush with the pages.

The LED strip has an adhesive back that helped me to attach it to the wood strips. However, I just used very rough scrap wood and did not think to sand the strips because they would not be seen. The problem with not sanding them is that the LED adhesive back did not stick too well. I had to hot glue some parts of the strip to the wood.

Power

The lights are 12V lights. With LED strips you have to be careful to make sure you have enough amperage to run the lights. However, since I am using such a small number of lights, I figure just about any 12V power supply that I have will be strong enough (I really should do the math and find out).

12V power supply. 5.5 mm plug and jack.
12V power supply. 5.5 mm plug and jack.

I have a box full of wall plugs (or wall warts, or AC-DC power supplies). I found one with the right plug and had a positive center polarity. Because I was wiring this up myself I could have reversed the wiring if I needed. However, what seems to be standard in the maker world (and is a standard in my personal construction) is that the center pin of an electronics project should be positive polarity. By always doing it this way I never have to guess as to how I wired a project. But you must check your power supply because they aren’t always wired this way.

Another seeming standard is the size of the plug and jack used in these types of projects. Unlike the commercial world where every manufacturer wants to have a different size plug and wiring standard, we use 5.5 mm plugs and jacks. Not the smallest, but easy to work with. Plus by using the same thing every time all you have to look for in your pile of power supplies is one with the right voltage.

A Painful Switch

The main thing that held up my project is I wanted a certain type of switch. My desire was to have the light automatically turn on when the book was opened. When explaining my project to a friend he introduced me to a reed switch. This is one that is activated in the presence of a magnet.

What I needed was a normally closed (NC) reed switch. The magnet would be on the cover of the book with the switch being near a metal plate under the diffused acrylic sheet. When the book was closed the magnet would stick to the metal plate and activate the switch. This activation of the switch would open the circuit and cause the light to go off. The normally closed reed switch allows the electricity to turn the lights on when the magnet was not close to it.

But going back to one of the premises of the project—spend as little money as necessary—I quickly found that reed switches could be had for next to nothing or be very expensive. A normally open (NO) reed switch cost about a quarter each if you didn’t go for the bottom of the barrel (the ones I bought were 11 cents each). But NC reed switches are about $2 each. I didn’t want to spend that much for a simple switch.

So the project went on hold every time I started into it again because I could not get the switch mechanism the way I wanted it. There is a fairly simple way to make an NO switch act like an NC switch using a transistor, but, the way I understand it, it uses a little bit of electrical current to not turn on the lights. If the book is plugged in and not being used it is still draining electricity. I didn’t want to do that.

Two magnets and a normally closed push button make up the on-off switch.
Two magnets and a normally closed push button make up the on-off switch.

Enter Maker Faire and my renewed interest. I dug through all the switches I have at the house and could not quite find what I needed. My friend who introduced me to reed switches said that he had an extra normally closed pushbutton left over from another project. It was much bigger than what I wanted, but I was able to make it fit the project.

Demagnetizing a Magnet

I needed more magnetic strength to close the book and push down the switch than what I had originally put into the book. So I replaced my metal closing plate with another magnet. However, because these were strong neodymium magnets, they were almost too strong to be able to open the book without tearing all the glue apart.

Book lights up just by opening it. It makes a nice reading lamp or mood lamp.
Book lights up just by opening it. It makes a nice reading lamp or mood lamp.

Did you know you can demagnetize a magnet? I found out through Google searching that you could heat a magnet and it would demagnetize it. This is because a magnet (at least a neodymium magnet) is made by heating the material inside a magnetic field. The hot molecules in the neodymium align to the polarity of the magnetic field. Keep the new magnet in the magnetic field as it cools off and those molecules are fixed in that North/South arrangement and you end up with a strong magnet. By heating the magnet outside of a magnetic field those molecule get all jumbled up and it is no longer a strong magnet. I assume this would work with any type of magnet, though you probably should not do it with rubbery craft magnets unless you like potential toxic materials filling your kitchen.

My wife cooked my magnet with lunch. 450 degrees for 10 minutes did the trick. It was still magnetic but not nearly as strong. The book opens and closes very easily now but is still strong enough to push the plunger on the push button switch.

Maker Faire Connection

I took my local librarian to the Maker Fair in Atlanta with me. She enjoyed all the different projects she saw. She went because she is interested in various STEAM programming that could be done in the library to attract young people. We have talked about me teaching various projects to kids at the library, but have not found the right project. We did know that we wanted to do something with soldering irons so the kids could learn that useful skill.

I was hesitant to take a book that I butchered into the library to show it off. But when I showed it to the librarian last week she immediately knew that is what she wanted to do for our Teen Tech Week program coming up in a few months. And, as barbaric as it sounds, she volunteered to gather the needed books and cut out the pages. I guess she would rather have the kids wielding 350 degree hot pokers than using a bunch of box cutters.

What I Learned

The purpose of this project was to be a learning experience. And it was. I learned that it is possible to weaken a magnet by heating it. I learned how to work with the LED light strips. I have even made another project since then using the LEDs—the tracing table that the book is sitting on when I took the pictures. I also learned that cutting out the guts of a book is harder than it looks. Though the switch isn’t what I originally planned, I learned that sometimes it is better to complete a project than to insist that it be a certain way and never be completed.

I also learned that not all librarians will weep at the sight of a hacked book.