SEN0405: Accelerometer data at 100Hz

userHead Nicolas_1410 2024-06-26 17:29:10 68 Views2 Replies

Hi

 

I try to retrieve data using a SEN0405 accelerometer on a raspberry Pi4.

Using examples, I manage to get data, but at a low rate. However, I do configure it with the appropirate rate.

I tried several power modes.

Data are recorded in a file with timestamping. There is no consistency in time and only 10-12 data per seconds.

How is it possible to get data at a real rate of 100Hz, with a timestamping incrementing 10ms.

 

Thanks, any help with this issue would be greatly appreciated

2024-07-17 21:41:22

I also use chatGPT with no success

Solution is to go with C.

Python is way too slow to manage register reading efficiently. With C, I can go to the max sampling freq easily

The ST C exemples are very great

https://github.com/STMicroelectronics/STMems_Standard_C_drivers/blob/master/lis2dw12_STdC/examples/lis2dw12_read_fifo.c

 

userHeadPic Nicolas_1410
2024-07-01 17:44:25

To achieve a data sampling rate of 100Hz with timestamping at 10ms intervals using the SEN0405 accelerometer on a Raspberry Pi 4, you need to ensure several factors are correctly configured and optimized. Here’s a step-by-step guide to help you achieve this:

### 1. Hardware Setup

- **Connection**: Ensure the SEN0405 accelerometer is correctly connected to your Raspberry Pi 4. Typically, this involves connecting the sensor’s communication lines (such as I2C or SPI) to the corresponding GPIO pins on the Raspberry Pi.

### 2. Software Setup

#### a. Library Installation

- **Python Library**: Use a suitable Python library to interface with the SEN0405 accelerometer. Libraries like `smbus` or specific sensor libraries provided by the manufacturer (if available) can simplify communication.

#### b. Data Acquisition Setup

- **Configure Data Rate**: Set the accelerometer to output data at 100Hz. This is typically done by configuring the accelerometer’s internal registers using specific commands or functions provided by the library.

- **Timestamping**: Use the Raspberry Pi’s system clock (time module in Python) to timestamp each data point as it is received from the accelerometer.

#### c. Code Example

Here’s a basic example in Python to get you started. Note that exact commands and initialization procedures may vary based on the specific library and accelerometer model you are using:

```python
import smbus
import time

# Initialize I2C (replace with correct bus number for your Raspberry Pi)
bus = smbus.SMBus(1)

# SEN0405 accelerometer address
address = 0x19  # Replace with your accelerometer's I2C address

# Configuration commands for SEN0405 (example)
bus.write_byte_data(address, 0x20, 0x77)  # Set CTRL_REG1 to 100Hz output rate

# Open file for recording data with timestamp
file_name = "accel_data.csv"
with open(file_name, 'w') as f:
   f.write("Timestamp,X,Y,Z\n")  # Header

   # Read and record data continuously
   start_time = time.time()
   while True:
       # Read data from accelerometer
       data = bus.read_i2c_block_data(address, 0x28 | 0x80, 6)  # Example: read X, Y, Z axes

       # Convert data to meaningful values (example)
       x = data[0] << 8 | data[1]
       y = data[2] << 8 | data[3]
       z = data[4] << 8 | data[5]

       # Get current timestamp in milliseconds
       timestamp = int((time.time() - start_time) * 1000)

       # Write data to file
       f.write(f"{timestamp},{x},{y},{z}\n")
       f.flush()  # Flush buffer to ensure data is written immediately

       # Sleep to maintain 100Hz rate (adjust sleep time based on actual sensor response time)
       time.sleep(0.01)  # 10ms interval

```

### Explanation:

- **I2C Initialization**: Initialize the SMBus object (`bus`) for communication with the accelerometer.
- **Configuration**: Set the accelerometer’s control registers (`CTRL_REG1`) to achieve a 100Hz output rate (`0x77` is an example configuration).
- **Data Reading**: Continuously read data from the accelerometer, convert it into meaningful values (like acceleration in X, Y, Z axes), and timestamp each reading based on the system time.
- **Data Logging**: Write timestamped data to a CSV file (`accel_data.csv`), ensuring each data point is logged with a 10ms timestamp increment.

### Additional Considerations:

- **Sensor Specifics**: Consult the datasheet of your SEN0405 accelerometer for exact register settings and communication protocols.
- **Timing Accuracy**: Achieving exact 100Hz sampling can be challenging due to processing delays and sensor response times. Adjust the sleep interval (`time.sleep(0.01)`) based on actual sensor performance.  https://www.pneda.com/
- **Data Integrity**: Ensure the data recorded matches the expected format and contains accurate timestamps relative to the start time (`start_time`).

By following these steps and adjusting parameters as necessary based on your specific sensor and application requirements, you should be able to achieve consistent data sampling at 100Hz with accurate timestamping on your Raspberry Pi 4. Adjustments may be needed based on sensor response times and system performance.

userHeadPic JAMES.JACK