Ultrasonic sensor & GPS module on one arduino

I'm working on a project for school. The idea is to make a device that will be installed on a train that will take a measurement of the gap from the train car to the platform and also take a fix of where it is (which station it is at) and then I will log this data on an SD card reader/writer. I have the ultrasonic sensor and the GPS module set up correctly; they both work perfectly individually. However, when I try to include both of them in the same sketch, the readings that I'm getting in the serial monitor are not consistent for the distance measurement. I do not need them running at the same time, I just need them to take accurate readings one after the other, it does not matter which order. Here is the code (it's long but any help would be much appreciated);

#include <Adafruit_GPS.h> //Install the adafruit GPS library
#include <SoftwareSerial.h> //Load the softwareserial library
SoftwareSerial mySerial(3,2); //Sets up new serial port on pins 3 and 2
Adafruit_GPS GPS(&mySerial); //Create the GPS object

int trigPin = 11; //trig pin connected to pin 11
int echoPin = 12; //echo pin connected to pin 12
float pingTime; //time for ping to travel from sensor to target AND return
float targetDistance; //distance to target in inches
float speedofSound= 776.5; //speed of sounds in mph when temp is 77 degrees

String NMEA1; //Variable for first NMEA sentence
String NMEA2; //Variable for second NMEA sentence
char c; //to read characters coming from the GPS
float deg; //will hold pisition data in degree format
float degWhole; //variable for the whole part of position
float degDec; //variable for decimal part of degree

unsigned long previousMillis = 0; //Will store time that the sensor last ran

const long interval = 3000; //interval at which the sensor will start

void setup() {

Serial.begin(9600); //turn on serial monitor
GPS.begin(9600); //turn on GPS at 9600 baud rate
GPS.sendCommand("$PGCMD,33,0*6D"); //Sending command to turn off status updates about external antenna
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); //set update rate to 1Hz
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //request RMC and GGA sentences only
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);

delay(1000);

}

void loop() {
unsigned long currentMillis = millis(); //currentMillis is the time that the arduino has been running in milliseconds

if(currentMillis - previousMillis>interval){ //should be a 3 second delay without the delay function
previousMillis = currentMillis;
ultrasensor(); //run the ping sensor
} else {
readGPS();
}

}

void readGPS() {

clearGPS();
while(!GPS.newNMEAreceived()) { //loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //parse that last good NMEA sentence
NMEA1=GPS.lastNMEA();

while(!GPS.newNMEAreceived()) { //loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //parse that last good NMEA sentence
NMEA2=GPS.lastNMEA();

//Serial.println(NMEA1);
//Serial.println(NMEA2);
//Serial.println("");

Serial.print(GPS.latitude);
Serial.print(",");
Serial.print(GPS.longitude);
Serial.print(",");
Serial.println(GPS.speed);

}

void clearGPS() { //Clear old and corrupt data from serial port
while(!GPS.newNMEAreceived()) { //loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //parse that last good NMEA sentence
while(!GPS.newNMEAreceived()) { //loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //parse that last good NMEA sentence
while(!GPS.newNMEAreceived()) { //loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //parse that last good NMEA sentence

}

void ultrasensor() { //makes function outside of the loop so when commanded in the loop, it will perform this function
digitalWrite(trigPin, LOW); //set trigger pin low
delayMicroseconds(2000); //let signal settle
digitalWrite(trigPin, HIGH); //set trigger high
delayMicroseconds(15); //delay in high state
digitalWrite(trigPin, LOW); //ping has now been sent
delayMicroseconds(10); //delay in low state

pingTime = pulseIn(echoPin, HIGH); //in microseconds
pingTime = pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
pingTime = pingTime/3600; //convert pingTime to hours by dividing by 3600 (seconds in one hour)
targetDistance = speedofSound * pingTime; // this will be in miles, since speed of sound was mph
targetDistance = targetDistance/2; //ping travels to target and back, so must divide by 2 for actual target distance
targetDistance = targetDistance*63360; //convert miles to inches by multiplying by 63360 (inches per mile)

Serial.print("The Distance to Target is: "); //Serial monitor will display this
Serial.print(targetDistance); //serial monitor will display this after previous message
Serial.println(" inches"); //last part of the line in the serial monitor (thats why its a println)
}

Have you got an inadequate power supply?
You've certainly got a shortage of code tags.

I just have it connected to my computer via the USB cord, what do you mean by code tags?

I included a picture of my wiring.

Design_device.JPG

zack2321:
I just have it connected to my computer via the USB cord,

And is that sufficient?

what do you mean by code tags?

The things that should enclose the code you posted.

Yeah the USB is sufficient. when I open up the serial monitor, it is showing what I tell it to but, for example, if I have an object at a distance of 4 inches. the serial monitor will say its 4 inches, but then the next reading will be 1.00 inches, 0.5 inches, then back to 4 inches. So there is no real consistency to the readings. The GPS unit works fine though, it presents accurate information every reading in the serial monitor.

1 Like
void loop() {
unsigned long currentMillis = millis(); //currentMillis is the time that the arduino has been running in milliseconds
  
  if(currentMillis - previousMillis>interval){  //should be a 3 second delay without the delay function
    previousMillis = currentMillis; 
    ultrasensor(); //run the ping sensor 
    } else {
     // readGPS(); 
    }
}

And now?

(Those are code tags, BTW. They're explained in the read me in bold at the top of this, and just about every other, section of the forum)

The ultrasonic sensor reports accurate values now. I see that you made the readGPS a comment so it has to be something with that function that is screwing up the consistency of the distance sensor?

Refer this thread -
as i am using cheap china made gps module
for me it start working bit better at - "const long interval = 2000;" but still i was getting garbage values.

Actually the line which set the baud rate for GPS ( E.x = GPS.begin(9600); ) creates conflict between GPS and ultrasonic sensor. So another way to tackle this is to turn on and off the gps baud rate code by using "GPS.begin(9600);" and "GPS.end();", since in most case constant data from ultrasonic sensor is required but gps coordinates are required time to time.