Reading Sensors over I2C with Raspberry Pi
Reading Sensors over I2C with Raspberry Pi I2C is a two-wire protocol letting you chain multiple sensors on two GPIO pins. BME280, SSD1306 displays, and RTC modules all use I2C. Enable I2C bash sudo raspi-config Interface Options > I2C > Enable Wiring Multiple I2C devices share the same SDA/SCL — each has a unique address. Detect Devices bash sudo apt install i2c-tools -y sudo i2cdetect -y 1 Shows addresses of all connected devices (e.g. 0x76 for BME280). BME280 Temperature/Humidity/Pressure bash pip install adafruit-circuitpython-bme280 python import board import adafruitbme280.basic as adafruitbme280 i2c = board.I2C() bme280 = adafruitbme280.AdafruitBME280I2C(i2c) print(f"Temperature: {bme280.temperature:.1f} C") print(f"Humidity: {bme280.humidity:.1f} %") print(f"Pressure: {bme280.pressure:.1f} hPa") SSD1306 OLED Display bash pip install adafruit-circuitpython-ssd1306 pillow python import board, adafruitssd1306 from PIL import Image, ImageDraw i2c = board.I2C() oled = adafruitssd1306.SSD1306I2C(128, 64, i2c) img = Image.new("1", (128, 64)) draw = ImageDraw.Draw(img) draw.text((0, 0), "Hello World", fill=255) oled.image(img) oled.show() Multiple Devices Each I2C device needs a unique address. If two devices share an address, use a TCA9548A I2C multiplexer.
Related Guides
- I2C vs SPI vs UART: How to Choose and Use Serial Communication Protocols
- Raspberry Pi GPIO: Complete Beginner Guide with Python Examples
- I2C Wiring and Protocol Guide for Arduino, ESP32, and Raspberry Pi
- How to Control GPIO Pins on Raspberry Pi with Python
- Automated Plant Watering System with Raspberry Pi
- Controlling GPIO Outputs with Python — LED, Relay, and Buzzer
- Flipper Zero GPIO: Reading Sensors and Controlling LEDs
- Flipper Zero GPIO Pinout and Hardware Expansion: UART, I2C, ADC, 1-Wire, and Sensor Wiring