Bird
Raised Fist0
Raspberry Piprogramming~5 mins

Reading sensor data over I2C in Raspberry Pi - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is I2C and why is it used in sensor communication?
I2C (Inter-Integrated Circuit) is a simple communication protocol that allows multiple devices to talk to each other using just two wires: one for data and one for clock. It is used in sensors because it is efficient and requires few pins.
Click to reveal answer
beginner
Which Python library is commonly used on Raspberry Pi to read data from I2C sensors?
The 'smbus2' library is commonly used to communicate with I2C devices on Raspberry Pi. It provides easy functions to read and write data over I2C.
Click to reveal answer
beginner
What are the two main lines used in I2C communication?
The two main lines are SDA (Serial Data Line) for sending data and SCL (Serial Clock Line) for timing the data transfer.
Click to reveal answer
beginner
How do you find the I2C address of a sensor connected to Raspberry Pi?
You can use the command 'sudo i2cdetect -y 1' in the terminal. It scans the I2C bus and shows the addresses of connected devices.
Click to reveal answer
intermediate
What is the typical process to read sensor data over I2C in Python?
1. Import the smbus2 library. 2. Open the I2C bus. 3. Specify the sensor's I2C address. 4. Read bytes or words from sensor registers. 5. Convert raw data to meaningful values.
Click to reveal answer
Which two wires are used in I2C communication?
ASDA and SCL
BTX and RX
CMOSI and MISO
DVCC and GND
What command helps you find I2C devices connected to Raspberry Pi?
Agpio readall
Blsusb
Cifconfig
Dsudo i2cdetect -y 1
Which Python library is best for I2C communication on Raspberry Pi?
Arequests
Bsmbus2
Cnumpy
Dmatplotlib
What does the SCL line do in I2C?
ASupplies power
BCarries data signals
CCarries clock signals
DGrounds the circuit
What is the first step to read sensor data over I2C in Python?
AImport the smbus2 library
BWrite data to the sensor
CConvert data to string
DClose the I2C bus
Explain the steps to read data from an I2C sensor on Raspberry Pi using Python.
Think about how you start communication, get data, and make it useful.
You got /5 concepts.
    Describe the role of SDA and SCL lines in I2C communication.
    One line sends data, the other controls timing.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is the main purpose of using the I2C protocol on a Raspberry Pi when working with sensors?
      easy
      A. To communicate with multiple sensors using unique addresses
      B. To increase the Raspberry Pi's processing speed
      C. To power the sensors directly from the Raspberry Pi
      D. To store sensor data permanently on the Raspberry Pi

      Solution

      1. Step 1: Understand I2C communication basics

        I2C allows multiple devices to connect using unique addresses on the same bus.
      2. Step 2: Identify the role of I2C with sensors

        The Raspberry Pi uses I2C to talk to sensors by addressing each one uniquely.
      3. Final Answer:

        To communicate with multiple sensors using unique addresses -> Option A
      4. Quick Check:

        I2C = Multiple devices, unique addresses [OK]
      Hint: I2C uses addresses to talk to many devices [OK]
      Common Mistakes:
      • Thinking I2C increases processing speed
      • Confusing communication with power supply
      • Assuming I2C stores data permanently
      2. Which Python library is commonly used on Raspberry Pi to read data from an I2C sensor?
      easy
      A. pygame
      B. smbus
      C. matplotlib
      D. requests

      Solution

      1. Step 1: Recall common Python libraries for hardware

        For I2C communication on Raspberry Pi, the smbus library is standard.
      2. Step 2: Eliminate unrelated libraries

        pygame is for games, matplotlib for plotting, requests for web requests, so they don't fit.
      3. Final Answer:

        smbus -> Option B
      4. Quick Check:

        Python I2C library = smbus [OK]
      Hint: Use smbus for I2C sensor communication [OK]
      Common Mistakes:
      • Choosing pygame or matplotlib for sensor reading
      • Confusing web libraries with hardware libraries
      3. Given this Python code snippet reading from an I2C sensor, what will be printed?
      import smbus
      bus = smbus.SMBus(1)
      address = 0x48
      value = bus.read_byte_data(address, 0x00)
      print(value)
      medium
      A. The string 'value' printed literally
      B. An error because SMBus(1) is invalid
      C. The I2C address 0x48 printed as decimal
      D. The byte value stored at register 0x00 of device 0x48

      Solution

      1. Step 1: Understand the code's function

        The code reads one byte from register 0x00 of the device at I2C address 0x48.
      2. Step 2: Identify the output of print(value)

        It prints the numeric byte value read from that register, not the address or a string.
      3. Final Answer:

        The byte value stored at register 0x00 of device 0x48 -> Option D
      4. Quick Check:

        read_byte_data returns register byte value [OK]
      Hint: read_byte_data reads register value, not address [OK]
      Common Mistakes:
      • Thinking SMBus(1) is invalid (it's correct for Raspberry Pi)
      • Confusing address with data value
      • Expecting string output instead of number
      4. What is wrong with this code snippet for reading I2C sensor data?
      import smbus
      bus = smbus.SMBus(0)
      address = 0x40
      data = bus.read_byte_data(address, 0x01)
      print(data)
      medium
      A. read_byte_data requires three arguments, missing one
      B. The address 0x40 is invalid for I2C devices
      C. SMBus(0) is incorrect; Raspberry Pi usually uses bus 1
      D. print(data) should be print(bus.read_byte_data)

      Solution

      1. Step 1: Check SMBus bus number

        On Raspberry Pi, I2C bus 1 is standard; bus 0 is usually invalid or unused.
      2. Step 2: Verify other code parts

        Address 0x40 is valid, read_byte_data takes two arguments, and print(data) is correct.
      3. Final Answer:

        SMBus(0) is incorrect; Raspberry Pi usually uses bus 1 -> Option C
      4. Quick Check:

        Use SMBus(1) on Raspberry Pi [OK]
      Hint: Use SMBus(1) for Raspberry Pi I2C bus [OK]
      Common Mistakes:
      • Using SMBus(0) instead of SMBus(1)
      • Thinking I2C addresses like 0x40 are invalid
      • Misunderstanding read_byte_data arguments
      5. You want to read temperature data from a sensor at I2C address 0x5A which stores temperature in two registers: 0x07 (MSB) and 0x08 (LSB). How do you correctly combine these bytes to get the temperature value in Python?
      hard
      A. temp = (bus.read_byte_data(0x5A, 0x07) << 8) | bus.read_byte_data(0x5A, 0x08)
      B. temp = bus.read_byte_data(0x5A, 0x07) + bus.read_byte_data(0x5A, 0x08)
      C. temp = bus.read_byte_data(0x5A, 0x08) << 8 | bus.read_byte_data(0x5A, 0x07)
      D. temp = bus.read_byte_data(0x5A, 0x07) * bus.read_byte_data(0x5A, 0x08)

      Solution

      1. Step 1: Understand byte order for temperature

        MSB is the high byte, so shift it left by 8 bits to make space for LSB.
      2. Step 2: Combine MSB and LSB correctly

        Use bitwise OR to combine shifted MSB with LSB for full 16-bit value.
      3. Final Answer:

        temp = (bus.read_byte_data(0x5A, 0x07) << 8) | bus.read_byte_data(0x5A, 0x08) -> Option A
      4. Quick Check:

        Combine MSB << 8 with LSB using OR [OK]
      Hint: Shift MSB 8 bits left, OR with LSB to combine bytes [OK]
      Common Mistakes:
      • Adding bytes instead of bitwise OR
      • Swapping MSB and LSB order
      • Multiplying bytes instead of combining bits
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard