Now type this code in Arduino:
void setup()
{
pinMode (12,OUTPUT);
pinMode (13,OUTPUT);
}
void loop()
{
digitalWrite (12,HIGH);
digitalWrite (13,LOW);
analogWrite (10,120); //this uses pin 10 to set the motor to half speed
delay(1500);
analogWrite (10,255) //sets motor to full speed
delay(1500);
}
Pin 10 in the Arduino is marked with a *. This means it can be used to control the speed of a motor. This is done using the analogWrite command.
analogWrite(10, 0) will make the motor stop completely.
analogWrite (10,255) will make the motor go full speed.
You can get other speeds by using numbers between 0 and 255
The H-Bridge chip which you used in the last unit can also be used to control the speed of the motor. You just have to change one wire from the circuit you have already used. It is the purple wire shown in the picture below:
Here is an example of some code to get the motor working with pwm control of the speed:
Here is a more advanced program you can try to gradually increase the speed and then slow it down again:
The program above only speeds up the motor. To slow it down use this command:
for(n = 20; n>1; n--) This starts at n = 20 and makes n get smaller each time
Can you make the motor speed up and then slow down? It's just another few lines added to the program above.