Motor keeps running

Hi guys

I develop this project and for some reason my motor is always running when it should only run if the value is < 35

either way if I change my if condition to true the code within the if statement is executed and nothing happens to the motor it keeps running.

I think I got my circic wrong can someone double check this for me attach is the picture of how I wire all this is attach and the document that I wrote can be found on here:

unfortunately I don’t know electronics can code only

any support would very much be appreciated

Hi, im not an expert on this, but i'd try to add a pull down resistor (10k) on the gate. Are you sure you got the correct mosfet? I'd also add a 220ohm resistor in parallel with the gate to protect the arduino.. Your code is obfuscated, imo. And you havn't shown any pictures of your actual connections.

Qdeathstar:
Hi, im not an expert on this, but i'd try to add a pull down resistor (10k) on the gate. Are you sure you got the correct mosfet? I'd also add a 220ohm resistor in parallel with the gate to protect the arduino.. Your code is obfuscated, imo. And you havn't shown any pictures of your actual connections.

It's not a MOSFET, he's using an NPN Darlington pair transistor.

That being said, OP, I think you should use a MOSFET instead of the Darlington pair. Also, what's with the diode connecting the base and the collector?

Also, your Fritzing schematic is rather unprofessional and difficult to look at with wires being routed in zany directions. Wire paths should have 90 degree angles and should take the shortest path without covering other components.

Lastly, as to your problem: Connect the Arduino digital pin to the base with a current limiting resister in series, get rid of the diode, connect the collector straight to the DC motor negative lead, ground the emitter, and tie the DC motor to the battery 6V.

The diode needs to go across the motor, not across the switching device. Otherwise you could get
really large voltage spikes from the motor.

MarkT:
The diode needs to go across the motor, not across the switching device. Otherwise you could get
really large voltage spikes from the motor.

Oh, that's what its for. Thanks!

All other suggestions I made still apply.

Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
PLEASE not a fritzy, a hand drawn circuit with LABEL’S on components and leads of components will help immensely.
A picture of you project would help so we can see your layout.

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Thanks… Tom… :slight_smile:

Thank you all so much for prompt replies, really do appreciate all the input, some of you have stated that the “Fritzing schematic is rather unprofessional and difficult to look”, I do accept your input, this short tutorial was design for people with no background in electronics and is picture driven. A student who suffer from a condition call “Dyslexia” find pictures and clear lines easy to follow.

any example of how I should merge this type of teaching into Industry standard would be very much appreciated.
I am not an electronic student as such; I can not construct a PCB board yet or design an electronic circuit.

any tutorial on beginner would also be well come

other comments such as “code is obfuscated” am afraid I would disagree with this point all of my code are well commented and this what I believe the industry also requires.

here is my sketch code:

  //*******************************************************
// Autor: Waheed Rafiq Research Student Birmingham City University 
// Date Written: 27/06/2016
// Project: iPot (c) (TM) 2001 , Plant Pot that water itself , 
// able to tell user Humidity within the pot dome. 
// Version : 1.1 
// All CopyRight Reserve (c) 1988
//Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
//Everyone is permitted to copy and distribute verbatim copies of this 
//license document, Modification is allowed please do credit the autor.
// Happy Coding , Keep the Peace , Love Technology
// www.waheedrafiq.net 








#include "dht.h"
#include "U8glib.h" // for the OLED screen
//**************************************************************
  U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);
//**************************************************************
#define DHTPIN 2     // what pin we're connected to digital pin 
//#define DHTTYPE DHT22   // DHT 22  (AM2302
#define DHTTYPE           DHT11     // DHT 11 
dht DHT;
#define DHT11_PIN 2
// Initialize DHT sensor for normal 16mhz Arduino
//DHT dht(DHTPIN, DHTTYPE);
//Variables 
int chk;
float hum; // Stores humidity value
float temp; // Stores temperature value
const int soilSensorPin=0; // intialise soil A0 pin 
int sensorValue = analogRead(soilSensorPin);
//2-Way motor control

int motorPin =9; // Pin that motor is connected to
int x=0;

void draw(void) {
  // graphic commands to redraw the complete screen should be placed here  
  u8g.setFont(u8g_font_unifont);

//************************************************************
//      Soil sensor / Humminity sensor data 
//***********************************************************

// Read data and store it to variables hum and temp
   int chk = DHT.read11(DHT11_PIN);
  // hum= dht.readHumidity(); 
  // temp= dht.readTemperature(); 
  hum = (DHT.humidity);
  temp =(DHT.temperature);
//  Serial.println("Humidity: ");
//  Serial.println(DHT.humidity,1);
//  Serial.println("Temparature: ");
//  Serial.println(DHT.temperature,1);
  
    

    
    //map the value to a percentage
    sensorValue = map(soilSensorPin, 485, 1023, 100, 0);   
    delay(1000); // delay for one second 
    
    // Print temp and humidity values to serial monitor
    // print out the soil water percentage you calculated:
    Serial.println("Soil M : ");// output to serial monitor for testing purpose.
    Serial.println(sensorValue);
   
    delay(1000);// wait for one second... 
    //********************************************
    //    OLED Soil screen OUTPUT
    //********************************************
    u8g.drawStr( 0, 49, "Soil M: "); // leave this at postion 0 , 30
    u8g.setPrintPos(78,49);    // move soil postion back if you want to 0,50 
    u8g.print(sensorValue);
    delay(5000);// delay for 5 second to allow OLED screen to refresh
  
    Serial.print("Humidity: ");// output to serial monitor for testing purpose.
    Serial.println(hum);
    delay(1000); // delay for one second ... 
    
    //********************************************
    //    OLED Humidity screen
    //********************************************
     
    u8g.drawStr( 0, 28, "Humidity:"); // leave this at postion 0 , 30
    u8g.setPrintPos(78,26);    // move soil postion back if you want to 0,50
    u8g.print(hum);
    delay(3000); 


    //*********************************************
    //    NO space on OLED Screen code not included
    //*********************************************
    Serial.print(" %, Temp: ");// output to serial monitor for testing purpose.
    Serial.println(temp);
    Serial.println(" Celsius ");
    delay(2000);
   
   // if soilSensorPin value is less than 35 in soil moist then 
   // trigger the water pump keep checking until above 35 in soil moist.
   if(! isnan(sensorValue)) // this code deals with nan values 
   {
    sensorValue = sensorValue +1;
    //soilSensorPin = soilSensorPin +1;
    Serial.println("New value soil: ");
    Serial.print(sensorValue);
   }
    if(sensorValue > 35 )
    {
      Serial.println(" Soil dry : ");
      Serial.print(sensorValue);
      //********************************************
     //    OLED  OUTPUT "soil dry" + value
    //********************************************
      u8g.drawStr( 0, 46, " Soil dry: "); // leave this at postion 0 , 30
      u8g.setPrintPos(78,49);    // move soil postion back if you want to 0,50 
      u8g.print(sensorValue);
      digitalWrite(motorPin, HIGH); // turn motor on
      delay(6000); // delay 5 seconds 
      Serial.println("motor off"); // test purpose only 
      digitalWrite(motorPin,LOW); // turn motor off 
      delay(10000); // delay 10 second before turning to the top of the code. 
    
    }
   
    else if(sensorValue < 35)
    {
      Serial.println("Soil not dry"); 
      Serial.println("Soil M: ");
      Serial.print(sensorValue); 
      Serial.println("");
     // do nothing to OLED screen  
     
    }  
}


void setup()
{
  Serial.begin(9600);
//  dht.begin(); 
  // initialize the digital pins as an output:

  pinMode(motorPin, OUTPUT); // initialize motor mode 

//**************************************************************
//          OLED Code below 
//**************************************************************

// flip screen, if required
  // u8g.setRot180();
  
  // set SPI backup if required
  //u8g.setHardwareBackup(u8g_backup_avr_spi);

  // assign default color value
  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }
  
  pinMode(8, OUTPUT);

   
}


// General Loop continue to check 

void loop()
{

//************************************************
//  OLED  code below 
//************************************************

  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
    
}// end of loop

another comment about NPN transistor it is used in the project to control the "On / Off " device

As well as being used as a semiconductor switch to turn load currents “ON” or “OFF” by controlling the Base signal to the transistor in ether its saturation or cut-off regions, Bipolar NPN Transistors can also be used in its active region to produce a circuit which will amplify any small AC signal applied to its Base … found on here

I believe the diode is there to ensure the current flow is going in the correct direction

Thank you all so much for your replies and comments all of which have been noted.

I believe the diode is there to ensure the current flow is going in the correct direction

No it is to short out back EMF generated when you turn off the motor.

MrCoolTech:
Thank you all so much for prompt replies, really do appreciate all the input, some of you have stated that the "Fritzing schematic is rather unprofessional and difficult to look", I do accept your input, this short tutorial was design for people with no background in electronics and is picture driven. A student who suffer from a condition call "Dyslexia" find pictures and clear lines easy to follow.

I didn't say you shouldn't use Fritzing. I think it's a great idea. The problem is when you have wire traces that have odd angles in random directions for no reason. It's much easier to follow wire traces when your wires follow predictable, straight paths that run either top of the page to bottom or left to right. Having strange diagonal traces and needless bends make it hard to read.

MrCoolTech:
another comment about NPN transistor it is used in the project to control the "On / Off " device

MOSFETs are better suited for such switching applications.

MrCoolTech:
As well as being used as a semiconductor switch to turn load currents “ON” or “OFF” by controlling the Base signal to the transistor in ether its saturation or cut-off regions, Bipolar NPN Transistors can also be used in its active region to produce a circuit which will amplify any small AC signal applied to its Base ... found on here

Not sure why you're mentioning these points about BJTs. We're well aware of how BJTs work and their applications (this is why I'm urging you that using a MOSFET is a better idea for what you want to do). Also, is there a reason why you mention AC signal amplification? Are you trying to do AC amplification with your project?