Challenge - 5 Problems
Raspberry Pi Python Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
GPIO Pin Setup and Output
What is the output of this code when run on a Raspberry Pi with an LED connected to GPIO pin 18?
Raspberry Pi
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) GPIO.output(18, GPIO.HIGH) time.sleep(1) GPIO.output(18, GPIO.LOW) print("LED toggled") GPIO.cleanup()
Attempts:
2 left
💡 Hint
Focus on what the print statement outputs after toggling the LED.
✗ Incorrect
The code sets GPIO pin 18 as output, turns the LED on, waits 1 second, turns it off, then prints 'LED toggled'. No errors occur if run with proper permissions.
❓ Predict Output
intermediate2:00remaining
Reading a Button Press
What will be printed if the button connected to GPIO pin 23 is pressed when this code runs?
Raspberry Pi
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) if GPIO.input(23) == GPIO.LOW: print("Button Pressed") else: print("Button Not Pressed") GPIO.cleanup()
Attempts:
2 left
💡 Hint
Remember that the button pulls the pin LOW when pressed due to pull-up resistor.
✗ Incorrect
The code reads GPIO 23 input with pull-up resistor enabled. When pressed, the pin reads LOW, so it prints 'Button Pressed'.
🔧 Debug
advanced2:30remaining
Fix the PWM Frequency Error
This code is intended to make an LED connected to GPIO 12 fade in and out using PWM. What error will it raise?
Raspberry Pi
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(12, GPIO.OUT) pwm = GPIO.PWM(12, 1000) pwm.start(0) try: for dc in range(0, 101, 5): pwm.ChangeDutyCycle(dc) time.sleep(0.05) for dc in range(100, -1, -5): pwm.ChangeDutyCycle(dc) time.sleep(0.05) finally: pwm.stop() GPIO.cleanup()
Attempts:
2 left
💡 Hint
Check the data type of the frequency argument in GPIO.PWM.
✗ Incorrect
The frequency argument is given as a string '1000' instead of a number 1000, causing a TypeError.
❓ Predict Output
advanced2:00remaining
I2C Sensor Data Reading
What will this code print if the I2C device at address 0x48 returns the byte 0x1A when reading register 0x00?
Raspberry Pi
import smbus bus = smbus.SMBus(1) address = 0x48 register = 0x00 value = bus.read_byte_data(address, register) print(f"Sensor value: {value}")
Attempts:
2 left
💡 Hint
Remember that 0x1A in hexadecimal equals 26 in decimal.
✗ Incorrect
The read_byte_data returns an integer. 0x1A hex is 26 decimal, so the print shows 'Sensor value: 26'.
🧠 Conceptual
expert3:00remaining
Understanding Raspberry Pi GPIO Pull-up and Pull-down
Which statement correctly explains the difference between pull-up and pull-down resistors in Raspberry Pi GPIO input pins?
Attempts:
2 left
💡 Hint
Think about how to keep a GPIO pin at a known voltage level when no button is pressed.
✗ Incorrect
Pull-up resistors connect the pin to 3.3V to keep it HIGH by default; pull-down resistors connect to ground to keep it LOW by default. This prevents floating inputs.