difficulty interfacing ak09970n with arduino mega2560

 // set SCL pin rate to 100 Hz
  Wire.setClock(100);

Really?

Please remember to use code tags when posting code.

I'm sorry, I don't know how to use code tags.

What's wrong with the Wire.setClock() line?

aapatil:
I'm sorry, I don't know how to use code tags.

I suggest you do some more reading on the subject.

What's wrong with the Wire.setClock() line?

Did you read this?
Why not?

Also, what is wrong in the following:

Wire.endTransmission(true));

// set SCL pin rate to 100 Hz
Wire.setClock(100);

A motor car is moving at 500 m/hr. Is this car in motion?

Your ATmega328P MCU of the UNO/NANO/MEGA is running at 16 MHz; you want to exchange data between two devices at 100 bit/sec using I2C Bus. Do you think that the I2C bus will become active? Please, read this as said in Post#3.

You can remove the Wire.setClock(), then at least you do not use it in the wrong way :wink:

According to the datasheet, the AK09970N is a 3.3V sensor.
The Arduino Mega 2560 is a 5V Arduino board, and it has 10k pullup resistors from SDA to 5V and from SCL to 5V.

Some 3.3V sensors are broken when you connect the SDA and SCL of the Arduino Mega 2560 to those sensors.
If that is not enough, you push 5V into the ODINT pin of the sensor.

When a sketch stops inside the Wire.endTransmission(), there might be a shortcut of SDA to GND or from SCL to GND or a shortcut between SDA and SCL. In this case, you might also have blown the sensor.

Assume that the sensor is broken and start again without destroying the sensor with 5V. You better use a 3.3V Arduino board.

Thank you for pointing that out.

My endTransmission() still never ends. If I place a print statement right after the line, it never prints. In reference to the 5V thing, I did make sure to choose the 3.3V setting on the board. Furthermore, to ensure that the sensor wasn't shorted, I checked that the ODINT, SCL, and SDA pin had appropriate voltage drops, which they did.

I'm also finding this

Serial.begin(100);

hard to believe.

What should the baud rate be? I know that standard mode is 100 kHz and that the measurement speed is 100 Hz. I chose 100 Hz, because that's how quickly the sensor measured the data.

Please, remove the Wire.setClock() :stuck_out_tongue_closed_eyes: Just remove it.
For everyone else: the source code for Wire.setClock() is kind of dumb. I did not calculate the result, but the resulting speed could be between 40kHz and 800kHz.

How did you create the voltage drop for SDA, SCL, and ODINT ?

I think that you might have connected a 5V 40 mA digital output form the Arduino Mega 2560 board directly connected to the ODINT pin of the sensor. Does the sensor get hot ? Do you see the magic smoke ?

You chose to run an interface one thousand times slower than it is intended to be run?

The baud rate should be whatever the serial monitor or terminal emulator is set to run at.

I haven't seen rates that low since I worked on telex, in the early 1980s.

On the breadboard, I have one 20K resistor for the ODINT pin and a 3.3K resistor for the SDA and SCL pin.

i2c_wiring_diagram.PNG

So you are indeed pushing 5V 40mA into the ODINT pin of the sensor.

Throw this sensor away, buy a 3.3V Arduino board and start again.

Koepel:
When a sketch stops inside the Wire.endTransmission(), there might be a shortcut of SDA to GND or from SCL to GND or a shortcut between SDA and SCL. In this case, you might also have blown the sensor.

The Master could not come out-of Wire.begin/Wire.end session due to (very probably) non-availability of the ACK signal/pulse from the Slave. The following diagram indicates that the SDA line quickly assumes LH level after the sampling of the ACK (present or not present) signal, and the SCK is already at LH level during the sampling of the ACK signal. If ACK is not received, SCL will be stretching as much as is needed until WDT/waiting boundary comes into action. Therefore, it is hard to justify the opinion that the SDA/SCL lines might get shorted together or with GND during this situation?

Koepel:
I think that you might have connected a 5V 40 mA digital output form the Arduino Mega 2560 board

Should it be 40 mA or 20 mA?

The Arduino Wire library can get stuck in a while-loop. I have not heard that a timing problem could cause it. You can try the I2C scanner and connect SDA or SCL to GND and see what happens :wink:

The 40 mA is the shortcut current to GND. From 5V to 3.3V will not be as much as 40 mA, but I assume that it is above 20 mA.
The 20 mA is the maximum current for normal operation. Most of us don't like to go beyond the 20 mA. With a capacitive load (for example a piezo element) the peak currents can be 40 mA.

I mentioned the 5V 40 mA to add some drama to it :o Some sensors go into an automatic sleep mode and use almost no current, in that case pushing 5V 1 mA or perhaps 0.1 mA into the sensor might already damage the sensor.

I just realized that I never thanked you all! (How inconsiderate of me :stuck_out_tongue: ). You've all been very helpful :slight_smile:

Can you please run the following codes and report if you have seen 0 (zero) on the Serial Monitor?

#include<Wire.h>
byte busStatus;

void setup()
{
   Serial.begin(9600);
   Wire.begin();
   
   do
   {
      Wire.beginTransmission(AKAddress);  //enter actual 7-bit value for AKAddress
      busStatus = Wire.endTransmission();
    }
    while(busStatus != 0x00);
    
    Serial.println(busStatus, HEX);  //prints 0
    Serial.println("Slave has responded to its address..!");  
}

void loop()
{

}

I have been receiving a 2- "2:received NACK on transmit of address"

Also, would the Pro Mini 3.3V fix my issue with the 5V output: https://www.amazon.com/Diymall-Atmega328-Atmega328p-Arduino-Esp8266/dp/B00NWF2DAU/ref=sr_1_6?s=electronics&ie=UTF8&qid=1532124680&sr=1-6&keywords=arduino+mini+3.3v

aapatil:
I have been receiving a 2- "2:received NACK on transmit of address"

Now, it is confirmed that the sensor is not seen by the host at the specified slaveAddress.