📖 Scenario: You are working on a Raspberry Pi project that controls an LED light. To do this, you need to set up the GPIO pins correctly. Raspberry Pi allows two ways to number pins: BCM (Broadcom chip numbering) and BOARD (physical pin numbering on the header).Understanding the difference helps you connect your hardware and write code that controls the pins properly.
🎯 Goal: Build a simple Python program that sets up the GPIO pins using both BCM and BOARD numbering modes. You will create a dictionary of pins, choose a numbering mode, and then print the pin numbers according to the chosen mode.
📋 What You'll Learn
Create a dictionary with pin names and their BCM and BOARD numbers
Create a variable to store the chosen numbering mode ('BCM' or 'BOARD')
Use a dictionary comprehension to create a new dictionary with pin names and their numbers based on the chosen mode
Print the resulting dictionary of pins with their numbers
💡 Why This Matters
🌍 Real World
When building Raspberry Pi projects, you must know how to refer to pins correctly to connect sensors, buttons, and LEDs safely and effectively.
💼 Career
Understanding GPIO pin numbering is essential for hardware engineers, IoT developers, and anyone working with embedded systems using Raspberry Pi.
Progress0 / 4 steps
1
Create the GPIO pins dictionary
Create a dictionary called gpio_pins with these exact entries: 'LED': {'BCM': 17, 'BOARD': 11}, 'Button': {'BCM': 27, 'BOARD': 13}, and 'Sensor': {'BCM': 22, 'BOARD': 15}.
Raspberry Pi
Hint
Think of gpio_pins as a map where each device name points to another map of its BCM and BOARD pin numbers.
2
Set the numbering mode
Create a variable called numbering_mode and set it to the string 'BCM'.
Raspberry Pi
Hint
This variable will decide which pin numbering system your program uses.
3
Create a dictionary with pins using the chosen numbering mode
Use a dictionary comprehension to create a new dictionary called pins that maps each pin name to its number using the numbering_mode. For example, pins = {name: gpio_pins[name][numbering_mode] for name in gpio_pins}.
Raspberry Pi
Hint
Use a dictionary comprehension to pick the right pin number for each device based on the numbering_mode.
4
Print the pins dictionary
Write a print statement to display the pins dictionary.
Raspberry Pi
Hint
Use print(pins) to show the final pin numbers.
Practice
(1/5)
1. Which GPIO pin numbering mode uses the physical pin numbers on the Raspberry Pi header?
easy
A. BOARD numbering mode
B. BCM numbering mode
C. WIRING numbering mode
D. PIN numbering mode
Solution
Step 1: Understand GPIO numbering modes
GPIO pins can be numbered by their physical location (BOARD) or by the Broadcom chip numbering (BCM).
Step 2: Identify physical pin numbering
BOARD mode refers to the physical pin numbers on the Raspberry Pi header.
Final Answer:
BOARD numbering mode -> Option A
Quick Check:
Physical pins = BOARD [OK]
Hint: BOARD mode matches physical pin numbers on the header [OK]
Common Mistakes:
Confusing BCM with physical pin numbers
Thinking BCM is the physical numbering
Assuming WIRING or PIN modes exist
2. Which of the following is the correct way to set GPIO pin numbering to BCM mode in Python?
easy
A. GPIO.setmode(GPIO.BOARD)
B. GPIO.setupmode(GPIO.BCM)
C. GPIO.mode(GPIO.BCM)
D. GPIO.setmode(GPIO.BCM)
Solution
Step 1: Recall the correct function to set GPIO mode
The function to set numbering mode is GPIO.setmode().
Step 2: Use the correct constant for BCM mode
GPIO.BCM is the constant for BCM numbering mode.
Final Answer:
GPIO.setmode(GPIO.BCM) -> Option D
Quick Check:
Use setmode with BCM constant [OK]
Hint: Use GPIO.setmode(GPIO.BCM) to select BCM numbering [OK]
Common Mistakes:
Using GPIO.mode instead of GPIO.setmode
Using BOARD instead of BCM constant
Calling GPIO.setupmode which doesn't exist
3. Given the code snippet:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
GPIO.output(7, GPIO.HIGH)
Which physical pin is set HIGH?
medium
A. Pin 11 on the physical header
B. GPIO pin number 7 on the Broadcom chip
C. Pin 7 on the physical header
D. GPIO pin number 11 on the Broadcom chip
Solution
Step 1: Identify the numbering mode used
The code uses GPIO.setmode(GPIO.BOARD), so pin numbers refer to physical header pins.
Step 2: Check which pin is set HIGH
GPIO.setup(7, GPIO.OUT) and GPIO.output(7, GPIO.HIGH) set physical pin 7 HIGH.
Final Answer:
Pin 7 on the physical header -> Option C
Quick Check:
BOARD mode pin 7 = physical pin 7 [OK]
Hint: BOARD mode means pin number is physical header pin [OK]
Common Mistakes:
Confusing BOARD pin 7 with BCM GPIO 7
Assuming pin 7 means BCM numbering
Mixing physical pin numbers with BCM numbers
4. What is wrong with this code snippet?
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(10, GPIO.OUT)
GPIO.output(10, GPIO.HIGH)
Assuming the LED is wired to physical pin 10 on the header.
medium
A. Pin 10 in BCM does not match physical pin 10 wiring
B. GPIO.setmode(GPIO.BOARD) should be used instead
C. GPIO.output() requires pin number as string
D. GPIO.setup() cannot use BCM numbering
Solution
Step 1: Understand numbering mismatch
GPIO.setmode(GPIO.BCM) means pin numbers refer to Broadcom chip pins, not physical pins.
Step 2: Check wiring vs numbering
Physical pin 10 corresponds to BCM GPIO 15. The code uses BCM pin 10 (physical pin 19), so it does not control the wired physical pin 10.
Final Answer:
Pin 10 in BCM does not match physical pin 10 wiring -> Option A
Quick Check:
BCM 10 controls physical 19 ≠ pin 10 [OK]
Hint: Match numbering mode to wiring to avoid pin mismatch [OK]
Common Mistakes:
Assuming physical pin numbers work with BCM mode
Using string instead of int for pin number
Thinking GPIO.setup can't use BCM numbering
5. You want to control an LED connected to physical pin 12 on the Raspberry Pi header. Which code snippet correctly sets the pin HIGH using BCM numbering?
hard
A. GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
GPIO.output(12, GPIO.HIGH)
B. GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.HIGH)
C. GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)
GPIO.output(12, GPIO.HIGH)
D. GPIO.setmode(GPIO.BOARD)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.HIGH)
Solution
Step 1: Identify physical pin 12's BCM number
Physical pin 12 corresponds to BCM GPIO 18 on Raspberry Pi.
Step 2: Use BCM numbering mode and correct pin
Set mode to GPIO.BCM and use pin 18 in setup and output.
Final Answer:
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.HIGH) -> Option B
Quick Check:
Physical pin 12 = BCM 18, use BCM mode [OK]
Hint: Convert physical pin to BCM before coding [OK]
Common Mistakes:
Using physical pin number with BCM mode
Using BCM pin number with BOARD mode
Mixing pin numbers without conversion
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.