Advertisement
pleasedontcode

"Temperature Display" rev_01

Jul 18th, 2024
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Temperature Display"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-07-18 09:04:33
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Display temperature readings from a DS18B20 sensor */
  21.     /* on an LCD1602 I2C display using ESP32. The DS18B20 */
  22.     /* is connected to GPIO 4, and the LCD uses I2C pins */
  23.     /* SDA (GPIO 21) and SCL (GPIO 22) with address 0x27. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Wire.h>
  28. #include <DS18B20.h>  // https://github.com/matmunk/DS18B20
  29. #include <LCDIC2.h>   // https://github.com/offcircuit/LCDIC2
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t Temp_DS18B20_DQ_PIN_D4 = 4;
  37.  
  38. /***** DEFINITION OF I2C PINS *****/
  39. const uint8_t LCD_I2C_LCD1602I2C_I2C_PIN_SDA_D21 = 21;
  40. const uint8_t LCD_I2C_LCD1602I2C_I2C_PIN_SCL_D22 = 22;
  41. const uint8_t LCD_I2C_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27; // Typically 0x27 for LCD1602
  42.  
  43. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  44. DS18B20 ds(Temp_DS18B20_DQ_PIN_D4);  // Initialize the DS18B20 sensor with the data pin
  45. LCDIC2 lcd(LCD_I2C_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2); // Initialize the LCD with I2C address, width, and height
  46.  
  47. void setup(void)
  48. {
  49.   // put your setup code here, to run once:
  50.   Serial.begin(9600);
  51.  
  52.   // Initialize the I2C communication for the LCD
  53.   Wire.begin(LCD_I2C_LCD1602I2C_I2C_PIN_SDA_D21, LCD_I2C_LCD1602I2C_I2C_PIN_SCL_D22);
  54.  
  55.   // Initialize the LCD
  56.   lcd.begin();
  57.   lcd.print("Hello, World!");
  58.  
  59.   // Check if the DS18B20 sensor is connected
  60.   uint8_t address[8];
  61.   if (ds.selectNext(address)) {
  62.     Serial.println("DS18B20 device found!");
  63.   } else {
  64.     Serial.println("DS18B20 device not found!");
  65.   }
  66. }
  67.  
  68. void loop(void)
  69. {
  70.   // put your main code here, to run repeatedly:
  71.   uint8_t address[8];
  72.   if (ds.selectNext(address)) {
  73.     float temp = ds.getTempC();
  74.     if (!isnan(temp)) {
  75.       Serial.print("Temperature: ");
  76.       Serial.print(temp);
  77.       Serial.println(" C");
  78.       lcd.setCursor(0, 1); // Move cursor to the second line
  79.       lcd.print("Temp: ");
  80.       lcd.print(temp);
  81.       lcd.print(" C");
  82.     } else {
  83.       Serial.println("Failed to read temperature!");
  84.     }
  85.   } else {
  86.     Serial.println("DS18B20 device not valid!");
  87.   }
  88.  
  89.   delay(10000);
  90. }
  91.  
  92. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement