BME680 error code -2 is a sign of communication error. Could you please confirm the schematic of the BME680 connections? For example if using the I2C interface, does the I2C address selected in hardware match the I2C address indicated in your software?
On a side note, please keep in mind that the Arduino Mega operates at 5V while the BME680 can only operate at up to 3.3V (3.6V Max), therefore level shifting may be needed in your setup.
... View more
**PS: Handytech's code incorrectly sets the burst length to 1 byte, it should be 8 bytes.
To shortly comment about this::
In Setup Mode (or manual mode) of the Magnetometer interface:
The number of data bytes in each (burst) read transaction on the bus is indeed defined by aux_rd_burst_len. But in case the number of bytes requested is larger than aux_rd_burst_len, I believe the sensor API handles that and generates the necessary amout of smaller transactions.
The number of data bytes in each write transaction is always 1 (i.e. no burst write is supported, regardless of aux_rd_burst_len).
In Data Mode of the Magnetometer interface:
Reading of the data is always done in a single I2C burst read operation with a fixed length of 8 bytes (for the BMM150/BMX160 this corresponds to MAG_[X-Z] and RHALL registers).
Based on the above, one could reduce the amount of transactions to the magnetometer during the initialization by setting aux_rd_burst_len to 8 bytes, but I believe my code snippet using the official sensor API should work anyhow?
... View more