Ultrasonic Sensor Error With NEO6M GPS

Hey guys!

I’m trying to make an led turn on when something gets close to my ultrasonic sensor. I also have a neo-6m gps that I’m using to get time and other info. The problem is, my ultrasonic sensor prints 0 randomly when I put something close to it. But I learnt that when I disconnected the gps, it didn’t print 0. This is my code.

//temp & humidity and ultrasonic sensor & gps module library
#include <Adafruit_HTU21DF.h>
#include <TinyGPS++.h>
#include <NewPing.h>
#include <Wire.h>
#include "U8glib.h"
#include <SoftwareSerial.h>

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);  // I2C / TWI 
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
NewPing sonar(2, 3, 1000);

static const int RXPin = 5, TXPin = 4;
static const uint32_t GPSBaud = 9600;

const int trigPin = 2;
const int echoPin = 3;

// defines variables
long duration;
int distance;
int tempc;
int tempf;
int hum;
unsigned long starttime;

// The TinyGPS++ object
TinyGPSPlus gps;

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

//temp display function
void temp(void) {

  //temp
  u8g.setFont(u8g_font_profont22);
  u8g.setPrintPos(70, 35);
  u8g.print(tempf);
  u8g.setPrintPos(18, 35);
  u8g.print(tempc);

  //degree
  u8g.setFont(u8g_font_profont12);
  u8g.drawStr(94, 25, "o");
  u8g.drawStr(42, 25, "o");

  //celcius / fahrenheit 
  u8g.setFont(u8g_font_profont17);
  u8g.drawStr(103, 35, "F");
  u8g.drawStr(51, 35, "C");

  //seperating lines
  u8g.drawLine(17, 40, 111, 40);
  u8g.drawLine(64, 40, 64, 20);

  //humidity
  u8g.setFont(u8g_font_profont22);
  u8g.setPrintPos(50, 60);
  u8g.print(hum);
  u8g.drawStr(73, 60, "%"); 

}

//check distance
void checkDis(){
    
  delay(50);
  int distance = sonar.ping_cm();
  Serial.print("Distance: ");
  Serial.println(distance);
  
  if (distance > 14){
    digitalWrite(12, LOW);
  }
  else if (distance <= 14){
    digitalWrite(12, HIGH);
  }
}


//get gps time
void gpsTime(){
  while (ss.available() > 0){
    gps.encode(ss.read());
    if (gps.location.isUpdated()){
      String ampm;
      Serial.println(gps.location.lat(), 6);
      int hr = gps.time.hour() - 5;
      int what = gps.time.hour();
      int mi = gps.time.minute();
      
      if (hr<12 and hr>0){ 
        if(hr==0){
          hr = 12;
        }
        ampm = "AM";
      }
      else if (hr==12){
        ampm = "PM";
      }
      else if (hr>12){
        
        hr = hr-12;
        ampm = "PM";
      }
      else if (hr<0){
        int savehr = hr;
        hr = 12 + savehr;
        ampm = "PM";
      }
      else{
        
      }

      Serial.print(hr); 
      Serial.print(":");
      if (mi>9){
        Serial.print(mi); 
        Serial.print(" ");
      }        
      else{
        Serial.print("0");
        Serial.print(mi);
        Serial.print(" ");
      }
      Serial.println(ampm);
    }
  }
}

void setup(){
  Serial.begin(9600);
  ss.begin(GPSBaud);
  
  // rotate lcd
  u8g.setRot180();

  //define pins
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT);

  //led pin
  pinMode(12, OUTPUT);

  //find sensor
  Serial.println("HTU21D-F test");
  if (!htu.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);
  }
}


void loop(){

  //get temp & humidity
  tempc = round(htu.readTemperature());
  tempf = round((tempc * 1.8) + 32);
  hum = round(htu.readHumidity());

  //check distance
  checkDis();

  //get gps time
  gpsTime();

 
  //display temp
  u8g.firstPage();  
  do {  
    u8g.drawLine(0, 12, 130, 12);
    temp();
  } while( u8g.nextPage() );

    
}

Any help or work-around would be appreciated! Ask me any questions if clarification is needed.

Is this related to this?

Yes. However I'm using a different library, newPing.