Having trouble with a water pump code?

The attached file is our schematic for the water pump code. How it works is that a soil sensor in the dirt will detect moisture level of the dirt. If the dirt level is low enough, the pump with water the dirt until it is wet enough. The board is powered by 12v power supply for the motor that goes into Vin

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int thresholdUp = 400;
int thresholdDown = 250;
int mostureSensor = A0;
int Water_Pump = A5;
int sensorVaule = mostureSensor;
void setup() {
  // put your setup code here, to run once:

  pinMode(Water_Pump, OUTPUT);

  lcd.begin(16, 2); //Initialize the 16x2 LCD


  lcd.clear();  //Clear any old data displayed on the LCD

}

void loop() {
  // initialize the mosture value from the soil sensor
  int sensorValue = analogRead(mostureSensor);
  Serial.println(mostureSensor);
   //If the sensor is detecting 0 to 300 mosture value
  if (sensorValue > 0 && sensorValue < 300)
  {
    lcd.print("Dry Soil!"); // Display the dry soil message on the LCD!
    lcd.setCursor(0, 1);  //Set the (invisible) cursor to column 0, row 1.     delay(2000);
    digitalWrite(Water_Pump, HIGH); //water the plant until else if
  }
  else if (sensorValue <= 300 && sensorValue < 700)  //If the sensor value is detecting 300 to 700 mosture,
  {
    delay(2000);
    digitalWrite(Water_Pump, LOW); //stop the water pump
    lcd.print("Plant happy!"); // Display a message on the LCD!
    lcd.setCursor(0, 1);  //Set the (invisible) cursor to column 0,row 1.

  }
  else if (sensorValue = 0) // if there is no reading, then
  {
    lcd.print("Please check the sensor!"); // Display the sensor message on the LCD!
    lcd.setCursor(0, 1);  //Set the (invisible) cursor to column 0, row 1.

  }
}

*Please note we have changed the pin to A5 in the schematic.

But couple of things aren't working;

  1. The LCD is displaying just white squares

  2. The pump is on continuously, and won't stop even if the dirt is wet enough.

The only thing that is working is soil sensor.

Hmmmm.....

So the soil sensor is expected to give a reading on a digital input (on/off)? - I would have attached this to an analog input. The code does say A0, but the diagram shows D0 ??? You CANNOT digital write to an anlaog pin like this.

Also the pump is on an analog output with HIGH and LOW values? - I would put that on a digital out.

So remove the digital 0 cable and put it on an analog in and change the code for that. Take the analog motor and move to a digital output.

As for the LCD display... Is there an adjustment on the back for contrast? IF so a very small flat blade screwdriver should be used to adjust this so you can read it. Double check your code too...

Then try again.

While running a complicated program, my group may have done something wrong to break down the 16x2 LCD screen.

I tried running a test circuit below
https://learn.sparkfun.com/tutorials/sik-experiment-guide-for-arduino---v32/experiment-15-using-an-lcd

What I am seeing is
Hello, w
(seconds since the message)

I am also seeing a bunch of white squares, some transparent around the text.

The following is part of the bigger circuit. Since It did not work on that circuit, I figured I might as well as break it down to smaller bits to test each part of the circuit.

So how can I test this 12v DC motor?

Uh - to test the motor itself? Just apply 12v to it.

Your diagram shows a P-channel mosfet (and one probably too small to drive most motors), but you have it wired as if it were an N-channel one.

We don't like Fritzing diagrams here - because they encourage people to substitute parts, and then we think that the diagram represents what they have, when it actually doesn't, and thus we waste everyone's time.

Remove the wiring and start from the beginning double checking each one as you go.

If it is still as you say then post the sketch but ensure you use code tags ( </> ) to do so.

The transistor itself is a PN2222A (an NPN). I am pretty sure everything is hooked up correctly.

const int motorPin= A5; //LED connected to digital pin 10

void setup()
{
pinMode(motorPin, OUTPUT); //sets the digital pin as output
}

void loop()
{
analogWrite(motorPin,HIGH); //turns the LED on
delay(1000);
analogWrite(motorPin,LOW);
delay(1000);
}

As far as I can tell, this is all one problem. My apologies if merging all four threads was a poor choice.

@sgy0003, STOP CROSS-POSTING.

@sgy0003, at least TRY to post in an appropriate section.

Also, while testing the motor by itself, it looks like the transistor is also heating up

Also, while testing the motor by itself, it looks like the transistor is also heating up

Wow! You can see heat?

Your analogWrite's should be digitalWrite's.

void loop()
{
digitalWrite(motorPin,HIGH); //turns the LED on
delay(1000);
digitalWrite(motorPin,LOW);
delay(1000);
}

Check the spelling on your variables.

You declare "sensorVaule", and then use "sensorValue".

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

A circuit diagram like below would help, even if you draw it by hand and photograph it.


We need to see labeled pins, connections and components.
A picture of you project will help too, so we can see your component layout.

Thanks.. Tom... :slight_smile:

KiCAD is a fantastic piece of free software to draw circuit diagrams - something you should always do first for all but the simplest of projects, even before you're doing a Fritzing wiring thing (those are pretty unreadable and actual part numbers and values are missing, so great as wiring guide but that's about it).

That you feel your transistor heating up is no surprise. Motors can easily pull 1-3 Amp, and that kind of currents are a problem for many transistors. MOSFETs are more useful, they can switch shocking amounts of current without the need of a heat sink. Your transistor is rated at max 600 mA continuous, and the voltage drop at that current is 1V, which means it'd be dissipating 600 mW in it's tiny TO-92 housing.

Another thing: your transistor's gain is about 50, a 1K resistor between the output pin and the transistor limits the base current to 5 mA (5V output) so maximum current available for your pump will be about 250 mA. Maybe you should lower that value to 470R or so.

So what you could do, to test your pump, is to load good old Blink on your Arduino, and let it "blink" your pump. Then you know whether your transistor is switching it on and off as expected.