error when using pinMode

Hello , i'm new at arduino tbh and i have project to do
so i'm trying to make watering plants iot and using blynk too
when i didn't include pinmode at my code it works pretty well and it can connect the blynk
but when i trying to include pinmode, the serial monitor looping at the void setup, the output at serial monitor is weird and the blynk repeatedly connect and then disconnected.
this is my code from setup

void setup() {
  
  Serial.begin(9600); // See the connection status in Serial Monitor
  Serial.println(F("DHTxx test!"));
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  pinMode(PumpAir, OUTPUT);
  //Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop() {
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer*/ // Blynk
}

void sendSensor(){
  int soil_analog = analogRead(SoilSensor);
  float soilSensorValue = map(soil_analog,1024,290,0,100);
  float moisture_percentage = ( 100 - ( (soil_analog/1024.00) * 100 ) );
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, soilSensorValue);  //V5 is for Soil Moisture
  Blynk.virtualWrite(V6, t);  //V6 is for Temperature

  DateTimeNow();
  Serial.print("Moisture Percentage = ");
  Serial.print(moisture_percentage);
  Serial.print("%  Moisture Index = ");
  Serial.print(soil_analog);
  Serial.print("  \nMoisture Index with Maping = ");
  Serial.print(soilSensorValue);
  Serial.print("\n\n");

  if (soilSensorValue < 70)
  {
    Serial.print("Harus disiram\n");
    digitalWrite(PumpAir, HIGH);
  }
  else if (soilSensorValue > 80)
  {
    Serial.print("Terlalu basah\n");
    digitalWrite(PumpAir, LOW);
  }
  Serial.print(F("\nHumidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print("\n\n");
}

Please post all your code.

IN CODE TAGS

I see no declaration for 'PumpAir', you probably didn't post your entire sketch. If that is the case, please do.

#include "DHT.h"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] =*AUTH* //Enter the Auth code which was send by Blink
char ssid[] = *SSID*//Enter your WIFI Name
char pass[] =  *PASS* //Enter your WIFI Password

const int SoilSensor = A0;
#define DHTPIN 4          // Digital pin 4
#define DHTTYPE DHT11 
const int PumpAir = 9;
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

void setup() {
  
  Serial.begin(9600); // See the connection status in Serial Monitor
  
  Serial.println(F("DHTxx test!"));
  Blynk.begin(auth, ssid, pass);

  dht.begin();
  pinMode(PumpAir, OUTPUT);
  //Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop() {
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer*/ // Blynk
}

void sendSensor(){
  int soil_analog = analogRead(SoilSensor);
  float soilSensorValue = map(soil_analog,1024,290,0,100);
  float moisture_percentage = ( 100 - ( (soil_analog/1024.00) * 100 ) );
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, soilSensorValue);  //V5 is for Soil Moisture
  Blynk.virtualWrite(V6, t);  //V6 is for Temperature

  Serial.print("Moisture Percentage = ");
  Serial.print(moisture_percentage);
  Serial.print("%  Moisture Index = ");
  Serial.print(soil_analog);
  Serial.print("  \nMoisture Index with Maping = ");
  Serial.print(soilSensorValue);
  Serial.print("\n\n");

  if (soilSensorValue < 70)
  {
    Serial.print("Harus disiram\n");
    digitalWrite(PumpAir, HIGH);
  }
  else if (soilSensorValue > 80)
  {
    Serial.print("Terlalu basah\n");
    digitalWrite(PumpAir, LOW);
  }
  Serial.print(F("\nHumidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print("\n\n");
}

What is connected to pin 9?

TheMemberFormerlyKnownAsAWOL:
What is connected to pin 9?

Its the relay pin IN and its connected with pump 5V

What is "the relay"?

TheMemberFormerlyKnownAsAWOL:
What is "the relay"?

Sorry, i mean it's Channel Relay 5V to connect the pump.
I see many people using Channel Relay to turn on/off the pump

He meant, "what kind of relay". The physical one. "Channel Relay 5V" is just a vague generic description.

It's likely that energizing the relay, is affecting the processor via the power supply.

aarg:
He meant, "what kind of relay". The physical one. "Channel Relay 5V" is just a vague generic description.

It's likely that energizing the relay, is affecting the processor via the power supply.

so the code is OK but then i need to check the wiring ?

val_val97:
so the code is OK but then i need to check the wiring ?

Maybe. There are no guarantees. Why not begin by actually answering the questions that you are asked?

ESP8266 can only output 3.3V and may not be able to source or sink enough current, does pin 9 have some alternate function?

HI,
Can you post a circuit diagram please, showing power supply and component and pin labels.
A picture of a hand drawn diagram would be fine.

Thanks.. Tom.. :slight_smile: