pleasedontcode

Pressure Relay rev_01

Dec 16th, 2025
32
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: Pressure Relay
  13.     - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
  14.     - Source Code created on: 2025-12-17 01:04:44
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Allumer relais lorsque presion entre 1 et 3 bar */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23.  
  24. /* START CODE */
  25.  
  26. // System Requirements: "Allumer relais lorsque presion entre 1 et 3 bar".
  27. // Use ADS1115 to measure pressure and control relay accordingly.
  28. // Libraries
  29. #include <Wire.h>
  30. #include <DFRobot_ADS1115.h>
  31.  
  32. // Constants
  33. const uint8_t PRESSURE_SENSOR_I2C_ADDRESS = 72; // 0x48
  34. const uint8_t RELAY_PIN = D0; // GPIO 16
  35.  
  36. // Instance of ADS1115
  37. DFRobot_ADS1115 ads(&Wire);
  38.  
  39. // Variable to hold pressure value
  40. int pressure_mV;
  41.  
  42. void setup() {
  43.   Serial.begin(115200); // Initialize serial communication
  44.   // Initialize I2C
  45.   Wire.begin();
  46.   // Setup ADS1115
  47.   ads.setAddr_ADS1115(PRESSURE_SENSOR_I2C_ADDRESS); // Set I2C address 0x48
  48.   ads.setGain(eGAIN_TWO); // Configure for pressure range suitable
  49.   ads.setMode(eMODE_SINGLE); // Single shot mode
  50.   ads.setRate(eRATE_128); // 128SPS
  51.   ads.setOSMode(eOSMODE_SINGLE); // Single conversion
  52.   // Setup relay pin
  53.   pinMode(RELAY_PIN, OUTPUT); // Relay control pin
  54.   digitalWrite(RELAY_PIN, LOW); // Ensure relay off initially
  55.   // Initialize ADS1115
  56.   ads.init();
  57.   delay(100); // Wait for setup
  58. }
  59.  
  60. void loop() {
  61.   if (ads.checkADS1115()) { // Check connection
  62.     // Read pressure in mV from channel 0
  63.     pressure_mV = ads.readVoltage(0);
  64.     Serial.print("Pressure: ");
  65.     Serial.print(pressure_mV);
  66.     Serial.println(" mV");
  67.     // Convert voltage to pressure in bar (assuming voltage corresponds to pressure range)
  68.     // For example, if 0-6.144V covers 0-10 bar, then 1V = 1.63 bar
  69.     float pressure_bar = (pressure_mV / 1000.0) * (10.0 / 6.144); // Adjust as per sensor calibration
  70.     Serial.print("Pressure in bar: ");
  71.     Serial.println(pressure_bar);
  72.     // Control relay based on pressure
  73.     if (pressure_bar >= 1.0 && pressure_bar <= 3.0) {
  74.       digitalWrite(RELAY_PIN, HIGH); // Turn relay on
  75.     } else {
  76.       digitalWrite(RELAY_PIN, LOW); // Turn relay off
  77.     }
  78.   } else {
  79.     Serial.println("ADS1115 Disconnected!");
  80.     // Turn relay off if sensor disconnected
  81.     digitalWrite(RELAY_PIN, LOW);
  82.   }
  83.   delay(1000); // Wait 1 second before next reading
  84. }
  85.  
  86. /* END CODE */
  87.  
Advertisement
Add Comment
Please, Sign In to add comment