DC motors


What is a DC motor?

   DC motor is a rotating electrical machine that uses the direct current (DC) to produce mechanical energy from the electrical energy.

 How does it work?

 Its working principle depends on the magnetic and electric field. By exposing magnetic field into a current carrying conductor, a force is produced, which move the wire. The direction of the Movement depends on the current and field direction, where it is described using the left hand rule.

  How to connect it?  

DC motor has two leads, positive and negative. Switching the polarity switches the rotation direction. However to do that you need IC motor driver. Fortunately, Junkbot board has an embedded one. So all you need is too connect your motors into the motors pin directly. Note that the pins numbers are : 2,3,4 and 5.

how to program it ?

Remember that each motor has two pins. According to the embedded IC driver, one of them controls the speed and the other control the spinning direction. The motors are connected in pin number 2, 3, 4 and 5. 2 and 4 control the direction. Setting them as High moves the motors forward while low moves them backward. On the other hand, 4 and 5 control the speed since they are PWM pins. 

Here is an example code that moves the motors forward and backward when keys are pressed and stop it when keys are released.