基于Raspberry PI+BHI 160转接板+BMM150:二进制取数及驱动适配问询
Binary Data Transfer from BHI160 Breakout + BMM150 to Raspberry Pi & Driver Compatibility Check
Hey there! Let's walk through how to pull raw binary data from your BHI160 breakout and BMM150 sensor to your Raspberry Pi, plus figure out if your specified driver will work for this.
1. First: Confirm Sensor Connection & Communication Bus
Both sensors usually communicate over I2C (the go-to for small sensors) or SPI. Let's start with I2C since it's simpler to set up on the Pi:
- Enable I2C on your Pi: Run
sudo raspi-config, navigate to Interface Options > I2C and select "Enable". Reboot your Pi to apply changes. - Verify the sensors are detected: Run
i2cdetect -y 1(use0instead of1for older Pi models). You should see the I2C addresses of the BHI160 (typically0x68or0x69) and BMM150 (usually0x10or0x11) pop up in the output.
2. How to Implement Binary Data Transfer
Binary transfer means reading the raw byte values straight from the sensor's registers—no conversion to ASCII or formatted values. Here's a step-by-step breakdown with a practical example:
- Use a low-level I2C library: For Python,
smbus2is lightweight and easy to use; for C, you can use the Linuxi2c-devinterface orwiringPi. - Example: Reading raw binary data from BMM150 with Python
First, install the library:pip install smbus2
Then run this script to pull raw binary values:import smbus2 import time # Initialize I2C bus (bus 1 for Pi 3+, 0 for older models) bus = smbus2.SMBus(1) BMM150_I2C_ADDR = 0x10 # Default address; adjust if yours is different # Set BMM150 to continuous measurement mode (check datasheet for register details) bus.write_byte_data(BMM150_I2C_ADDR, 0x4C, 0x01) while True: # Read 6 raw bytes from the data registers (X/Y/Z axes, 2 bytes each) raw_bytes = bus.read_i2c_block_data(BMM150_I2C_ADDR, 0x42, 6) # Combine the 2-byte values into raw binary integers (follow datasheet endianness!) mag_x_raw = (raw_bytes[1] << 8) | raw_bytes[0] mag_y_raw = (raw_bytes[3] << 8) | raw_bytes[2] mag_z_raw = (raw_bytes[5] << 8) | raw_bytes[4] # These are your raw binary values—you can process them or send them as-is print(f"Raw Binary Values (hex): X={hex(mag_x_raw)}, Y={hex(mag_y_raw)}, Z={hex(mag_z_raw)}") time.sleep(1) - For the BHI160, the process is identical: Initialize the sensor via its control registers, then read the raw byte blocks from its acceleration/gyroscope data registers, then combine them into raw binary values.
3. Compatibility with Your Specified Driver
Whether your driver works depends on what it's designed to do, but here's a quick breakdown:
- Bosch Official Linux Kernel Drivers: If you're using Bosch's official kernel-level drivers for BHI160/BMM150, these will absolutely work. Once installed, you can read raw binary data directly from sysfs nodes (e.g.,
cat /sys/bus/iio/devices/iio:device0/in_accel_x_rawreturns the raw binary integer value for acceleration X-axis). - Third-Party User-Space Drivers: Most Python/C libraries for these sensors (like
bhi160orbmm150PyPI packages) are built on top of low-level I2C reads. Even if they default to returning converted physical values (e.g., m/s² for acceleration), you can dig into the driver's code to find functions that read raw register bytes—those will give you the binary data you need. - Custom/Proprietary Drivers: If it's a custom driver, check if it exposes raw register read functionality. If it can communicate directly with the sensor's I2C/SPI registers, it can pull binary data.
4. Quick Tips for Smooth Binary Transfer
- Double-check the sensor datasheets for register addresses and endianness (whether the high byte comes first or vice versa)—getting this wrong will give you garbage values.
- If you need to send the binary data to another device (e.g., a server), use Python's
structmodule to pack the raw integers into byte streams, then send them over sockets. - For faster transfer speeds, you can bump the I2C baud rate: Edit
/boot/config.txtand adddtparam=i2c_arm=on,i2c_arm_baudrate=400000to switch from 100kHz to 400kHz (make sure your sensor supports this speed first!).
内容的提问来源于stack exchange,提问作者Harshil Makwana
相关产品推荐
相关产品推荐

