What is the frequency of the PWM signal generated by analogWrite() on an Arduino Uno pin 9 by default?
Arduino Uno uses timers with default PWM frequencies around a few hundred Hz on most pins.
On Arduino Uno, analogWrite() on pins 3, 9, 10, and 11 uses Timer1 with default PWM frequency about 490 Hz.
What will be the motor speed value printed by this Arduino code snippet?
int motorPin = 9;
int speed = 128;
void setup() {
Serial.begin(9600);
analogWrite(motorPin, speed);
Serial.println(speed);
}
void loop() {}int motorPin = 9; int speed = 128; void setup() { Serial.begin(9600); analogWrite(motorPin, speed); Serial.println(speed); } void loop() {}
The analogWrite() function sets the PWM duty cycle but does not change the variable.
The code prints the value of speed which is 128. This value sets the PWM duty cycle to about 50%.
What error will this Arduino code cause when trying to control motor speed with PWM?
int motorPin = 9;
void setup() {
pinMode(motorPin, INPUT);
}
void loop() {
analogWrite(motorPin, 200);
delay(1000);
}int motorPin = 9; void setup() { pinMode(motorPin, INPUT); } void loop() { analogWrite(motorPin, 200); delay(1000); }
Check the pin mode before using analogWrite().
Setting the pin as INPUT disables PWM output. The motor will not receive the PWM signal and will not run.
Which statement best describes how PWM duty cycle affects the speed of a DC motor?
Think about how average power to the motor changes with duty cycle.
Increasing the PWM duty cycle increases the average voltage applied to the motor, which increases its speed.
Consider this Arduino code snippet. What will be the output on the Serial Monitor?
int motorPin = 9;
int pwmValue = 180;
void setup() {
Serial.begin(9600);
pinMode(motorPin, OUTPUT);
analogWrite(motorPin, pwmValue);
int readValue = analogRead(motorPin);
Serial.println(readValue);
}
void loop() {}int motorPin = 9; int pwmValue = 180; void setup() { Serial.begin(9600); pinMode(motorPin, OUTPUT); analogWrite(motorPin, pwmValue); int readValue = analogRead(motorPin); Serial.println(readValue); } void loop() {}
Remember that analogRead() reads voltage on analog input pins, not PWM output pins.
Reading an analog value from a digital PWM output pin returns unpredictable values, often near 0, because PWM pins are not analog inputs.
