Consider this Arduino code that sets LED brightness using PWM:
int ledPin = 9;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
analogWrite(ledPin, 128);
delay(1000);
}What brightness level will the LED have?
int ledPin = 9; void setup() { pinMode(ledPin, OUTPUT); } void loop() { analogWrite(ledPin, 128); delay(1000); }
PWM values range from 0 (off) to 255 (fully on). 128 is about half of 255.
The analogWrite function sets the PWM duty cycle. A value of 128 is roughly half of 255, so the LED will be about half as bright as fully on.
Which of the following best explains why PWM is used to control LED brightness instead of just changing voltage?
Think about how LEDs respond to voltage and how PWM works.
LEDs are either on or off at a given voltage. PWM controls brightness by turning the LED on and off very fast, changing the time it is on versus off, which is efficient and safe.
Look at this Arduino code snippet:
int ledPin = 9;
void setup() {
pinMode(ledPin, OUTPUT);
analogWrite(ledPin, 300);
}
void loop() {
}The LED does not light up as expected. What is the problem?
Check the valid range for PWM values in Arduino.
Arduino PWM values must be between 0 and 255. Using 300 is invalid and causes unexpected behavior.
Find the syntax error in this Arduino code:
int ledPin = 9
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
analogWrite(ledPin, 128);
delay(1000);
}Check the end of the first line carefully.
The first line is missing a semicolon at the end, which is required in Arduino C++ syntax.
You want to write Arduino code to smoothly increase LED brightness from off to fully on over 5 seconds using PWM on pin 9. Which code snippet achieves this?
Think about how to increase brightness gradually with small steps and short delays.
Option C increases PWM value from 0 to 255 in steps of 1 with 20ms delay, totaling about 5 seconds for smooth brightness increase.
