I'm am completing a project of mine that uses an MCP3424 i2c 18bit ADC and an arduino nano to measure temperature with an NTC thermistor but am having trouble with the arduino wire library. This is my first project using i2c so I'm not very knowledgeable about the subject but I have rigorously read the datasheet of the MCP3424 that can be found at (http://ww1.microchip.com/downloads/en/devicedoc/22088b.pdf) and tried many different ways to get it working. Also I'm new to posting on this stackexchange so pardon me if my syntax and what not is not right(I'm doing my best). Here is the code I have so far that is on my arduino.
#include<Wire.h>
#define ReadMode 0xDD //Device will output conversion data (7 bits, device address. 1 bit, Read/Write mode)
#define WriteMode 0xDC //Device expects config byte (7 bits, device address. 1 bit, Read/Write mode)
#define ConfigByte18 0x8c //PGA = 1, SPS = 3.75, One-Shot mode, channel 1, initiate new conversion.
#define ConfigByte16 0x88 //PGA = 1, SPS = 15, One-Shot mode, channel 1, initiate new conversion.
void setup() {
Serial.begin(9600);//Initiate Connection to computer
Wire.begin();//Initiate Wire library
Wire.setClock(100000);//make sure clock speed is standard mode
delay(500);//Allow boot up and settle of ADC
Wire.beginTransmission(WriteMode);//begin transmission with ADC in Write mode
Wire.write(ConfigByte18);//configure the ADC with settings found in "ConfigByte18"
Wire.endTransmission();
Serial.print("Everything worked\n");
}
void loop() {
unsigned char x, y, z, a, b, c;
//Serial.println("Bytes ready to be read:");
Wire.begin();
Wire.setClock(100000);//make sure clock speed is standard mode
Wire.beginTransmission(ReadMode);//begin transmission with ADC in write mode
Wire.requestFrom(0x6E, 6);//request 6 Bytes of information from MCP3424
x = Wire.read();//read bytes
delay(10);
y = Wire.read();
delay(10);
z = Wire.read();
delay(10);
a = Wire.read();
delay(10);
b = Wire.read();
delay(10);
c = Wire.read();
Serial.print(x);//print bytes
Serial.print(", ");
Serial.print(y);
Serial.print(", ");
Serial.print(z);
Serial.print(", ");
Serial.print(a, HEX);
Serial.print(", ");
Serial.print(b, HEX);
Serial.print(", ");
Serial.println(c, HEX);
Wire.endTransmission();
delay(1000);
}
So an explanation of my understanding: In the “void setup” section I begin transmission in write mode (see page 21 of datasheet to follow along with my explanation figure and table 5-3) in the begin transmission function I address the slave with 0xDC because the device address begins with 1101 (pre programmed) and then 110 as I have the two Adr pins tied to +5v and finally 0 as I want to change configuration settings. This translates to 0xDC in hexadecimal. Then I send the slave a byte of information for the configuration register (see page 18 section 5.2). I send 0x8C as I want my settings to be PGA = 1, SPS = 3.75, One-Shot mode, channel 1, initiate new conversion. Which should mean I need to send the slave 10001100 or 0x8c. This communication then ends as “Any byte sent after this second byte will be ignored” according to page 21. Then in the loop I begin transmission the same way apart from now I open transmission in read mode which is 0xDD as the last bit (R/W bit) is now 1. I then request six bytes of information and read the six bytes. If you now go to page 23 of the datasheet you can follow along again. Here I should get three data bytes from the slave and every subsequent byte should be the configuration register settings repeated. For now I am uninterested in the first three data bytes as I first of all just want to know how to change the configuration settings. What gets printed by the arduino instead is three data bytes from the slave, as expected, and then three of the exact same value, also as expected as they should all be the configuration settings. What is being printed in the second three bytes is “90”. Not what I expected. Now I’ve tried to figure out what has happened (hard without an oscilloscope). 90 converted from hex to binary is 10010000 and I believe the default settings are (which can be found on page 18) 00001001 could this be the default settings if so can anyone tell me whats going on and how to fix it? Thank you in advance for the help and thank you for taking the time to read my problem.