Go Down

Topic: While relay is working, Serial Print is not printing data. (Read 257 times) previous topic - next topic


Hello all. I'm quite new with arduino. My project consist in the following. I have a Capacitive Soil Moisture Sensor v1.2 in a plant soil. I also have a Adafruit Peristaltic Pump in order to water my plants when "mositure content is <= 25%". Actually, everything is working fine. But for the project I need to retrieve all the data, and with my actual code I am not getting Serial.print data while the relay is working.

If its of any help, I've tried connecting the relay VCC to arduino's 5 V, which i got to read that its not advised to do so. So i bought a 5 VDC power supply to power the relay via JD-VCC, and ground also goes to the relay. The low currents coming from arduino are only digital signal port 7 and 5 VCC (which, to be honest, I don't know why, but I followed a tutorial). The thing works. It water the plants when it should, but as I was saying, when the relay is working there is no viewable data in the Serial port (but I know there should be, because it stops when it should i.e. when soil moisture is over 25%). Hence, I believe i've got a problem with my code. Also, if possible, i'd like to print the state of the relay besides "soilmoisturepercent", which I haven't been able to do so.

I believe that my problem is located at the "delay(10000)" inside the condition for the relay to work. Nevetheless, I don't know how I could change that. Every 10 seconds, with Millis() function, arduino will check the state of the moisture content, if the condition for the relay to get working is satisfied, then the relay should work for 10 seconds (the latter delay time I was talking about) and check after 10 seconds again the moisture content to continue or stop.

FYI: relay high and low values are switched because I'm using JD-VCC power from external power supply.

Code: [Select]

const int AirValue = 591;   // Calibration of Soil Moist. Sensor
const int WaterValue = 311;  // Calibration of Soil Moist. Sensor
int soilMoistureValue = 0;
int soilmoisturepercent = 0;
int relay = 7;

unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 10000;  //the value is a number of milliseconds

void setup()
  pinMode(relay, OUTPUT);
  startMillis = millis();

void loop()
  soilMoistureValue = analogRead(A0);  // Soil moisture sensor plain data
  soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);

  // Execution time for the moisture sensor shown data

  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= period)  //test whether the period has elapsed... 10 seconds
    if (soilmoisturepercent <= 25){
      delay(10000); // Execution time for the relay.
    else if (soilmoisturepercent >25)
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.

  // Printing
  if(soilmoisturepercent > 100)
    Serial.println("100 %");
  else if(soilmoisturepercent <= 0)
    Serial.println("0 %");
  else if(soilmoisturepercent >=0 && soilmoisturepercent <= 100)
    Serial.print(" ");


Thanks in advance.


I believe that my problem is located at the "delay(10000)" inside the condition for the relay to work.

You are already using millis() to manage another time interval (line 30). Use the same technique for the 10 second interval.

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

Two or three hours spent thinking and reading documentation solves most programming problems.


Hello Robin2,

Thank you! The solution was quite straightforward.

Go Up