Need help with sketch to control 2 cooling fans

Hi

I have used the uploaded sketch and it works on my breadboard.

Briefly what happens is this - The LM35 monitors the ambient temperature, if the ambient temperature reaches 30 degrees C the code energises the relayPin, i have a separate 12 volt source thought the relay to drive a 12 volt fan.

I thought it would be nice to have a second Fan to come on when and if the temperature rises to 35 degrees C, to assist the first fan to reduce the ambient temperature.

Can anyone provide a sketch to do this ? i guess it would carry on in the Void Loop, but i am not sure i have tried following the tutorials but without much luck.

the uploaded sketch

What sketch would that be ?

Void Loop

You mean the function whose name is loop that is declared void because it does not return a value, I imagine.

Say out loud 100 times "There is no such thing in C++ as 'a void' but there are functions that return no value which is indicated by preceding the function name with the keyword 'void'"

Whoops, many apologies, here is my sketch.

//Sketch to energise a 5 volt coiled relay by Temperature control, using lm35 temperature sensor with a set point of 30 deg’s C.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int reading = 0;
int sensorPin = 0; //Center pin on LM35 connected to outout pin A0 on Arduino:
int relay = 13; //Relay on pin 13, on Arduino
int celsius;
float tempC;

int SENSOR = 0;
float D = 0;
float T = 0;

void setup()

{
pinMode(relay,OUTPUT);
lcd.begin(16, 2); // set up the LCD’s number of columns and rows:
lcd.setCursor(2,0); // sets the cursor to chosen line and column:
lcd.print(“CNC cabinet”); // Print a message to the LCD:
lcd.setCursor(0,1); // sets the cursor to chosen line and column:
lcd.print(“Temperature:”); // Print a message to the LCD:

}

void loop()

{
Serial.begin(9600);
reading = analogRead(sensorPin);
int celsius = reading/2;
lcd.setCursor(12,1); // sets the cursor to chosen line and column:
lcd.print(celsius, DEC); // Print a message to the LCD:
lcd.print(“C”); // Print a message to the LCD:
if(celsius>25) //Temperature set point to turn relay HIGH

digitalWrite(13,HIGH);
else
digitalWrite(13,LOW);

delay(1000);
}

  if(celsius>25)               //Temperature set point to turn relay HIGH


      digitalWrite(13,HIGH);
    else 
      digitalWrite(13,LOW);

I thought you said that the relay was turned on if the temperature reached 30 degrees. So what is that test for greater than 25 all about ? Is it because you have had to apply a "fudge factor" to the LM35 output ? If so, why bother dividing the value by 2 earlier in the program ?

As to adding a second relay triggered at 35 degrees, then the simplest way would be to repeat the code above to test for a higher temperature and turn on another pin.

Sorry again, no fudge factor, just thought i would raise set point? sorry for the confusion.

Are you sure that dividing the value returned by the sensor by 2 gives you a temperature reading in degrees C ? What is this based on and what are you using as a reference voltage ?

Many thanks UK Helibob

Your sketch suggestion works great, fan 1 on @ 25 degs, fan 2 on @ 30 degs.

your question about celsius, i don't really understand your question, but i have another temperature gauge in the room i am working in, and when the fans are off the LCD reads the same as the room Gauge, 20 degs C, to raise the temperature on the LM35 i use a glass phial filled with hot water, and just touch it on the sensor.

May i ask you one more question, now i have a working sketch i would like to save my Arduino unit, and put the chip on the Breadboard, is it as simple as removing the chip from the Arduino and putting it on the breadboard then re do the connections?

Ray

A quick and dirty method would be to take all of this:

reading = analogRead(sensorPin);
   int celsius = reading/2;
        lcd.setCursor(12,1);         // sets the cursor to chosen line and column:
   lcd.print(celsius, DEC);     // Print a message to the LCD:
   lcd.print("C");              // Print a message to the LCD:
   if(celsius>25)               //Temperature set point to turn relay HIGH


      digitalWrite(13,HIGH);
    else
      digitalWrite(13,LOW);

and copy and paste it just above your delay(1000). Then go through it line by line and rename the necessary variables: SensorPin and celsius, replace the constants 12, 25 and 13 with whatever makes sense. Declare the new variables you've used: SensorPin1, Celcius1. Job done.

It leaves you though with very low quality code. The variable names could use much better names, those blank lines need cleaning up, as does the indentation. Serial,begin belongs in setup, not loop, except that there are no other serial commands so you don't need it at all at the moment.

Duplication of code (copy and paste) as I've suggested is a sure sign that something isn't right. A function that takes a sensorPin, a temperature, an LCD column and a relayPin would clean things up a bit. ALternatively, putting those items into an array of structs would be cleaner too.

However, the dirty way should get you going. Just remember to come back to it when you've learned a bit more and recoil in horror at what you did.

Thanks wildbill

Though a lot of what you have said has gone over my head, after all i am a newbie, if you could give me an example of what you are saying in the form of a sketch, i would be very gratfull.

Ray

Here’s a tweaked version - somewhat nicer:

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int sensorPin = 0;           // Center pin on LM35 connected to outout pin A0 on Arduino:
const int PrimaryFanPin = 13;
const int SecondaryFanPin = 10;

void setup()
{
Serial.begin(9600); //Not needed unless you add some debug
pinMode(PrimaryFanPin,OUTPUT);
pinMode(SecondaryFanPin,OUTPUT);
lcd.begin(16, 2);                  // set up the LCD's number of columns and rows: 
lcd.setCursor(2,0);                // sets the cursor to chosen line and column:
lcd.print("CNC cabinet");          // Print a message to the LCD:
lcd.setCursor(0,1);                // sets the cursor to chosen line and column:
lcd.print("Temperature:");         // Print a message to the LCD: 
}

void loop()
{
ManageFan(12,25,PrimaryFanPin);
ManageFan(6,35,SecondaryFanPin); // I chose arbitrary pin and column, likely need changing
delay(1000);
}

void ManageFan(int LCDColumn,int FanOnTemp,int FanControlPin)
{
int reading = analogRead(sensorPin);
int celsius = reading/2;
lcd.setCursor(LCDColumn,1);         // sets the cursor to chosen line and column:
lcd.print(celsius, DEC);            // Print a message to the LCD:
lcd.print("C");                     // Print a message to the LCD:
if(celsius>FanOnTemp)               // Temperature set point to turn relay HIGH
  digitalWrite(FanControlPin,HIGH);
else 
  digitalWrite(FanControlPin,LOW);
}

Compiled, not tested.

There’s still a bit of cleanup to do - I left some magic numbers (e.g. 12, 25) that would be better as const.

Hi Widbill

Thanks for your sketch and advice.

Has this Arduino business always been frustrating, i tried your sketch, it did not work, so i followed your advise and changed the parameters you suggested, since then my LCD screen is showing jibberrish, tried all kinds of stuff to no avail. So i re reun my original sketch which i know to work only to find out that the LCD screen though not showing jibberish it only shows 2 rows of black squares. I have followed the wiring on my breadboard several times, everything seems to be fine, i know i am a Mechanical engineer, but i do not consider myself thick, what am i doing wrong, if my original sketch does not work anymore can you think of anything that might have happened or something i have done.

Regards

Ray

Sounds like either a wiring issue. Black squares only might be that you've somehow got the contrast adjustment maxed out.

Either way, I'd get a very basic sketch that just puts a simple message on the screen in setup and use that to verify your electricals.

Hi Wildblll

I have re wired and tried the "HELLO WORLD" sketch, it works ok.

Any more ideas??

Ray

UKHeliBob:
Are you sure that dividing the value returned by the sensor by 2 gives you a temperature reading in degrees C ? What is this based on and what are you using as a reference voltage ?

It’s close enough: for 5v it should be 500/1023 but wth.

Now you've rewired it, how do your sketch or mine work now?

Hi wildbill

sketch works well, finally found one of my breadboard jumper wires was beoken inside shieving giving intermittent fault.

May i indulge once more just to finish off my project, i thought it best to use a relay board so i bought a Sainsmart 5 volt coil, 2 channel relay board, i have tried several websites to get a wiring circuit, i have uploaded what i feel might work, but again don't want to kill anything by guessing.

In my sketch the primary fan is on output 8, and the secondary fan on output 9, can you confirm this is ok please.

Regards

Ray

Damn

I keep doing this, it’s an age thing, here is my jpeg of my proposed circuit.

Ray

manofgresley:
Hi wildbill

sketch works well, finally found one of my breadboard jumper wires was beoken inside shieving giving intermittent fault.

May i indulge once more just to finish off my project, i thought it best to use a relay board so i bought a Sainsmart 5 volt coil, 2 channel relay board, i have tried several websites to get a wiring circuit, i have uploaded what i feel might work, but again don't want to kill anything by guessing.

In my sketch the primary fan is on output 8, and the secondary fan on output 9, can you confirm this is ok please.

Regards

Ray

Make sure the relays on the board are 5V relays and if you are feeding them using processor board via USB you may have problems operating both relays same time. Than use additional , external 5V power supply for the relay board itself. I am sure there is a jumper for external 5V source on the relay board.

PS WildBill deserves big ATTA_BOOY for nice demonstration of how functions are cool.

UKHeliBob:
Are you sure that dividing the value returned by the sensor by 2 gives you a temperature reading in degrees C ? What is this based on and what are you using as a reference voltage ?

manofgresley:
your question about celsius, i don't really understand your question,

The LM35 is 10mV/C, which is 10 x 0.001V/C or 0.01V/C so 100C/V.

Therefore a 5V analog pin has the capacity to read 500C (not that the sensor can produce 5V, of course). Since there are 1023 steps in that 5V or 500C, it's 500C/1023steps or ~0.5 C/step. So if the analog pin reads 40, that's 40, 1/2 degree steps, or 20 degrees.

So yep, roughly, divide the analog reading by 2 for C.

(In reality, the 40 is 40x500/1023 which is 19.55)

I suppose since the LM35 can only read 150C, and thus never outputs more than 1.5V, using a 5V analog input is throwing away some precision?

finally found one of my breadboard jumper wires was beoken inside shieving giving intermittent fault.

How annoying is that? Even your jumper wires stab you in the back!

I’m really not the person to be advising you on the electronics side of the house - I can muddle my way through it is all.

That said, the fan side of the relay board looks strange, I’d expect the power to be attached to the leftmost terminal of each three. As you have it, I can’t see how there would ever be a complete circuit.

I’d suggest playing with it with a separate power 5V supply - batteries even, and some LEDs (with current limiting resistors) to figure out what you need before you make a mistake that’s more expensive.