int rs = 7; int en = 8; int d4 = 9; int d5 = 10; int d6 = 11; int d7 = 12; int sensorPin = 2; void sendCommand(byte command) { digitalWrite(rs, LOW); // Send first 4 bits digitalWrite(d4, (command >> 4) & 1); digitalWrite(d5, (command >> 5) & 1); digitalWrite(d6, (command >> 6) & 1); digitalWrite(d7, (command >> 7) & 1); digitalWrite(en, HIGH); delayMicroseconds(1); digitalWrite(en, LOW); // Send remaining 4 bits digitalWrite(d4, command & 1); digitalWrite(d5, (command >> 1) & 1); digitalWrite(d6, (command >> 2) & 1); digitalWrite(d7, (command >> 3) & 1); digitalWrite(en, HIGH); delayMicroseconds(1); digitalWrite(en, LOW); delay(2); } // LCD DATA FUNCTION void sendData(byte data) { digitalWrite(rs, HIGH); // Send first 4 bits digitalWrite(d4, (data >> 4) & 1); digitalWrite(d5, (data >> 5) & 1); digitalWrite(d6, (data >> 6) & 1); digitalWrite(d7, (data >> 7) & 1); digitalWrite(en, HIGH); delayMicroseconds(1); digitalWrite(en, LOW); // Send remaining 4 bits digitalWrite(d4, data & 1); digitalWrite(d5, (data >> 1) & 1); digitalWrite(d6, (data >> 2) & 1); digitalWrite(d7, (data >> 3) & 1); digitalWrite(en, HIGH); delayMicroseconds(1); digitalWrite(en, LOW); delayMicroseconds(50); } // PRINT TEXT ON LCD void printText(char text[]) { int i = 0; while (text[i] != '\0') { sendData(text[i]); i++; } } // DS18B20 RESET bool resetSensor() { pinMode(sensorPin, OUTPUT); digitalWrite(sensorPin, LOW); delayMicroseconds(480); pinMode(sensorPin, INPUT); delayMicroseconds(70); bool presence = (digitalRead(sensorPin) == LOW); delayMicroseconds(410); return presence; } // WRITE ONE BIT void writeBit(byte value) { pinMode(sensorPin, OUTPUT); digitalWrite(sensorPin, LOW); if (value == 1) { delayMicroseconds(6); pinMode(sensorPin, INPUT); delayMicroseconds(64); } else { delayMicroseconds(60); pinMode(sensorPin, INPUT); delayMicroseconds(10); } } // WRITE ONE BYTE void writeByte(byte value) { for (int i = 0; i < 8; i++) { writeBit(value & 1); value = value >> 1; } } // READ ONE BIT byte readBit() { byte value; pinMode(sensorPin, OUTPUT); digitalWrite(sensorPin, LOW); delayMicroseconds(6); pinMode(sensorPin, INPUT); delayMicroseconds(9); value = digitalRead(sensorPin); delayMicroseconds(55); return value; } // READ ONE BYTE byte readByte() { byte value = 0; for (int i = 0; i < 8; i++) { if (readBit() == HIGH) { value = value | (1 << i); } } return value; } // ---------------- SETUP ---------------- void setup() { // LCD pins pinMode(rs, OUTPUT); pinMode(en, OUTPUT); pinMode(d4, OUTPUT); pinMode(d5, OUTPUT); pinMode(d6, OUTPUT); pinMode(d7, OUTPUT); // Initialize LCD delay(50); sendCommand(0x33); sendCommand(0x32); sendCommand(0x28); sendCommand(0x0C); sendCommand(0x06); sendCommand(0x01); } // ---------------- LOOP ---------------- void loop() { // Reset DS18B20 if (resetSensor()) { // Skip ROM writeByte(0xCC); // Start temperature conversion writeByte(0x44); // Wait for conversion delay(750); // Reset sensor again resetSensor(); // Skip ROM writeByte(0xCC); // Read scratchpad writeByte(0xBE); // Read temperature data byte lowByte = readByte(); byte highByte = readByte(); // Combine bytes int rawTemperature = (highByte << 8) | lowByte; // Convert to Celsius float temperature = rawTemperature / 16.0; // Clear LCD sendCommand(0x01); // First row sendCommand(0x80); printText("Temperature:"); // Second row sendCommand(0xC0); // Convert temperature to integer int temp = (int)temperature; // Display hundreds digit if (temp >= 100) { sendData((temp / 100) + '0'); } // Display tens digit if (temp >= 10) { sendData(((temp / 10) % 10) + '0'); } // Display ones digit sendData((temp % 10) + '0'); printText(" C"); } delay(500); }