Hi,
I am working on connecting adafruit MPL3115A2 to Arduino MEGA 2560, and running the basic Arduino example code for MPL3115A2. The code compiles and uploads fine, but the serial monitor shows that its stuck in the if loop (in void loop function)and displays "couldn't find sensor". I debugged the hardwire few times and nothing looked wrong. I dont know why the code is getting stuck in void loop function.
Please Help!!
The code is:
#include <Wire.h>
#include <Adafruit_MPL3115A2.h>
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();
void setup() {
Serial.begin(9600);
Serial.println("Adafruit_MPL3115A2 test!");
}
void loop() {
if (! baro.begin()) {
Serial.println("Couldnt find sensor");
return;
}
float pascals = baro.getPressure();
Serial.print(pascals/3377); Serial.println(" Inches (Hg)");
float altm = baro.getAltitude();
Serial.print(altm); Serial.println(" meters");
float tempC = baro.getTemperature();
Serial.print(tempC); Serial.println("*C");
delay(250);
}
This code goes in setup(), not in loop().
if (! baro.begin()) {
Serial.println("Couldnt find sensor");
Please use code tags when posting code, so it looks like the above, and tell us how you connected the sensor to the Arduino. The post "How to use this forum" gives useful instructions.
When connecting a new I2C device for the first time, always use the I2C Address Scanner program to check the connections.
Thanks for your reply. The hardware setup was pretty simple.
Arduino MPL3115A2
SCL <=> SCL
SDA <=> SDA
3.3V <=> Vin
GND <=> GND
I ran the i2C address scanner program, and the serial monitor displayed "no i2c devices found". Does that mean there is something wrong with the hardware setup? But the wiring is pretty straight forward.
Thanks
You need pullup resistors (typically 2.2K to 4.7K) from SDA and SCL to 3.3V. Check the hardware.
Thanks for your reply. I tried using the 2k, 5k pullup resistor, but it still does not work. Its not able to detect i2c device. Do you have any idea what else might be going wrong here.
Please see the attached pictures for the hardware setup.
In the picture you can see pin connection and pull resistors.
Appreciate your time and patience.
Thanks
It looks like you have not soldered the header pins to the module. If not, that is the problem.
Soldering tutorial: Tools | Adafruit Guide To Excellent Soldering | Adafruit Learning System
You can't put "return" in a void, especially not in the loop.
If you want the program to stop, you could use while(1);
You can't put "return" in a void, especially not in the loop.
Not correct. You can put the keyword return in any function, including the function named loop. There is an implied return at the end of the function, if you do not explicitly include the keyword.
Hint: the keyword void before a function name informs the compiler that the function does not return a value.
It Worked!!!
Thanks a lot for your help. Soldering resolved the issue. It worked without using any pullup resistor.
Thanks for all your help!!