Using i2c Display and Rx,Tx GPS Simultaneously, Arduino nano

I have an NEO-6M GPS Module and Adafruit_SH1106G display.
Gps works on Rx and Tx pins (0,1) and display communicates by SDA and SCL.
I'm trying to make a code that will request velocity from gps and display a diffrent image on the screen based on velocity output. I've run into issues with communication where i can't get screen and gps working at the same time. Here's my current state of code that should display current speed every .5s. I'm new to programming and i'm not sure why it's not working. Help appreciated.


#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>

static const int RXPin = 0, TXPin = 1;
static const uint32_t GPSBaud = 9600;

TinyGPSPlus gps;
SoftwareSerial ss(RXPin, TXPin);

/* Uncomment the initialize the I2C address , uncomment only one, If you get a totally blank screen try the other*/
#define i2c_Address 0x3c //initialize with the I2C addr 0x3C Typically eBay OLED's
//#define i2c_Address 0x3d //initialize with the I2C addr 0x3D Typically Adafruit OLED's

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1   //   QT-PY / XIAO
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2


#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH  16
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000,
  B00000001, B11000000,
  B00000001, B11000000,
  B00000011, B11100000,
  B11110011, B11100000,
  B11111110, B11111000,
  B01111110, B11111111,
  B00110011, B10011111,
  B00011111, B11111100,
  B00001101, B01110000,
  B00011011, B10100000,
  B00111111, B11100000,
  B00111111, B11110000,
  B01111100, B11110000,
  B01110000, B01110000,
  B00000000, B00110000
};


void setup()   {

  Serial.begin(9600);


  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.

  delay(250); // wait for the OLED to power up
  display.begin(i2c_Address, true); // Address 0x3C default
 //display.setContrast (0); // dim display
 
  display.display();
  delay(2000);

  // Clear the buffer.
  display.clearDisplay();

  // draw a single pixel
  display.drawPixel(10, 10, SH110X_WHITE);
  // Show the display buffer on the hardware.
  // NOTE: You _must_ call display after making any drawing commands
  // to make them visible on the display hardware!
  display.display();
  delay(2000);
  display.clearDisplay();

        display.setTextSize(1);
        display.setTextColor(SH110X_WHITE);
        display.setCursor(0, 0);

      ss.begin(GPSBaud);

}


void loop() {
 while (ss.available() > 0){
    gps.encode(ss.read());
    if (gps.location.isUpdated()){

      // Speed in kilometers per hour (double)
      Serial.print("Speed in km/h = "); 
      Serial.println(gps.speed.kmph()); 


       display.println(gps.speed.kmph());
        display.display();
      delay (500);
      display.clearDisplay();
    }
  }
}


1 Like

Well:

  Serial.begin(9600);

initializes the hardware serial port on pins 0 and 1. And then

static const int RXPin = 0, TXPin = 1;
...
SoftwareSerial ss(RXPin, TXPin);
...
      ss.begin(GPSBaud);

initializes a software serial port on the same pins.

You can't do that. You can have a hardware serial port running on 0 and 1 or a software serial port on 0 and 1. You can't have both.

Either move the GPS I/O to something other than pins 0 and 1, or get rid of all the software serial stuff, get rid of the debugging statements to Serial, replace ss.available() with Serial.available() and ss.read() with Serial.read(). Don't try and use the serial port to listen to the GPS and for debugging unless you're just hooking up the GPS's Tx line to the Uno.

1 Like

I got rid of all the software serial and now it works like a charm! Thanks a lot <3

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.