I dont know if my arduino uno is hitting code limit or power limit

When i comment out some codes (either LCD, or ESP-01 or all sensors), then it works fine. separately every single functionality works fine. When i put all of them together, the code gets stuck in blynk.begin() function.

#define BLYNK_TEMPLATE_ID "TMPL6q522FnRw"
#define BLYNK_TEMPLATE_NAME "Faw"
#define BLYNK_AUTH_TOKEN "UZMxeaRLAumm1VVfdGGhtNiTmGOz8OJe"
#define BLYNK_PRINT Serial
#define ESP8266_BAUD 38400

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>

#define RX_PIN 2
#define TX_PIN 3
#define TEMP_PIN 4
#define BUTTON_PIN 5
#define SERVO_PIN 6
#define TRIG_PIN 9
#define ECHO_PIN 10
#define PH_PIN A0
#define GREEN_LED_PIN 11
#define RED_LED_PIN 13
#define sec 1000

char ssid[] = "Look Ma, No Wires";
//char ssid[] = "Fawzan";
char pass[] = "APD2F2206";
//char pass[] = "1223334444";

Servo myservo;
OneWire oneWire(TEMP_PIN);
DallasTemperature sensors(&oneWire);
SoftwareSerial EspSerial(RX_PIN, TX_PIN);
ESP8266 wifi(&EspSerial);
LiquidCrystal_I2C lcd(0x27, 20, 4);

bool blynkManualFeed, blynkAutoFeed;
int blynkRotation, blynkFeedingFreq;
String lastTimeFed_Str;
unsigned long lastTimeFed = 0;
unsigned long previousMillisFood = 0;

int temp_L, temp_H;
unsigned long lastSensorUpdated = 0;
unsigned long previousMillisSensor = 0;

BLYNK_WRITE(V0) 
{
  int switch_value = param.asInt(); 
  if (switch_value == 1) 
  {
    blynkManualFeed = true;
  }
  else 
  {
    blynkManualFeed = false; 
  }
}

BLYNK_WRITE(V1) 
{
  int switch_value = param.asInt(); 
  if (switch_value == 1) 
  {
    blynkAutoFeed = true; 
    lcd.setCursor(0, 2);
    lcd.print("Auto Feed: ON");
  }
  else 
  {
    blynkAutoFeed = false; 
    lcd.setCursor(0, 2);
    lcd.print("Auto Feed: OFF");
  }
}

BLYNK_WRITE(V2) 
{
  blynkFeedingFreq = param.asInt();
}

BLYNK_WRITE(V3)
{
  blynkRotation = param.asInt();
}

BLYNK_WRITE(V10)
{
  temp_L = param.asInt();
}

BLYNK_WRITE(V12)
{
  temp_H = param.asInt();
}

void setup()
{
  lcd.init();
  lcd.backlight();

  lcd.setCursor(0, 0);            // move cursor the first row
  lcd.print("Starting....        ");
  Serial.begin(115200);
  Serial.println("Setup");
  Serial.println("0");
  myservo.attach(SERVO_PIN);
  myservo.write(0);
  pinMode(GREEN_LED_PIN, OUTPUT);
  pinMode(RED_LED_PIN, OUTPUT);

  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  
  digitalWrite(GREEN_LED_PIN, HIGH);
  digitalWrite(RED_LED_PIN, LOW);
  
  sensors.begin();
  EspSerial.begin(ESP8266_BAUD);
  Serial.println("1");
  Blynk.begin(BLYNK_AUTH_TOKEN, wifi, ssid, pass, "blynk.cloud", 80);
  Serial.println("2");
  temp_L = 25;
  Blynk.virtualWrite(V10, temp_L);
  temp_H = 35;
  Blynk.virtualWrite(V12, temp_H);
  blynkAutoFeed = false;
  Blynk.virtualWrite(V0, 0);
  blynkAutoFeed = false;
  Blynk.virtualWrite(V1, 0);
  blynkFeedingFreq = 4;
  Blynk.virtualWrite(V2, blynkFeedingFreq);
  blynkRotation = 3;
  Blynk.virtualWrite(V2, blynkRotation);

  Blynk.virtualWrite(V8, 1);
  Blynk.virtualWrite(V9, 0);
  Blynk.virtualWrite(V11, "Starting....");
  Blynk.virtualWrite(V15, "");
  
  
}

void loop()
{
  lcd.setCursor(0, 0);
  lcd.print("Started            ");
  Blynk.run();

  lastTimeFed = millis() - previousMillisFood;
  unsigned long hours = lastTimeFed / 3600000; // 1 hour = 3600000 milliseconds
  unsigned long minutes = (lastTimeFed % 3600000) / 60000; // 1 minute = 60000 milliseconds

  

  if (digitalRead(BUTTON_PIN) == 1 || blynkManualFeed == true || (blynkAutoFeed == true && hours >= blynkFeedingFreq)) // if the button is pressed or the switch is ON
  {
    
    rotateServo(blynkRotation);
    if (blynkManualFeed == true)
    {
      Blynk.virtualWrite(V0, 0);
      blynkManualFeed = false;
    }
    previousMillisFood = millis();
  }
  
  lastTimeFed_Str = String(hours) + "h" + String(minutes) + "m";
  if (previousMillisFood != 0)
  {
    Blynk.virtualWrite(V4, lastTimeFed_Str);
      
  }
  lcd.setCursor(0, 1);            // move cursor the first row
      lcd.print("Last Fed: " + String(lastTimeFed_Str) + " ago");
  
  lastSensorUpdated = millis() - previousMillisSensor;
  if (lastSensorUpdated >= 1 * sec)
  {
    sensors.requestTemperatures();
    float temp = sensors.getTempCByIndex(0);
    Serial.println(temp);
    Blynk.virtualWrite(V5, temp);
    int d = getFoodAmount();
    Blynk.virtualWrite(V7, d);
    Serial.println("D %: " + String(d));
    if (temp < temp_L || temp > temp_H)
    {
      Blynk.virtualWrite(V15, "Critical Temp");
      digitalWrite(GREEN_LED_PIN, LOW);
      digitalWrite(RED_LED_PIN, HIGH);
      Blynk.virtualWrite(V8, 0);
      Blynk.virtualWrite(V9, 1);
      Blynk.virtualWrite(V11, "Warning!");
    }
    else
    {
      Blynk.virtualWrite(V11, "All Good!");
      Blynk.virtualWrite(V15, "Normal Temp");
      digitalWrite(GREEN_LED_PIN, HIGH);
      digitalWrite(RED_LED_PIN, LOW);
      Blynk.virtualWrite(V8, 1);
      Blynk.virtualWrite(V9, 0);
    }

    lcd.setCursor(0, 0);            // move cursor the first row
    lcd.print("Temp:" + String(temp) + "C  pH:14.0");
    previousMillisSensor = millis();
    
  }

}

int getFoodAmount()
{
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);

  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);

  digitalWrite(TRIG_PIN, LOW);
  float  duration = pulseIn(ECHO_PIN, HIGH);
  
  float distance = duration * 0.034 / 2;
  Serial.println("Distance: " + String(distance));
  int distancePercentage = round((12 - distance) * 100 / 12);
  return distancePercentage;
}

void rotateServo(int n)
{
  
  for (int i = 0; i < n; i++)
  {

    if (myservo.read() == 0)
    {
      myservo.write(180);
    }
    else if (myservo.read() == 180)
    {
      myservo.write(0);
    }
    delay(1 * sec);
    Serial.println(i);

  }
}
Sketch uses 22900 bytes (70%) of program storage space. Maximum is 32256 bytes.
Global variables use 1243 bytes (60%) of dynamic memory, leaving 805 bytes for local variables. Maximum is 2048 bytes.

I moved your topic to an appropriate forum category @fawzanalim.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Welcome to the forum

You started a topic in the Uncategorised category of the forum whose description is

:warning: DO NOT CREATE TOPICS IN THIS CATEGORY :warning:

Your topic has been moved to the Programming category

What numbers did the build report give you? (at the bottom of the IDE window)

After the program compiles, you can cut and paste that report into (code tags), then we’ll know as much as you do.

You could start by changing all you ā€œcharacterā€ literals into F(ā€œcharacterā€) literals.

Also as your skill level improves, you might look into eliminating ā€˜Strings’ if possible.. it’s preferable to use char[arrays] to save a lot of memory issues.

1 Like

could be either code limit and/or power limit
LCD libraries often create a buffer in SRAM which can cause problems on microcontrolers with small SRAM such as the UNO (2K SRAM)
if you have a Mega try that (8K SRAM)
it could be simpler in the long run to move to a Microcontroller with onboard WiFi such as the ESP32

I have updated it at the bottom of my code.

how do i isolate the problem and figure out a workaround? i dont have any other boards.

I have changed all the "character" to F("character"). Still same issue, the code gets stuck in Blynk.begin()

#define BLYNK_TEMPLATE_ID "TMPL6q522FnRw"
#define BLYNK_TEMPLATE_NAME "Faw"
#define BLYNK_AUTH_TOKEN "UZMxeaRLAumm1VVfdGGhtNiTmGOz8OJe"
#define BLYNK_PRINT Serial
#define ESP8266_BAUD 38400

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>

#define RX_PIN 2
#define TX_PIN 3
#define TEMP_PIN 4
#define BUTTON_PIN 5
#define SERVO_PIN 6
#define TRIG_PIN 9
#define ECHO_PIN 10
#define PH_PIN A0
#define GREEN_LED_PIN 11
#define RED_LED_PIN 13
#define sec 1000

char ssid[] = "Look Ma, No Wires";
//char ssid[] = "Fawzan";
char pass[] = "APD2F2206";
//char pass[] = "1223334444";

Servo myservo;
OneWire oneWire(TEMP_PIN);
DallasTemperature sensors(&oneWire);
SoftwareSerial EspSerial(RX_PIN, TX_PIN);
ESP8266 wifi(&EspSerial);
LiquidCrystal_I2C lcd(0x27, 20, 4);

bool blynkManualFeed, blynkAutoFeed;
int blynkRotation, blynkFeedingFreq;
char lastTimeFed_Str[10];
unsigned long lastTimeFed = 0;
unsigned long previousMillisFood = 0;

int temp_L, temp_H;
unsigned long lastSensorUpdated = 0;
unsigned long previousMillisSensor = 0;

BLYNK_WRITE(V0) 
{
  int switch_value = param.asInt(); 
  if (switch_value == 1) 
  {
    blynkManualFeed = true;
  }
  else 
  {
    blynkManualFeed = false; 
  }
}

BLYNK_WRITE(V1) 
{
  int switch_value = param.asInt(); 
  if (switch_value == 1) 
  {
    blynkAutoFeed = true; 
    lcd.setCursor(0, 2);
    lcd.print(F("Auto Feed: ON"));
  }
  else 
  {
    blynkAutoFeed = false; 
    lcd.setCursor(0, 2);
    lcd.print(F("Auto Feed: OFF"));
  }
}

BLYNK_WRITE(V2) 
{
  blynkFeedingFreq = param.asInt();
}

BLYNK_WRITE(V3)
{
  blynkRotation = param.asInt();
}

BLYNK_WRITE(V10)
{
  temp_L = param.asInt();
}

BLYNK_WRITE(V12)
{
  temp_H = param.asInt();
}

void setup()
{
  lcd.init();
  lcd.backlight();

  lcd.setCursor(0, 0);            // move cursor the first row
  lcd.print(F("Starting....        "));
  Serial.begin(115200);
//  Serial.println("Setup");
//  Serial.println("0");
  myservo.attach(SERVO_PIN);
  myservo.write(0);
  pinMode(GREEN_LED_PIN, OUTPUT);
  pinMode(RED_LED_PIN, OUTPUT);

  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  
  digitalWrite(GREEN_LED_PIN, HIGH);
  digitalWrite(RED_LED_PIN, LOW);
  
  sensors.begin();
  EspSerial.begin(ESP8266_BAUD);
//  Serial.println("1");
  Blynk.begin(BLYNK_AUTH_TOKEN, wifi, ssid, pass, "blynk.cloud", 80);
  Serial.println(F("2"));
  temp_L = 25;
  Blynk.virtualWrite(V10, temp_L);
  temp_H = 35;
  Blynk.virtualWrite(V12, temp_H);
  blynkAutoFeed = false;
  Blynk.virtualWrite(V0, 0);
  blynkAutoFeed = false;
  Blynk.virtualWrite(V1, 0);
  blynkFeedingFreq = 4;
  Blynk.virtualWrite(V2, blynkFeedingFreq);
  blynkRotation = 3;
  Blynk.virtualWrite(V2, blynkRotation);

  Blynk.virtualWrite(V8, 1);
  Blynk.virtualWrite(V9, 0);
  Blynk.virtualWrite(V11, F("Starting...."));
  Blynk.virtualWrite(V15, "");
  
  
}

void loop()
{
  lcd.setCursor(0, 0);
  lcd.print(F("Started            "));
  Blynk.run();

  lastTimeFed = millis() - previousMillisFood;
  unsigned long hours = lastTimeFed / 3600000; // 1 hour = 3600000 milliseconds
  unsigned long minutes = (lastTimeFed % 3600000) / 60000; // 1 minute = 60000 milliseconds

  

  if (digitalRead(BUTTON_PIN) == 1 || blynkManualFeed == true || (blynkAutoFeed == true && hours >= blynkFeedingFreq)) // if the button is pressed or the switch is ON
  {
    
    rotateServo(blynkRotation);
    if (blynkManualFeed == true)
    {
      Blynk.virtualWrite(V0, 0);
      blynkManualFeed = false;
    }
    previousMillisFood = millis();
  }

  snprintf(lastTimeFed_Str, "%dh%dm", hours, minutes);
//  lastTimeFed_Str = String(hours) + "h" + String(minutes) + "m";
  if (previousMillisFood != 0)
  {
    Blynk.virtualWrite(V4, lastTimeFed_Str);
      
  }
  lcd.setCursor(0, 1);            // move cursor the first row
  lcd.print(F("Last Fed: "));
  lcd.print(lastTimeFed_Str);
  lcd.print(F(" ago"));
  
  lastSensorUpdated = millis() - previousMillisSensor;
  if (lastSensorUpdated >= 1 * sec)
  {
    sensors.requestTemperatures();
    float temp = sensors.getTempCByIndex(0);
    Serial.println(temp);
    Blynk.virtualWrite(V5, temp);
    int d = getFoodAmount();
    Blynk.virtualWrite(V7, d);

    if (temp < temp_L || temp > temp_H)
    {
      Blynk.virtualWrite(V15, F("Critical Temp"));
      digitalWrite(GREEN_LED_PIN, LOW);
      digitalWrite(RED_LED_PIN, HIGH);
      Blynk.virtualWrite(V8, 0);
      Blynk.virtualWrite(V9, 1);
      Blynk.virtualWrite(V11, F("Warning!"));
    }
    else
    {
      Blynk.virtualWrite(V11, F("All Good!"));
      Blynk.virtualWrite(V15, F("Normal Temp"));
      digitalWrite(GREEN_LED_PIN, HIGH);
      digitalWrite(RED_LED_PIN, LOW);
      Blynk.virtualWrite(V8, 1);
      Blynk.virtualWrite(V9, 0);
    }

    lcd.setCursor(0, 0);            // move cursor the first row
    lcd.print(F("Temp:"));
    lcd.print(temp);
    lcd.print(F("C  pH:14.0"));

    previousMillisSensor = millis();
    
  }

}

int getFoodAmount()
{
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);

  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);

  digitalWrite(TRIG_PIN, LOW);
  float  duration = pulseIn(ECHO_PIN, HIGH);
  
  float distance = duration * 0.034 / 2;
//  Serial.println("Distance: " + String(distance));
  int distancePercentage = round((12 - distance) * 100 / 12);
  return distancePercentage;
}

void rotateServo(int n)
{
  
  for (int i = 0; i < n; i++)
  {

    if (myservo.read() == 0)
    {
      myservo.write(180);
    }
    else if (myservo.read() == 180)
    {
      myservo.write(0);
    }
    delay(1 * sec);
    Serial.println(i);

  }
}

What processor is this code running on ?
I see you’re talking to an ESP.

SoftwareSerial is pretty fragile the higher your comms speed gets.
It’s bit-bashed, so depends on cou cycles and no holdups in the background code….

could be the project as configured is too large for a UNO
could you remove the LCD and just use the serial monitor to display information?

I am running Arduino UNO. I am using ESP01 to connect to blynk IoT app.

without lcd it works fine. but i wanted to use the lcd. does this mean i cant use both lcd and esp01 at the same time? i can use both of them separately.

i dont mind avoiding softwareserial. any alternative you could suggest?

Try bringing the softwareserial and ESP both down to a lower bitrate for comms.

I’ve never tried exactly what you’re doing, but in my life, I’d lose the UNO, and run everything directly on the ESP.

it is possible you are running out of SRAM - enabling/disabling the devices seperatly may help
a quick fix is get a Mega increasing SRAM from 2K to 8K

in the longer run may be to move the whole project to a ESP32 as @lastchancename suggested

I switched to an arduino Mega. I connected RX and TX of ESP01 to TX and RX of serial 1 in arduino mega. Baud Rate of my ESP01 is now 115200. Somebody said to use the baid rate 384000, tried it. didnt work. Now, using 115200.

#define BLYNK_TEMPLATE_ID "******"
#define BLYNK_TEMPLATE_NAME "Faw"
#define BLYNK_AUTH_TOKEN "*********************"

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Look Ma, No Wires";
char pass[] = "APD2F2206";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);

void setup()
{
  // Debug console
  Serial.begin(9600);
  
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

//  Blynk.begin(BLYNK_AUTH_TOKEN, wifi, ssid, pass);
  // You can also specify server:
  Blynk.begin(BLYNK_AUTH_TOKEN, wifi, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(BLYNK_AUTH_TOKEN, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);
}

void loop()
{
  Blynk.run();
}

Output


19:44:02.051 ->     ___  __          __
19:44:02.051 ->    / _ )/ /_ _____  / /__
19:44:02.051 ->   / _  / / // / _ \/  '_/
19:44:02.051 ->  /____/_/\_, /_//_/_/\_\
19:44:02.051 ->         /___/ v1.3.2 on Arduino Mega
19:44:02.051 -> 
19:44:02.051 ->  #StandWithUkraine    https://bit.ly/swua
19:44:02.051 -> 
19:44:02.051 -> 
19:44:02.559 -> [518] Connecting to Look Ma, No Wires
19:44:03.588 -> [1529] ESP is not responding

it would be good idea to make your Serial baudrate at least as fast as Serial1, i.e. 115200
otherwise you may receive data from Serial1 too fast for Serial to display and overrun the buffers

Edit: I assume you used a potential divider on the Mega Tx (5V logic) pin to ESP-01 Rx (3.3V logic) pin, e.g.

I tried it, did not work. I know my ESP01 is working fine, because i used it on arduino uno, with baud rate 38400. Works just fine.

I tried following this thread: Troubles connecting ESP8266-01 with Arduino MEGA 2560 - #20 by jijaji

He seems to fix the problem by just reflashing the esp01. I tried doing it, but the only 1.7.x version i could find was this: ESP8266 SDK released | å®‰äæ”åÆē§‘ęŠ€. Boantong AT firmware. Could you help me find a proper one please? i dont know if this is the right one

try this simple program in the ESP-01 it transmits a pattern 0101010to Serial changing every second

// ESP8266 - transmit 0101010 in timing loop - read new loop time from keyboard
//  Serial 0101010 output will be sent to mega to blink LED

void setup() {
  Serial.begin(9600);    // <<<<<<< note 9600baud
}

void loop() {
  static long timer=millis();
  //  send 010101 etc every time interval
  static int onoff=0, timeonoff=1000;
  if((millis()-timer)>timeonoff) {
    timer=millis();
    Serial.print(onoff);
    onoff=!onoff;                   // invert value 0 or 1
  }
  // if text available read next time value
  if(Serial.available()==0) return; // wait for serial input
  timeonoff=Serial.parseInt();      // read time value
  //Serial.println(timeonoff);
  delay(10);
  while(Serial.available()) Serial.read(); // flush input
}

run this in the mega - it receives data 0 or 1 from the ESP-01 on Serial1 port to blink a LED

// Mega - read Serial1 0101010 input from ESP8266 to blink LED
//    ESP-01 transmits 0 or 1 to Serial1 to switch LED OFF/ON
//    enter new blink period in mSec on keyboard, e.g. 500 for 0.5sec

void setup() {
  Serial.begin(115200);
  Serial.println("\nMega to ESP-01 Serial1 test");
  Serial.println("ESP-01 transmits 0 or 1 to Serial1 to switch LED OFF/ON");
  Serial.println("enter new blink period in mSec on keyboard, e.g. 500 for 0.5sec");
  Serial1.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  // if text available read next time value and send to ESP8266 over Serial1
  if (Serial.available() > 0) {         // wait for serial input
    int timeonoff = Serial.parseInt();  // read time value
    Serial1.println(timeonoff);         // send to ESP8266
    Serial.print("\n\Period in mSec sent to ESP-01 ");
    Serial.println(timeonoff);
    delay(10);
    while (Serial.available()) Serial.read();  // flush input
  }
  if (Serial1.available() > 0) {  // wait for Serial1 input
    char ch = Serial1.read();     // should 0 or 1 to blient LED
    Serial.print(ch);
    if (ch == '0') digitalWrite(LED_BUILTIN, LOW);  // turn the LED off by making the voltage LOW
    else if (ch == '1') digitalWrite(LED_BUILTIN, HIGH);           // turn the LED on (HIGH is the voltage level)
  }
}

connect ESP-01 to mega as shown in post 17 and the ESP-01 will send command sequence 01010 to blink the mega LED every second
a new time period can be entered into the Mega serial monitor

once you know the Mega <> ESP-01 communication works you can implement your own code

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