Hi there, relative n00b here. Learning my way though.
I'm making a little project to turn a light bulb on and off to generate heat for a small greenhouse I am building. I have purchased a relay, I have also got a temp sensor.
I can get the values from the digital temp sensor, and I can work out how to turn the light on and off, but I can't seem to figure out how to take a value from the serial monitor and then act on it.
In pseudo code I would like for the light to come on when the temp is below a range, let's say 25 and go off again when it gets to 25, although part of me thinks it will then be flicking on and off, so perhaps a range would be better?
Here is what I have:
/* Arduino DS18B20 temp sensor tutorial
More info: http://www.ardumotive.com/how-to-use-the-ds18b20-temperature-sensor-en.html
Date: 19/6/2015 // www.ardumotive.com */
//Include libraries
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
const int relayPin = 10; //sets the relay pin to #10
const int threshold = 24;
void setup(void)
{
pinMode(relayPin, OUTPUT);
Serial.begin(9600); //Begin serial communication
Serial.println("Arduino Digital Temperature // Serial Monitor Version"); //Print a message
sensors.begin();
}
void loop(void)
{
// Send the command to get temperatures
sensors.requestTemperatures();
Serial.print("Temperature is: ");
Serial.println(sensors.getTempCByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
//Update value every 1 sec.
delay(1000);
int TempValue = digitalRead(ONE_WIRE_BUS);
if (TempValue > threshold) {
digitalWrite(relayPin, HIGH);
} else {
digitalWrite(relayPin, LOW);
}
}
Please help This is the 1st project where I am using a library, is this causing the problems?
void loop(void)
{
// Send the command to get temperatures
sensors.requestTemperatures();
delay(1000); //Update value every 1 sec.
float TempValue = sensors.getTempCByIndex(0);
Serial.print("Temperature is: ");
Serial.println(TempValue);
if (TempValue >= threshold)
{
digitalWrite(relayPin, HIGH);
}
if (TempValue < threshold - 1.0)
{
digitalWrite(relayPin, LOW);
}
}
I decided to start again and instead of using the library write it myself, I've put together this code to get values from the sensor but it seems to just be giving me '1' in Serial Monitor.
Are you really using a DS18B20?
Do you have a resistor?
You need to use the library - your current level of understanding and skill is not sufficient to execute this project.without a library
A simple digitalRead will not work
Strange, I have it running on a Nano using a DS18B20 and it's working perfectly, what kind of relay are you using?
Here's a few lines from Serial monitor:
Temperature is: 24.25
Temperature is: 24.37
Temperature is: 24.37
Temperature is: 24.25
Temperature is: 24.25
Temperature is: 24.37
Temperature is: 24.37
Temperature is: 24.37
outsider:
Strange, I have it running on a Nano using a DS18B20 and it's working perfectly, what kind of relay are you using?
Here's a few lines from Serial monitor:
Temperature is: 24.25
Temperature is: 24.37
Temperature is: 24.37
Temperature is: 24.25
Temperature is: 24.25
Temperature is: 24.37
Temperature is: 24.37
Temperature is: 24.37
Ok, so I modified everything and now have this:
//Include libraries
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
const int relayPin = 10; //sets the relay pin to #10
const int threshold = 50.00;
void setup(void)
{
pinMode(relayPin, OUTPUT);
Serial.begin(9600); //Begin serial communication
Serial.println("Arduino Digital Temperature // Serial Monitor Version"); //Print a message
sensors.begin();
}
void loop(void)
{
// Send the command to get temperatures
sensors.requestTemperatures();
delay(1000); //Update value every 1 sec.
float TempValue = sensors.getTempCByIndex(0);
Serial.print("Temperature is: ");
Serial.println(TempValue);
if (TempValue >= threshold)
{
digitalWrite(relayPin, HIGH);
}
if (TempValue < threshold - 1.0)
{
digitalWrite(relayPin, LOW);
}
}
This is now giving me the output temperature but no matter what the temperature is the relay doesn't seem to kick in. The relay I bought is this one from ebay https://www.ebay.co.uk/itm/401551024674?ViewItem=&item=401551024674 It works fine when I just do a HIGH / LOW command on digitalWrite so I know it is working.
Managed to fix it with the help of a colleague; turns out I had stupidly put the cable in pin 11 and not 10. A simple on/off command made me realise and now all works.
The code is here:
//Include libraries
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
const int relayPin = 10; //sets the relay pin to #10
const int threshold = 20; //sets the threshold to monitor for
void setup(void)
{
pinMode(relayPin, OUTPUT); //defines the relayPin as an output and not an input
Serial.begin(9600); //Begin serial communication
Serial.println("Arduino Digital Temperature // Serial Monitor Version"); //Print a message
sensors.begin();
}
void loop(void)
{
// Send the command to get temperatures
sensors.requestTemperatures();
delay(1000); //Update value every 1 sec.
float TempValue = sensors.getTempCByIndex(0);
Serial.print("Temperature is: ");
Serial.println(TempValue);
if (TempValue >= threshold)
{
digitalWrite(relayPin, LOW);
}
if (TempValue < threshold - 1.0)
{
digitalWrite(relayPin, HIGH); //this section sets the temp threshold to turn the light on or off
}
}
Happy to hear of any ways to make this more efficient?