safwan092

LCD 3xLED Buzzer Ultrasonic Sensor Trash Detector

Dec 10th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3.  
  4. // Initialize LCD (16x2)
  5. LiquidCrystal_I2C lcd(0x27, 16, 2);
  6.  
  7. // LED Pins
  8. #define GREEN_LED 8
  9. #define YELLOW_LED 9
  10. #define RED_LED 10
  11.  
  12. // Ultrasonic Sensor Pins
  13. #define TRIG_PIN 11
  14. #define ECHO_PIN 12
  15.  
  16. // Buzzer Pin
  17. #define BUZZER_PIN 13
  18.  
  19. // Bin Height (in cm)
  20. const int BIN_HEIGHT = 30; // Adjust based on the actual bin height
  21.  
  22. void setup() {
  23. // Initialize Serial Monitor
  24. Serial.begin(9600);
  25.  
  26. // Initialize LCD
  27. lcd.init();
  28. lcd.init();
  29. lcd.backlight();
  30. lcd.setCursor(0, 0);
  31. lcd.print("Trash Bin Level");
  32.  
  33. // Set LED pins as OUTPUT
  34. pinMode(GREEN_LED, OUTPUT);
  35. pinMode(YELLOW_LED, OUTPUT);
  36. pinMode(RED_LED, OUTPUT);
  37.  
  38. // Set Buzzer pin as OUTPUT
  39. pinMode(BUZZER_PIN, OUTPUT);
  40.  
  41. // Set Ultrasonic pins
  42. pinMode(TRIG_PIN, OUTPUT);
  43. pinMode(ECHO_PIN, INPUT);
  44.  
  45. delay(1000);
  46. }
  47.  
  48. void loop() {
  49. // Measure distance using Ultrasonic sensor
  50. int distance = getUltrasonicDistance();
  51. int trashLevel = BIN_HEIGHT - distance; // Calculate filled level
  52. trashLevel = max(0, min(trashLevel, BIN_HEIGHT)); // Clamp value between 0 and BIN_HEIGHT
  53.  
  54. // Calculate percentage of trash
  55. int percentage = map(trashLevel, 0, BIN_HEIGHT, 0, 100);
  56.  
  57. // Display level and percentage on LCD
  58. lcd.setCursor(0, 0);
  59. lcd.print("Level: ");
  60. lcd.print(trashLevel);
  61. lcd.print(" cm ");
  62.  
  63. lcd.setCursor(0, 1);
  64. lcd.print("Full: ");
  65. lcd.print(percentage);
  66. lcd.print("% ");
  67.  
  68. // Update LED indicators
  69. updateLEDs(percentage);
  70.  
  71. // Control Buzzer
  72. if (percentage >= 80) {
  73. //digitalWrite(BUZZER_PIN, HIGH); // Turn buzzer ON
  74. tone(BUZZER_PIN, 1000, 500); // Beep at 1000Hz for 500ms
  75. } else {
  76. digitalWrite(BUZZER_PIN, LOW); // Turn buzzer OFF
  77. }
  78.  
  79. // Print details to Serial Monitor (for debugging)
  80. Serial.print("Distance: ");
  81. Serial.print(distance);
  82. Serial.print(" cm, Level: ");
  83. Serial.print(trashLevel);
  84. Serial.print(" cm, Full: ");
  85. Serial.print(percentage);
  86. Serial.println("%");
  87.  
  88. delay(500); // Update every 500ms
  89. }
  90.  
  91. // Function to measure distance using HC-SR04 with averaging
  92. int getUltrasonicDistance() {
  93. long totalDistance = 0;
  94.  
  95. // Take 10 readings and sum them up
  96. for (int i = 0; i < 10; i++) {
  97. // Send trigger pulse
  98. digitalWrite(TRIG_PIN, LOW);
  99. delayMicroseconds(2);
  100. digitalWrite(TRIG_PIN, HIGH);
  101. delayMicroseconds(10);
  102. digitalWrite(TRIG_PIN, LOW);
  103.  
  104. // Measure the duration of the echo pulse
  105. long duration = pulseIn(ECHO_PIN, HIGH);
  106.  
  107. // Calculate distance (in cm)
  108. int distance = duration * 0.034 / 2;
  109.  
  110. totalDistance += distance;
  111. delay(50); // Small delay between readings to stabilize
  112. }
  113.  
  114. // Calculate the average
  115. int averageDistance = totalDistance / 10;
  116.  
  117. return averageDistance;
  118. }
  119.  
  120.  
  121. // Function to update LED indicators based on trash percentage
  122. void updateLEDs(int percentage) {
  123. if (percentage < 10) {
  124. // Low level: Green LED ON
  125. digitalWrite(GREEN_LED, LOW);
  126. digitalWrite(YELLOW_LED, LOW);
  127. digitalWrite(RED_LED, LOW);
  128. } else if (percentage < 50) {
  129. // Low level: Green LED ON
  130. digitalWrite(GREEN_LED, HIGH);
  131. digitalWrite(YELLOW_LED, LOW);
  132. digitalWrite(RED_LED, LOW);
  133. } else if (percentage < 80) {
  134. // Medium level: Yellow LED ON
  135. digitalWrite(GREEN_LED, HIGH);
  136. digitalWrite(YELLOW_LED, HIGH);
  137. digitalWrite(RED_LED, LOW);
  138. } else {
  139. // High level: Red LED ON
  140. digitalWrite(GREEN_LED, HIGH);
  141. digitalWrite(YELLOW_LED, HIGH);
  142. digitalWrite(RED_LED, HIGH);
  143. }
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment