Continuing our Android Things series, let’s learn how to use a sensor. If you look at the driver samples you’ll see samples for the BMX280. The BMX280 is a temperature/pressure sensor that is manufactured by Bosch. It consists of two models. One is the BME280 that can measure temperature, pressure and relative humidity. There is also another, called the BMP280, which only measures temperature and pressure. The current Android Things kits use the BMP280. If you look at the samples, they reference two sensor boards that contain this sensor. One is the Rainbow Hat For Android Things which costs about $25, and the other is the AdaFruit BMP I2C or SPI sensor for $10.
Less expensive options
While these sensors work well, they are pretty expensive. To be fair, the AdaFruit sensor has great documentation and a voltage regulator that allows it to work with either 5 V or 3.3 V circuits, and they’re guaranteed first run sensors. With a Pi, we only need to support 3.3 volts, which is the operating voltage for the bare sensor. Luckily, there are less expensive options available on Amazon for ~$6 and Ebay for ~$1. Warning: The cheap $1 ones tend to have a high DOA rate and may not be accurate. The quality of any sensors under $4 is somewhat suspect, because they’re priced below the actual cost of the components. The hookup and use is a bit different from the documentation provided with the samples, so let’s learn how to use these generic boards.
Hooking it up
Like the AdaFruit board, this one still has 4 wires.The main difference is that we connect it to a 3.3 V-lead instead of a 5 V one. The diagram shows the correct wiring. Some boards do not have the address select hooked up. If that’s the case, you’ll need to hook up the SDO-labeled connector to a ground (for address 0x76) or 3.3 V (for address 0x77).
I2C addresses
I2C allows multiple devices to be on one set of wires. Each device has an address that needs to be unique. Based on that, you can access the various devices to send them commands and read values from them. For more information on I2C, check out this article from SparkFun. If you read the data sheet, you’ll see that the BMP280 has a pin that is either pulled down to ground or up to the line voltage to assign an address of 0x76, or 0x77, respectively.
The Android Things contrib-drivers defaults to address 0x77, which happens to be the default address for the AdaFruit sensor and hat. The default address for the generic board is 0x76.
Modifying the samples to use 0x76
The samples do not show you how to specify the address, so here’s what you need to do.
First, you need to make sure that you have version 0.3 or higher of the BMX280 driver. It is specified in the BMX280 gradle file.
dependencies {
compile 'com.google.android.things.contrib:driver-bmx280:0.3'
provided 'com.google.android.things:androidthings:0.4-devpreview'
}
Next, in the onCreate method, you need to replace single parameter call to the BmxSensorDriver constructor with a dual parameter one that specifies the address of the sensor.
try {
mTemperatureSensorDriver = new Bmx280SensorDriver(BoardDefaults.getI2CPort(), 0x76);
mTemperatureSensorDriver.registerTemperatureSensor();
} catch (IOException e) {
Log.e(TAG, "Error configuring sensor", e);
}
Now, provided that everything is hooked up correctly, the app should run and you should see something like this in your logcat:
-20 04:53:28.315 12334-12334/com.example.androidthings.driversamples I/TemperatureActivity: sensor changed: 29.394226
07-20 04:53:28.358 12334-12334/com.example.androidthings.driversamples I/TemperatureActivity: sensor changed: 29.389133
07-20 04:53:28.526 12334-12334/com.example.androidthings.driversamples I/TemperatureActivity: sensor changed: 29.378952
07-20 04:53:28.574 12334-12334/com.example.androidthings.driversamples I/TemperatureActivity: sensor changed: 29.373861
07-20 04:53:28.661 12334-12334/com.example.androidthings.driversamples I/TemperatureActivity: sensor changed: 29.378952
07-20 04:53:28.704 12334-12334/com.example.androidthings.driversamples I/TemperatureActivity: sensor changed: 29.368769
07-20 04:53:28.748 12334-12334/com.example.androidthings.driversamples I/TemperatureActivity: sensor changed: 29.35349
07-20 04:53:28.878 12334-12334/com.example.androidthings.driversamples I/TemperatureActivity: sensor changed: 29.358585
07-20 04:53:28.921 12334-12334/com.example.androidthings.driversamples I/TemperatureActivity: sensor changed: 29.35349
07-20 04:53:29.046 12334-12334/com.example.androidthings.driversamples I/TemperatureActivity: sensor changed: 29.343311
07-20 04:53:29.051 12334-12334/com.example.androidthings.driversamples I/TemperatureActivity: sensor changed: 29.3484
07-20 04:53:29.094 12334-12334/com.example.androidthings.driversamples I/TemperatureActivity: sensor changed: 29.343311
Pressure Readings
If you want to take pressure readings you will also need have temperature readings and the callbacks for it enabled for it to work. This is a current bug with the drivers.
Stay tuned for our next post as we continue to delver deeper into Android Things!