I am getting started with GPS modules, for my project I am using the NEO 6M GPS module with a Ceramic Antenna. So, earlier I tried to connect the GPS module with an Arduino Nano and was able to successfully display the GPS NEMA sentences output on the Arduino IDE Serial Monitor, although the GPS module was not able to connect to any satellites.
Now, I am trying to add an SSD1306 OLED module to this project. So, that the latitude and the longitude values can be fetched from the GPS module and can be displayed on the OLED Screen whenever the GPS updates its position.
The code which I have written so far was compiled properly in the Arduino IDE, but the OLED module failed to display the expected information. I kept on getting the "SSD1306 allocation failed" error on the Serial Monitor.
I have already verified the I2C Address for my OLED display module which is 0x3C instead of 0x3D.
I have also tested the OLED display with a separate code and it is working fine.
Then also I am not able to figure out what's wrong with this code that it is leading to these unexpected results.
The code which I am using for this project is attached below.
So, if a fellow programmer from the community can help me solve this issue then that would be really appreciated.
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#define OLED_WIDTH 128
#define OLED_HEIGHT 64
#define OLED_RESET -1
#define OLED_ADDR 0x3C
Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, OLED_RESET);
SoftwareSerial serial_connect(4, 3); //Rx:pin(4) Tx:pin(3)
TinyGPSPlus gps;
void setup()
{
Serial.begin(9600);
serial_connect.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!oled.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR))
{
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
Serial.println("GPS start");
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0, 0);
oled.print("Detecting GPS");
oled.setCursor(0, 10);
oled.print("Coordinates");
oled.display();
delay(5000); //5 seconds delay
}
void loop()
{
while(serial_connect.available())
{
gps.encode(serial_connect.read());
}
if(gps.location.isUpdated())
{
Serial.println("Satellite Count:");
Serial.println(gps.satellites.value());
Serial.println("Latitude:");
Serial.println(gps.location.lat(),6);
Serial.println("Longitude:");
Serial.println(gps.location.lng(),6);
Serial.println("Speed MPH:");
Serial.println(gps.speed.mph());
Serial.println("Altitude Feet:");
Serial.println(gps.altitude.feet());
Serial.println(" ");
oled.clearDisplay();
oled.setCursor(0, 0);
oled.print("Lati=");
oled.setCursor(0, 10);
oled.print(gps.location.lat(),6);
oled.setCursor(10, 0);
oled.print("Long=");
oled.setCursor(10, 10);
oled.print(gps.location.lng(),6);
oled.display();
delay(500);
}
}