Motor Control
Anand RamaswamiShare
Welcome Back, Maker!
You have controlled LEDs. You have read buttons. You have measured light and voltage.
Now it is time to make something move.
In this blog we are going to control a DC motor — one of the most exciting parts of any robotics project. By the end of this blog your motor will spin forward, spin backward, start, stop, and even change speed.
This is where electronics starts feeling like real engineering.
What Is a DC Motor?
Have you ever opened up an old toy car or a broken fan and found a small cylinder with two wires sticking out? That little cylinder is a DC motor and it is one of the most useful inventions in the history of electronics.
A DC motor converts electrical energy into rotation.
Give it power ,it spins. Remove the power , it stops. Reverse the direction of the power , it spins the other way.
That is it. That is all a DC motor does.
But do not let that simplicity fool you. That spinning motion is what powers:
- Robot wheels
- Drone propellers
- Electric fans
- Toy cars
- Conveyor belts
- Electric toothbrushes
- Hard drives inside computers
Almost everything around you that moves electrically has a DC motor inside it somewhere.
What Is Inside a DC Motor?
Inside every DC motor there are two main parts:
The Stator — this is the outer part. It contains permanent magnets that stay fixed and never move. These magnets create a magnetic field inside the motor.
The Rotor — this is the inner part. It is a coil of wire wrapped around an iron core. It sits inside the magnetic field created by the stator and is free to spin.
When you pass electricity through the rotor coil, it creates its own magnetic field. This magnetic field interacts with the stator's magnetic field and because opposite magnetic poles attract and like poles repel, the rotor gets pushed and pulled and starts to spin.
A small component called the commutator keeps switching the direction of the current in the rotor coil as it spins , this keeps the rotor spinning continuously in one direction instead of just rocking back and forth.
The faster you push electricity through the motor, the faster it spins. And here is the cool part , if you reverse the direction of the current, the motor spins the other way.
That is exactly how we control a DC motor with Arduino by controlling the direction and amount of current.
Why Can't We Connect the Motor Directly to Arduino?
An Arduino pin can supply a maximum of about 40 milliamps of current. That is just enough to light up an LED.
A DC motor needs much more current than that — sometimes 10 to 20 times more. If you connect a motor directly to an Arduino pin and turn it on, one of two things will happen:
- The Arduino pin gets permanently damaged, or
- The whole board stops working
Neither of those is fun.
This is why we always use a motor driver between the Arduino and the motor.
What Is a Motor Driver?
A motor driver is a special chip that acts as a middleman between the Arduino and the motor.
The Arduino sends small, safe control signals to the motor driver. The motor driver uses those signals to switch a much larger current to the motor safely.
Think of it like this.
Imagine you want to open a very heavy gate. You are not strong enough to push it directly. But there is a small button on the wall connected to a powerful electric motor that opens the gate for you. You press a small button , the motor does the heavy work. Your small press controls something much more powerful.
The Arduino is your finger pressing the button. The motor driver is the powerful gate motor. The DC motor is the gate.
The great news — on the NanoMake Pro the motor driver is already built into the board. You do not need to buy or connect any extra module. It is sitting right there on the board ready to go.
How Does the Motor Driver Control Direction?
The motor driver uses a circuit called an H-Bridge inside it.
An H-Bridge is a clever arrangement of four electronic switches. By opening and closing different combinations of these switches, the circuit can send current through the motor in either direction.
Here is the simple version:
- Combination A → current flows left to right through the motor → motor spins forward
- Combination B → current flows right to left through the motor → motor spins backward
- All switches open → no current flows → motor stops
The Arduino controls which switches are open and closed by setting two pins HIGH or LOW. You do not need to worry about the switches themselves — the motor driver chip handles all of that internally. You just set the pins and the motor does the rest.
Motor Ports on the NanoMake Pro
Look at your NanoMake Pro board. On the left side you will see two red LEDs labeled LED1 and LED2, and right next to them the label MOTOR. That is Motor Port 1.
Motor Port 1 is controlled by two pins:
|
Pin |
Role |
|
D5 |
IN1 — controls one side of the motor |
|
D6 |
IN2 — controls the other side of the motor |
By setting these two pins HIGH or LOW in different combinations you can control exactly what the motor does:
|
D5 |
D6 |
What Happens |
|
HIGH |
LOW |
Motor spins forward |
|
LOW |
HIGH |
Motor spins in reverse |
|
LOW |
LOW |
Motor stops |
There is also something cool happening on the board — LED1 and LED2 light up to show the motor direction. When the motor goes forward LED1 glows. When it reverses LED2 glows. When it stops both go off. This visual indicator helps you understand exactly what the motor is doing at any moment.
Keep this table in your head. Everything in this blog comes back to these three combinations.
Now we have everything we need. Let's build!
Project 1: Motor Forward and Reverse Using Buttons
Let's start simple and fun. In this project Button 1 makes the motor spin forward and Button 3 makes it spin in reverse. If neither button is pressed the motor stops.
You are directly controlling the direction of a spinning motor with your fingers. That is the essence of robotics — human input controlling machine movement.
What You Need:
- 1 × NanoMake Pro board
- 1 × DC motor
- 1 × USB-C cable
Pin Connections:
|
Component |
Pin |
Note |
|
Button 1 |
D2 |
Already on board |
|
Button 3 |
D8 |
Already on board |
|
Motor IN1 |
D5 |
Already wired on board |
|
Motor IN2 |
D6 |
Already wired on board |
|
DC Motor + wire |
Motor Port 1 — Pin 1 |
Connect red wire here |
|
DC Motor - wire |
Motor Port 1 — Pin 2 |
Connect black wire here |
|
Power |
USB-C port |
Power |
Code:
|
void setup() { pinMode(2, INPUT); // Button 1 - Forward pinMode(8, INPUT); // Button 3 - Reverse pinMode(5, OUTPUT); // Motor IN1 pinMode(6, OUTPUT); // Motor IN2 } void loop() { int btn1 = digitalRead(2); // Read Button 1 int btn3 = digitalRead(8); // Read Button 3 if (btn1 == HIGH) { // Forward digitalWrite(5, HIGH); digitalWrite(6, LOW); } else if (btn3 == HIGH) { // Reverse digitalWrite(5, LOW); digitalWrite(6, HIGH); } else { // Stop digitalWrite(5, LOW); digitalWrite(6, LOW); } } |
Explanation:
pinMode(2, INPUT) and pinMode(8, INPUT) set up both buttons as inputs. On the NanoMake Pro the buttons have external pull-down resistors so we use INPUT — not INPUT_PULLUP.
pinMode(5, OUTPUT) and pinMode(6, OUTPUT) set the two motor control pins as outputs.
digitalRead(2) and digitalRead(8) read the state of both buttons every loop.
When Button 1 is pressed — btn1 == HIGH — D5 goes HIGH and D6 goes LOW. The motor driver sees this combination and sends current through the motor in the forward direction. Watch LED1 light up on the board at the same time.
When Button 3 is pressed — btn3 == HIGH — D5 goes LOW and D6 goes HIGH. The current reverses and the motor spins the other way. Watch LED2 light up instead.
When neither button is pressed — both pins go LOW — no current flows and the motor stops. Both LEDs go off.
Press Button 1 and feel the motor spin forward. Press Button 3 and feel it reverse instantly. Let go of both and it stops. You just built a manual motor controller!
Output:

Project 2: Motor Speed Steps Using PWM
Now let's control speed. In this project the motor automatically steps through three speed levels — slow, medium, and fast — pauses, and then repeats. You do not need to press anything. Just watch and listen.
Close your eyes and listen to the motor. You can actually hear it spinning faster and slower with each step. That sound is PWM controlling electrical power in real time.
What You Need:
- 1 × NanoMake Pro board
- 1 × DC motor
- 1 × USB-C cable
Remember PWM from the previous blog?
One More Thing — PWM and Motor Speed
PWM lets us control how much power a pin delivers by switching it ON and OFF very rapidly. We used it to control LED brightness.
The exact same technique works for motors.
Instead of digitalWrite() which only gives full ON or full OFF we use analogWrite() which gives any speed in between.
|
analogWrite Value |
Motor Speed |
|
0 |
Stopped |
|
80 |
Slow |
|
150 |
Medium |
|
255 |
Full speed |
Code:
|
void setup() { pinMode(5, OUTPUT); // Motor IN1 pinMode(6, OUTPUT); // Motor IN2 Serial.begin(9600); } void loop() { digitalWrite(6, LOW); // Fix direction as forward // Slow Serial.println("Speed: SLOW"); analogWrite(5, 150); delay(2000); // Medium Serial.println("Speed: MEDIUM"); analogWrite(5, 200); delay(2000); // Fast Serial.println("Speed: FAST"); analogWrite(5, 255); delay(2000); // Stop Serial.println("Speed: STOP"); analogWrite(5, 0); delay(2000); } |
Explanation:
digitalWrite(6, LOW) fixes IN2 as LOW throughout the whole project. This locks the direction as forward. We only use PWM on IN1 to vary the speed.
analogWrite(5, 150) sends a PWM signal of 150 to the motor. The motor turns slowly.
analogWrite(5, 200) steps up to 200 to a motor . The motor speeds up noticeably.
analogWrite(5, 255) gives the motor power. It runs at maximum speed.
analogWrite(5, 0) cuts power completely. The motor stops.
Each speed level lasts 2 seconds before stepping to the next. Open the Serial Monitor and you will also see the current speed level printed — SLOW, MEDIUM, FAST, STOP — as the motor steps through each one.
Output:
Project 3: Start and Stop Motor Using Button
In this project Button 1 is a toggle switch for the motor. Press it once — the motor starts. Press it again — the motor stops. Press it a third time — it starts again.
This is called a toggle and it is everywhere in real life. The power button on your fan, your drill, your electric toothbrush — they all work exactly this way. One button. Press to start. Press to stop.
Code:
|
bool motorRunning = false; // Tracks motor state - starts OFF void setup() { pinMode(2, INPUT); // Button 1 pinMode(5, OUTPUT); // Motor IN1 pinMode(6, OUTPUT); // Motor IN2 Serial.begin(9600); } void loop() { int btnState = digitalRead(2); if (btnState == HIGH) { motorRunning = !motorRunning; // Flip the state if (motorRunning) { digitalWrite(5, HIGH); digitalWrite(6, LOW); Serial.println("Motor ON ▶"); } else { digitalWrite(5, LOW); digitalWrite(6, LOW); Serial.println("Motor OFF ■"); } delay(300); // Prevent multiple triggers from one press } } |
Explanation:
bool motorRunning = false creates a true/false variable that remembers whether the motor is currently ON or OFF. It starts as false — motor is off.
digitalRead(2) reads Button 1 every loop.
if (btnState == HIGH) checks if the button is pressed. On the NanoMake Pro pressed means HIGH.
motorRunning = !motorRunning flips the value every time the button is pressed. If the motor was OFF it becomes ON. If it was ON it becomes OFF.
if (motorRunning) checks the new state. If the motor should be running we set D5 HIGH and D6 LOW. The motor starts spinning and LED1 lights up on the board.
else covers the stopped state — both D5 and D6 go LOW. The motor stops and both LEDs go off.
delay(300) adds a short pause after each press. Without this the Arduino might detect one press as many presses because your finger is still touching the button while the loop runs multiple times very fast.
Try it. One press to start. One press to stop. One press to start again. Watch LED1 on the board go on and off with each press.
Output:


Project 4: Potentiometer Controls Motor Speed
This is the most satisfying project in this blog. The potentiometer knob on your NanoMake Pro directly controls how fast the motor spins — in real time, smoothly and continuously.
Turn the knob to the left — motor slows down and stops. Turn it to the right — motor gradually speeds up to full power. Every position of the knob gives a different speed. You are the speed controller now.
What You Need:
- 1 × NanoMake Pro board
- 1 × DC motor
- 1 × USB-C cable
Code:
|
void setup() { pinMode(5, OUTPUT); // Motor IN1 pinMode(6, OUTPUT); // Motor IN2 Serial.begin(9600); } void loop() { int potValue = analogRead(A6); // Read potentiometer int speed = map(potValue, 0, 1023, 0, 255); // Convert to PWM range digitalWrite(6, LOW); // Fix direction as forward analogWrite(5, speed); // Set motor speed Serial.print("Knob: "); Serial.print(potValue); Serial.print(" Motor Speed: "); Serial.println(speed); delay(100); } |
Explanation:
analogRead(A6) reads the potentiometer on pin A6. The value is between 0 and 1023 depending on how far you have turned the knob.
map(potValue, 0, 1023, 0, 255) converts that value into the 0 to 255 range that analogWrite() needs. If the knob is at halfway the motor gets about half power — around 127.
digitalWrite(6, LOW) keeps IN2 fixed as LOW throughout this project. This locks the direction as forward so only the speed changes.
analogWrite(5, speed) sends the PWM signal to the motor driver on pin D5. The motor speed instantly changes to match the knob position.
The Serial Monitor prints both the raw potentiometer reading and the converted motor speed side by side. Open it and watch the numbers change in real time as you turn the knob — and feel the motor respond at the same time.
At 0 the motor is completely stopped. At 255 it is running at full speed. Everything in between is a smooth controlled speed set by your hand on the knob.
That connection between your hand and the spinning motor — that is the whole point of learning electronics.
Output:

Real-Life Examples of Motor Control
When you press the accelerator in an electric car, a controller reads how far you pressed the pedal and sends a proportional PWM signal to the motor. Gentle press — slow speed. Hard press — full power. That is motor speed control using PWM — exactly like Project 4.
In a drone, four motors spin at slightly different speeds to tilt, rotate, and hover. The flight controller adjusts each motor's PWM signal hundreds of times per second to keep the drone stable in the air. That is motor direction and speed control — exactly like what you just built.
In a washing machine, the drum motor spins forward for washing and reverses direction for rinsing. The same H-Bridge principle we used in Project 1 is inside every washing machine on the planet. That is motor direction control.
The common idea in all of these is simple — control the direction and the speed of a motor and you can make almost anything in the world move.
What You Learned in This Blog
You started this blog knowing nothing about motors. Now look at what you know.
You know what a DC motor is and what is inside it — the stator, the rotor, the commutator, and how magnetism creates rotation.
You know why you cannot connect a motor directly to Arduino and why a motor driver is needed.
You know what an H-Bridge is and how it controls direction by switching current flow.
You know how to use digitalWrite() to control direction and analogWrite() to control speed.
You know that on the NanoMake Pro the buttons have external pull-down resistors — so pressed means HIGH and not pressed means LOW.
You know how to connect a real DC motor to the NanoMake Pro using the 2-pin Motor Port 1 header and power it all from a single USB-C cable.
And most importantly — you built four real projects that actually move something in the physical world.
A spinning motor responding to your button press. A motor changing speed with the turn of a knob. These are not just beginner exercises — these are the exact same principles used in robots, drones, electric vehicles, and industrial machines.
You are thinking like an engineer now.
Keep experimenting, keep building, and keep making.