Guest User

Untitled

a guest
Nov 8th, 2025
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. #include "FS.h"
  2. #include "SD.h"
  3. #include <SPI.h>
  4.  
  5. #define AOUT_PIN1 36 // ESP32 pin GPIO36 (ADC0) that connects to AOUT pin of moisture sensor
  6. #define AOUT_PIN2 39 // ESP32 pin GPIO36 (ADC3) that connects to AOUT pin of moisture sensor
  7.  
  8. #define SD_CS 5
  9. String dataMessage;
  10.  
  11. int moisture_threshold = 2500;
  12. int ledPin1 = 2;
  13. int ledPin2 = 4;
  14.  
  15. int value1 = 0;
  16. int value2 = 0;
  17.  
  18. void setup() {
  19. Serial.begin(9600);
  20. // set the ADC attenuation to 11 dB (up to ~3.3V input)
  21. analogSetAttenuation(ADC_11db);
  22. pinMode(ledPin1, OUTPUT);
  23. pinMode(ledPin2, OUTPUT);
  24.  
  25. // Initialize SD card
  26. SD.begin(SD_CS);
  27. if(!SD.begin(SD_CS)) {
  28. Serial.println("Card Mount Failed");
  29. return;
  30. }
  31. uint8_t cardType = SD.cardType();
  32. if(cardType == CARD_NONE) {
  33. Serial.println("No SD card attached");
  34. return;
  35. }
  36. Serial.println("Initializing SD card...");
  37. if (!SD.begin(SD_CS)) {
  38. Serial.println("ERROR - SD card initialization failed!");
  39. return; // init failed
  40. }
  41. File file = SD.open("/data.txt");
  42. if(!file) {
  43. Serial.println("File doens't exist");
  44. Serial.println("Creating file...");
  45. writeFile(SD, "/data.txt", "ESP32 and SD Card \r\n");
  46. }
  47. else {
  48. Serial.println("File already exists");
  49. }
  50. file.close();
  51.  
  52.  
  53.  
  54. }
  55.  
  56. void loop() {
  57.  
  58. readMsensors();
  59. logSDCard();
  60. delay(2000); //Wait for 2 seconds before writing the next data
  61.  
  62. }
  63.  
  64. void logSDCard() {
  65. dataMessage = "Moisture Value 1 = " + String(value1) + ". Moisture Value 2 = " + String(value2) + ".\n";
  66. Serial.print("Save data: ");
  67. Serial.println(dataMessage);
  68. appendFile(SD, "/data.txt", dataMessage.c_str());
  69. }
  70.  
  71. // Write to the SD card (DON'T MODIFY THIS FUNCTION)
  72. void writeFile(fs::FS &fs, const char * path, const char * message) {
  73. Serial.printf("Writing file: %s\n", path);
  74. File file = fs.open(path, FILE_WRITE);
  75. if(!file) {
  76. Serial.println("Failed to open file for writing");
  77. return;
  78. }
  79. if(file.print(message)) {
  80. Serial.println("File written");
  81. } else {
  82. Serial.println("Write failed");
  83. }
  84. file.close();
  85. }
  86.  
  87. // Append data to the SD card (DON'T MODIFY THIS FUNCTION)
  88. void appendFile(fs::FS &fs, const char * path, const char * message) {
  89. Serial.printf("Appending to file: %s\n", path);
  90. File file = fs.open(path, FILE_APPEND);
  91. if(!file) {
  92. Serial.println("Failed to open file for appending");
  93. return;
  94. }
  95. if(file.print(message)) {
  96. Serial.println("Message appended");
  97. } else {
  98. Serial.println("Append failed");
  99. }
  100. file.close();
  101. }
  102.  
  103. void readMsensors() {
  104. value1 = analogRead(AOUT_PIN1); // read the analog value from sensor
  105. value2 = analogRead(AOUT_PIN2);
  106.  
  107. Serial.print("Moisture value 1: ");
  108. Serial.print(value1);
  109. Serial.print(". Moisture value 2: ");
  110. Serial.println(value2);
  111.  
  112. if (value1 > moisture_threshold) {
  113. digitalWrite(ledPin1, HIGH);
  114. } else {
  115. digitalWrite(ledPin1, LOW);
  116. }
  117.  
  118. if (value2 > moisture_threshold) {
  119. digitalWrite(ledPin2, HIGH);
  120. } else {
  121. digitalWrite(ledPin2, LOW);
  122. }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment