Reading The Value Of An Analog Input - OLIMEX MOD-IO User Manual

Open-source hardware uext extension board with relays
Hide thumbs Also See for MOD-IO:
Table of Contents

Advertisement

OLIMEX© 2020

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();
i2cSend(0xb0);
i2cSend(0x30);
i2cStop();
i2cStart();
i2cSend(0xb1);
l_byte = i2cRead();
h_byte = i2cRead(); //Read the high 2 bit of the ADC reading
i2cStop();
/* 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;
//Send start condition
//This is 0×58 shifted to left one time and added 0 as W
//Read analog value of IN1
//Send stop condition
//Send start condition. You can use i2cRestart() instead
//This is 0×58 shifted to left one time and added 1 as W
//Read the low 8 bits of the ADC reading
//Send stop condition
Page 12 of 30
MOD-IO user's manual

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the MOD-IO and is the answer not in the manual?

Table of Contents