Analog to digital converter (ADC) – PCF8591

There are many sensors that just provide an analog value: moisture, smoke, raindrop, light, temperature, and so on, and as we mentioned several times, the Android Things developer kits do not have any analog inputs. To be able to use these sensors as input, we need a special component called an Analog to Digital Converter (ADC).

An ADC reads an analog value and converts it to a digital value. The resolution depends on the chip; most chips offer 8 or 10 bits.

There is a driver for an ADC in the contrib-drivers library, but the component itself has been retired, so we will be using a different one: PCF8591.

The ADC included in the contrib-drivers set is no longer available

This chip has four analog input channels with eight bits of resolution, that are values between 0 and 255. Many breakout circuits based on this chip include a potentiometer, a thermistor, and a light resistor on the board; which can be connected to the analog inputs using jumpers. I find that quite handy to be used as example without needing any extra wiring.

For example, in the breakout circuit, the potentiometer is INPUT0, the light resistor is INPUT1, and the thermistor is INPUT2, and they can easily be connected to AIN2 (AIN stands for Analog Input,) respectively:

A breakout circuit of PCF8591 with a potentiometer, a light variable resistor, and a thermistor included on the board

To use this ADC, we just need to add the driver to our list of dependencies:

dependencies {
    [...]
    implementation 'com.plattysoft.things:pcf8591:+'
}

Then we can just invoke Pcf8591.open() to get an instance of the driver mapped to the default I2C bus and address:

private val pcf8591 = Pcf8591.open()

Once the Pcf8591 object is created, you can read the value from any analog input just by calling readValue and passing the channel name, or readAllValues to get them all in an array. Values are in the range from 0 to 255 (eight bits of resolution).

// Reading the channel from AIN2
val value = pcf8591.readValue(2)
// Reading the value from all channels
val allValues = pcf8591.readAllValues()

So, that is how we read analog values, but what kind of sensors can we read from?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.153.69