Search code examples
c++arduinosensorsi2cserial-communication

Serial Communication freezes with BMP180 and Arduino Mega


I recently bought an ELEGO Mega 2560, or in other words an Arduino Mega. I bought a bmp180 sensor as well. I connected the bmp in this fashion, VCC - 3.3v, GND - GND, SCL - 21, SDA - 20. I uploaded a simple code which just displayes altitude. When I go to the Serial Monitor to view the results, nothing pops up. It is suppose to say BMP init success if it connects, and fail if it doesn't. When I go to the monitor, it just doens't say anything. When I disconnect the sensor, it says fail. It appears as if the Serial Monitor just freezes. Also a headsup, my code is very messy, I'm sorry if it's hard to keep up.

#include <Wire.h>
#include <SFE_BMP180.h>

SFE_BMP180 bmp180;
float Po = 1014.9;

#define ledPin 7
#define TransmitPin 5

//int Altitude = 5;

int sendValue;
String incomingString;

unsigned long lastTransmission;
const int interval = 1000;

void setup() {
  
  Wire.begin();
  pinMode(ledPin, OUTPUT);
  pinMode(2, INPUT);
  pinMode(10, OUTPUT);
  pinMode(TransmitPin, OUTPUT);

  bool success = bmp180.begin();
  Serial.begin(115200);

  if (success) {
    Serial.println("BMP180 init success");
  }
  else 
    Serial.println("fail");
}

void loop() {
  sendValue = digitalRead(29);
  if (sendValue == HIGH) {
    if (millis() > lastTransmission + interval) {
      Serial.println("AT+SEND=1,8,Return");
      digitalWrite(TransmitPin, HIGH);
      delay(100);
      digitalWrite(TransmitPin, LOW);
      lastTransmission = millis();
    }
  }

  if (Serial.available()) {
    incomingString = Serial.readString();
    if (incomingString.indexOf("Testing!") > 0) {
      digitalWrite(10, HIGH);
      delay(100);
      digitalWrite(10, LOW);
    }
  }

  char status;
  double T, P, alt;
  bool success = false;

  status = bmp180.startTemperature();

  if (status != 0) {
    delay(1000);
    status = bmp180.getTemperature(T);

    if (status != 0) {
      status = bmp180.startPressure(3);

      if (status != 0) {
        delay(status);
        status = bmp180.getPressure(P, T);
        if (status != 0) {
          if (millis() > lastTransmission + interval) {
            alt = bmp180.altitude(P, Po);
            Serial.print("AT+SEND=1,8,");
            int altAsFoot = alt * 3.281;
            Serial.println(altAsFoot);
            digitalWrite(TransmitPin, HIGH);
            delay(100);
            digitalWrite(TransmitPin, LOW);
          }
          for (int i = 0; i < 1800; i++) {
            delay(1);
            if (Serial.available()) {
              incomingString = Serial.readString();
              if (incomingString.indexOf("+OK") > 0) {
                digitalWrite(ledPin, HIGH);
                delay(100);
                digitalWrite(ledPin, LOW);
              }
              if (incomingString.indexOf("Testing!") > 0) {
                digitalWrite(10, HIGH);
                delay(100);
                digitalWrite(10, LOW);
              }
            }
          }
        }
      }
    }
  }
}

Solution

  • Turns out it was a hardware issue. I had ground shorted to SDA. I'm assuming the same will happen if it's shorted to SCL. Make sure both SDA and SCL aren't shorted to each other or ground.