.begin() Needs to be run once during the setup, or after any settings have been modified. In order to let the sensor’s configuration take place, the BME280 requires a minimum time of about 2 ms in the sketch before you take data.
Unfortunately, this doesn’t really help. I am running the example provided by Sparkfun after hooking up the sensor. There is already a call to begin. I just added an extra call to time.sleep but I still get negative altitudes reported. Here is the code, as supplied by SparkFun, with the additional sleep:
from __future__ import print_function
import qwiic_bme280
import time
import sys
def runExample():
print("\nSparkFun BME280 Sensor Example 1\n")
mySensor = qwiic_bme280.QwiicBme280()
if mySensor.connected == False:
print("The Qwiic BME280 device isn't connected to the system. Please check your connection", \
file=sys.stderr)
return
mySensor.begin()
time.sleep(1)
while True:
print("Humidity:\t%.3f" % mySensor.humidity)
print("Pressure:\t%.3f" % mySensor.pressure)
print("Altitude:\t%.3f" % mySensor.altitude_feet)
print("Temperature:\t%.2f" % mySensor.temperature_fahrenheit)
print("")
time.sleep(1)
if __name__ == '__main__':
try:
runExample()
except (KeyboardInterrupt, SystemExit) as exErr:
print("\nEnding Example 1")
sys.exit(0)
It runs just fine, but the pressure/altitude settings are wrong.
Do you have an Arduino so that you could try the hardware along with a BME280 library there to ensure that your device isn’t the source of the problem?