Bird
0
0
Arduinoprogramming~10 mins

Motor speed control with PWM in Arduino - Interactive Code Practice

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

Complete the code to set the motor speed using PWM on pin 9.

Arduino
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'
ALOW
BHIGH
C9
D255
Attempts:
3 left
💡 Hint
Common Mistakes
Using HIGH or LOW instead of a PWM value.
Using the pin number instead of the speed value.
2fill in blank
medium

Complete the code to gradually increase motor speed from 0 to 255.

Arduino
int motorPin = 6;
void setup() {
  pinMode(motorPin, OUTPUT);
}
void loop() {
  for (int speed = 0; speed <= 255; speed++) {
    analogWrite(motorPin, [1]);
    delay(10);
  }
}
Drag options to blanks, or click blank then click option'
Aspeed
BmotorPin
C255
Ddelay
Attempts:
3 left
💡 Hint
Common Mistakes
Using the pin number instead of the speed variable.
Using a fixed value instead of the loop variable.
3fill in blank
hard

Fix the error in the code to set motor speed using PWM on pin 3.

Arduino
int motorPin = 3;
void setup() {
  pinMode(motorPin, OUTPUT);
}
void loop() {
  analogWrite([1], 128);
  delay(500);
}
Drag options to blanks, or click blank then click option'
AmotorPin
B128
COUTPUT
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the speed value instead of the pin number.
Using the pin number directly instead of the variable.
4fill in blank
hard

Fill both blanks to create a dictionary that maps motor pins to their PWM speeds if speed is above 100.

Arduino
int motorPins[] = {5, 6, 9};
int speeds[] = {90, 150, 200};
void setup() {
  for (int i = 0; i < 3; i++) {
    pinMode(motorPins[i], OUTPUT);
    if (speeds[i] [1] 100) {
      analogWrite(motorPins[i], [2]);
    }
  }
}
void loop() {}
Drag options to blanks, or click blank then click option'
A>
BmotorPins[i]
Cspeeds[i]
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator.
Using the pin number instead of the speed value for PWM.
5fill in blank
hard

Fill all three blanks to read an analog sensor, map its value to PWM range, and set motor speed.

Arduino
int sensorPin = A0;
int motorPin = 10;
void setup() {
  pinMode(motorPin, OUTPUT);
}
void loop() {
  int sensorValue = analogRead([1]);
  int motorSpeed = map(sensorValue, 0, 1023, [2], [3]);
  analogWrite(motorPin, motorSpeed);
  delay(100);
}
Drag options to blanks, or click blank then click option'
AsensorPin
B0
C255
DmotorPin
Attempts:
3 left
💡 Hint
Common Mistakes
Using the motor pin instead of sensor pin for reading.
Mapping to wrong PWM range.