Arduino nano beginner greenhouse project guidance

Hello everybody, lets start by saying i’m an arduino beginner, so please be kind on my coding/electronics errors.
So my problem is that i received an ESP32 board while i was working on a simple greenhouse project and wanted to transfer it to get bluetooth and wifi connectivity (reading values directly on smartphones or computers), but as a beginner the compatibility wall between the two is confusing me. First i’m really confused on the absence of direct analog ports on the ESP, then i have trouble to find the interrupt pins in the ESP and lastly i’m simply wondering if there are other compability problems between the two, like in the use of libraries or specific commands.

EDIT: ok this topic changed as i saw that my ability is far under what needed to switch to an ESP, so i’m seeking guidance in perfecting my greenhouse project in which an arduino nano will read air temperature and humidity (DHT11), soil humidity (YL-69) and water level (water level sensor) to control a water pump (to water plants), a fan (for air circulation) and a display (TM1637 showing the values).

Untitled.ino (4.03 KB)

Hi,
Some beginning ESP32 info here:

https://arduinoinfo.mywikis.net/wiki/Esp32

This is how you should post code on this forum. Please read the sticky post!

#include <SimpleDHT.h>
#include <TM1637Display.h>

byte pinDHT11 = 11;
SimpleDHT11 dht11(pinDHT11);
byte CLK = 5; 
byte DIO = 4; 
TM1637Display display(CLK, DIO);  //set the display.
byte humidity_sensor_pin = A1;
byte humidity_sensor_vcc = 3;  //to turn on and off the sensor only when needed (to not oxidize the sensor)
byte pinLEDred = 9; 
byte pinLEDblue = 8;
byte pinLEDgreen =7;
byte pinPump = 6;
byte pinAllarm= A0;
byte pinVel = 10; //set the pin for the fan speed regulation
byte pinHallSensor = 2; //setthe pin for the speed reading
volatile byte half_revolutions; 
int rpm;
long timeold;
byte T = 25; //set limit values to be normalized in the greenhouse conditions for Temperature
byte y = 90; //for Terrain Hum
byte x = 50 ; //for Air hum
byte z = 200; //for Water level

void setup() {
  pinMode(humidity_sensor_vcc, OUTPUT); 
  pinMode(pinLEDblue, OUTPUT);
  pinMode(pinLEDred, OUTPUT);
  pinMode(pinLEDgreen, OUTPUT);
  pinMode(pinPump, OUTPUT);
  pinMode(pinVel,OUTPUT);
  pinMode(pinHallSensor,INPUT);
  digitalWrite(2, HIGH);
  digitalWrite(humidity_sensor_vcc, LOW);
  Serial.begin(9600); 
  display.setBrightness(0); 
  attachInterrupt(0, rpm_fun, RISING);
  half_revolutions = 0;
  rpm = 0;
  timeold = 0;
}
int read_humidity_sensor() { 
  digitalWrite(humidity_sensor_vcc, HIGH);
  delay(100);
  int value = analogRead(humidity_sensor_pin);
  digitalWrite(humidity_sensor_vcc, LOW);
  int percValue = map(value,0,1023,99,0); //map the value between 0 (air) and 99 (salty water)
  return percValue;
  
}

void loop() {
  int allarm = analogRead(pinAllarm); 
  Serial.println("=================================");
  byte temperature = 0; 
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;
  if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Failed Read"); Serial.println(err);delay(1000);
    return; 
  }
  if (half_revolutions >= 20) { 
     rpm = 30*1000/(millis() - timeold)*half_revolutions;
     timeold = millis();
     half_revolutions = 0;
     }

  Serial.println("Valori: ");
  Serial.print("Temperature=");
  Serial.print((int)temperature); Serial.println("*C"); 
  Serial.print("Air Humidity=");
  Serial.print((int)humidity); Serial.println("%H");
  Serial.print("Terrain Humidity=");
  Serial.print((int)read_humidity_sensor()); Serial.println("%H");
  Serial.print("RPM Fan="); 
  Serial.print(rpm,DEC); Serial.println("RPM");
 if (allarm>z) { 
  Serial.println("Water Detected");
 }
 else if (allarm<z) { 
  Serial.println("Water not Detected");
 }


 int val = 35;
 uint8_t data[] = {0x0, 0x0, 0x0, 0x0}; 
 data[0]= display.encodeDigit(12); // C of celsius 
 display.setSegments(data); 
  delay(1000);
  display.showNumberDec(temperature); 
  delay(1000);
 data[0]= display.encodeDigit(14); // E of external 
 display.setSegments(data);  
  delay(1000); 
  display.showNumberDec(humidity);
  delay(1000);
 data[0]= display.encodeDigit(1); // I of internal 
 display.setSegments(data); 
  display.showNumberDec(read_humidity_sensor());


if (read_humidity_sensor()>y || allarm>z) //High humidity AND/OR allarm on turn the pump and the blue led off
 {
    digitalWrite(pinPump, LOW);
    digitalWrite(pinLEDblue, LOW);                                    
  }
  
else if (read_humidity_sensor()<=y && allarm<z) //Low humidity AND allarm off turn the pump and the blue led on
  { 
    digitalWrite(pinPump, HIGH);   
    digitalWrite(pinLEDblue, HIGH);                            
 }

if (temperature>T) //High temperature turn the red led off
 {
    digitalWrite(pinLEDred, LOW);                                    
  }
  
else if (temperature<=T) //Low temperature turn the red led on
  { 
    digitalWrite(pinLEDred, HIGH);                            
 }

if (humidity>x) //High air humidity turn the fan and the green led on
 {
    digitalWrite(pinLEDgreen, HIGH);     
    digitalWrite(pinVel, HIGH);     
  }
else if (humidity<x) //Low air humidity turn the fan and the green led off
  { 
    digitalWrite(pinLEDgreen, LOW);  
    digitalWrite(pinVel, LOW);                          
 }
}

void rpm_fun(){
   half_revolutions++; 
}

Immagine.png
What a mess! Can you draw a proper schematic for us please? Use the “Scematic” view in Fritzing.

Did you really not plug the Nano into the breadboard? That is kind of the point with a Nano!

Are you really running a fan and a motor off the Nano’s 5V rail? And the Nano still works?

You seem to be trying to use a PNP transistor as a low-side switch. It should be an NPN. Also you have no flyback diode. I really am surprised you got this far without destroying the Nano!

What’s that green rectangle? Is that the DHT11? If so, why is it labelled “4 pin fan”. If it is a fan, where is the DHT11?

I think you have work to do before you think about upgrading to an ESP.


Ok so this is the scematic of the project. First i need to point that, as you could see, it was my first attempt at using fritzing. So i added the DHT11 that was missing and corrected the connections to make them more understandable.
On the nano i have connected a DHT11, for temperature and air humidity monitoring, a YL38 igronomic sensor, for soil humidity monitoring, and a water sensor, that is needed simply to stop the motor if water is sensed in the greenhouse bottom. Then i have added 3 leds, for the instant monitoring of the surpassing of limit values for temperature and humidity, a TM1637 screen for showing simply the values, a motor, for water pumping, and a fan, for air circulation.
I'm running all devices on the nano 5V alone, and as of now both the fan and the motor (which is a small, 5V, water pump) works. I tried to add an external 9v circuit to control the two, but even the nano 5V circuit is enough alone to make them work at the needed power.

The purpose of the fan is to run low, it is only needed allow air to move in the greenhouse and it does actually work at a satisfactory speed, even when the motor is functioning.
As of now the project on the nano works, but obviously it has still a LOT of space for tuning. I in fact decided to add the flyback diode as even in the arduino manual it is recommended, but i thought it was needed only when using external circuits to power high voltage components like the motor, but not when working only on the 5V circuit of the nano.
As for the transistor i didn't fully grasp the difference, but the one i'm using is in fact a NPN (PN2222a 6E) and in fact it works (even if i have choosen it out of luck).

So do you guys suggest to still add the flyback diode? should i add other components (i still have it hard to choose when or which condenser, diode or transistor use) before switching?

The flyback diode is important. When any motor, pump, solenoid, relay coil etc is turned off, a reverse polarity voltage is generated which can damage the rest of the circuit. The flyback diode shorts this voltage harmlessly, protecting the rest of the circuit.

Your 4-pin fan will have a small circuit board with a transistor (probably a mosfet) and its own flyback diode.

Your npn transistor seems ok but you must also have a resistor between the Arduino and the transistor's base to limit the current, or the Arduino pin will be damaged. Anything between 220R and 4K7 is probably ok, but you need to know the current draw of the pump to know the correct value.

You have not explained what power supply you are using, is rating, volts & amps. The Nano's regulator can't supply much current at 5V without overheating and shortening is life. You need to measure the current draw of each of the components. The sensors will only be a few mA, but the fan, pump and even the display will be considerably more. If the total comes to more than a couple of hundred mA, you need to think about an external power supply. This can supply the Nano also.

It looks from your code that more than one of the red, green and blue LEDs can be on at the same time. If so, they each need their own series resistor.

A good tip for making your schematics look simpler and easier to understand is to use plenty of "5V" and "GND" symbols located near each major component or group of components, so that the 5V and GND lines don't have to be linked up all over the diagram, making it look messy, with wires crossing each other.

Ok so i should change the topic subject and i did add series resistors to the leds and will add a 1K resistor on the transistor (between the base pin connected and the board?).

As PSU i used a 9V battery (400-600mAh, 9V, and around 30-300mA) or an old powerbank (2200mAh, 5V, 1000mA, or it should be, but it has in fact lower current than my other 1000mA powerbank and similar to the 9V battery).

I did an awful lot of research and i did find current consumption of most of the components:
The motor has a tension of 2,5-6Vdc and should draw 100-200mA
The fan is a 12Vdc, 0,40A 4pin fan
The display TM1637 should draw around 15mA of current
The DHT11 draws 2,5mA max during conversion
The YL-69 draws 35mA
For the water sensor it draws 20mA
Leds only consume around 20mA at max

Well i'm far from using the components at their max consumption, but still they are quite over the current the PSU can support...

So is an external power supply needed? I tried to use one, but i had some problems with isolating tha two circuits so that they would not interfere (probably because i didn't use a flyback diode and so the external PSU would also power the board without being plugged directly, but only to the most power draining components), so seeing the system on and working i decided to drop it.

Also i had some problems of read stability with some sensors, could that be due to my work overload on the board? would the external circuit help to stabilize the reads?

Man i know i'm a biologist and an amateur, but little did i know electronics would be so complex

Edit your post to remove the F word, before the forum mods see it. There are kids on the forum.

Some of those current values don't sound right. 15mA for the display sounds too low. 35mA and 20mA sound too high for sensors. Better to measure them yourself with your multimeter. But the fan and pump alone, as I suspected, mean you need an external power supply. If the fan runs ok for you at 5V, then an external 5V 1A~1.5A power supply should be enough. You can then run the Nano off that as well, by feeding 5V to the 5V pin (not the Vin pin).

Oh sorry i removed it. I was thinking of using the small Powerbank to power the board (directly through the nano USB), but i will add an external battery.

Should i simply power the fan and the motor with it and only control them with transistors from the board? in the arduino manual they implement the external circuit by linking the gnd lines of the external line to the arduino, but i do not understand the meaning of this.

If the power supply has comfortably more than enough current capacity to run all the components, then it can run all the components. Is important to know, however, than when the motor in a fan or a pump starts up, the current drawn in that moment can easilly be double or more compared to the current when it's running steadily. I guess that was the point of the capacitor shown on your schematic: to help with that sudden current surge when the motor is switched on. 100uF is just not enough to do much, unfortunately. 1000uF would be better. If the sudden rush of current is not smoothed, the voltage in the whole circuit can drop, causing the Arduino to reset itself unexpectedly.

Yes, you need a transistor to switch the current to that motor (and flyback diode). An Arduino pin can only supply 40mA absolute max, for occasional short periods. 20mA to 30mA should be the limit for long term use.

The fan will not need a transistor or flyback diode, because a 4 pin fan contains a small circuit board with both those components. This would not be true with a 2 pin fan.

If you are running any circuit from two supplies, such as USB (=5V) and a 7.5V, 9V or 12V external power supply, is important to link the ground connections of both power supplies. Without a "common ground", signals and data from one part of a circuit cannot be picked up by another part of the circuit.

Ok i tried to add an external 9V psu, the problem is that i did not understand where to put the flyback diode and if i attach the 9v battery the motor and the fan work perfectly, but the serial do not give values for the sensor (that the part of the code that i used to control the DHT11 show on the serial as "Failed Read").

Well, now you are showing a 9v battery. The only 9V batteries commonly available these days are the PP3 size used in smoke detectors. They are useless for powering pumps or fans or Arduinos. Please consider my advice for using 5V, GND and also 9V symbols in your schematic, because it is still very difficult to read.

Ok so i added the flyback diode and the resistors and in fact it works! before the flyback i had unexpected shut down of the board, but now the motor works perfectly on the external psu (for now i have tried the 9V, but i intend to use a 4AA 4.8V PSU from now on. I will search for a 1000uf capacitor, but i was wondering if the pn2222a (the one i'm using now) is suitable as a transistor for the motor.


Also, is this what you meant by adding ground and power symbols?

The flyback diode is the wrong way around! Is that how you connected it? Because it would cause an immediate short as soon as the motor starts, maybe blow itself and the transistor, and then the motor would run, and the circuit would still be unprotected. Remember, the flyback diode is meant to short out a reverse voltage generated by the pump motor when it stops (in other words, a positive voltage appears at the pump's negative terminal).

5V & GND symbols: yes, that's the idea. There are a few more you could add.

yeah sorry i did draw it uncorrectly, in fact i did try to put it in the wrong way and it became really hot, but i corrected it right away

Some advice on the soil sensor: I have plenty experience with the type you propose to use but be aware that they are eaten away pretty fast. If you want a more permanent solution you will have to upgrade a bit, search on Aliexpress for "Capacitive Soil Moisture Sensor Module". The price won't kill you.

Looks like Steve is powering the soil sensor from an Arduino digital pin. This allows the sensor to be powered for just a short time when a reading is required, then the power is switched off again. Assuming readings are taken only a few times per hour, the corrosion of the sensor will be minimised and it should have a much longer life. But eventually, perhaps after a year or two, the metal parts in contact with the soil will corrode and the sensor will fail, so the capacitive sensor is a good idea for the replacement when the first sensor fails.

Yeah it was one of the advices on the soil sensor. I work with electrophoresis system that works in a similar way so i knew the problem of oxidation.
I have still one question:
I wanted to add two buttons to set the maximum and minimum humidity value so that by putting the sensor in a dry terrain i could, by pressing the first button, set the value zero, while by putting the sensor in a completely soaked terrain i could set, with the second button, the max value. I have problems thinking on how to code it simply.

PaulRB:
Looks like Steve is powering the soil sensor from an Arduino digital pin. This allows the sensor to be powered for just a short time when a reading is required, then the power is switched off again. Assuming readings are taken only a few times per hour, the corrosion of the sensor will be minimized and it should have a much longer life. But eventually, perhaps after a year or two, the metal parts in contact with the soil will corrode and the sensor will fail, so the capacitive sensor is a good idea for the replacement when the first sensor fails.

This assumes that there is no potential between power supply ground and Earth ground.
But for the cost, an annual replacement would only have a down side of leaving lead in the soil. Not a big deal if the plants are not for human consumption.
I have never seen any data on degradation of sensitivity with the loss of metal as it ages.

I intended to use a small jar with soil only for the sensor, without plants in it, but still watered and use it as standard for the other jars in which i will grow greens, so no lead in the soil used for plants.