GPS and COMPASS

So ask the chatGPT why his code is not working.

1 Like

I'm sorry I did it to try something new, not to offend anyone, I'm here to write, as in the past to get help from people who know more than me.

Then why ask a mindless robot like chatGPT?

Follow the advice in post #8 and come back if you have further questions.

A few suggestions:

Get rid of the delay(), that will cause the receive buffer to overflow and corrupt the GPS data.

Do not use SoftwareSerial, the hardware Serial port can be used for both sending data to the serial monitor, and receiving data from the GPS (there is no need to have a Tx connection to the GPS). The only inconvenience is having to disconnect the Rx pin from the GPS when uploading code.

Move the code for reading from the serial input to TinyGPS++ to a function, and call it often enough that there is NEVER a delay of more than 64mS between reads. At 9600 baud, the receive buffer can overflow after 65mS, and that corrupts the GPS data. Be careful of printing too much to Serial and having to wait for space in the Tx buffer.

Use a page buffer for the OLED display, not a full buffer. Writing the full buffer to the display, especially when using SoftwareSerial, can take long enough that the receive buffer will overflow. Using a page buffer, the serial data can be fed to TinyGPS++ inside the firstPage() / nextPage() loop. Additionally, only update the display when the data changes.

Ok thanks a lot everyone, I'll try it now and let you know.

For my simplicity I'm currently starting from this code for the compass and it works correctly (then I wanted to implement the gps code which seems more complex to me).
The screen is connected to the magnetic sensor (GY-271) and to the arduino on pins A4 and A5.
At this point I connect the GPS sensor to pins D2 and D3.
Here with the simple compass code active this no longer works correctly, from random and always different degrees.
I believe the i2c protocol has interferense issues between the sensors.
but i don't know how to split the data streams.
Can you give me a tip please.

// I2C Library
#include <Wire.h>
// QMC5883L Compass Library
#include <QMC5883LCompass.h>
// OLED Library
#include <U8g2lib.h>

U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0);

QMC5883LCompass compass;

void setup() {
  // Initialize the serial port.
  Serial.begin(9600);
  // Initialize I2C.
  Wire.begin();
  // Initialize the Compass.
  compass.init();
  // Initialize the OLED display
  u8g2.begin();
}

void loop() {
  int x, y, z;

  // Read compass values
  compass.read();

  x = compass.getX();
  y = compass.getY();
  z = compass.getZ();

  // Calculate the heading in degrees
  float heading = atan2(y, x);
  if(heading < 0) heading += 2*PI;
  float headingDegrees = heading * 180/M_PI;

  Serial.print("X: ");
  Serial.print(x);
  Serial.print("   Y: ");
  Serial.print(y);
  Serial.print("   Z: ");
  Serial.print(z);
  Serial.print("   Heading: ");
  Serial.print(headingDegrees);
  Serial.println(" degrees");

  // Print the heading to the OLED display
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_ncenB08_tr);
  //u8g2.drawStr(0, 10, "Compass Heading:");
  u8g2.setFont(u8g2_font_fur20_tr);
  u8g2.setCursor(0, 30);
  u8g2.print(headingDegrees);
  //u8g2.print(" degrees");
  u8g2.sendBuffer();

  delay(300);
}

Please clarify - is this code works or not?

Yes the code works, but only without GPS connected.

Have you tried using a paged buffer with the OLED instead of the full buffer? With a full buffer, the code you posted earlier in the discussion uses almost 1800 bytes of ram, your sketch could be running out of ram and overwriting the variables. An indication of this is often random pixels appearing in the lower corner of the OLED.

The problem is that if I connect the GPS and don't modify the sketch the compass goes crazy.
Among other things, the i2c scanner only returns the oled and the magneto sensor to me as connected.

Only connect one I2C device at a time, run the scanner, write down the device ID, then to the next device.

Are all the address different?

Also, I found that many of these QMC5883L are imitations and do not work well.

How are you powering everything?

Can you power up the GPS without connecting it to the arduino? The compass sensor may be sensitive to some interference being generated by the GPS, although I would not think a GPS would be transmitting anything.

The GPS does not connect to the I2C bus, does it?

I never see the GPS in the i2c scanner. If I connect it alone to the screen and the arduino it works correctly and I see all its data, but when I then reconnect the magneto sensor it stops working.
I'm powering everything from the 3.3v pin on the arduino

There is a good reason for that - it is connected to the serial port.

2 Likes

Ok well I learned another thing, thanks, but I don't understand why once attached to the serial the compass goes mad.

Hi,
Can you please post some images of your project?

Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

The 3.3V pin can only supply a small amount of current (I've seen around 150mA stated), that is likely not enough. It is a very bad idea to run 3.3 peripherals on a 5V UNO without voltage translators between the peripherals and the UNO, can you post a complete schematic?
Sorry, forgot you were using a Nano.

Are you using a 3.3V Nano? A 5V Nano would have multiple problems with 3.3V peripherals.

How are you powering the Nano? You cannot power that many peripherals off the on-board voltage regulation for 3.3V, use an external 3.3V supply.

This is the scheme.

Hi,
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

This would make it even more readable.

Thanks... Tom... :smiley: :+1: :coffee: :australia:


Sorry I think it's more complete now.