#include <TM1637Display.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Pin definitions for TM1637 displays
#define CLK1 2 // First display CLK
#define DIO1 3 // First display DIO
#define CLK2 5 // Second display CLK
#define DIO2 6 // Second display DIO
// Pin definitions for DS18B20 sensors
#define ONE_WIRE_BUS1 4 // First DS18B20 data pin
#define ONE_WIRE_BUS2 8 // Second DS18B20 data pin
// Pin definition for LED
#define LED_PIN 7
// Setup OneWire and DallasTemperature for two sensors
OneWire oneWire1(ONE_WIRE_BUS1);
OneWire oneWire2(ONE_WIRE_BUS2);
DallasTemperature sensors1(&oneWire1);
DallasTemperature sensors2(&oneWire2);
// Variables
TM1637Display display1(CLK1, DIO1); // First display
TM1637Display display2(CLK2, DIO2); // Second display
const uint8_t C[] = {
SEG_B | SEG_A | SEG_F | SEG_G,
SEG_A | SEG_D | SEG_E | SEG_F
};
float temperature1; // Temperature from first sensor
float temperature2; // Temperature from second sensor
const int threshold = 25; // Fixed threshold temperature in Celsius
void setup() {
// Initialize serial
Serial.begin(9600);
// Initialize DS18B20 sensors
sensors1.begin();
sensors2.begin();
// Initialize displays
display1.setBrightness(0x0f);
display2.setBrightness(0x0f);
// LED pin
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW); // Ensure LED is off initially
}
void loop() {
// Read temperatures from both DS18B20 sensors
temperature1 = readTemperature1();
temperature2 = readTemperature2();
// Display temperatures on both displays
displayTemperature1();
displayTemperature2();
// Control LED based on temperature vs threshold
if (temperature1 > threshold || temperature2 > threshold) {
digitalWrite(LED_PIN, HIGH); // Turn LED on if either temp > threshold
} else {
digitalWrite(LED_PIN, LOW); // Turn LED off if both temps <= threshold
}
}
// Function to read temperature from first DS18B20
float readTemperature1() {
sensors1.requestTemperatures();
float tempCelsius = sensors1.getTempCByIndex(0);
if (tempCelsius == DEVICE_DISCONNECTED_C) {
return -127.0; // Error value if sensor is disconnected
}
return tempCelsius;
}
// Function to read temperature from second DS18B20
float readTemperature2() {
sensors2.requestTemperatures();
float tempCelsius = sensors2.getTempCByIndex(0);
if (tempCelsius == DEVICE_DISCONNECTED_C) {
return -127.0; // Error value if sensor is disconnected
}
return tempCelsius;
}
// Function to display the temperature on the first TM1637 display
void displayTemperature1() {
int displayTemp = (int)(temperature1 * 1); // No decimal place
display1.showNumberDecEx(displayTemp, 0b0000, false, 2, 0); // No colon or decimal
display1.setSegments(C, 2, 2); // Show "C" symbol
}
// Function to display the temperature on the second TM1637 display
void displayTemperature2() {
int displayTemp = (int)(temperature2 * 1); // No decimal place
display2.showNumberDecEx(displayTemp, 0b0000, false, 2, 0); // No colon or decimal
display2.setSegments(C, 2, 2); // Show "C" symbol
}