📖 Scenario: You have a small DC motor connected to an Arduino through a transistor driver. You want to control the motor speed by sending a PWM signal from the Arduino to the transistor base pin.This project will guide you step-by-step to write the Arduino code to control the motor speed.
🎯 Goal: Build an Arduino program that sets up the motor control pin, defines a speed value, sends the PWM signal to the transistor to control the motor speed, and prints the speed value to the Serial Monitor.
📋 What You'll Learn
Use pin 9 as the motor control pin connected to the transistor base
Define a variable called motorSpeed with the value 128
Use analogWrite to send the PWM signal to the motor control pin
Print the motor speed value to the Serial Monitor using Serial.println
💡 Why This Matters
🌍 Real World
Controlling DC motors with transistors is common in robotics and small electronics projects where you need to adjust motor speed safely.
💼 Career
Understanding how to control motors with microcontrollers and transistor drivers is a key skill for embedded systems and hardware engineers.
Progress0 / 4 steps
1
Set up the motor control pin
Write a line of code to create a variable called motorPin and set it to 9.
Arduino
Hint
Use int motorPin = 9; to assign pin 9 to the motorPin variable.
2
Define the motor speed variable
Add a line of code to create a variable called motorSpeed and set it to 128.
Arduino
Hint
Use int motorSpeed = 128; to set the speed to half of the maximum PWM value.
3
Send PWM signal to control motor speed
Write the setup() function to set motorPin as an output. Then write the loop() function to use analogWrite(motorPin, motorSpeed); to send the PWM signal.
Arduino
Hint
Use pinMode(motorPin, OUTPUT); in setup() and analogWrite(motorPin, motorSpeed); in loop().
4
Print motor speed to Serial Monitor
Add a line inside the loop() function to print the value of motorSpeed to the Serial Monitor using Serial.println(motorSpeed);.
Arduino
Hint
Use Serial.println(motorSpeed); to print the speed. Add delay(1000); to slow down the output.
Practice
(1/5)
1. What is the main role of a transistor when controlling a DC motor with an Arduino?
easy
A. To act as a switch that controls the motor safely
B. To increase the motor speed directly
C. To power the Arduino board
D. To measure the motor's temperature
Solution
Step 1: Understand transistor function
A transistor acts like a switch or amplifier in circuits.
Step 2: Apply to motor control
In Arduino motor control, the transistor switches the motor on/off safely without damaging the board.
Final Answer:
To act as a switch that controls the motor safely -> Option A
Quick Check:
Transistor = switch for motor control [OK]
Hint: Think of transistor as a safe on/off switch for the motor [OK]
Common Mistakes:
Confusing transistor with motor speed controller
Thinking transistor powers the Arduino
Assuming transistor measures anything
2. Which Arduino code snippet correctly sets a transistor pin to control a DC motor?
easy
A. pinMode(9, INPUT); digitalRead(9);
B. pinMode(9, INPUT); digitalWrite(9, HIGH);
C. pinMode(9, OUTPUT); digitalRead(9);
D. pinMode(9, OUTPUT); digitalWrite(9, HIGH);
Solution
Step 1: Set pin as output for controlling transistor
To control a transistor, the Arduino pin must be OUTPUT.
Step 2: Write HIGH to turn motor on
digitalWrite(9, HIGH) sends voltage to transistor base to switch motor on.
Final Answer:
pinMode(9, OUTPUT); digitalWrite(9, HIGH); -> Option D
Quick Check:
Output pin + HIGH = motor ON [OK]
Hint: Use OUTPUT mode and digitalWrite to control transistor [OK]
Common Mistakes:
Setting pin as INPUT when controlling transistor
Using digitalRead instead of digitalWrite
Not setting pinMode before writing
3. What will be the effect of this Arduino code controlling a transistor connected to a DC motor?
Pin controlling transistor must be OUTPUT, not INPUT.
Step 2: Identify conflict with digitalWrite
digitalWrite on an INPUT pin causes undefined behavior or no effect.
Final Answer:
Pin is set as INPUT but digitalWrite is used -> Option B
Quick Check:
OUTPUT needed for digitalWrite [OK]
Hint: Set pinMode to OUTPUT before digitalWrite [OK]
Common Mistakes:
Using INPUT mode with digitalWrite
Confusing digitalWrite with digitalRead
Ignoring pinMode setup
5. You want to control a DC motor speed using PWM on pin 9 with a transistor driver. Which additional component is essential to protect your circuit from voltage spikes caused by the motor?
hard
A. An LED connected in parallel with the transistor
B. A capacitor connected to the Arduino 5V pin
C. A diode connected across the motor terminals
D. A resistor in series with the motor
Solution
Step 1: Understand motor voltage spikes
When motor stops suddenly, it can create voltage spikes that damage components.
Step 2: Identify protection method
A diode placed across motor terminals (flyback diode) safely redirects spikes, protecting transistor and Arduino.
Final Answer:
A diode connected across the motor terminals -> Option C
Quick Check:
Flyback diode protects circuit from spikes [OK]
Hint: Always add diode across motor for spike protection [OK]
Common Mistakes:
Skipping diode and risking transistor damage
Using resistor instead of diode for spike protection
Confusing capacitor or LED as spike protection
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.