Datasheet

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.
13
CIRC-03
More details, where to buy more parts, where to ask more questions:
http://ardx.org/CIRC03
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).
Controlling speed: In the loop() section change it to this
We played with the Arduino's ability to control the
// motorOnThenOff();
motorOnThenOffWithSpeed();
brightness of an LED earlier now we will use the same
// motorAcceleration();
feature to control the speed of our motor. The Arduino
Then upload the programme. You can change the speeds by
does this using something called Pulse Width
changing the variables onSpeed and offSpeed.
Modulation (PWM). This relies on the Arduino's ability to
operate really, really fast. Rather than directly
Accelerating and decelerating:
controlling the voltage coming from the pin the Arduino
Why stop at two speeds, why not accelerate and decelerate
will switch the pin on and off very quickly. In the
the motor. To do this simply change the loop() code to read
// motorOnThenOff();
computer world this is going from 0 to 5 volts many
// motorOnThenOffWithSpeed();
times a second, but in the human world we see it as a
motorAcceleration();
voltage. For example if the Arduino is PWM'ing at 50%
we see the light dimmed 50% because our eyes are not
Then upload the program and watch as your motor slowly
quick enough to see it flashing on and off. The same
accelerates up to full speed then slows down again. If you
feature works with transistors. Don't believe me? Try it
would like to change the speed of acceleration change the
out.
variable delayTime (larger means a longer acceleration time).
Still Not Working?
Sometimes the Arduino board
will disconnect from the
computer. Try un-plugging and
then re-plugging it into your
USB port.
Download the Code from ( http://ardx.org/CODE03 )
(then simply copy the text and paste it into an empty Arduino Sketch)
int motorPin = 9; //pin the motor is connected to
void setup() //runs once void motorOnThenOffWithSpeed(){
{ int onSpeed = 200;// a number between
pinMode(motorPin, OUTPUT); //0 (stopped) and 255 (full speed)
} int onTime = 2500;
int offSpeed = 50;// a number between
void loop() // run over and over again //0 (stopped) and 255 (full speed)
{ int offTime = 1000;
motorOnThenOff(); analogWrite(motorPin, onSpeed);
//motorOnThenOffWithSpeed(); // turns the motor On
//motorAcceleration(); delay(onTime); // waits for onTime milliseconds
} analogWrite(motorPin, offSpeed);
// turns the motor Off
/* delay(offTime); // waits for offTime milliseconds
* motorOnThenOff() - turns motor on then off }
* (notice this code is identical to the code we
void motorAcceleration(){
used for
int delayTime = 50; //time between each speed step
* the blinking LED)
for(int i = 0; i < 256; i++){
*/
//goes through each speed from 0 to 255
void motorOnThenOff(){
analogWrite(motorPin, i); //sets the new speed
int onTime = 2500; //on time
delay(delayTime);// waits for delayTime milliseconds
int offTime = 1000; //off time
}
digitalWrite(motorPin, HIGH);
for(int i = 255; i >= 0; i--){
// turns the motor On
//goes through each speed from 255 to 0
delay(onTime); // waits for onTime milliseconds
analogWrite(motorPin, i); //sets the new speed
digitalWrite(motorPin, LOW);
delay(delayTime);//waits for delayTime milliseconds
// turns the motor Off
}
delay(offTime);// waits for offTime milliseconds
}
}
NOT WORKING? (3 things to try)
MAKING IT BETTER
CODE (no need to type everything in just click)
MORE, MORE, MORE: