Arduino + ESP-01 keeps restarting

I have an Arduino Mega connected in ESP-01 Wifi Module, I've tested the wifi module multiple times with a sample code (sending data to firebase) and its working fine but once I combine it with a pre-made code, the ESP-01 keeps restarting (connecting to wifi then suddenly disconnects and connect again)

I also have this in my serial:
ets Jan 8 2013,rst cause:4, boot mode:(3,7) wdt reset load 0x4010f000, len 1384, room 16 tail 8 chksum 0x2d csum 0x2d vac02aff5 ~ld

this is my code:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include<Servo.h>
#include <ESP8266WiFi.h>
#include <FirebaseESP8266.h>

#define FIREBASE_HOST "smarttrash-d96fa-default-rtdb.firebaseio.com"
#define FIREBASE_AUTH "dOVh2HQBz4FGqsyFYzlUZwLHTlQxR3Ag1wNMMqdA"
#define WIFI_SSID "Pastorpili_2.4g"
#define WIFI_PASSWORD "Pastorpili_Wifi_2.4G"

FirebaseData firebaseData;

LiquidCrystal_I2C lcd(0x27, 20, 4);

Servo servo1;
Servo servo2;

const int servo1pin = 13;
const int servo2pin = 12;
const int relayPin = 11;
const int infrared = 10;
const int inductive = 9;
const int highPin = 8;
const int moderatePin = 7;
const int lowPin = 6;

bool flag = true;
int detectWait = 5;
const int dispenseTime = 5000; //in millisecs
int metalCounter = 0;
int plasticCounter = 0;
int metalLimit = 2;
int plasticLimit = 2;
bool metal = true;
bool plastic = true;

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

  ConnectToWifi();

  lcd.begin();
  lcd.backlight();
  lcd.clear();

  pinMode(infrared, INPUT);
  pinMode(inductive, INPUT);

  pinMode(highPin, INPUT);
  pinMode(moderatePin, INPUT);
  pinMode(lowPin, INPUT);

  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, HIGH);

  servo1.attach(servo1pin);
  servo2.attach(servo2pin);

  servo1.write(120);
  servo2.write(70);
  delay(50);
}

void loop() {

  int highVal = digitalRead(highPin);
  int moderateVal = digitalRead(moderatePin);
  int lowVal = digitalRead(lowPin);

  if (digitalRead(infrared) == 0) {

    if (flag) {

      if (waitFor(inductive)) {
        OpenMetalLid();
      }
      else {
        OpenPlasticLid();
      }

      flag = false;
    }

  }
  else {
    flag = true;

    lcd.setCursor(0, 0);
    lcd.print("Plastic: ");
    lcd.print(plasticCounter);
    lcd.print("|");
    lcd.print(plasticLimit);

    lcd.setCursor(0, 1);
    lcd.print("Metal: ");
    lcd.print(metalCounter);
    lcd.print("|");
    lcd.print(metalLimit);

    lcd.setCursor(0, 2);
    lcd.print("Scan: ");
    lcd.print("N/A");
    lcd.print("       ");
  }

  if (highVal == 0) {
    lcd.setCursor(0, 3);
    lcd.print("Alcohol: ");
    lcd.print("FULL");
    lcd.print("    ");
  }
  else if (moderateVal == 0) {
    lcd.setCursor(0, 3);
    lcd.print("Alcohol: ");
    lcd.print("MODERATE");
  }
  else if (lowVal == 0) {
    lcd.setCursor(0, 3);
    lcd.print("Alcohol: ");
    lcd.print("LOW");
    lcd.print("     ");
  }
  else {
    lcd.setCursor(0, 3);
    lcd.print("Alcohol: ");
    lcd.print("EMPTY");
    lcd.print("   ");

    metal = false;
    plastic = false;
  }

  if ( metalCounter >= metalLimit) {
    metal = false;
  }
  if ( plasticCounter >= plasticLimit ) {
    plastic = false;
  }

  yield();
}

bool waitFor(int pin) {

  for (int i = 0; i < detectWait; i++) {

    if (digitalRead(pin) == 1 ) {
      return true;
    }

    lcd.setCursor(0, 2);
    lcd.print("Scan: ");
    lcd.print("PROCESSING");
    delay(500);
    lcd.setCursor(0, 2);
    lcd.print("Scan: ");
    lcd.print("          ");
    delay(500);
  }

  return false;
}

void OpenPlasticLid() {

  if (plastic) {
    Serial.println("Plastic");

    lcd.setCursor(0, 2);
    lcd.print("Scan: ");
    lcd.print("PLASTIC");
    lcd.print("   ");

    servo1.write(0); //open
    servo2.write(20);
    delay(2000);
    servo1.write(120); //close
    servo2.write(70);
    delay(50);

    plasticCounter++;

    delay(1000);
    Dispense();
  }
  else {
    lcd.setCursor(0, 2);
    lcd.print("Scan: ");
    lcd.print("PLASTIC");
    lcd.print("   ");
    delay(2000);
  }
}

void OpenMetalLid() {

  if (metal) {
    Serial.println("Metal");

    lcd.setCursor(0, 2);
    lcd.print("Scan: ");
    lcd.print("METAL");
    lcd.print("     ");

    servo2.write(180); //open
    servo1.write(170);
    delay(2000);
    servo2.write(70); //close
    servo1.write(120);
    delay(50);

    metalCounter++;

    delay(1000);
    Dispense();
  }
  else {
    lcd.setCursor(0, 2);
    lcd.print("Scan: ");
    lcd.print("METAL");
    lcd.print("     ");
    delay(2000);
  }
}

void Dispense() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Dispensing...");

  digitalWrite(relayPin, LOW);
  delay(dispenseTime);
  digitalWrite(relayPin, HIGH);
  delay(50);
  lcd.clear();
}

void ConnectToWifi() {
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);                                     //try to connect with wifi
  Serial.print("Connecting to ");
  Serial.print(WIFI_SSID);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  Serial.println();
  Serial.print("Connected to ");
  Serial.println(WIFI_SSID);
  Serial.print("IP Address is : ");
  Serial.println(WiFi.localIP());                                            //print local IP address
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);   // connect to firebase

  Firebase.reconnectWiFi(true);
  delay(1000);

  if (Firebase.setInt(firebaseData, "/state", 0)) {    // On successful Write operation, function returns 1
    Serial.println("Connected to Firebase");
    delay(1000);
  }

  else {
    Serial.println(firebaseData.errorReason());
  }
}

And this is the code you have uploaded to the ESP-01 i guess ?
Aren't you supposed to upload a part to the Mega and a part to the ESP ?
If you google this part

rst cause:4, boot mode:(3,7)

you will find that there is an infinite loop somewhere triggering the wdt reset.
It might be somewhere in the LCD library ... Still i guess you have the LCD connected to the Mega, and not to the ESP.

Yes this is the full code

why do mean by this?

Well the Mega and the ESP-01 are 2 separate boards with a microprocessor each, and they should have a separate code uploaded to each. It was a rhetorical question i guess.
All the parts that deal with things that are connected to the Mega, like the LCD & servos belong on the Mega, with a Serial communication part that communicates with the ESP. The ESP takes care of the wifi connection and should communicate over Serial with the Mega.

your code of post #1has various println() statements which should guide you to where the WDT goes off
upload a printout from the serial monitor?

I'm sorry, I am a beginner in using ESP-01. May I ask, if they are seperate, then how can I send data that is in the arduino mega to firebase using esp-01?

are you using a Mega-WiFi_R3_ATmega2560_ESP8266? if so you set DIP switch so that the Mega Serial3 connects to the Serial of the ESP-01 and transfer data over the serial line

if not how have you connected the Mega to the ESP-01? serial, I2C, ???

That much i got, since you seemed to think it is a single board. They are actually to separate ones.

Like that. But you will have to create your own Serial transfer functions in which you send the data from the Mega and receive them on the other side (the ESP-01)
As the dipswitches are set now, the Serial of the ESP connects directly to USB, which enables you to view what is being sent by the ESP as well as allows you to upload a sketch to it.

Unfortunately i did not find a decent tutorial on how to use your board, but let's first verify what you actually have. Please show us the link of the board you've bought.

[/quote]
but let's first verify what you actually have. Please show us the link of the board you've bought.
[/quote]

https://shopee.ph/Mega-2560-R3-Motherboard-Compatible-with-Arduino-Mega2560-ATMEGA2560-CH340G-Makerlab-Electronics-i.18252381.190149371?sp_atk=1960e89b-585d-4c56-9a94-d58cb086e059&xptdk=1960e89b-585d-4c56-9a94-d58cb086e059

I have the R3 version and ESP-01 wifi module

the Mega uses 5V logic and the ESP-01 3.3V so you require a level change on the Mega serial Tx line, e.g.

So a loose ESP-01 module connected to the Mega ? how have you been connecting the ESP-01 so far ?
Are you sure that that is the correct board, or is the ESP-01 integrated in the Mega in some way ?

[/quote]
how have you been connecting the ESP-01 so far ?
[/quote]

My connections when flashing:
Arduino RX - RX ESP-01
Arduino TX - TX ESP-01
Arduino 3V - VCC ESP-01
Arduino 3V- EN ESP-01
Arduino GND - GND ESP-01
Arduino GND - GPIO0 ESP-01

I just removed the GPIO0 from the GND when done flashing the ESP-01

So you should add a voltage divider on the ESP's RX pin. Arduino RX -> 1K -> ESP RX -> 2K -> GND, to prevent 5v on the pin. Those pins are not 5v tolerant !! Your ESP will probably break at some point if you don't add the voltage divider.

And i suppose you have not uploaded anything to the Mega that uses Serial.
You can communicate between the ESP & the Mega by crossing the TX/RX lines (with a voltage divider on the ESP's RX pin) using any of the Mega's Serial ports. Normally i would suggest Serial 1, 2 or 3, since Serial0 is also used by the USB port.
Then you can setup a communication between the 2 boards.
Have a look at Serial Input Basics for reliable methods of reception (keep in mind that you have to send the data in such a way that you will receive it the way you want)
The whole thing that refers to the LCD & servos should be uploaded to the Mega, since i guess you have connected those parts to that board.

The ESP is a substantially more capable processor than the Mega 2560.

Why are you using the Mega 2560? You appear to have six I/O used; while the ESP-01 has only four available I/O, a WeMOS D1 Mini has at least six and programming would be much easier than attempting to use a hybrid of two processors. :sunglasses:

So true. I always figure that you have what you have and work with that.

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