Go Down

Topic: MCP3421 (Read 2 times) previous topic - next topic

aspire

Hi all,

I am trying to interface MCP3421 with Arduino.

Hardware Setup:
SCL (MCP3421) -- Analog 5 (Arduino)
SDA (MCP3421) -- Analog 4(Arduino)
SCL & SDA pulled up using 1.1k resistor to 5V



Arduino Code
#include <Wire.h>

int addr = 0x34;   //Tried 0x68 as well

int config;
byte U8_data;
byte L8_data;

void setup()
{
Serial.begin(9600);
Wire.begin();
delay(10);
Wire.beginTransmission(addr);  
Wire.send(0xFB);
Wire.endTransmission();
}

void loop()
{
Wire.requestFrom(addr, 1);
// wait until there is data to read
while(Wire.available() < 1)
{;}
Serial.print("available:");
Serial.println(Wire.available());

// now read it
U8_data = Wire.receive();
L8_data = Wire.receive();
config = Wire.receive();

Serial.print("U8_data:");
Serial.println(U8_data);
Serial.print("L8_data:");
Serial.println(L8_data);
Serial.print("config:");
Serial.println(config);
delay(10);
}


Its not working  :(
Kindly suggest what could be possibly wrong  :-/

Thanks!

Ran Talbott

Code: [Select]
Wire.requestFrom(addr, 1);

You're only requesting 1 byte from the chip,  but trying to read 3.

You should also either make sure Wire.available() is >= 3 before you start reading,  or wait until it's > 0 before each read,  just to make sure that your code doesn't get ahead of your incoming data.




aspire

Good point!
I updated the code, but still no success.

----
#include <Wire.h>

int addr = 0x34;

int config;
int U8_data;
int L8_data;

void setup()
{
Serial.begin(9600);
Wire.begin();
delay(10);
Wire.beginTransmission(addr);  
Wire.send(0xFB);
Wire.endTransmission();
}

void loop()
{

//  Wire.beginTransmission(addr);
//  Wire.send(0b0001);
//  Wire.endTransmission();
//  delay(10);
 Wire.requestFrom(addr, 3);
// wait until there is data to read
while(Wire.available() >= 3)
{;}
Serial.print("available:");
Serial.println(Wire.available());
// now read it
U8_data = Wire.receive();
L8_data = Wire.receive();
config = Wire.receive();

Serial.print("U8_data:");
Serial.println(U8_data);

Serial.print("L8_data:");
Serial.println(L8_data);


Serial.print("config:");
Serial.println(config);

delay(10);
}
----

jbvw96

Did you have any success? I would like to use the same component, but I don't really know where to start.

PaulS

Code: [Select]
while(Wire.available() >= 3)
{;}
Serial.print("available:");
Serial.println(Wire.available());
// now read it
U8_data = Wire.receive();
L8_data = Wire.receive();
config = Wire.receive();

While there is 3 or more bytes of data to read, do nothing. Then, report how much is available and read three bytes.

This does not work, huh? Can't say I'm surprised...

fat16lib


jbvw96

Thank you very much! this totally fixed my problem in 5 minutes... turns out the last three bites of my address are not 000 (which should be the default setting) but 011, who knew. Thanks for the nice code.

Go Up