🎁📬❄️📦 Holiday Shipping Deadlines are approaching! International customers and domestic ground - get your orders in 12/9/2020 📦❄️📬🎁
0

LED and Solenoid valve not working
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

LED and Solenoid valve not working

by CodeNamiti on Thu Nov 12, 2020 11:03 am

I used this link below to build the circuit but the output is not coming out for LED and solenoid valve.

https://iotdesignpro.com/projects/smart-irrigation-system-using-iot

I have done the circuit based on the tutorial but it is still not working. I have check the LED and it is not blown yet but just not lighting up.

https://photos.app.goo.gl/scn36i7nQgvp5fYE6
https://photos.app.goo.gl/3ibNhDJtsB5hBZQE8

This code below is the one I used. It is able to generate the soil moisture output but LED and solenoid valve is not working. LED is supposed to light up when LDR is blocked and solenaid valve is supposed to switch on when the soil moisture is below 30%.

Code: Select all | TOGGLE FULL SIZE

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

/************************* WiFi Access Point *********************************/

#define WLAN_SSID       "......."
#define WLAN_PASS       "......."

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "......"
#define AIO_KEY         "..........."

/************ Global State ******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;


// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

/****************************** Feeds ***************************************/

//Set up the feed you're publishing to
Adafruit_MQTT_Publish moisture = Adafruit_MQTT_Publish(&mqtt,AIO_USERNAME "/feeds/moisture");


//Set up the feed you're subscribing to
 Adafruit_MQTT_Subscribe light = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/light");
 Adafruit_MQTT_Subscribe waterpump = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/waterpump");
 
/*************************** Sketch Code ************************************/

#define ledPin D6
#define ldrPin D1
#define moisturePin A0            // moisture sensor pin
#define motorPin D0
float moisturePercentage;              //moisture reading

void MQTT_connect();

void setup() {
  Serial.begin(115200);
  delay(10);

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // Setup MQTT subscription for light and water pump
  mqtt.subscribe(&light);
  mqtt.subscribe(&waterpump);
}


void loop() {
  // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).  See the MQTT_connect
  // function definition further below.
  MQTT_connect();

  int ldrStatus = analogRead(ldrPin);

    if(ldrStatus <= 200) {
   
    digitalWrite(ledPin, HIGH);
   
    Serial.print("Its DARK, Turn on the LED : ");
   
    Serial.println(ldrStatus);
   
    }
    else {
   
    digitalWrite(ledPin, LOW);
   
    Serial.print("Its BRIGHT, Turn off the LED : ");
   
    Serial.println(ldrStatus);
   
    }
   
    moisturePercentage = ( 100.00 - ( (analogRead(moisturePin) / 1023.00) * 100.00 ) );

    Serial.print("Soil Moisture is ");
    Serial.print(moisturePercentage);
    Serial.println("%");
   

if (moisturePercentage > 10 && moisturePercentage < 30) {
  digitalWrite(motorPin, HIGH);        //turn on motor pump
}
if (moisturePercentage > 30) {
  digitalWrite(motorPin, LOW);          // turn off mottor
}


if (! moisture.publish(moisturePercentage))
       {                     
         delay(5000);   
       }
Adafruit_MQTT_Subscribe * subscription;
while ((subscription = mqtt.readSubscription(5000)))
     {
   
   if (subscription == &light)
     {
      //Print the new value to the serial monitor
      Serial.println((char*) light.lastread);
     
   if (!strcmp((char*) light.lastread, "OFF"))
      {
        digitalWrite(ledPin, HIGH);
    }
    if (!strcmp((char*) light.lastread, "ON"))
      {
        digitalWrite(ledPin, LOW);
    }
 }
   
   if (subscription == &waterpump)
     {
      //Print the new value to the serial monitor
      Serial.println((char*) waterpump.lastread);
     
   if (!strcmp((char*) waterpump.lastread, "OFF"))
      {
        digitalWrite(motorPin, HIGH);
      }
   if (!strcmp((char*) waterpump.lastread, "ON"))
      {
        digitalWrite(motorPin, LOW);
      }
   
     }
 
    if(! mqtt.ping()) {
    mqtt.disconnect();
    }

  }
 
}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
       retries--;
       if (retries == 0) {
       while (1);
       }
  }
  Serial.println("MQTT Connected!");
}

CodeNamiti
 
Posts: 9
Joined: Fri Nov 06, 2020 4:47 am

Re: LED and Solenoid valve not working

by dastels on Thu Nov 12, 2020 11:14 am

Pins in the Arduino framework default to inputs and I don't see you setting the pinMode of your outputs to OUTPUT.

Dave

dastels
 
Posts: 4251
Joined: Tue Oct 20, 2015 3:22 pm

Re: LED and Solenoid valve not working

by CodeNamiti on Thu Nov 12, 2020 11:49 am

Thank you Dave! I am still learning so thank you for your guidance!

CodeNamiti
 
Posts: 9
Joined: Fri Nov 06, 2020 4:47 am

Re: LED and Solenoid valve not working

by CodeNamiti on Fri Nov 13, 2020 12:24 am

I have added this two line of code in the void setup portion and the LED works but the waterpump doesn't. Does it mean the pump is spoil?

Code: Select all | TOGGLE FULL SIZE
  pinMode(ledPin, OUTPUT);
  pinMode(motorPin, OUTPUT);

CodeNamiti
 
Posts: 9
Joined: Fri Nov 06, 2020 4:47 am

Re: LED and Solenoid valve not working

by dastels on Fri Nov 13, 2020 10:18 am

Does the relay work as expected? You can test by monitoring the connection between the common and NO output connections.

Dave

dastels
 
Posts: 4251
Joined: Tue Oct 20, 2015 3:22 pm

Re: LED and Solenoid valve not working

by CodeNamiti on Fri Nov 13, 2020 10:39 am

Well the relay has both red and green LED lighted up and I make sure that they are secured. It is also a 12V one way relay and I make sure the connection is not loose.

CodeNamiti
 
Posts: 9
Joined: Fri Nov 06, 2020 4:47 am

Re: LED and Solenoid valve not working

by dastels on Fri Nov 13, 2020 11:00 am

Right, but is the relay working? And does the pump work if you connect it directly to its power? Test each piece on it's own... unless you know each piece works, there's little change to figure out what's wrong.

Dave

dastels
 
Posts: 4251
Joined: Tue Oct 20, 2015 3:22 pm

Re: LED and Solenoid valve not working

by CodeNamiti on Fri Nov 13, 2020 11:44 am

Well the pump is working when connected to power although it drips out rather than flow out. As for relay, I probably need test although it is a newly bought one and no burnt smell from it.

CodeNamiti
 
Posts: 9
Joined: Fri Nov 06, 2020 4:47 am

Re: LED and Solenoid valve not working

by dastels on Fri Nov 13, 2020 12:11 pm

Knowing that the relay works correctly will let you rule that out as the problem.

Dave

dastels
 
Posts: 4251
Joined: Tue Oct 20, 2015 3:22 pm

Re: LED and Solenoid valve not working

by CodeNamiti on Wed Nov 25, 2020 7:01 am

Sorry Dave but I have another issue with the solenoid valve. I was able to open the solenoid valve but unable to close it despite setting to it to close when moisture level is above 30% or by toggling the switch in io adafruit. Does this mean the solenoid valve is faulty? Although I do notice I switch and off the valve button, esp 8266 red led turns on and off instead of relay.


This is the link for the solenoid valve.
https://adosia.io/product/solenoid-water-valve/

CodeNamiti
 
Posts: 9
Joined: Fri Nov 06, 2020 4:47 am

Re: LED and Solenoid valve not working

by dastels on Wed Nov 25, 2020 10:21 am

Test the solenoid value on it's own to see if it works or not. There's several things and communication between them; test as much as you can separately to find the problem.

Dave

dastels
 
Posts: 4251
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.