I am trying to combine a moisture sensor and a dc motor I am trying to get the motor to turn on and off depending on the moisture level but when I upload it to the board the motor just stays on. Here is my programming.
#include <Wire.h>
int val = 0; //value for storing moisture value
int soilPin = A0;//Declare a variable for the soil moisture sensor
int soilPower = 7;//Variable for Soil moisture Power
int motorPin = 9;//Variable for Motor Power
void setup()
{
Serial.begin(9600); // Begin the serial monitor at 9600bps
}
//---------------------------------------------------------------
void loop()
{
Serial.print("Soil Moisture = ");
//get soil moisture value from the function below and print it
Serial.println(readSoil());
delay(1000); // delay execution next instruction 1 sec or 1000 msec
if (val < 750) digitalWrite(9, HIGH);
if (val > 850) digitalWrite(9, LOW);
}
int readSoil()
{
val = analogRead(soilPin);//Read the SIG value from sensor
return val;//send current moisture value
}
The code should be sending the soil moisture value to the serial monitor. What does it say?
It is sending the value of soil moisture, the problem is that the motor will not turn off, it continuously runs.
Is there a reason you have used a separate function to read the moisture sensor? It is literally just an analogRead.
What motor are you using?
Can you give us a schematic of how everything is wired up?
You need to define the motor power pin as an output.
Adding a serial print to each if() statement can help you see how the moisture value is being assessed, something like this:
Serial.println(readSoil());
if (val < 750)
{
digitalWrite(9, HIGH);
Serial.println("Moisture value low");
}
if (val > 850)
{
digitalWrite(9, LOW);
Serial.println("Moisture value high");
}
delay(1000); // delay execution next instruction 1 sec or 1000 msec
I am new to the arduino language and I do not understand where to input that section of coding u just replied with. When I added the pinMode(9, OUTPUT) the code did not verify and errors appeared, I wrote this line below my void setup() line.
Here is some information about setting digital pins to be outputs. The pinMode() statement needs to be in between the curly braces that start and end the setup function.
So I added in the pinMode function and the code verified then I used the function you gave me in replacement of the old functions and I recieved errors. This is what I have now.
void loop()
Serial.println(readSoil());
if (val < 750)
{
digitalWrite(9, HIGH);
Serial.println("Moisture value low");
}
if (val > 850)
{
digitalWrite(9, LOW);
Serial.println("Moisture value high");
}
delay(1000); // delay execution next instruction 1 sec or 1000 msec
Please check out this thread- particularly the part about using code tags when you post the code. It makes it much easier for other people. You'll also need to post all of your code each time (not just part) and also the error messages that you get. If you press CTRL + T in the IDE it will also auto-format your code so that it is easier for other people to read your code.
ProjectGreenhouse:
So I added in the pinMode function and the code verified then I used the function you gave me in replacement of the old functions and I recieved errors. This is what I have now.
void loop()
Serial.println(readSoil());
if (val < 750)
{
digitalWrite(9, HIGH);
Serial.println("Moisture value low");
}
if (val > 850)
{
digitalWrite(9, LOW);
Serial.println("Moisture value high");
}
delay(1000); // delay execution next instruction 1 sec or 1000 msec
You'll have to post your whole code, but at the very least you are missing the opening braces on your loop() function.
This is what I have as of right now but the dc motor still is not shutting off when the moisture level reaches 850.
#include <Wire.h>
int val = 0; //value for storing moisture value
int soilPin = A0;//Declare a variable for the soil moisture sensor
int soilPower = 7;//Variable for Soil moisture Power
int motorPin = 9;//Variable for Motor Power
void setup()
{
pinMode(9, OUTPUT);
Serial.begin(9600); // Begin the serial monitor at 9600bps
}
void loop()
{
Serial.print("Soil Moisture = ");
//get soil moisture value from the function below and print it
Serial.println(readSoil());
delay(1000); // delay execution next instruction 1 sec or 1000 msec
if (val < 750) digitalWrite(9, HIGH);
if (val > 850) digitalWrite(9, LOW);
}
int readSoil()
{
val = analogRead(soilPin);//Read the SIG value from sensor
return val;//send current moisture value
}
try replacing this section:
if (val < 750) digitalWrite(9, HIGH);
if (val > 850) digitalWrite(9, LOW);
with this:
if (val < 750)
{
digitalWrite(9, HIGH);
Serial.println("Value low");
}
if (val > 850)
{
Serial.println("Value high");
digitalWrite(9, LOW);
}
And for goodness sake, please start using code tags.