Temperature reading

Our range of tiny modules are designed to make it easy for you to quickly add functionality to your projects. [Module List]
Post Reply
Chris
Posts: 17
Joined: Sat Aug 30, 2014 5:37 am
Location: Adelaide, South Australia

Temperature reading

Post by Chris » Fri Oct 10, 2014 10:36 am

Hi guys. Can someone take a look at this sketch and advise me where and what I should be putting in to sketch somewhere to be able to switch relay 1 on if the temp gets above 28.

Code: Select all

// Libraries
#include <Wire.h> 
#include <OneWire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );
#include <DallasTemperature.h> //will find addresses of sensors automatically
const char degree = 223;   //This will give us the degree symbol after the temp 



// Data wire from DS18B20 to Arduino Digital PIn 2
#define ONE_WIRE_BUS 2
//Relays
int relay_1 = 22;       //Digital Pin for Relay1
int relay_2 = 23;       //Digital Pin for Relay2
int relay_3 = 24;       //Digital Pin for Relay3



// Setup oneWire instance to communicate with devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

void setup()
{
  
 //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(relay_1, HIGH);
  digitalWrite(relay_2, HIGH);
  digitalWrite(relay_3, HIGH);
   
  
//---( THEN set pins as outputs )----  
  pinMode(relay_1, OUTPUT);   
  pinMode(relay_2, OUTPUT);  
  pinMode(relay_3, OUTPUT);  

  
  
  
  Serial.begin(9600);
  // set up the LCD's number of rows and columns:
  // initialize the Relay pin as an output:
  lcd.begin(16, 2);
  lcd.print ("Spiders");
  lcd.setCursor(2,1);
  lcd.print ("Controller");
  delay(2000);
  lcd.clear();
  lcd.setCursor (2,0);
  lcd.print ("Target Temp:");
  lcd.setCursor (6,1);
  lcd.print ("27");
  lcd.print (degree);
  lcd.print ("C");
  delay(2000);
  lcd.clear();
  // Start the OneWire library
  sensors.begin();
}

void readtemp(void)
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  sensors.requestTemperatures(); // Get temps
  lcd.setCursor ( 0, 0 ); //Print Display temp on top line
  lcd.print ("Display: ");
  lcd.print(sensors.getTempCByIndex(1)); //Use this if you want C
  //lcd.print(sensors.getTempCByIndex(1)* 9/5 * .54); //convert to F
  lcd.print (degree); //display degree symbol after temp value
  //lcd.print ("f"); //farenheit
  lcd.setCursor ( 0, 1 ); //Print Room temp on bottom line
  lcd.print ("Room: ");
  lcd.print(sensors.getTempCByIndex(0)); //Use this if you want C
  //lcd.print(sensors.getTempCByIndex(0)* 9/5 * .54); //convert to F
  lcd.print (degree);
  //lcd.print ("c");
}
void loop(){
   
    
  lcd.setCursor ( 15, 0 ); //symbol "*" flashes on top line as temps are read
  lcd.print("*");
  readtemp();
  delay (500);//reads temp every .5 second
  lcd.setCursor ( 15, 0 );
  lcd.print(" ");
  delay (500);
  
  if(temp>28)
{
digitalWrite(relay_1, HIGH);
}
  if(temp>27)
{
digitalWrite(relay_1, LOW); 
}
  
      
   
}
 



Chris

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: Temperature reading

Post by andrew » Fri Oct 10, 2014 11:01 pm

Code: Select all

// Libraries
#include <Wire.h>
#include <OneWire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );
#include <DallasTemperature.h> //will find addresses of sensors automatically
const char degree = 223;   //This will give us the degree symbol after the temp



// Data wire from DS18B20 to Arduino Digital PIn 2
#define ONE_WIRE_BUS 2
//Relays
int relay_1 = 22;       //Digital Pin for Relay1
int relay_2 = 23;       //Digital Pin for Relay2
int relay_3 = 24;       //Digital Pin for Relay3



// Setup oneWire instance to communicate with devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

void setup()
{

  //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(relay_1, HIGH);
  digitalWrite(relay_2, HIGH);
  digitalWrite(relay_3, HIGH);


  //---( THEN set pins as outputs )---- 
  pinMode(relay_1, OUTPUT);   
  pinMode(relay_2, OUTPUT); 
  pinMode(relay_3, OUTPUT); 




  Serial.begin(9600);
  // set up the LCD's number of rows and columns:
  // initialize the Relay pin as an output:
  lcd.begin(16, 2);
  lcd.print ("Spiders");
  lcd.setCursor(2,1);
  lcd.print ("Controller");
  delay(2000);
  lcd.clear();
  lcd.setCursor (2,0);
  lcd.print ("Target Temp:");
  lcd.setCursor (6,1);
  lcd.print ("27");
  lcd.print (degree);
  lcd.print ("C");
  delay(2000);
  lcd.clear();
  // Start the OneWire library
  sensors.begin();
}

void readtemp(void)
{
  float temperature;
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  sensors.requestTemperatures(); // Get temps
  lcd.setCursor ( 0, 0 ); //Print Display temp on top line
  lcd.print ("Display: ");
  lcd.print(sensors.getTempCByIndex(1)); //Use this if you want C
  //lcd.print(sensors.getTempCByIndex(1)* 9/5 * .54); //convert to F
  lcd.print (degree); //display degree symbol after temp value
  //lcd.print ("f"); //farenheit
  lcd.setCursor ( 0, 1 ); //Print Room temp on bottom line
  lcd.print ("Room: ");
  lcd.print(sensors.getTempCByIndex(0)); //Use this if you want C
  //lcd.print(sensors.getTempCByIndex(0)* 9/5 * .54); //convert to F
  lcd.print (degree);
  //lcd.print ("c");
  
  temperature = sensors.getTempCByIndex(1);
     
  if(temperature > 28)
  {
    digitalWrite(relay_1, HIGH);
  } else if (temperature <= 28)
  {
    digitalWrite(relay_1, LOW);
  }
}

void loop()
{
  lcd.setCursor ( 15, 0 ); //symbol "*" flashes on top line as temps are read
  lcd.print("*");
  readtemp();
  delay (500);//reads temp every .5 second
  lcd.setCursor ( 15, 0 );
  lcd.print(" ");
  delay (500);
}



Chris
Posts: 17
Joined: Sat Aug 30, 2014 5:37 am
Location: Adelaide, South Australia

Re: Temperature reading

Post by Chris » Sat Oct 11, 2014 2:11 am

Thanks :mrgreen:

Post Reply