Entire Project freez

Hi So im having some issues with my arduino project wich is using Arduino UnoR3 and Arduino ide 2.3.3;
So im using a GSM (Sim800l) + Alcohhol sensor + Buzzer + relay +Vibration sensor + HC-SR04+Oled screen , to detect and prevent Car Accident and all of it work fine but when i add The Gps module to send location with sms the entire project stop working even the Oled stop displaying but i got no error in the compilation .
Here the code im using

#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <SoftwareSerial.h>
#include <TinyGPSPlus.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

 // SIM800L Rx is connected to Arduino D10 and SIM800L Tx is connected to Arduino D11
SoftwareSerial gpsSerial(2, 3); // GPS Rx is connected to Arduino D2 and GPS Tx is connected to Arduino D3

const int motor_Pin = 7;
const int led_Pin = 13;
const int buzzer_Pin = 5;
const int alcohol_Sensor = A1;
const int temp_Sensor = A0;
const int vibration_Sensor = 4;
const int trigPin = 9;
const int echoPin = 8;

int THRESHOLD = 300;
int temp;
float grad;
long duration;
int distance;
bool crashDetected = false;
bool vibrationDetected = false;

TinyGPSPlus gps;

void setup() {
  pinMode(motor_Pin, OUTPUT);
  pinMode(led_Pin, OUTPUT);
  pinMode(buzzer_Pin, OUTPUT);
  pinMode(alcohol_Sensor, INPUT);
  pinMode(temp_Sensor, INPUT);
  pinMode(vibration_Sensor, INPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  Serial.begin(9600);
  gpsSerial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextSize(1.25);
  display.setTextColor(WHITE);
  display.setCursor(10, 10);
  display.println("ACCIDENT DETECTION ");
  display.setCursor(10, 40);
  display.println("SYSTEM STARTED ");
  display.display();
  delay(3000);

  temp = analogRead(A0);
  grad = (temp * 0.322265625);

  display.clearDisplay();
  display.setCursor(0, 0);
  display.print("Temperature : ");
  display.print(grad);
  display.println(" °C");
  display.display();

  delay(2000);
}

void loop() {
  if (!crashDetected) {
    int alcohol_Value = analogRead(alcohol_Sensor);
    Serial.print("Alcohol Value: ");
    Serial.println(alcohol_Value);

    if (grad > 90) {
      Serial.println("Temperature is too high, motor will not turn on.");
    } else if (alcohol_Value > 245) {
      digitalWrite(led_Pin, HIGH);
      digitalWrite(buzzer_Pin, HIGH);
      digitalWrite(motor_Pin, LOW);
      delay(200);
      display.clearDisplay();
      display.setCursor(0, 0);
      display.println("Alcohol Detected");
      display.display();
      delay(500);
    } else {
      digitalWrite(led_Pin, LOW);
      digitalWrite(buzzer_Pin, LOW);
      digitalWrite(motor_Pin, HIGH);
      display.clearDisplay();
      display.setCursor(0, 0);
      display.println("No Alcohol Detected");
      display.display();
      delay(500);
    }

    int vibration_Value = digitalRead(vibration_Sensor);

    if (vibration_Value == HIGH) {
      vibrationDetected = true;
    }

    if (vibrationDetected) {
      digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance = duration * 0.034;
      if (distance < 10) {
        crashDetected = true;
        Serial.println("Crash detected due to vibration!");
        display.clearDisplay();
        display.setCursor(0, 0);
        display.println("Crash detected due to vibration!");
        display.display();

        // Stop the motor and alcohol detection
        digitalWrite(motor_Pin, LOW);
        digitalWrite(motor_Pin, LOW);

        // Print GPS coordinates
        gps_read();
      }
    }
  }
}

void gps_read()
{
  while (gpsSerial.available())
    if (gps.encode(gpsSerial.read()))
      displayInfo();

  if (millis() > 5000 && gps.charsProcessed() < 10)
  {
    Serial.println(F("No GPS detected: check wiring."));
    while(true);
  }
}

void displayInfo()
{
  if (gps.location.isUpdated())
  {
    Serial.print("Satellite Count:");
    Serial.println(gps.satellites.value());
    Serial.print("Latitude:");
    Serial.println(gps.location.lat(), 6);
    Serial.print("Longitude:");
    Serial.println(gps.location.lng(), 6);
    
    Serial.print("Altitude Feet:");
    Serial.println(gps.altitude.feet());
    Serial.println("");

    Serial.print("Date: ");
    if (gps.date.isValid())
    {
      Serial.print(gps.date.month());
      Serial.print("/");
      Serial.print(gps.date.day());
      Serial.print("/");
      Serial.println(gps.date.year());
    }
  
    Serial.print("Time: ");
    if (gps.time.isValid())
    {
      if (gps.time.hour() < 10) Serial.print(F("0"));
      Serial.print(gps.time.hour());
      Serial.print(":");
      if (gps.time.minute() < 10) Serial.print(F("0"));
      Serial.print(gps.time.minute());
      Serial.print(":");
      if (gps.time.second() < 10) Serial.print(F("0"));
      Serial.print(gps.time.second());
      Serial.print(".GMT");
    }
    Serial.println("");
    delay(2000);
  }
}

i already tested the entire project without the gps multiple time and its working fine with this code

#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <SoftwareSerial.h>

#define SCREEN_WIDTH 128  // OLED display width
#define SCREEN_HEIGHT 64  // OLED display height

// Initialize the OLED display using Wire library
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

SoftwareSerial mySerial(2, 3);  // SIM800L Rx is connected to Arduino D3 and SIM800L Tx is connected to Arduino D2

const int motor_Pin = 7;         // DC Motor
const int led_Pin = 13;          // LED
const int buzzer_Pin = 5;        // Buzzer
const int alcohol_Sensor = A1;   // MQ-3 alcohol sensor
const int temp_Sensor = A0;      // LM35 temperature sensor
const int vibration_Sensor = 4;  // Vibration sensor

// HC-SR04 pins
const int trigPin = 9;  // HC-SR04 Trigger pin
const int echoPin = 8;  // HC-SR04 Echo pin

int THRESHOLD = 300;  // Threshold value for alcohol detection
int temp;             // Variable to store the analog value
float grad;           // Variable for the temperature in degrees Celsius
long duration;
int distance;
bool crashDetected = false;
bool vibrationDetected = false;

void setup() {
  pinMode(motor_Pin, OUTPUT);
  pinMode(led_Pin, OUTPUT);
  pinMode(buzzer_Pin, OUTPUT);
  pinMode(alcohol_Sensor, INPUT);
  pinMode(temp_Sensor, INPUT);
  pinMode(vibration_Sensor, INPUT);
  pinMode(trigPin, OUTPUT);  // Sets the trigPin as an Output
  pinMode(echoPin, INPUT);   // Sets the echoPin as an Input

  Serial.begin(9600);                         // Start serial communication at 9600 baud
  mySerial.begin(9600);                       // Start SIM800L communication at 9600 baud
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // Initialize the OLED
  display.clearDisplay();
  display.setTextSize(1.25);
  display.setTextColor(WHITE);
  display.setCursor(10, 10);
  display.println("ACCIDENT DETECTION ");
  display.setCursor(10, 40);
  display.println("SYSTEM STARTED ");
  display.display();
  delay(3000);  // Wait for 3 seconds

  temp = analogRead(A0);        // Read the analog value of the sensor
  grad = (temp * 0.322265625);  // Convert to temperature (°C)

  display.clearDisplay();
  display.setCursor(0, 0);
  display.print("Temperature : ");
  display.print(grad);
  display.println(" °C");
  display.display();

  delay(2000);  // Wait for 2 seconds
}

void loop() {
  if (!crashDetected) {
    int alcohol_Value = analogRead(alcohol_Sensor);
    Serial.print("Alcohol Value: ");  // Print the value of alcohol to the serial monitor
    Serial.println(alcohol_Value);

    if (grad > 90) {
      Serial.println("Temperature is too high, motor will not turn on.");
    } else if (alcohol_Value > 245) {
      digitalWrite(led_Pin, HIGH);     // Turn on the LED
      digitalWrite(buzzer_Pin, HIGH);  // Turn on the buzzer
      digitalWrite(motor_Pin, LOW);    // Turn off the motor
      delay(200);
      display.clearDisplay();
      display.setCursor(0, 0);
      display.println("Alcohol Detected");
      display.display();
      delay(500);
    } else {
      digitalWrite(led_Pin, LOW);     // Turn off the LED
      digitalWrite(buzzer_Pin, LOW);  // Turn off the buzzer
      digitalWrite(motor_Pin, HIGH);  // Turn on the motor
      display.clearDisplay();
      display.setCursor(0, 0);
      display.println("No Alcohol Detected");
      display.display();
      delay(500);
    }

    // Read vibration sensor
    int vibration_Value = digitalRead(vibration_Sensor);

    if (vibration_Value == HIGH) {
      // Vibration detected
      vibrationDetected = true;
    }

    if (vibrationDetected) {
      // Check ultrasonic sensor for an object within 4 cm
      // Check ultrasonic sensor for an object within 4 cm
      digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance = duration * 0.034;
      if (distance < 10) {
        crashDetected = true;
        Serial.println("Crash detected due to vibration!");
        display.clearDisplay();
        display.setCursor(0, 0);
        display.println("Crash detected due to vibration!");
        display.display();

        // Stop the motor and alcohol detection
        digitalWrite(motor_Pin, LOW);
        digitalWrite(motor_Pin, LOW);
      }
    }
  }
}

so what do you think ? is the problem with board or the code is wrong ?

I'm reasonably sure that the Adafruit SSD1306 library uses dynamic memory allocation. That means that the allocated memory is not counted in the memory report and you might be running out of memory when adding the gps module.

I suggest that you add the code in https://docs.arduino.cc/learn/programming/memory-guide/#sram-memory-measurement and do a call to display_freeram() at the end of setup to see how much dynamic memory you actually have available.

5 seconds, 5000 ms, after system is powered up, You apply an emergency brake if no charcaters. That action looks bad, odd, to me.

Sure i will try this Thank you very much for your answer.

Did You write a test code to verify the function of the GPS? If not, do that!

does this interfere with the Oled displaying as the program starts?

yes i did i tried the gps alone and it worked.

No idea.

Good. Then the wiring ought to be good.

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