OLIMEX© 2020
MOD-IO user's manual
2.7.3 Reading the value of an analog input
Get the voltage applied to one of the analogue inputs of the board. The board features four 10bit
resolution analogue inputs (input voltages from 0 – 3.3V) and each of them is read with a separate
command. Command should have the following common format:
************************************
S aaaaaaaW cccccccc P S aaaaaaaR dddddddd 000000dd P
************************************
,where
S – start condition
aaaaaaa – slave address of the board
W – write mode, should be 0
cccccccc – command code, should be 0×30 for AIN1, 0×31 for AIN2, 0×31 for AIN3, 0×31 for
AIN4.
P – Stop condition
R – read mode, should be 1
dddddddd 000000dd – Little Endian (LSB: MSB) 10bit binary encoded value corresponding to the
input voltage. Range is 0 – 0×3FF and voltage on the pin is calculated using the following simple
formula: voltage = (3.3 / 1024) * (read value) [Volts]
Note: Successive readings from the board without reissuing the command code will not get an updated
value of the voltage (i.e. the user will read the same value) until another command is issued.
Example:
Reading AN1 in pseudo code:
i2cStart();
//Send start condition
i2cSend(0xb0);
//This is 0×58 shifted to left one time and added 0 as W
i2cSend(0x30);
//Read analog value of IN1
i2cStop();
//Send stop condition
i2cStart();
//Send start condition. You can use i2cRestart() instead
i2cSend(0xb1);
//This is 0×58 shifted to left one time and added 1 as W
l_byte = i2cRead(); //Read the low 8 bits of the ADC reading
h_byte = i2cRead(); //Read the high 2 bit of the ADC reading
i2cStop();
//Send stop condition
/* Since l_byte is (LSB:MSB) we need to convert it to (MSB:LSB). To swap bits one by one do
the following */
analog = 0;
for(int index = 0; index < 8; index++){
analog |= ((l_byte & 0x80) ? 1 : 0) << index;
l_byte <<= 1;
}
/* Now add the high 2 bit to the value */
analog |= ((h_byte & 0x02) ? 1 : 0) << 8;
analog |= ((h_byte & 0x01) ? 1 : 0) << 9;
Page 12 of 30