Buffer doesn't empty using SIM800L with ESP32

Hi, i'm using 2 chips : ESP-32S from NodeMCU and sim800l from SimCom .
Sim800l connected to LIPO battery with 3.7v, esp-32 via usb to computer.

#include <HardwareSerial.h>
HardwareSerial SerialPort2(1);

void setup() {
  Serial.begin(9600);

  SerialPort2.begin(9600, SERIAL_8N1, 16, 17);

  Serial.println("Initializing...");
  Serial.println("\nSIM800 EVB to ESP32 Serial port 2 test");

  SerialPort2.write("AT\r\n");
}

void loop() {
  readFromSim800l();
}

void readFromSim800l() {
  delay(500);
  while (SerialPort2.available() > 0) {
    SerialPort2.read();
    Serial.print("Bytes available ");
    Serial.println(SerialPort2.available());
  }
}

My problem is that serial buffer on sim800l almost never gets empty. I've tried a bunch of code snippets, that are covering sim800l and esp32 communication. In those tutorials serial buffer is being read in a while loop. And it provides nice output. For AT command it gives nice OK. In my case it looks like OKOKOKOKOKOKOK... and it never stops.

If to execute at the code above executes the output looks something like this:

Bytes available 256
Bytes available 255
Bytes available 254
Bytes available 253
...
Bytes available 230
...
Bytes available 255
Bytes available 254
Bytes available 253

Other commands involving communication with sim800l seem to work. But i'm having a huge trouble executing them one after another, to debug and set everything correctly.
I would appreciate any clues, what am i doing wrong.

Also, either i'm malfunctioning or esp32 chips i've bought, but i can't use different Hardware Serial Port, and different pins. In both cases it leads to some broken output.
It's the only setup that worked more or less fine.

Can you try this code

const byte RXD2 = 16;
const byte TXD2 = 17;

void setup() {
  Serial.begin(115200);
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2); 
  Serial.println("Ready");
}

void loop() {
  if (Serial2.available()) Serial.write(Serial2.read());
  if (Serial.available()) Serial2.write(Serial.read());
}

Open the serial monitor, set it to 115200 bauds and sending CR+LF as end of line

Type
ATreturn

What do you see ?

(Code typed here from an iPhone so mind typos)

Make sure the GND from the SIM800 module is connected to the GND of the ESP (and that Tx goes to Rx and Rx to Tx)

1 Like

Thank you very much for your response.

Connecting SIM800 to GND of ESP helped. AT command returned OK, without any artifacts.
Before i had GND and VCC connected to LIPO battery, and only serial pins were going into ESP chip.
I'm sorry, but maybe you can please explain, why connection to the GND helped?
I have ideas, that chip internals are made this way, and probably it's sort of specification. However, i don't understand what does it have to do with the issue i wrote above? Bc, some of the commands as listing available operators, or checking for the network status were giving right responses, but in a weird way.

Anyway, thank you very much, you saved me.

All electic systems need a common point for reference of voltage levels. Ground is that "zero" reference point, so ground must be shared and common to all systems.

A device not sharing a common ground will not have the same reference "zero" voltage as the other devices and will have different/bad voltage levels.

These bad voltage levels will cause errors.

1 Like

UART signals (the 0s and 1s) are transmitted as voltage changes (LOW and HIGH) relative to each system’s ground. If the grounds are not the same, each system will interpret the voltage levels differently, resulting in incorrect or garbled data.

for example If you don't do that a 3.3V on one side could look like 0V on the other side if it's GND is off by 3.3V and thus won't register as a HIGH. Worse could happen also as negative voltage could be seen which could damage the chip.

(extreme) example where the GND are off by 3.3V


The ESP's HIGH is seen as a LOW on the other side and the ESP's LOW is seen as -3.3V on the other side which would damage the SIM800
The SIM800 LOW would be seen as a HIGH on the ESP32 and the SIM800 HIGH would be seen as 6.6V on the ESP's side which would damage the ESP32

So as a general rule, to enable digital communication between two systems, both need to share a common reference voltage, which is achieved by connecting their ground (GND) lines. Doing so ensures that both devices measure voltages from the same baseline or reference, allowing them to correctly interpret the high and low signals in the communication.

PS/ remember a voltage is a difference of potential.

Voltage is often compared to a waterfall because both represent the concept of potential energy driving a flow. Imagine a waterfall where water is held at a height, and gravity creates a potential energy difference between the top and bottom of the fall. This height difference causes water to flow downward.

Similarly, in an electrical circuit, voltage is the difference in electric potential energy between two points, often measured between a positive and a negative terminal. This "difference in potential" acts like the height in the waterfall analogy: it creates the driving force that makes electric charge flow through a circuit, much like gravity makes water flow down a waterfall.

Just as the height of a waterfall affects the strength and speed of the water flowing down, higher voltage in a circuit means a stronger push that can drive more current, assuming the resistance remains the same. This analogy helps explain why voltage is essential to understanding how energy moves through a circuit and powers electrical devices.

1 Like

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