Lesson/Homework 3: transistors to control high current circuits

posted Mar 3, 2015, 6:26 AM by Earl Morse   [ updated Jul 29, 2016, 9:38 AM ]
In these sessions we'll be looking at using transistors to control small d.c. motors.  As you'll recall, we can only use our Arduinos to directly power low current 5 Volt d.c. devices.  If we try to pass more than about 150 milliamps through our Arduino we stand a good chance of blowing it out.  Neither Arduinos nor USB ports can stand more than about 150 milliamps before they become toast. Up till now we've been using LED's which draw only about 20 milliamps each and voltage divider circuits which use even less.  

However, we can use our Arduino to trigger or regulate devices that use much more current, or even different d.c. voltages.  In order to do this well we suggest setting up your breadboard like this:

To do this, we use transistors or similar components (MOSFET's usually) to electronically switch on/off devices that are wired to our high current bus. The Arduino simply sends a signal to the base pin of the transistor (via a current limiting resistor)  telling it to switch the high current bus.  No high current passes through the Arduino.  In general, any Arduino sketch that controls LED's (like Fade, for example) can be used to fire a transistor's "base" pin and switch on a higher current device like a motor, servo, siren, or large array of LED's, Moreover, you can have the high power bus be a different DC Voltage, like 12Volts, to power automotive devices like windshield washing pumps. The grounds of the busses must still be connected together as in the above diagram.  Please note that the little 2N2222 transistor can only handle smallish currents and you may have to use MOSFETS or high current N-type transistors if your high power device draws too much current.  

Here's how a transistor works:

A transistor is a device that uses a very tiny current from any Arduino PWM pin to control a much larger current to flow through it. We'll use a N-type transistor called a 2N222.  We'll also use a protection diode to protect our Arduino and transistor.

Here's a good website of how a transistor works to control a motor:

There is a major problem in powering d.c. motors with the Arduino.

DC motors can generate electricity!

D.C. motors are made with permanent magnets and will work as generators if an LED is attached to them and the motor shaft is spun in one direction by hand.  If you attach a battery, they work as a motor (using electricity)... BUT, if you remove the battery, connect an LED directly to the motor, and spin the motor in both directions one of the spun directions will generate electricity and light the led. In a motor control circuit the generated current can damage the Arduino.   So when your Arduino controls your motor it can be used to turn it on or off and even regulate its speed... BUT AS SOON AS YOUR ARDUINO STOPS POWERING YOUR MOTOR IT KEEPS ON SPINNING FOR A SECOND OR SO BECOMES A GENERATOR, GENERATING ELECTRICITY which can feed back into the Arduino damaging it.  So our circuit must have a diode in it to prevent this generated current from getting back to the Arduino.

Here's Jeremy Blum's #5 tutorial  video to help you understand.  You need to view it only to 7:35. We'll save the servo section for later:

Jeremy shows a capacitor and a diode in the circuit parallel to the motor.  While the diode is absolutely needed in the circuit the capacitor isn't required with our motors.  The transistor's base is connected to the PWM 9 pin in our circuit. Be sure to copy Jeremy's code into a blank sketch and try it out.  Make sure the diode's white band is oriented correctly. The diode's white band faces the +5Volt high current bus.

Here's another (more advanced) site you might find helpful:

Here's some additional code to allow you to set the speed of your motor (setmotorspeedinserialbox.ino). 

Here's another Arduino sketch that will fade control a motor (fademotororledonpin9.ino).  It's a bit like Jeremy's code... but a little different.Go to our Download page, download it and try it out. 

You can use the light or temperature sensors to set a threshold to turn the motor on or off using the IfStatementConditional sketch found in the Arduino Control directory.  

Here's a good explanation of how Pulse Width Modulation (pwm) is used to control motor speed or LED brightness. You only need to view up to 5:00, we'll get to servos later.


H bridges can be to control motor direction or, in our case,  pulse our latching EHCOtech DDT-ML-4.5vdc water control solenoid between "ON" and "OFF" ):  This $13 solenoid needs to have the 5V current's polarity reversed for 0.2 seconds to control the garden hose water to our reservoir, topping off our system.

We use an H-bridge integrated circuit L293D (about $1) to pulse the solenoid.  It has all the needed component on-board and is easy to set up. The solenoid replaces the motor in the diagrams below.

If you ever want to use motors to control a wheeled robot you'll have to control Both the speed and direction of at least two motors using "H" bridge circuits. We won't cover them here, but here's a site with some general information:

Keep in mind that we would use four transistors (rather than mechanical switches) to make an H bridge motor control circuit.

Here's another video explaining H bridge circuits:

Circuit diagram of how four transistors can be switched to reverse the direction of a DC motor.
Please note that in the diagrams below the top transistors used (2n4403) are PNP type, different from the NPN type 2N2222 transistors we are currently using. The bottom transistors (2n4401) are NPN types similar to our 2N2222. In any case,  you'll be able to see how an H bridge can reverse the direction of current flowing through the motor causing the motor to reverse direction.  The red line shows the path of current through the motor to ground.