First of all, it took me forever to craft this topic and to give you as many details as possible, so I'd really appreciate it if you took the time to help me.
Seriously, I do.
I've tried getting my I2C connection to work for a full week now with on average 3 hours of trying and googling every day.
Please bare with me, I'm pretty new to Arduino and aside from a vibration sensor and a stepper motor I haven't gotten much to work.
Oh and by the way sorry for such a long post, but I haven't found any spoiler tags to shrink it.
Part 1 of the problem has more to do with electronics, you can safely skip to part 2 if you don't want to read a wall of text.
Anyway, let's get started:
- My Board: Arduino Pro Micro China Clone
- The SDA / SCL pins are digital 2 and 3 as shown in the pinout
- My two magnetometers: HMC5883l
- My two resistors: 4700 Ohm
My scanner code:
// I2C Scanner
// Written by Nick Gammon
// Date: 20th April 2011
#include <Wire.h>
void setup() {
Serial.begin (115200);
// Leonardo: wait for serial port to connect
while (!Serial){}
Serial.println(SDA);
Serial.print(SCL);
Serial.println ();
Serial.println ("I2C scanner. Scanning ...");
byte count = 0;
Wire.begin();
for (byte i = 1; i < 120; i++)
{
Serial.print("0");
Wire.beginTransmission (i);
Serial.print("1");
if (Wire.endTransmission () == 0)
{
Serial.print ("Found address: ");
Serial.print (i, DEC);
Serial.print (" (0x");
Serial.print (i, HEX);
Serial.println (")");
count++;
delay (1); // maybe unneeded?
} // end of good response
Serial.println("2");
} // end of for loop
Serial.println ("Done.");
Serial.print ("Found ");
Serial.print (count, DEC);
Serial.println (" device(s).");
} // end of setup
void loop() {}
Part 1: (skippable)
Different behavior I've noticed:
- Wire.endTransmission() does not hang when I just run the code without anything connected to the Arduino.
- Wire.endTransmission() does not hang when I just run the code without connecting anything but my second Magnetometer to the Arduino (more on that later).
In this case, this is the output:
2
3
I2C scanner. Scanning ...
012
012
012
012
[keeps going like this]
012
012
012
012
Done.
Found 0 device(s).
Wire.endTransmission() initially hangs when I have my setup like this:
This is the same setup as the one before, but I've left out some things not relevant to this problem.
Connecting my Magnetometers or anything else to SDA/SCL does not change the outcome of this.
The first one should just give you an overview:
However, as I've checked with a little voltage meter I got from E-Bay, there should be no current flowing after the resistors.
My Voltage meter does work:
Wire.endTransmission() stops hanging (and resumes code execution) afterwards when:
- I disconnect SDA
- I disconnect SCL
(If it's the first thing I'm doing to fix the problem)
Wire.endTransmission() keeps hanging (and freezes) when:
-
I link SDA and SCL via a jumper cable Image- - I disconnect the first Magnetometer's SDA or SCL pin and reconnect it (This isn't reproducible with my second magnetometer so I guess the first one must be broken, but I just wanted to mention it here)
-
whenever I resume the program by disconnecting SDA or SCL and reconnecting it again while the program is still running (aka the second "hang" crashes it)
In that case, this is what the output looks like:
I2C scanner. Scanning ...
012
012
012
012
012
012
012
012
012
012
012
01
[... and it hangs again.]
In these cases, the Arduino freezes unrecoverable and when I try to reupload the code, It says "upload to I/O board" and hangs. If this happens, I have to completely cut the power supply and replug it into my computer.
I have not manage to reproduce this "freezing my Arduino" thing with the second Magnetometer, but it still hangs initially for as long as I keep it connected. It looks like connecting my second Magnetometer has absolutely NO effect on the outcome of this experiment whatsoever.
Notice how I used a second Arduino (an Arduino Nano V3 connected to a Chromebook) as the power supply for the module.
If I don't do this and I use my Pro Micro as the power supply, the code never hangs, but also no device is found.
- Part 2:*
The normal setup without all the strange behavior from above:
In this case, it doesn't hang whatsoever, but the scanner never finds anything.
It doesn't matter if I use the resistors here or not, the outcome is the same.
It doesn't work if I do the same setup with two different magnetometers and three different Arduinos (Uno, Pro Micro and Nano V3) which makes me think I'm just too stupid for all of this ...
Again, I would be insanely grateful if anyone could spare some time to help me because I just don't know what to do now.
Even just a "both of your magnetometers must be dead" would be really helpful!