Arduino Tutorial Bundle .:Arduino Experimentation Kit:. (ARDX)

 by oomlout
Featured

Step 5: .:Spin Motor Spin:. (Transistor & Motor) - CIRC03

CIRC03-square.jpg
CIRC03-assembled.jpg
CIRC03-pieces.jpg
CIRC03-3d.png
CIRC03-3dexploded.png
CIRC03-scem.png
CIRC03-sheet.png

What We're Doing:
The Arduino's pins are great for directly controlling small electric items like LEDs. However, when dealing with larger items (like a toy motor or washing machine), an external transistor is required. A transistor is incredibly useful. It switches a lot of current using a much smaller current. A transistor has 3 pins. For a negative type (NPN) transistor you connect your load to collector and the emitter to ground. Then when a small current flows from base to the emitter a current will flow through the transistor and your motor will spin (this happens when we set our Arduino pin HIGH). There are literally thousands of different types of transistors, allowing every situation to be perfectly matched. We have chosen a P2N2222AG a rather common general purpose transistor. The important factors in our case are that its maximum voltage (40 v) and its maximum current (600 milliamp) are both high enough for our toy motor (full details can be found on its datasheet http://tinyurl.com/o2cm93 )
note: the transistor we use has a in order: Base Collector Emitter pinout (differing from some other popular transistors)
(The 1N4001 diode is acting as a flyback diode for details on why its there visit: http://tinyurl.com/b559mx)

(you can also download the breadboard layout sheet from the bottom of this step)


The Parts:
  • CIRC-03 Breadboard sheet
  • 2 Pin Header (x4)
  • Transistor (P2N2222AG) (TO92) (x1)
  • 2.2k ohm Resistor (red-red-red) (x1)
  • Toy Motor (x1)
  • Diode (1N4001) (x1)


The Circuit and Plugging Everything In:
A Small Video of Everything Being Plugged in


The Code: - http://tinyurl.com/dagyrb
int motorPin = 9; //pin the motor is connected to                  void setup() //runs once{ pinMode(motorPin, OUTPUT); }void loop()        // run over and over again{ motorOnThenOff(); //motorOnThenOffWithSpeed(); //motorAcceleration();}/* * motorOnThenOff() - turns motor on then off  * (notice this code is identical to the code we used for * the blinking LED) */void motorOnThenOff(){  int onTime = 2500;  //on time  int offTime = 1000; //off time  digitalWrite(motorPin, HIGH);                  // turns the motor On  delay(onTime); // waits for onTime milliseconds  digitalWrite(motorPin, LOW);                   // turns the motor Off  delay(offTime);// waits for offTime milliseconds}void motorOnThenOffWithSpeed(){  int onSpeed = 200;// a number between                     //0 (stopped) and 255 (full speed)   int onTime = 2500;      int offSpeed = 50;// a number between                     //0 (stopped) and 255 (full speed)   int offTime = 1000;     analogWrite(motorPin, onSpeed);         // turns the motor On  delay(onTime);      // waits for onTime milliseconds  analogWrite(motorPin, offSpeed);        // turns the motor Off  delay(offTime);    // waits for offTime milliseconds}void motorAcceleration(){  int delayTime = 50; //time between each speed step  for(int i = 0; i < 256; i++){       //goes through each speed from 0 to 255   analogWrite(motorPin, i);   //sets the new speed   delay(delayTime);// waits for delayTime milliseconds  }  for(int i = 255; i >= 0; i--){       //goes through each speed from 255 to 0    analogWrite(motorPin, i);   //sets the new speed    delay(delayTime);//waits for delayTime milliseconds  }}


Not Working?
  • Motor Not Spinning? - If you sourced your own transistor, double check with the data sheet that the pinout is compatible with a P2N2222A (many are reversed)
  • Still No Luck? - If you sourced your own motor, double check that it will work with 5 volts and that it does not draw too much power.
  • Still Not Working? - Sometimes the Arduino board will disconnect from the computer. Try un-plugging and then re-plugging it into your USB port.


Making it Better:
Controlling Speed:
We played with the Arduino's ability to control the brightness of an LED earlier now we will use the same feature to control the speed of our motor. The arduino does this using something called Pulse Width Modulation (PWM). This relies on the Arduino's ability to operate really really fast. Rather than directly controlling the voltage coming from the pin the Arduino will switch the pin on and off very quickly. In the computer world this is going from 0 to 5 volts many times a second, but in the human world we see it as a voltage. For example if the Arduino is PWM'ing at 50% we see the light dimmed 50% because our eyes are not quick enough to see it flashing on and off. The same feature works with transistors. Don't believe me? Try it out.

In the loop() section change it to this
// motorOnThenOff();   motorOnThenOffWithSpeed(); //motorAcceleration();
Then upload the programme. You can change the speeds by changing the variables onSpeed and offSpeed

Accelerating and decelerating:
Why stop at two speeds, why not accelerate and decelerate the motor. To do this simply change the loop() code to read
// motorOnThenOff();// motorOnThenOffWithSpeed();   motorAcceleration();
Then upload the program and watch as your motor slowly accelerates up to full speed then slows down again. If you would like to change the speed of acceleration change the variable delayTime (larger means a longer acceleration time)


CIRC03-sheet.pdf(612x792) 138 KB
 
Remove these adsRemove these ads by Signing Up
daveard says: Mar 3, 2013. 1:56 PM
I am just getting started with Arduino and am having a problem with Circ03. The motor will run, but does not go on and off, and the variable speed just runs at the same speed.
bar8393 says: Oct 14, 2012. 9:15 AM
N00b here. I've built the Circ01 and Circ02 projects and now as i hit Circ03 the question still remains:

Exactly what are the 2-pin Headers for?

thanks
doverland says: Mar 11, 2012. 3:18 PM
I just got the kit, and I couldn't get this to work until I changed out the 10K resistor with the other 330 resistor.
Troubleshooting was educational, though!
jmj6879 in reply to doverlandAug 31, 2012. 10:04 PM
Same here.. I spent over an hour troubleshooting this same project and while I swapped out the resistor for another 10k, I would've never changed to the 330. I was about to pull my hair out and found your comment.. much appreciated.. that leaves some question in my mind as to why this is the case and how someone learning these circuits is supposed to figure something like that out. I love the Arduino but this sparkfun kit has a number of flaws..
-pink says: Jan 4, 2012. 3:34 PM
This seems like a great kit, but I still cant understand how to make the motor run both ways.. Did anybody manage and is it at all possible?
I am also planning to combine several to contol 5 motors. Did anybody try that yet?

I will be very greatful for your help!

Pink
Metalshadow24 says: Dec 21, 2011. 4:15 PM
how can you make the motor run in forward and reverse? is there a series of code that I can type in, or is it all in how I plug it up? thanks, Andrew
tuttlesmow says: Sep 12, 2011. 7:14 PM
NEVERMIND- i was using my datasheet for a 2n2222, not the one provided with the link (p2n2222). now i guess i know what that tiny "p" means.
tuttlesmow says: Sep 12, 2011. 4:10 PM
what i thought i knew is now suspect. on the datasheet for the p2n2222ag (and what i thought i already knew) when the flat side of the npn transistor facing me, with a to-92 package,with the pins down, the order from left to right is, emitter, base, and collector on the right. why is this not true on the layout sheet. ?
enxion says: Jul 5, 2011. 8:24 PM
I tried this out and got it to work. Thanks for the tutorial I learned a thing or two.

I am however, wondering why the same circuit doesn't work when I replace the 5v and ground from the arduino with a power and ground from a 6v battery pack. Shouldn't it? The motor and transistor are rated for such loads. The only thing I've changed is the power source. I'm trying to make the jump from learning about motor controllers to actually constructing a useful one with it's power independent of the arduinos power.

I'd appreciate any help any one could point out.
Will2MakesStuff in reply to enxionAug 22, 2011. 4:36 PM
i had a similar problem, where i was powering the arduino and motors from separate power sources.

It turned out they both had to share the same ground, i just plugged the negative terminal from my battery pack in to the ground rail on my breadboard and then connected a ground pin from my arduino to the same breadboard rail, then everything worked
enxion in reply to Will2MakesStuffAug 22, 2011. 7:18 PM
Thanks for the reply. I've moved on since I posted that but I'll no doubt be reviewing it some day. You might want to post that message in the comments so others can see it and learn.
biggestnoob says: Jul 18, 2010. 6:31 PM
Hi there, I am wondering if I can run my dc motor reverse. is there any special code to make it reverse? if there is, please tell me. thank you,
jrigvd in reply to biggestnoobSep 25, 2010. 11:57 AM
the red wire is normally going in the + rail, pull it out and connect it to the Collector of the transistor and the black wire coming from the motor goes into the + rail
eshneto says: Feb 13, 2010. 10:10 AM
 Hello there, nice job,thanks! Maybe you could help me here.

I am trying to figure out why is the 2.2k resistor required? I have seen in the transistor datasheet something like "Emitter-Base voltage = 6.0Vdc", doesn't it mean that we could plug the device directly to Arduino?

I am trying to figure out if I may use a 2N60B instead, since I have one of these here. I just don't know why would I need a resistor.

Thanks in advance.

P.S.: The code appears without line breaking (using google-chrome for Linux) for me.
olmstw in reply to eshnetoFeb 14, 2010. 1:49 PM
You ask very intelligent questions... (NO sarcasm intended)

The 2N60B, however would not be a good choice here, as it is a MOSFET not a JUNCTION transistor.  The gate voltage required for full turn-on is 10V, TWICE the output voltage of the Arduino.   Also, the Rds(on) of the 2N60B with 10V applied to the gate can be as high as 5 ohms. This means that the transistor behaves as if there were a 5 ohm resistor in series with the output.  If the Motor draws any kind of current at all, the votage dropped across the transistor (Drain to Source) would cause the motor to run slow (or not at all).

You can see the data sheet here: http://pdf1.alldatasheet.com/datasheet-pdf/view/54779/FAIRCHILD/2N60B.html

A better choice, in this case, if you cannot find a 2N2222, would be a 2N3904.

Wayne
olmstw in reply to eshnetoFeb 14, 2010. 1:17 PM
The "Emitter-Base voltage = 6.0Vdc" statement may be the confusing thing here; The maximum REVERSE voltage that may be applied to the Base-Emitter junction is ~6V But the normal FORWARD drob across the base-emitter junction is approximately 0.7V...  The current into the base MUST be limited to prevent the transistor form being distroyed be excessive base current.

Since the 0.7V forward base voltage is significantly less than the 5V output of the Arduino port pin, then the current flowing into the base of the transistor would likely exceed the rated maximum of both the Arduino pin AND the rated maximum input current od the base of the transistor.

The function of the resistor is to prevent damage (caused by excessive current) to either the transistor or the Adrion. 
eshneto in reply to olmstwFeb 14, 2010. 9:11 PM
 Thank you for your attention ;)

I found the mentioned info in the dataheet. Still I did not understand some details, however. How do I get to the 2.2k value? Am I supposed to assume a 2mA current at the base so that r=v/i=4.3/0.002=2150? How do I get that info in the sheet?

I am right now using a photocoupler to do the job of swtching the motor, I will make an instructable soon, but I still am not sure about the resistor values.

olmstw in reply to eshnetoFeb 15, 2010. 6:11 AM
If you are using a bipolar transistor (2n2222, 2n3904 etc), read the data sheet...

1. determine the current that the transistor must switch and divide it by the Hfe (current gain of the transistor at the desired load current per the data sheet)

2. multiply the result by 2 (safety margin)....  this is the ideal base current for your application

3. subtract 0.7V (the base-emitter drop) from the drive voltage from the Arduino pin (use the data sheet and the Minimum guaranteed Vh out.

4. divide this number by the base current you determined in 2 above.

5. this is the base resister value you need to use.

works every time ^_*

eshneto in reply to olmstwFeb 15, 2010. 8:21 AM
 Thanks, that's what I wanted to know: how to determine the base current. Now I know it is obtained by dividing the switched current by the transistor's Hfe.

Again thanks a lot!
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!