On a Raspberry Pi, why is Pulse Width Modulation (PWM) used to simulate analog output instead of directly outputting a varying voltage?
Think about what kind of signals Raspberry Pi pins can produce physically.
Raspberry Pi GPIO pins are digital, meaning they can only be fully on (3.3V) or off (0V). PWM rapidly switches the pin on and off to create an average voltage effect, which devices perceive as analog-like control.
What will be the brightness effect on an LED connected to a Raspberry Pi GPIO pin when the PWM duty cycle is set to 75%?
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) pwm = GPIO.PWM(18, 1000) # 1kHz frequency pwm.start(0) pwm.ChangeDutyCycle(75) time.sleep(2) pwm.stop() GPIO.cleanup()
Duty cycle controls how long the LED is on during each cycle.
Setting the duty cycle to 75% means the LED is on 75% of the time and off 25%, making it appear about 75% as bright as fully on.
Given this Raspberry Pi PWM code, why does the LED stay fully on instead of dimming?
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) pwm = GPIO.PWM(18, 1000) pwm.start(100) for dc in range(0, 101, 20): pwm.ChangeDutyCycle(dc) time.sleep(0.5) pwm.stop() GPIO.cleanup()
Check how the duty cycle changes inside the loop.
The PWM starts at 100% duty cycle (fully on). The loop changes the duty cycle from 0 to 100 in steps, so the LED dims and brightens as expected.
Which option contains a syntax error in setting up PWM on Raspberry Pi?
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) pwm = GPIO.PWM(18, 1000) pwm.start(50)
Look for missing commas or parentheses.
Option D is missing a comma between 18 and 1000 in the PWM constructor, causing a syntax error.
A Raspberry Pi GPIO pin outputs 3.3V when on. If PWM is set to 40% duty cycle, what is the average voltage the connected device experiences?
Multiply the duty cycle percentage by the voltage when on.
Average voltage = 3.3V * 0.40 = 1.32V