Some code only works when USB serial port connected

I’m using Arduino Uno as my microcontroller, but I got a strange behavior. I need to run several function at the same time. One to get GPS and temperature value then send every value to user’s phone number when a request received by SIM800L GSM module, and another to display those value on I2C LCD. Everything works fine when my USB port connected to my computer. But when I tried to unplug the USB and using external power source from Li-Po battery I can’t get a response from SIM800L module whenever I send a request message.

When I tried to create a separate program (just received a request message and then sent a response by SIM800L) it works fine even though using external power supply (Li-Po battery) without USB port plugged in. Another strange behavior is when I was using both of Li-Po battery and USB port connected then unplugged the USB port it works fine again. But when I unplugged my battery and then plugged it again without USB port connected, SIM800L couldn’t send a response. What is wrong ?

This is my full code:

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
#include "DHT.h"

//sms  
char ReceivedSms;
short DHT_OK=-1, GPS_OK=-1;
String DataSms;
int _timeout;
String _buffer;
String number = "+62811XXXXXX";

//dht
#define DHTPIN 5
#define DHTTYPE DHT11

LiquidCrystal_I2C lcd(0x27,20,4);
SoftwareSerial sim800l(2,3);
DHT dht(DHTPIN, DHTTYPE);


// The TinyGPS++ object
TinyGPSPlus gps;


void setup()
{
  Serial.begin(9600);
  sim800l.begin(9600);
  dht.begin();
  lcd.init();                      // initialize the lcd 
  // Print a message to the LCD.
  lcd.backlight();
  _buffer.reserve(50);
  ReceiveMode();
}

void loop()
{
   displayInfo();
  
}

void displayInfo()
{
  String RSMS;

  while(sim800l.available() > 0) {
      ReceivedSms = sim800l.read();
      RSMS.concat(ReceivedSms);
      GPS_OK = RSMS.indexOf("All"); 
   }
   
  if (gps.location.isValid())
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Latitude  : ");
    lcd.println(gps.location.lat(), 6);
    lcd.setCursor(0,1);
    lcd.print("Longitude : ");
    lcd.println(gps.location.lng(), 6);
    delay(5000);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Suhu       : ");
    lcd.println(dht.readTemperature());
    lcd.setCursor(0,1);
    lcd.print("Kelembaban : ");
    lcd.println(dht.readHumidity());
    delay(2000);
    
    if(GPS_OK!=-1)
        {
        String link = "http://www.google.com/maps/place/" + String(gps.location.lat(),6) + "," + String(gps.location.lng(),6);
        DataSms = "ALL\nSuhu = "+String(dht.readTemperature(),1)+" C"+" \nKelembaban ="+String(dht.readHumidity(),1)+" %"+" \nMaps = "+link;
        SendMessage();
        ReceiveMode();
        GPS_OK = -1;
        } 
  }
  else
  {
  
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("GPS not found");
    delay(2000);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Suhu      :");
    lcd.println(dht.readTemperature());
    lcd.setCursor(0,1);
    lcd.print("Kelembaban:");
    lcd.println(dht.readHumidity());
    delay(2000);
    
    if(GPS_OK!=-1)
       {
        DataSms = "ALL\nSuhu = "+String(dht.readTemperature(),1)+" C"+" \nKelembaban ="+String(dht.readHumidity(),1)+" %"+" \nMaps = Lokasi Tidak Ditemukan";
        SendMessage();        
        ReceiveMode();
        GPS_OK = -1;
       } 
  }

}

String _readSerial() {
  _timeout = 0;
  while  (!sim800l.available() && _timeout < 12000  )
  {
    delay(13);
    _timeout++;
  }
  if (sim800l.available()) {
    return sim800l.readString();
  }
}

void ReceiveMode() {
  sim800l.println("AT");
  delay(100);
  sim800l.println("AT+CMGF=1");
  delay(100);
  sim800l.println("AT+CNMI=2,2,0,0,0");
  delay(100);
}

void SendMessage()
{
  //Serial.println ("Sending Message");
  sim800l.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);
  //Serial.println ("Set SMS Number");
  sim800l.println("AT+CMGS=\"" + number + "\"\r"); //Mobile phone number to send message
  delay(1000);
  String SMS = "Hello from 800L...";
  sim800l.println(DataSms);
  delay(100);
  sim800l.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
  _buffer = _readSerial();
}

Just for your information I’m using Li-Po battery 7.4V 2200mAh

What made you think that your question has anything to do with the Website and Forum section which is clearly stated to be for “Improvements for the web system, applications to moderator, spam, etc.”? I have suggested to the Moderator to move it to the Programming section.

This sort of carelessness makes unnecessary work for the Moderators.

…R

omg I just realized it. thanks

It sounds like more of a power problem than a software problem. How is the battery connected to the Arduino and SIM800l (and any other peripherals)?

johnwasser: It sounds like more of a power problem than a software problem. How is the battery connected to the Arduino and SIM800l (and any other peripherals)?

I'm connecting my battery to a 5V step-down and then I'm supplying 5V Arduino pin from the output of that step-down. I'm not directly connecting step-down output to the 5V and GND pin of Arduino but put it first on a bread-board because I want to supply another sensor pin too. What is the proper way to handle this wiring?

A schematic showing how you have everything hooked up as well would be useful in diagnosing the issue.

Have you tried pressing the reset button after connecting? I've had the same issue with knock-off arduino uno's where they will wait for serial over USB before running...

Which brings me to another question: have you tried removing the Serial.begin() and print commands so that it isn't trying to interface with the computer over the serial port? Have also had issues with this and knock-off boards trying to connect but can't and then hang.

It's here right under setup:

  Serial.begin(9600);

Now some concerns:

  • LiPo is not something to mess around with if you don't have protection for the battery. You could cause a fire and seriously get injured or injure someone if it over discharges.
  • A lot of buck-converter style boards have an adjustment potentiometer to change the voltage that is supplied. They are highly efficient but if this board has an adjustment, it could be off spec.
  • Check to make sure that the converter board you are using can supply everything with enough current.

Supernovali:
A schematic showing how you have everything hooked up as well would be useful in diagnosing the issue.

Have you tried pressing the reset button after connecting? I’ve had the same issue with knock-off arduino uno’s where they will wait for serial over USB before running…

Which brings me to another question: have you tried removing the Serial.begin() and print commands so that it isn’t trying to interface with the computer over the serial port? Have also had issues with this and knock-off boards trying to connect but can’t and then hang.

It’s here right under setup:

  Serial.begin(9600);

Now some concerns:

  • LiPo is not something to mess around with if you don’t have protection for the battery. You could cause a fire and seriously get injured or injure someone if it over discharges.
  • A lot of buck-converter style boards have an adjustment potentiometer to change the voltage that is supplied. They are highly efficient but if this board has an adjustment, it could be off spec.
  • Check to make sure that the converter board you are using can supply everything with enough current.

This is a simple schematic wiring of how do I power up my Arduino Uno and another part. But in this schematic I don’t put my LCD-I2C yet

Welcome to the forums. You will find a lot of regulars here who just want to help. In the future, please don't call a Fritzing Lego picture a "schematic". As your projects get more complex, the Fritzing pictures are less useful to useless.

You didn't say which bucking voltage converter that you are using, but I'll guess that it doesn't have the capacity your Arduino and peripherals need.

If I am correct, plugging a 9V battery into the barrel power connector should solve the problem. And unplug the 5V from the Uno to the breadboard since the Arduino would be powered separately.

In general the Arduino boards can only supply enough current from the 5V pin reliably for a few LEDs, or one display, or one micro servo.

I have solved my problem. What I did was press the reset button pin on Arduino Uno after all of those component got a power / connecting

Glad you were able to figure it out! Also, in your final version, make sure to remove all Serial.xxx commands. That will help make sure you don’t have to reset it every time there is a power cycle. (USUALLY, again, watch out for the knock-off boards).

Now please, for your safety, if you don’t already have a protection circuit, put one together. :slight_smile:

And, yes, if you can draw some schematics or use a schematic builder, that would be helpful in the future. I personally still like drawing my schematics. There is this awesome product I think all proto-typers should use called a Rocket Notebook which allows you to draw your schematics and then use your phone to automatically upload it to your drive service. Then you use water and a towelette to clean the page and reuse it.

I’ve attached an image so you can see what one of my schematics look like :slight_smile:

P-channel mosfet driver, with uni-directional 3.3v to 5volt output.pdf (294 KB)

What exactly is it about the knockoff Arduinos that makes pressing the reset button necessary? It would seem to be a bootloader issue that could be fixed by burning the right bootloader. Is that right?

Not quite sure. But yeah, I've tried a couple boot loaders that help resolve this issue. Wish I knew but I dont feel like reverse engineering what's going on haha. I do know the Serial.begin() on the knock-offs, depending on who it is, is a surefire way to get it to hang during a power cycle. Without it, it just comes down to the hardware design.

Rule of thumb tho is if you are done debugging, remove serial commands unless you can test and reset the arduino from software.

Supernovali:
Not quite sure. But yeah, I’ve tried a couple boot loaders that help resolve this issue. Wish I knew but I dont feel like reverse engineering what’s going on haha. I do know the Serial.begin() on the knock-offs, depending on who it is, is a surefire way to get it to hang during a power cycle. Without it, it just comes down to the hardware design.

Rule of thumb tho is if you are done debugging, remove serial commands unless you can test and reset the arduino from software.

Thanks for your suggestion. I can remove all of those Serial.xxx except Serial.begin because I still need that for my GPS :frowning: :frowning: You might now that we can’t use 2 software serial in one program. Actually I have to replace my Arduino Uno with Arduino mega or teensy instead because they have more than 1 serial pin but I can’t do this because of budget limitation :confused: :frowning:

dinihuygens: Thanks for your suggestion. I can remove all of those Serial.xxx except Serial.begin because I still need that for my GPS :( :( You might now that we can't use 2 software serial in one program. Actually I have to replace my Arduino Uno with Arduino mega or teensy instead because they have more than 1 serial pin but I can't do this because of budget limitation :confused: :(

the rule of this project is using minimal budget :confused:

dinihuygens: Thanks for your suggestion. I can remove all of those Serial.xxx except Serial.begin because I still need that for my GPS :( :( You might now that we can't use 2 software serial in one program. Actually I have to replace my Arduino Uno with Arduino mega or teensy instead because they have more than 1 serial pin but I can't do this because of budget limitation :confused: :(

I've never tried it, but this link suggests you can use two software serial ports on the Uno if you don't use them at the same time:

https://www.arduino.cc/en/Tutorial/LibraryExamples/TwoPortReceive/

dinihuygens: I have solved my problem. What I did was press the reset button pin on Arduino Uno after all of those component got a power / connecting

That still tells me that you have a power issue. You should not have to press the reset button on the uno.

Got $9 for a Mega2560 with CH340 for USB? https://www.amazon.com/Development-ATmega2560-16AU-microcontroller-Intelligent-smartphones/dp/B07SJ3K4GH/ref=sr_1_122?crid=2OFLIL8OU3S2S&dchild=1&keywords=mega+2560+pro+mini&qid=1607013929&sprefix=mega2560+pro%2Caps%2C168&sr=8-122

Serial is only an object in the Arduino IDE. You can make multiple serial objects. If you are using the same serial for your sensors as you are for the usb port. You can get unpredictable results if one hasnt cleared the buffer and another one starts reading/writing. In order to prevent this, do a Serial.begin() for the serial monitor, then do something like GPSserial.begin() for your gps serial so the buffers dont get scrmabled.

ShermanP: I've never tried it, but this link suggests you can use two software serial ports on the Uno if you don't use them at the same time:

https://www.arduino.cc/en/Tutorial/LibraryExamples/TwoPortReceive/

The problem is I use both of them at the same time..

no one recommend me STM32F103C8T6 blue pill ? :o