0
0
Raspberry Piprogramming~15 mins

GPIO pin numbering (BCM vs BOARD) in Raspberry Pi - Hands-On Comparison

Choose your learning style9 modes available
GPIO Pin Numbering (BCM vs BOARD)
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(pins) to show the final pin numbers.