DC motor not running when temperature is high

Hello,
I am new to using Arduino and I am using Arduino Uno R3 to do my projects. I have been trying to include a motor into my program but it will simply not go on. I am trying to program the motor to start running when a certain temperature is reached, please kindly assist. this is my code :

#include <LiquidCrystal.h>

#define sensorPin  A0

#define rs 12
#define en 11
#define d4 5
#define d5 4
#define d6 3
#define d7 2
const int alarm = 8;
int dcmotor = 10;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  lcd.begin(16, 2);
}
void loop() {
pinMode(13,OUTPUT);
pinMode(8,OUTPUT);
pinMode(7,OUTPUT);
pinMode(10,OUTPUT);
  int analogValue;
  float temp;
  analogValue = analogRead(sensorPin);
  temp = float(analogValue)/1023;
  temp = temp*500;
  //////
  lcd.setCursor(0,1);
  lcd.print(temp);
  lcd.print((char)223);
  lcd.print("C");
  /////////

if(temp >= 27.1 && temp <= 125){
  digitalWrite(dcmotor,HIGH);
 lcd.setCursor(0,0);
lcd.print("HIGH TEMP");
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13,LOW);
delay(200);
tone(alarm,500);
delay(400);
noTone(alarm);

}
else if(temp <= 27 && temp >= -50 ){

lcd.setCursor(0,0);
lcd.print("TEMPERATURE:");
  digitalWrite(7, HIGH);
delay(600);
digitalWrite(7,LOW);
}
}

Hello
Insert some comments and names for the pins and variables to your sketch. Post a schematic, too.

Hello Ianpana,
First of all, welcome to Arduino! You will learn a lot about it as time goes on.
What is the motor connected to? The Arduino itself or a driver circuit?
Arduino cannot directly drive a motor on its own - it simply cannot output enough power.
It's a bad idea to try to run a motor directly from a digital pin (In your case, D10). This can damage the microcontroller.

Hi proxy303, my dc motor is connected to the arduino via a transistor (2N5401) , a diode (1N4007) and a 560 ohm resistor.

What is powering the motor?

Well, I think I found some problems. I would recommend you try changing these and seeing if it helps.

else if(temp <= 27 && temp >= -50 ){

lcd.setCursor(0,0);
lcd.print("TEMPERATURE:");
  digitalWrite(7, HIGH);
delay(600);
digitalWrite(7,LOW);
}

In this case the pin will turn on for 600 ms, turn off and then immediately turn back on because of the loop.

pinMode(13,OUTPUT);
pinMode(8,OUTPUT);
pinMode(7,OUTPUT);
pinMode(10,OUTPUT);

Move this to void setup() { }

int analogValue;
float temp;

Move this outside of void loop() { } (top of the program)

digitalWrite(dcmotor,HIGH);

I can see you turned the motor on, but does it ever get turned off?

What do pins 7 and 13 do?

else if(temp <= 27 && temp >= -50 ){  }

temp is taken from the analog input. Analog inputs cannot be negative, but you are saying that it could be as low as -50, which the analog input cannot do.

Why repeat the setting of these pins as OUTPUT? Only do this once, in setup().

If this ever happens, it never un-happens. In other words - nobody ever sets it back to LOW after this. Is that what you want?

Btw, I find the magic tricks with the pin names extremely confusing. d7 = 2, which is not pin7 which you use for something else (?)

Please show a schematic.

What's the diode for? And any chance you got it backwards? That gets me all the time.

Hi, I have attached the schematic, please check.

@proxy303 I have corrected most of these errors like you suggested but there is still no change. The motor will not run.

You're trying to run a motor from an Arduino board. That rarely is a good idea - it will only work with tiny motors. I don't know what kind your using, but I'd not recommend running ANY kind of motor from an Arduino board. Always use a separate power supply for it.
I'm not sure if this is the cause of your problem though. The connections as far as I can tell look OK - although I have to admit that I find these fritzing artworks horrible to read, although they look pretty. They're very confusing though, so I may well have missed a obvious error.

The motor is not connected to the transistor collector in your diagram, but to the base.

You have shown an NPN transistor (correct polarity for this connection topology), but you mention it being a 2N5401 which is PNP (which will not work in this circuit).

The 560 ohm base resistor is too high, 150 ohms is a reasonable compromize between allowing high current to the motor and not burning out the Arduino.

BTW Its always risky to power any motor from a logic 5V supply, as motors can put nasty spikes and dropouts on the power rail. Separate power for motors is a great idea.

Hi @MarkT thank you for that, the actual project was connected correctly, only the schematic is incorrect, I have fixed that. I managed to make it work and solved the problem by using the code below: note that I used digitalWrite(dcmotor,LOW); when it is actually supposed to be high, and vice versa , I interchanged high and low and I'm surprised at how it is actually working. For the transistor issue i only used the NPN in the schematic because it is the only one available on tinkercad, otherwise I used a PNP which serves the same purpose but the connection is interchanged because of the arrow that points a different direction as compared to an NPN transistor I hope I make sense.

#include <LiquidCrystal.h>

#define sensorPin A0

#define rs 12
#define en 11
#define d4 5
#define d5 4
#define d6 3
#define d7 2
const int alarm = 8;
int dcmotor = 10;
int analogValue;
float temp;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
lcd.begin(16, 2);
pinMode(13,OUTPUT);
pinMode(8,OUTPUT);
pinMode(7,OUTPUT);
pinMode(10,OUTPUT);
}
void loop() {

analogValue = analogRead(sensorPin);
temp = float(analogValue)/1023;
temp = temp*500;
//////
lcd.setCursor(0,1);
lcd.print(temp);
lcd.print((char)223);
lcd.print("C");
/////////

if(temp >= 27.1 && temp <= 125){
digitalWrite(dcmotor,LOW);
lcd.setCursor(0,0);
lcd.print("HIGH TEMP");
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13,LOW);
delay(200);
tone(alarm,500);
delay(400);
noTone(alarm);

}
else if(temp <= 27 && temp >= 0 ){
lcd.setCursor(0,0);
lcd.print("TEMPERATURE:");
digitalWrite(7, HIGH);
delay(600);
digitalWrite(7,LOW);
digitalWrite(dcmotor,HIGH);

}
}Preformatted text
I will definitely use a separate power source for the motor.

I only saw a NPN but I just realized the PNP is also available

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.