Bird
Raised Fist0
Arduinoprogramming~10 mins

DC motor with transistor driver in Arduino - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the motor pin as an output.

Arduino
const int motorPin = 9;

void setup() {
  pinMode([1], OUTPUT);
}

void loop() {
  // Motor control code here
}
Drag options to blanks, or click blank then click option'
AINPUT
BmotorPin
C9
DpinMode
Attempts:
3 left
💡 Hint
Common Mistakes
Using the number 9 directly instead of the variable name.
Using INPUT instead of OUTPUT.
2fill in blank
medium

Complete the code to turn the motor ON by setting the pin HIGH.

Arduino
const int motorPin = 9;

void setup() {
  pinMode(motorPin, OUTPUT);
}

void loop() {
  digitalWrite(motorPin, [1]);
  delay(1000);
  digitalWrite(motorPin, LOW);
  delay(1000);
}
Drag options to blanks, or click blank then click option'
AHIGH
BLOW
COUTPUT
DINPUT
Attempts:
3 left
💡 Hint
Common Mistakes
Using LOW instead of HIGH to turn the motor ON.
Confusing pinMode values with digitalWrite values.
3fill in blank
hard

Fix the error in the code to correctly control the motor speed using PWM.

Arduino
const int motorPin = 9;

void setup() {
  pinMode(motorPin, OUTPUT);
}

void loop() {
  analogWrite(motorPin, [1]);
  delay(1000);
}
Drag options to blanks, or click blank then click option'
AOUTPUT
BHIGH
C255
DdigitalWrite
Attempts:
3 left
💡 Hint
Common Mistakes
Using HIGH instead of a numeric PWM value.
Using digitalWrite instead of analogWrite.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps motor speeds to their PWM values if speed is greater than 100.

Arduino
int speeds[] = {50, 120, 200};

void setup() {
  // This is a conceptual example, Arduino C++ does not support dictionary comprehensions
  // Imagine a map: {speed: [1] for speed in speeds if speed [2] 100}
}

void loop() {}
Drag options to blanks, or click blank then click option'
Aspeed * 2
B<
C>
Dspeed + 10
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the condition.
Using incorrect expressions for the value.
5fill in blank
hard

Fill all three blanks to create a function that sets motor speed only if the speed is within 0 to 255.

Arduino
const int motorPin = 9;

void setup() {
  pinMode(motorPin, OUTPUT);
}

void setMotorSpeed(int speed) {
  if (speed [1] 0 && speed [2] 255) {
    analogWrite(motorPin, [3]);
  }
}

void loop() {
  setMotorSpeed(200);
}
Drag options to blanks, or click blank then click option'
A>=
B<=
Cspeed
DmotorPin
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators.
Passing motorPin instead of speed to analogWrite.

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

  1. Step 1: Understand transistor function

    A transistor acts like a switch or amplifier in circuits.
  2. Step 2: Apply to motor control

    In Arduino motor control, the transistor switches the motor on/off safely without damaging the board.
  3. Final Answer:

    To act as a switch that controls the motor safely -> Option A
  4. 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

  1. Step 1: Set pin as output for controlling transistor

    To control a transistor, the Arduino pin must be OUTPUT.
  2. Step 2: Write HIGH to turn motor on

    digitalWrite(9, HIGH) sends voltage to transistor base to switch motor on.
  3. Final Answer:

    pinMode(9, OUTPUT); digitalWrite(9, HIGH); -> Option D
  4. 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?
int motorPin = 9;
void setup() {
  pinMode(motorPin, OUTPUT);
}
void loop() {
  analogWrite(motorPin, 128);
  delay(2000);
  analogWrite(motorPin, 0);
  delay(2000);
}
medium
A. Motor runs at half speed for 2 seconds, then stops for 2 seconds repeatedly
B. Motor runs at full speed continuously
C. Motor never runs because analogWrite is wrong
D. Motor runs at full speed for 2 seconds, then stops for 2 seconds repeatedly

Solution

  1. Step 1: Understand analogWrite value

    analogWrite with 128 sets PWM to about 50% duty cycle, so motor runs at half speed.
  2. Step 2: Analyze timing in loop

    Motor runs at half speed for 2 seconds, then analogWrite(0) stops motor for 2 seconds, repeating.
  3. Final Answer:

    Motor runs at half speed for 2 seconds, then stops for 2 seconds repeatedly -> Option A
  4. Quick Check:

    analogWrite(128) = half speed [OK]
Hint: PWM 128 means half speed, 0 means stop [OK]
Common Mistakes:
  • Thinking analogWrite(128) is full speed
  • Confusing analogWrite with digitalWrite
  • Ignoring delay timing effects
4. Identify the error in this Arduino code controlling a transistor for a DC motor:
int motorPin = 9;
void setup() {
  pinMode(motorPin, INPUT);
}
void loop() {
  digitalWrite(motorPin, HIGH);
}
medium
A. Pin number 9 cannot be used for motor control
B. Pin is set as INPUT but digitalWrite is used
C. digitalWrite should be digitalRead
D. Missing delay in loop

Solution

  1. Step 1: Check pinMode for transistor control

    Pin controlling transistor must be OUTPUT, not INPUT.
  2. Step 2: Identify conflict with digitalWrite

    digitalWrite on an INPUT pin causes undefined behavior or no effect.
  3. Final Answer:

    Pin is set as INPUT but digitalWrite is used -> Option B
  4. 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

  1. Step 1: Understand motor voltage spikes

    When motor stops suddenly, it can create voltage spikes that damage components.
  2. Step 2: Identify protection method

    A diode placed across motor terminals (flyback diode) safely redirects spikes, protecting transistor and Arduino.
  3. Final Answer:

    A diode connected across the motor terminals -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard