UART And I2C Serial monitoring Simultaneously

Hi, I am new here, so, apologise for any mistake in advance. I have an Arduino UNO, a NEO6M GPS (uses UART) module and a BMP180 (uses I2C) pressure sensor. What I’m trying to do is getting data from both devices in order and monitoring them in the serial monitor. And if possible, saving them in a text file. I have written a code using a library example of BMP180 and added some lines for GPS but I could not get all the data from GPS module. The first few lines of GPS output (which is in NMEA format) appears and then BMP data comes. So, how can I make them come in order and give them a kind of ID so that another microcontroller can distinguish which data belongs to which sensor. The code and the serial monitor output are attached. Thank you :slight_smile:

multi_error.jpg

multi_test.ino (1.69 KB)

You need to rewrite your code and change your way of thinking about the issue from how you would do the task yourself and how the Arduino can do it.

First you need to remove the delay function. Try to not use it anymore in future projects. It makes your Arduino stop and waste time. Your Arduino is not human and does not need vacation. :slight_smile:

Next the goal is to let the loop function run as often as possible and only do a little thing at a time (no delay, no while, not waiting for anything). This allows you to add new stuff to the loop without stopping everything else from working.

Have a look at the File -> Examples -> 02.Digital -> BlinkWithoutDelay this will teach you to do things in intervals when the do not need to be executed every time you run trough loop.

Next separate tasks and let them communicate with each other trough status variables.

Your loop code could look like this

  • if new character from GPS store in buffer
  • if character was end character e.g. new line '\n' decode NMEA and clear buffer
  • if decode message valid store values in variables
  • every second read sensor and store in buffer
  • every minute compute average
  • if new average available print
  • ... add more stuff

You might want to look at the TinyGPS or TinyGPS++ library for parsing the NMEA protocol.

If you need some more hints, please post your new code. Use code tags so code appears inside the post without the need to download a file. There is a "How to post .." at the beginning of each forum section. You will get more feedback when people can just read the code in the browser. It should looks like this.

Your code