How it works...

The first read from the device after it has been switched on will return 0x80 and will also trigger the new sample from channel 0. If you read it a second time, it will return the sample previously read and generate a fresh sample. Each reading will be an 8-bit value (ranging from 0 to 255), representing the voltage to VCC (in this case, 0 V to 3.3 V). On the www.dx.com module, channel 0 is connected to a light sensor, so if you cover up the module with your hand and resend the command, you will observe a change in the values (darker means a higher value and lighter means a lower one). You will find that the readings are always one behind; this is because, as it returns the previous sample, it captures the next sample.

We use the following command to specify a particular channel to read:

sudo i2cset -y 1 0x48 0x01  

This changes the channel that is read to channel 1 (this is marked as AIN1 on the module). Remember, you will need to perform two reads before you see data from the newly selected channel. The following table shows the channels and pin names, as well as which jumper connectors enable/disable each of the sensors:

Channel

0

1

2

3

Pin Name

AIN0

AIN1

AIN2

AIN3

Sensor

Light-Dependent Resistor

Thermistor

External Pin

Potentiometer

Jumper

P5

P4

P6

Next, we control the AOUT pin by setting the analog output enable flag (bit 6) of the control register and using the next value to set the analog voltage (0V-3.3V, 0x00-0xFF), as follows:

sudo i2cset -y 1 0x48 0x40 0xff   

Finally, you can set bit 2 (0x04) to auto increment and cycle through the input channels as follows:

sudo i2cset -y 1 0x48 0x04

Each time you run i2cget -y 1 0x48, the next channel will be selected, starting with channel AIN0, then running from AIN1 through to AIN3 and back to AIN0 again.

To understand how to set a particular bit in a value, it helps to look at the binary representation of the number. The 8-bit value 0x04 can be written as b0000 0100 in binary (0x indicates the value is written in hexadecimal, or hex, and b indicates a binary number).

Bits within binary numbers are counted from right to left, starting with 0 - that is, MSB 7 6 5 4 3 2 1 0 LSB.

Bit 7 is known as the most significant bit (MSB) and bit 0 is known as the least significant bit (LSB). Therefore, by setting bit 2, we end up with b0000 0100 (which is 0x04).
..................Content has been hidden....................

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