Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
LED brightness control with PWM
📖 Scenario: You have a small LED connected to your Arduino board. You want to control how bright the LED glows by changing the power it receives. This is done using PWM (Pulse Width Modulation), which lets you adjust brightness smoothly.
🎯 Goal: Build a simple Arduino program that changes the brightness of an LED connected to pin 9 using PWM. You will set up the LED pin, create a brightness level variable, write the PWM signal to the LED, and then see the LED glow at the set brightness.
📋 What You'll Learn
Create a variable for the LED pin number set to 9
Create a variable called brightness to hold the LED brightness level
Use analogWrite() to send the brightness value to the LED pin
Print the brightness value to the Serial Monitor
💡 Why This Matters
🌍 Real World
Controlling LED brightness is common in electronics projects like mood lighting, indicators, and displays.
💼 Career
Understanding PWM and hardware control is important for embedded systems and IoT device programming.
Progress0 / 4 steps
1
Set up the LED pin
Create an int variable called ledPin and set it to 9. Then, in the setup() function, use pinMode(ledPin, OUTPUT) to set the LED pin as an output.
Arduino
Hint
Remember to use pinMode inside setup() to prepare the pin.
2
Create a brightness variable
Create an int variable called brightness and set it to 128 (half brightness).
Arduino
Hint
Brightness values go from 0 (off) to 255 (full brightness).
3
Write PWM signal to LED
In the loop() function, use analogWrite(ledPin, brightness) to send the brightness value to the LED pin.
Arduino
Hint
Use analogWrite to control LED brightness with PWM.
4
Print brightness to Serial Monitor
In setup(), start serial communication with Serial.begin(9600). Then, in loop(), use Serial.println(brightness) to print the brightness value.
Arduino
Hint
Use Serial.begin(9600) in setup() and Serial.println(brightness) in loop().
Practice
(1/5)
1. What does analogWrite(pin, 128) do to an LED connected to the specified pin?
easy
A. Sets the LED brightness to about half of its maximum brightness.
B. Turns the LED fully off.
C. Turns the LED fully on at maximum brightness.
D. Causes the LED to blink on and off repeatedly.
Solution
Step 1: Understand PWM value range
The analogWrite function accepts values from 0 (off) to 255 (full brightness).
Step 2: Interpret the value 128
128 is about half of 255, so the LED brightness will be about half of maximum.
Final Answer:
Sets the LED brightness to about half of its maximum brightness. -> Option A
A. The LED will stay at full brightness without change.
B. The LED will blink on and off 5 times quickly.
C. The code will cause a compile error due to wrong syntax.
D. The LED brightness will increase in 5 steps from off to full brightness.
Solution
Step 1: Analyze the for loop increments
The loop variable brightness starts at 0 and increases by 51 until it reaches 255, so values are 0, 51, 102, 153, 204, 255 (6 values, 5 steps).
Step 2: Understand analogWrite effect
Each loop sets LED brightness to the current value, increasing brightness in steps with 100ms delay.
Final Answer:
The LED brightness will increase in 5 steps from off to full brightness. -> Option D
Quick Check:
Brightness steps with delay = gradual brightness increase [OK]
Hint: Loop increments PWM value to fade LED brightness up [OK]
Common Mistakes:
Counting 6 steps instead of 5 increments
Thinking LED blinks on/off instead of fading
Assuming syntax error due to loop
4. Identify the error in this Arduino code that tries to fade an LED on pin 10:
for (int i = 0; i < 256; i++) {
analogWrite(10, i);
delay(50);
}
analogWrite(10, 256);
medium
A. The for loop should use <= 256 instead of < 256.
B. The value 256 is invalid for analogWrite; max is 255.
C. Pin 10 cannot be used with analogWrite on Arduino.
D. delay() cannot be used inside a for loop.
Solution
Step 1: Check analogWrite value limits
analogWrite accepts values from 0 to 255. Using 256 is out of range and invalid.
Step 2: Verify other code parts
The for loop correctly uses i from 0 to 255. Pin 10 supports PWM on most Arduino boards. delay() is allowed inside loops.
Final Answer:
The value 256 is invalid for analogWrite; max is 255. -> Option B
Quick Check:
analogWrite max value = 255, 256 causes error [OK]
Hint: PWM values max at 255; never use 256 [OK]
Common Mistakes:
Using values above 255 for analogWrite
Thinking delay() is not allowed in loops
Assuming pin 10 can't do PWM
5. You want to create a smooth breathing LED effect using PWM on pin 3. Which code snippet correctly achieves this effect?
hard
A. for (int b = 0; b <= 255; b++) { analogWrite(3, b); delay(10); } for (int b = 255; b >= 0; b--) { analogWrite(3, b); delay(10); }
B. analogWrite(3, 255); delay(1000); analogWrite(3, 0); delay(1000);
C. for (int b = 0; b < 256; b += 50) { analogWrite(3, b); delay(100); }
D. digitalWrite(3, HIGH); delay(500); digitalWrite(3, LOW); delay(500);
Solution
Step 1: Understand breathing LED effect
A breathing effect smoothly increases brightness from 0 to max, then back down to 0 repeatedly.
Step 2: Analyze each option
for (int b = 0; b <= 255; b++) { analogWrite(3, b); delay(10); } for (int b = 255; b >= 0; b--) { analogWrite(3, b); delay(10); } uses two loops: one increasing PWM from 0 to 255, then decreasing back to 0 with small delays for smoothness. analogWrite(3, 255); delay(1000); analogWrite(3, 0); delay(1000); just turns LED fully on and off abruptly. for (int b = 0; b < 256; b += 50) { analogWrite(3, b); delay(100); } increases brightness in large steps, not smooth. digitalWrite(3, HIGH); delay(500); digitalWrite(3, LOW); delay(500); uses digitalWrite, which only turns LED fully on or off.
Final Answer:
for (int b = 0; b <= 255; b++) { analogWrite(3, b); delay(10); } for (int b = 255; b >= 0; b--) { analogWrite(3, b); delay(10); } -> Option A
Quick Check:
Smooth increase and decrease PWM = breathing LED [OK]
Hint: Use increasing then decreasing PWM values for breathing effect [OK]
Common Mistakes:
Using digitalWrite instead of analogWrite for brightness