Pages: [1]   Go Down
Author Topic: MCP3421  (Read 2116 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-sad
Kindly suggest what could be possibly wrong  :-/

Thanks!
Logged

Rural Arizona
Offline Offline
Edison Member
*
Karma: 7
Posts: 1711
Incorrigible tinkerer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.



Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
}
----
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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...
Logged

0
Offline Offline
Edison Member
*
Karma: 64
Posts: 1639
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is a link to a demo sketch for the MCP342X ADCs

http://forums.adafruit.com/viewtopic.php?f=31&t=12269&p=76280&hilit=great+adc#p60335
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: