   Table of content

## Motor

A motor is a rotatory mechanical device that is made up of coil and magnetic arrangement, such that, when current flows through the coil, an electromagnetic field gets induced inside it which works against the magnetic field and thus the shaft is rotated.

A motor is used in different types of applications from electrical to mechanical devices and machines. Some of the most appreciated applications of motors are:

• Electric Machines : When we say about machines, we are talking about something that can move. It may be a linear machine or a rotatory machine, the main moving framework which is behind any dynamic machine is a motor which is a rotatory device.
• Mechanical Machines : What we look around the world which is full of machines is nothing, but mechanical machines. Even the power window of the car or the drone you see in the sky cannot work without a motor. Another example is the washing machine, which is nothing but a machine that works at a very high speed.
• Robotics : In Robotics, there are various components that are dynamic in nature. Such as the wheel or the leg part of the robot or the head which has to rotate at some angle.

There are different types of motion, such as linear motion, circular motion or angular motion. Behind any kind of motion, there is a motor that provides the force required.

Let us consider the Servo, it is nothing but a motor with different gear attachments. A small motor call really spins at a very high speed irrespective of its size. But the speed can be converted into linear or circular motion with more torque but with less speed.

This theory can be applied in the case of a Servo in which there is a small motor, but it spins at very high speed and with the help of different gears, the high speed is converted into angular motion with greater force.

## DC Motor with Arduino

We shall take the example of the 5v DC motor, as the power for the motor will be drawn directly from the Arduino Board itself.

A DC Motor has two pins, one is the minus(- or negative) and the plus (+ or the positive.) When the + terminal of the motor is attached to the positive terminal of the power source and the - terminal of the motor is attached to the negative terminal of the power source, the motor shaft rotates in the clockwise direction.

#### Rotating the motor clockwise:

Components Required :

• 5v DC Motor
• Arduino UNO Board
• Jumper Wires

Connecting the motor pins into the Arduino Board :

• Connect the + terminal of the motor into the PIN 3 of the Arduino(Analog PIN)
• Connect the - terminal of the motor into the PIN GND of the Arduino

Now edit the following code into the Arduino IDE

``````int motordrive = 3;
void setup() {
}
void loop() {
digitalWrite(motordrive, HIGH);
}``````

Program Analysis :

• The variable `motordrive` is assigned the analog pin 3 of the Arduino.
• The function `void setup()` is an empty function as no other pins and modules have to be set up.
• Inside the function `void loop() `we have used the function digitalWrite() to set the PIN 3 as high.

#### Rotating the motor clockwise and anti-clockwise

Connecting the motor pins into the Arduino IDE :

• Connect the + terminal of the motor into the PIN 3 of the Arduino(with PWM property)
• Connect the - terminal of the motor into the PIN 5 of the Arduino (with PWM property)

To rotate the motor in both the direction, we have to connect the (-) terminal into the PIN 3 and the (+) terminal into the PIN 5.

To rotate the motor in a clockwise direction, the minus pin (which is attached to PIN 3) has to be set HIGH and the plus pin (which is attached to PIN 5) has to be set LOW.

Then we have induced a delay of 1000 ms(1 sec) to keep the motor rotating for 1 second.

And then to rotate the motor in an anti-clockwise direction, the minus pin (which is attached to PIN 3) has to be set LOW and the plus pin (which is attached to PIN 5) has to be set HIGH.

Now edit the following code into the Arduino Editor :

``````int motorright = 3;
int motorleft = 5;
void setup() {
}
void loop() {
digitalWrite(motorright, HIGH);
digitalWrite(motorleft, LOW);
delay(1000);
digitalWrite(motorright, LOW);
digitalWrite(motorleft, HIGH);
}``````

Program Analysis :

• The variable `motorright` is assigned with PIN 3 of the Arduino.
• The variable `motorleft` is assigned with PIN 5 of the Arduino.
• Inside the function `void loop()`, the `digitalWrite()` function sets the variable `motorright` into HIGH and sets then sets the `motorleft` variable into LOW. This makes the motor spin into a clockwise direction.
• Then we set a delay of 1000 ms (1 Sec), to keep the motor rotating in the clockwise direction.
• Then again the `digitalWrite()` function sets the variable `motorleft` into HIGH and sets the variable `motorleft` into the LOW. This makes the motor spin in an anti-clockwise direction.

#### Controlling the Speed of the Motor:

To control the speed of the motor, the motor has to be fed with voltage from 0 to 5 with a discrete interval of values. For example, the Arduino provides a range of 0 to 255, for dividing the voltage levels between 0 to 5v.

So, now we shall use any of the I/0 pins which have the property of pulse width modulation, which is the one which can output analog values.

Connecting the Arduino PINs into the Arduino Editor :

• Connect the positive terminal into the PIN 3 of the Arduino.
• Connect the negative terminal into the GND pin of the Arduino.

Now edit the following code into the Arduino IDE :

``````int mypin = 3;
void setup() {
pinMode(mypin, OUTPUT);
Serial.begin(9600);
while (!Serial);
Serial.println("Enter any values between 0 to 255");
}
void loop() {
if (Serial.available()) {
int speed = Serial.parseInt();
if (speed >= 0 && speed <= 255) {
analogWrite(mypin, speed);
}
}
}``````

Program Analysis :

• `mypin` is the variable assigned with PIN 3 of the Arduino.
• Inside the `void setup()` function, the pin variable `mypin` is set as an OUTPUT.
• Inside the `void setup()` function, the `if(Serial.available())` checks whether any data is available at the serial port or not.
• And if any data is available at the port, the function `parseInt()` will return any non-negative integer found in the sequence.
• And the `analogWrite()` function will send different voltages into the pin `mypin` as per the value of the variable `speed`.

Now edit the code into the Arduino IDE