Hello I am a new Arduino user and was wondering when I code the temperature sensor the temperature is Put on a serial monitor. Can I make an if else command for this let’s say if the water temperature is above 70 degrees F then make pin 7 high. Something like that. Is that possible? Please let me know if I need to clarify something! Thanks
Yes it is possible. Acquire the temperature data and use an if - else just like that.
Cool thank you so much how would I write it?
I use a ultrasonic sensor so it’s like this
if(distance=<300)
{
}
else
{
}
Yes, that is the idea though your syntax is a bit off. Should be:
if(distance <= 300) // it is less (or greater) than, then equal
A demo code. Tested on an Uno with DS18B20 sensor. Tested on real hardware. Will light the LED on pin 7 if the temperature exceeds 70°F. LED extinguished if temperature under 70°F.
EDIT: Added hysteresis (see below) and changed threshold variables to float data type.
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
const float upperThresholdTempF = 95.0;
const float lowerThresholdTempF = 90.0;
// Data wire is plugged into pin 4 on the Arduino
const byte ONE_WIRE_BUS = 4;
// indicator LED is connected to pin 7
const byte ledPin = 7;
// 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);
void setup()
{
// start serial port
Serial.begin(115200);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
}
void loop()
{
static unsigned long timer = 0;
unsigned interval = 1000; // reading every 1 second
if (millis() - timer >= interval)
{
timer = millis();
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
// After we got the temperatures, we can print them here.
// We use the function ByIndex, and as an example get the temperature from the first sensor only.
float tempC = sensors.getTempCByIndex(0);
float tempF = sensors.getTempFByIndex(0);
// Check if reading was successful
if (tempC != DEVICE_DISCONNECTED_C)
{
Serial.println("Temperature for the device 1 (index 0) is: ");
Serial.print("temp C =");
Serial.print(tempC);
Serial.print("°C");
Serial.print(" temp F = ");
Serial.print(tempF);
Serial.println("°F");
Serial.println();
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
// turn on led on pin 7 if tempF over thresholdTempF, turn off at lowerThresholdTempF
if (tempF >= upperThresholdTempF)
{
digitalWrite(ledPin, HIGH);
}
else if (tempF <= lowerThresholdTempF)
{
digitalWrite(ledPin, LOW);
}
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
}
else
{
Serial.println("Error: Could not read temperature data");
}
}
}
If the temperature changes too slowly the output may oscillate. Add hysteresis to prevent that.
Is it logical to compare the tempF float number with an integer-type number?
I don't really think that it makes a difference, but I changed it. The compiler has no problem with comparing float to int. If it were float to unsigned int I could understand a problem.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.