Message Boards Message Boards

0
|
13158 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Getting data from an analog-to-digital converter

Posted 6 years ago

I am using the Grove microphone and the Grove analog-to-digital converter to sample sound. The Grove platform documentation provides the following example C code to read data from the ADC that I am trying to port to Mathematica:

#include <Wire.h>

#define ADDR_ADC121             0x55

#define V_REF 3.00

#define REG_ADDR_RESULT         0x00
#define REG_ADDR_ALERT          0x01
#define REG_ADDR_CONFIG         0x02
#define REG_ADDR_LIMITL         0x03
#define REG_ADDR_LIMITH         0x04
#define REG_ADDR_HYST           0x05
#define REG_ADDR_CONVL          0x06
#define REG_ADDR_CONVH          0x07

unsigned int getData;
float analogVal=0;         // convert
void init_adc()
{
  Wire.beginTransmission(ADDR_ADC121);        // transmit to device
  Wire.write(REG_ADDR_CONFIG);                // Configuration Register
  Wire.write(0x20);
  Wire.endTransmission();  
}

void read_adc()     //unsigned int *data
{


    Wire.beginTransmission(ADDR_ADC121);        // transmit to device
    Wire.write(REG_ADDR_RESULT);                // get result
    Wire.endTransmission();

    Wire.requestFrom(ADDR_ADC121, 2);           // request 2byte from device
    delay(1);
    if(Wire.available()<=2)
    {
      getData = (Wire.read()&0x0f)<<8;
      getData |= Wire.read();
    }
    Serial.print("getData:");
    Serial.println(getData);
    delay(5);
    Serial.print("The analog value is:");
    Serial.print(getData*V_REF*2/4096); 
    Serial.println("V");
}

The code writes 0x20 to 'register 2'. After some trial and error I found out that the Mathematica equivalent seems to be:

DeviceWrite[i2c, {0,0,32}]

because after that I can read data continuously using:

d = DeviceRead[i2c, 2]; n = d[[1]] * 256 + d[[2]]

Is this the way to set 'registers' on an ADC? Also, what does setting it achieve? The Grove documentation is rather vague on this point.

Thanks, Gijsbert

3 Replies
Posted 6 years ago

Hello cmasenas I want to discus about code you have posted about 3 phase SPWM on GITHUB can i contact you on any platform like facebook or somewhere else

POSTED BY: fahad ahmed
Posted 6 years ago

Check this out... Wolfram I2C documentation

POSTED BY: cmasenas

Hi,

Typically you will set registers exactly how you have done, by sending some specific command to to the interface that is only valid for that specific device. You typically have to consult documentation to see what specific commands correspond to the register you're trying to write to, I'm not familiar with Grove sensors, but it looks like their ADC module is based off of the ADC121C021 chip, which they have helpfully provided documentation for here : https://raw.githubusercontent.com/SeeedDocument/Grove-I2CADC/master/res/ADC121C021Datasheet.pdf

Consulting that document on page 23, you can see specifics of what commands are supposed to be used for this particular device. Hope this helps.

Thanks,

Ian

POSTED BY: Ian Johnson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract