Raspberry Pi GPIO Sensor and Display Projects

raspberry pi gpio projects
Interface or feature Pins and values Planning use
GPIO header 40 pins on Raspberry Pi boards Decide early whether your code uses BCM numbers or header positions, so wiring tables and scripts agree.
I2C Data GPIO2, clock GPIO3 Suits displays and sensors that share a pair of signal lines. Confirm the module is wired for I2C, not SPI.
SPI0 MOSI GPIO10, MISO GPIO9, SCLK GPIO11, CE0 GPIO8, CE1 GPIO7 Suits SPI displays and converter chips. The CE0 and CE1 lines select between devices on the bus.
Serial UART TX GPIO14, RX GPIO15 Suits serial modules such as GPS receivers. Cross the lines so TX meets the other device’s RX.
Hardware PWM GPIO12, GPIO13, GPIO18, GPIO19 Choose these pins when brightness or speed control must stay smooth. Other pins can still use software PWM.

Pin assignments in the table follow the Electropeak Raspberry Pi GPIO guide.

Raspberry Pi GPIO projects start with the 40-pin header, BCM pin numbers and a check of each module’s logic voltage. Pick I2C, SPI, UART or PWM pins by the interface your sensor or display needs.

Wiring comes first on this page and code second, because most failed builds trace back to a wrong pin rather than a wrong line of Python. A raspberry pi gpio pinout reference is the best companion while you work through the project areas below.

Choosing a first project

Start with the simplest part that matches what you want the Pi to do. An output such as an LED teaches pin setup. A sensor teaches reading values. A display shows the results, and an input device such as an encoder turns the whole thing into something you can operate.

  • Output first. If you have never wired a GPIO pin, an LED is the cheapest way to confirm that your numbering mode, resistor and code agree. The walkthrough for a raspberry pi led python project covers exactly that and nothing else.
  • Sensor next. Pick a sensor whose interface you already understand from the table above. A part on I2C or SPI uses fixed pins, so there is less to decide than with a plain digital input.
  • Display when you have data. A display is most useful once something produces readings worth showing. Wiring one before a sensor works tends to hide which half of the circuit is at fault.
  • Input device last. Encoders and joysticks add timing questions, such as how to catch fast changes without wasting processor time. They are easier once basic reads are routine.

Whichever order you choose, the next step is understanding how the header itself is numbered.

GPIO header and pin numbering

Raspberry Pi boards have a 40-pin header for connecting other components, according to the Electropeak GPIO tutorial. Not every pin is a free signal pin, and the interface pins in the glance table above are reserved for their roles once you enable those interfaces.

A cased board with its header still reachable. If the case covers the header, plan for a ribbon or extension before you commit to a layout.

BCM numbers versus header positions

The RPi.GPIO library lets you pick a numbering scheme. Calling GPIO.setmode(GPIO.BCM) tells the library to use BCM pin numbers, which are the GPIO names such as GPIO2 or GPIO18. That is the scheme used throughout this page, because every interface assignment in the table is given as a GPIO name.

Mixing the two schemes is the most common source of a circuit that looks right but never responds. Pick BCM, write the pin names in a comment beside each wire, and keep a single pin table for the project. If a script is copied from elsewhere, check its setmode line before trusting any pin number in it.

Reading an input

The call GPIO.input(pin) reads the state of an input pin, and it is the basic building block for buttons, encoder channels and digital sensor outputs. It returns the pin state at the instant of the call, which matters later when we look at fast inputs.

The header runs along one edge of the board, so the pin you count from is always the end nearest the board corner. Work out your orientation before attaching any jumper.

With the numbering settled, the next question is what voltage a given module expects on those pins.

Logic levels, pull-ups and safe wiring habits

Every module you attach to a GPIO pin has its own logic voltage, and the Pi’s pins will not suit every module. Read the module’s datasheet or product page before connecting a signal line, and confirm that its output swing matches what the Pi pin accepts. If a module powers up from a higher supply and drives its output at that same level, it needs a conversion stage or a different module.

This caution applies most to modules sold as generic hobby boards, where the silkscreen says little about the signal levels. Power pins and signal pins are separate questions. A module can accept a supply voltage that is fine for the board yet still drive its output pin at a level the Pi should not see. Treat each signal line as a separate check and write the result into your wiring table.

Pull-up resistors on inputs

A floating input reads random values, so buttons need a defined resting state. In the Electropeak example, a button pin is pulled up by default, so the resting state is high and pressing the button pulls it low. A write-up by Hagensieker on rotary encoders takes the same approach in software, setting up a shutdown button on GPIO 17 as an input with the internal pull-up enabled. Using the internal pull-up means the button needs only one wire to the pin and one to the other side of the switch, with no extra resistor on the breadboard.

A general breadboard example with LEDs and jumper wires. The board shown is Arduino-style, but the same layout habit of one wire per signal applies to a Pi.

Pin reuse warnings

When two scripts claim the same pin at once, RPi.GPIO prints a warning, as noted in the Electropeak tutorial. Treat that warning as a sign that your pin plan has a conflict, not as noise to suppress. Giving every project a single owner script for each pin avoids the issue.

Interfaces and Python libraries on Raspberry Pi OS

Raspberry Pi OS supports the common bus interfaces on fixed header pins, and its Python tooling covers most beginner needs without extra installs. The interface pins are listed in the glance table at the top, so this section concentrates on what to choose.

Which interface suits which part

  • I2C. Choose it for parts that offer an I2C option, since the bus uses only the data pin GPIO2 and the clock pin GPIO3. Many devices can share those lines.
  • SPI. Choose it when the part is SPI only, such as many small LCD modules and converter chips. The bus uses MOSI, MISO and SCLK on GPIO10, GPIO9 and GPIO11, with chip-enable pins GPIO8 and GPIO7.
  • UART. Choose it for modules that talk over a serial link, using TX on GPIO14 and RX on GPIO15.
  • PWM. Hardware PWM is available on GPIO12, GPIO13, GPIO18 and GPIO19, and software PWM works on all pins, per the Electropeak guide. Use a hardware PWM pin when you can and fall back to software PWM when the layout demands another pin.

RPi.GPIO, gpiozero and pigpio

The gpiozero library is available by default on Raspberry Pi OS, so nothing needs installing. It also makes PWM simple: setting an LED object’s value to 0.2 gives 20% brightness. For beginners, gpiozero is the quickest way to get a pin working because the code names the device, not the pin mechanics.

RPi.GPIO remains useful when you want direct control, with calls such as setmode and input. For interrupt-driven input, a forum discussion on GPIO events in Xojo points out that the pigpio library used there is also supported by Python. That is a practical option when a project has to react to fast pin changes.

Display projects

A display makes a Pi project usable without a monitor. The decision that matters is the bus: a display on I2C needs only GPIO2 and GPIO3, while one on SPI takes the SPI0 pins and a chip-enable line. Check which variant your module is before wiring, because boards of the same size are sold in both forms.

OLED over I2C

An I2C OLED suits status readouts and menus because its wiring is short and its signal lines can be shared with I2C sensors. A step-by-step guide on how to connect oled display to raspberry pi covers the wiring and bus setup. For the Python side, the walkthrough of an oled display on raspberry pi with python shows how to draw text and shapes.

The OLED is the better first display when you also plan to add I2C sensors, because they can reuse the same pins. Wait on it if your header is already crowded with SPI devices and you cannot spare the bus.

Small LCD over SPI

Small graphic LCD modules are often SPI devices, which means their data, clock and chip-enable lines map onto the SPI0 pins in the glance table. A guide to pcd8544 raspberry pi wiring is the place to start for that style of module. Because an LCD of this kind may take a separate logic supply, check its datasheet before connecting any signal line to the Pi.

Choose an SPI LCD when you want a simple, low-cost screen and are happy to spend more header pins. Choose the OLED when pin count matters more than anything else.

Sensor projects

Sensors split into three practical groups by the way they reach the Pi: pulse and digital sensors that use ordinary pins, analog sensors that need a converter, and bus sensors that use I2C, SPI or serial pins. Sort a new part into one of those groups and its wiring plan largely follows.

Distance and sound

A distance project with an ultrasonic module is a good lesson in timing, since the Python code has to measure how long a signal pin stays high. The guide on hc-sr04 raspberry pi python works through the wiring and the code. Check the module’s output level before connecting its echo line to a Pi pin.

For audio, a microphone or sound sensor board gives either a digital threshold signal or an analog level, and the choice decides which of the following wiring plans applies. The page on a sound sensor raspberry pi setup covers both cases.

Ultrasonic transducers and a small display module with coloured jumper wires. No Raspberry Pi is shown, so treat it as an example of the parts, not of the wiring.

Temperature, soil and analog readings

A digital temperature probe is a sensible first environmental sensor, because it needs few wires and returns a ready value. The tutorial on ds18b20 raspberry pi python walks through connecting one.

The Pi’s header has no analog inputs, so any sensor with a varying voltage output needs a converter chip. Using an mcp3008 raspberry pi python setup places that converter on the SPI0 pins from the glance table. A common use is garden monitoring, and the page on a soil moisture sensor raspberry pi shows how a converter turns a moisture probe into usable readings.

If your sensor outputs a voltage you cannot identify, assume it needs a converter and check the datasheet for its output range before wiring it to anything.

Motion and position

Motion sensors that report acceleration and rotation usually sit on I2C, so they share GPIO2 and GPIO3 with an OLED. The guide to the mpu6050 raspberry pi setup covers that case. For location, a GPS receiver talks over serial, so it uses the UART pins, TX on GPIO14 and RX on GPIO15. The page on adding a raspberry pi gps module covers the connection and reading data.

This breadboard shows general sensor wiring practice with short jumpers between each module and the board. It is not a Raspberry Pi circuit, so use it for layout ideas only.

Inputs: rotary encoders, buttons and joysticks

Inputs are where timing starts to matter. A rotary encoder produces a stream of pulses on a pair of channel pins, and reading them too slowly means missed steps. This section answers how to use a rotary encoder with a Raspberry Pi and how to keep input handling efficient.

Wiring a rotary encoder

A basic encoder has a left, a middle and a right pin. In the Hagensieker write-up, which connects an encoder to an IQAudio DAC+ board, the left pin goes to pin 16, the middle pin to pin 14 and the right pin to pin 18 on the DAC+. Those numbers refer to that board’s own connections, so do not copy them onto a bare Pi. Choose free GPIO pins for your build, record them in BCM terms and set each as an input.

Read each channel with GPIO.input, and use internal pull-ups where the module does not provide its own, as in the GPIO 17 button setup from the same write-up. A complete wiring and menu project is in the guide to rotary encoder raspberry pi python, which pairs the encoder with an OLED display.

Polling or interrupts

Reading a pin in a tight loop works for a slow button but wastes processor time. A developer thread on the Xojo forum reports loop polling at 97 to 100% CPU and argues that interrupts are the proper method for GPIO events. That test used Xojo, not Python, so read it as an illustration of what busy-loop polling costs. The advice carries over: use interrupts, supported through pigpio or RPi.GPIO event detection, for an encoder, and keep simple polling for a button you check occasionally.

Joysticks

A joystick with two analog axes needs the same converter approach as any other analog sensor, because the header has no analog inputs. The guide to a raspberry pi joystick gpio project shows the wiring. Its push switch can be handled like any other button, with an internal pull-up on the input.

Soundboard projects

A soundboard on a Raspberry Pi is a set of buttons wired to GPIO inputs, a speaker or audio output, and a Python script that plays a file when a button changes state. To make one, wire each button between a free GPIO pin and the other side of the switch, enable the internal pull-up in code, and trigger playback when the pin goes low. The GPIO 17 shutdown button in the Hagensieker example uses exactly that input pattern, so the wiring habit carries across.

Add one button per sound and keep the pin table written down, since an extra button is an extra pin. A complete build, including the speaker connection and the script structure, is described in the raspberry pi soundboard project. If a button seems to trigger twice, treat that as a signal that input handling needs the interrupt-style approach described earlier, not a faulty switch.

A commercial speaker with a cable, shown as an example of the output stage. It is not wired to a Pi here, and a Pi build needs its own audio connection.

Beyond the Raspberry Pi

The same wiring-first habits apply to other single-board computers, though pin names and numbers differ. An Orange Pi Zero, for example, has its own header map, so a Raspberry Pi pin table cannot be reused. The guide to infrared remote control on orange pi zero with lirc and python shows a receiver project on that board and is a useful comparison if you work with both.

Combining parts into a larger build

Once individual parts work, a combined project mostly becomes a pin-planning exercise. List every part, mark which interface it uses, and check that no two parts want the same pin or the same bus address. I2C parts can share GPIO2 and GPIO3, SPI parts share the SPI0 lines but need separate chip-enable pins, and a serial module takes the UART pins for itself.

A weather station is the natural example, since it combines a sensor, a display and a logging script. The raspberry pi weather station project shows how to put those pieces together. Build it up one part at a time and keep each part’s test script, so a fault is easy to isolate when a new part is added.

Frequently asked questions

How can I see the pin layout from the terminal?

Enter the pinout command in a terminal on Raspberry Pi OS and it prints the pin arrangement for the board you are using.

Does Raspberry Pi OS include a Python editor?

Yes. Raspberry Pi OS includes a pre-installed Python editor called Thonny, so you can write and run GPIO scripts without installing anything else.

Why does RPi.GPIO warn about a pin that is already in use?

A warning appears when the same pin is used in several scripts at the same time. Give each pin to a single script, or release it before another script claims it.

Do I need to install gpiozero?

No. The library is available by default on Raspberry Pi OS, so you can import it straight away in a new script.