Error: 'Blynk' was not declared in this scope Multiple Libraries?

Hi everyone,

I’m building an environmental monitoring system for a poultry farm using an Arduino Uno, DHT22, MQ135, and MQ4 sensors. I’m connecting to Blynk via USB Serial.

I’m getting the error “‘Blynk’ was not declared in this scope” when I try to compile my code. I think it might be a library conflict, as I’ve seen similar issues reported.

I’m using the following versions:

  • Arduino IDE: 2.3.4
  • Blynk Library: 1.3.2
  • DHT sensor library: 1.4.6
  • Adafruit Unified Sensor: 1.1.15

I’ve tried reinstalling the libraries and restarting the IDE, but the error persists."

Code:

#define BLYNK_TEMPLATE_ID "TMPL6kPAR8C3a"
#define BLYNK_TEMPLATE_NAME "RESEARCH SYSTEM"

#define BLYNK_PRINT Serial

#include <Blynk.h>  // Correct library for USB serial communication in Blynk 1.24.1
#include <DHT_U.h>

#define DHTPIN 2
#define DHTTYPE DHT22
#define MQ135_PIN A0
#define MQ4_PIN A1  

DHT dht(DHTPIN, DHTTYPE);

char auth[] = "4WOCbhomNhR4h37n37-aJGk--jIHQKZ3";

const float MQ135_CALIBRATION_FACTOR = 1.0; // Adjust if necessary
const float MQ4_CALIBRATION_FACTOR = 1.0;   // Adjust if necessary

void setup() {
  Serial.begin(115200);
  dht.begin();
  delay(30000); // Allow time for the DHT sensor to stabilize
  Blynk.begin(auth, Serial);  // Initialize Blynk with USB serial connection
}

void loop() {
  Blynk.run();  // Keeps Blynk communication running

  // Read temperature and humidity
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  // Check if the readings are valid
  if (isnan(temperature) || isnan(humidity)) {
    Serial.println("Failed to read from DHT sensor!");
  } else {
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.print(" *C, Humidity: ");
    Serial.print(humidity);
    Serial.println(" %");

    // Send the readings to Blynk virtual pins
    Blynk.virtualWrite(V1, temperature);  
    Blynk.virtualWrite(V2, humidity);     
  }

  // Read and process MQ135 sensor
  int mq135_value = analogRead(MQ135_PIN);
  float calibrated_mq135_value = mq135_value * MQ135_CALIBRATION_FACTOR; 
  Serial.print("Calibrated MQ135 Value: ");
  Serial.println(calibrated_mq135_value);
  Blynk.virtualWrite(V3, calibrated_mq135_value);  // Send to Blynk app

  // Read and process MQ4 sensor
  int mq4_value = analogRead(MQ4_PIN);
  float calibrated_mq4_value = mq4_value * MQ4_CALIBRATION_FACTOR;
  Serial.print("Calibrated MQ4 Value: ");
  Serial.println(calibrated_mq4_value);
  Blynk.virtualWrite(V4, calibrated_mq4_value);  // Send to Blynk app

  delay(2000); // Delay before the next loop iteration
}

this its response;

C:\Users\user-PC\Documents\Arduino\libraries\libraries\skethbalikbayot\skethbalikbayot.ino: In function 'void setup()':
C:\Users\user-PC\Documents\Arduino\libraries\libraries\skethbalikbayot\skethbalikbayot.ino:27:3: error: 'Blynk' was not declared in this scope
   Blynk.begin(auth, Serial);  // Initialize Blynk with USB serial connection
   ^~~~~
C:\Users\user-PC\Documents\Arduino\libraries\libraries\skethbalikbayot\skethbalikbayot.ino:27:3: note: suggested alternative: 'Blynk_h'
   Blynk.begin(auth, Serial);  // Initialize Blynk with USB serial connection
   ^~~~~
   Blynk_h
C:\Users\user-PC\Documents\Arduino\libraries\libraries\skethbalikbayot\skethbalikbayot.ino: In function 'void loop()':
C:\Users\user-PC\Documents\Arduino\libraries\libraries\skethbalikbayot\skethbalikbayot.ino:31:3: error: 'Blynk' was not declared in this scope
   Blynk.run();  // Keeps Blynk communication running
   ^~~~~
C:\Users\user-PC\Documents\Arduino\libraries\libraries\skethbalikbayot\skethbalikbayot.ino:31:3: note: suggested alternative: 'Blynk_h'
   Blynk.run();  // Keeps Blynk communication running
   ^~~~~
   Blynk_h

exit status 1

Compilation error: 'Blynk' was not declared in this scope

@Irs Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of both your code and your compiler error messages so that they display correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

#define BLYNK_TEMPLATE_ID “TMPL6kPAR8C3a”
#define BLYNK_TEMPLATE_NAME “RESEARCH SYSTEM”

#include <DHT.h>
#include <Blynk.h>

#define DHTPIN 2
#define DHTTYPE DHT22
#define MQ135_PIN A0
#define MQ4_PIN A1

DHT dht(DHTPIN, DHTTYPE);

char auth = “4WOCbhomNhR4h37n37-aJGk–jIHQKZ3”;

const float MQ135_CALIBRATION_FACTOR = 1.0; // Adjust this value after calibration
const float MQ4_CALIBRATION_FACTOR = 1.0; // Adjust this value after calibration

void setup() {
Serial.begin(115200);
dht.begin();

delay(30000); // Wait 30 seconds for MQ sensor preheating (adjust as needed)

Blynk.begin(auth, Serial); // USB connection
}

void loop() {
Blynk.run();

float temperature = dht.readTemperature();
float humidity = dht.readHumidity();

if (isnan(temperature) || isnan(humidity)) {
Serial.println(“Failed to read from DHT sensor!”);
} else {
Serial.print(“Temperature: “);
Serial.print(temperature);
Serial.print(” *C, Humidity: “);
Serial.print(humidity);
Serial.println(” %”);

Blynk.virtualWrite(V1, temperature); // Temperature on V1
Blynk.virtualWrite(V2, humidity); // Humidity on V2


}

I am encountering a persistent issue with my Arduino Uno project using the Blynk library. Despite having installed the Blynk library (version 1.5.2) through the Library Manager, I consistently receive the error “‘Blynk’ was not declared in this scope” when attempting to compile my code. I have created a new sketch and included the line #include <Blynk.h> after defining my Blynk Template ID, Template Name, and Auth Token. My code includes the lines #define BLYNK_TEMPLATE_ID "TMPL6KPAR8C3a", #define BLYNK_TEMPLATE_NAME "RESEARCH SYSTEM", #include <Blynk.h>, and char auth[] = "4WOCbhomNhR4h37n37-aJGk--jIHQKZ3";. In the setup() function, I have Serial.begin(115200); and Blynk.begin(auth, Serial);, and in the loop() function, I have Blynk.run();. The complete error message from the compiler is: “C:\Users\user-PC\AppData\Local\Temp.arduinoIDE-unsaved2025117-2236-764eww.Suto6\sketch_feb17a\sketch_feb17a.ino:15:3: note: suggested alternative: ‘Blynk_h’ Blynk.run(); Blynk_h Compilation error: ‘Blynk’ was not declared in this scope exit status 1 Compilation error: ‘Blynk’ was not declared in this scope”. I am using Arduino IDE version 2.3.4 and am working on Windows. I have tried reinstalling the Blynk library multiple times, restarting the Arduino IDE, manually deleting the Blynk library folder from the Arduino/libraries directory, and creating new sketches, but the error persists. Could someone or you please help me understand why the compiler is not recognizing the Blynk library and how to resolve this issue?

If you do as I asked, and EDIT YOUR ORIGINAL POST to correct the formatting of your code and compiler error message then I’ll explain what you are doing wrong.

Pete.

ahh okay, done… sorry

done pleaasseeee helpp mee, have to past this tomorrow, been doing this for 3 days and im still stuch here

No, you’ve put triple backticks around your code, but not around your compiler error messages.

Pete.

donttt leaveee mee hangginggg hereee fleaaasseeee

Take a look at this screenshot…

Do you see how adding triple backticks to your code has added scroll bars to that part of your post?
Can you see how the section below it, immediately after “this its response;” (sic) doesn’t have scroll bars?
This is because you haven’t put the necessary triple backticks in the locations indicated by the two red arrows on the left hand side.
Until you fix that issue, as I’ve asked you to do three times so far (this is the fourth time) then you’ll be “hangginggg” for another 23 hours, at which point your ability to edit your original post will end, and your post will be deleted because it contains an unformatted compiler error message.

Pete.

kk sorry

In that case, you need to replace this:

with this:

#include <BlynkSimpleStream.h>

However, as your Uno has only one serial port, and you are using that for communication with the Blynk server via the serial script, you can’t also use that serial port for debugging - as these debug messages will interfere with the communication messages.

That means that you need to remove these lines of code:

In addition, what you are doing within the void loop won’t work well with Blynk.
Blynk doesn’t like the use of delays, as these interfere with the constant back and forth communication between your device and the Blynk server. Delays stop all processing until the delay period is over, and interfere with Blynk communication.

This article:

explains the correct approach, which is to use BlynkTimer to call the code which takes the DHT22, MQ135 and MQ4 readings.

You may also want to stop trying to recalibrate your MQ135 and MQ4 sensors every 2 seconds.

It’s also worth considering that if you have a recently created Free Blynk subscription, you are limited to 30,000 messages per month.
Your current code does 4 Blynk.virtualWrites every two seconds. Each of these sends a message to the Blynk server, so that’s 120 messages per minute. This means that you will use-up your monthly message allowance in 250 minutes, or just over 4 hours.
To check if you have this type of account, go to the Billing screen in the web console and see if you have a message counter.

Pete.

if that’s the case? could you just improvise this code? I’m a bit confuse