#include <OneWire.h> // url: https://github.com/PaulStoffregen/OneWire
#include <DallasTemperature.h> // url: https://github.com/milesburton/Arduino-Temperature-Control-Library
// #include <DHT.h>
// #include <Wire.h>
#define PRJ_NAME "Arduino Nano DS18B20" // Project Name
#define VERSION "0.0.2"
#define PRINT_DASHED_LINES Serial.println(F("-------------------------------------------------------"));
#ifdef LED_BUILTIN
#define MY_LED LED_BUILTIN // LED_BUILTIN -> On Board Led
#else
#define MY_LED 13 // On Board Led
#endif
// These are for the temperature
#define ONE_WIRE_BUS 2 // DS18B20 pin - Temperature Sensor
#define DHTPIN 3 // DHT22 pin - Temperature & humidity Sensor
//#define DHTTYPE DHT22
// float TempDS18, TembDHT22;
// uint8_t Humidity;
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire device
DallasTemperature sensors(&oneWire); // Pass oneWire reference to DallasTemperature library
DeviceAddress insideDS18x20; // DS18x20 arrays to hold device address
// DHT dht(DHTPIN, DHTTYPE);
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// ======================================================================
void ReadTemperature() {
// Send the command to get temperatures
sensors.requestTemperatures();
float TempDS18 = sensors.getTempCByIndex(0);
//float TempDS18 = sensors.getTempC(insideDS18x20);
if (TempDS18 == DEVICE_DISCONNECTED_C) {
TempDS18 = random(-1100, 5990) / 100.0;
Serial.print("\r-[Error]: Could not find DS18B20. Send Random value for test - \n\r");
}
//float TembDHT22 = dht.readTemperature();
//uint8_t Humidity = dht.readHumidity();
/*
Serial.print("Temperature DS18B20 : ");
Serial.print(TempDS18);
Serial.print("\xB0""C");
Serial.print(" - Fahrenheit : ");
Serial.println(sensors.getTempFByIndex(0));
*/
//print the temperature in Celsius
Serial.print("Temperature DS18B20 : ");
Serial.print(sensors.getTempCByIndex(0));
Serial.print((char)176);//shows degrees character
Serial.println("C");
// PRINT_DASHED_LINES;
} //== Close ReadTemperature() ====
// ======================================================================
// function to print a device address
void printAddress(DeviceAddress deviceAddress) {
for (uint8_t i = 0; i < 8; i++) {
// zero pad the address if necessary
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
} //== Close printAddress ==
// ======================================================================
void sensors_setup() {
// ====================================================================
sensors.begin();
// Get ID of first sensor (at index 0)
if (!sensors.getAddress(insideDS18x20, 0))
Serial.println("Unable to find address for Device 0");
// set the resolution to 10 bit
sensors.setResolution(insideDS18x20, 10);
Serial.print("Dallas Library Vers.: ");
Serial.println(DALLASTEMPLIBVERSION);
// show the addresses we found on the bus
Serial.print("Device 0 Address : ");
printAddress(insideDS18x20);
Serial.println();
Serial.print("Device 0 Resolution : ");
Serial.println(sensors.getResolution(insideDS18x20), DEC);
// dht.begin();
PRINT_DASHED_LINES;
} //== Close sensors_setup() ==
// ======================================================================
void setup() {
// ====================================================================
Serial.begin(115200);
delay(10);
char buff[50];
Serial.print("\r\n--------------- Booting -------------------------------\r\n");
Serial.print(F( "Arduino IDE : "));
Serial.println( ARDUINO, DEC); // arduino ide version
Serial.print(F( "Compiler version : "));
Serial.println(__VERSION__); // gcc version
Serial.print(F( "Filename : "));
Serial.println(__FILE__); // file compiled
sprintf(buff, "Firmware name : %s v%s \r\n", PRJ_NAME, VERSION);
Serial.print(buff);
sprintf(buff, "Firmware Date : %s \r\n", (__DATE__ ", " __TIME__)); // compilation date, time
Serial.print(buff);
Serial.print("Author : Kernel Panic \r\n");
PRINT_DASHED_LINES;
pinMode(MY_LED, OUTPUT);
sensors_setup();
ReadTemperature();
}
// ======================================================================
void loop() {
// ====================================================================
static uint32_t last_tick;
if (millis() - last_tick >= 5000) {
last_tick = millis();
ReadTemperature();
}
yield();
digitalWrite(MY_LED, millis() % 500 < 40);
}