Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Pressure Relay
- - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
- - Source Code created on: 2025-12-17 01:04:44
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Allumer relais lorsque presion entre 1 et 3 bar */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- // System Requirements: "Allumer relais lorsque presion entre 1 et 3 bar".
- // Use ADS1115 to measure pressure and control relay accordingly.
- // Libraries
- #include <Wire.h>
- #include <DFRobot_ADS1115.h>
- // Constants
- const uint8_t PRESSURE_SENSOR_I2C_ADDRESS = 72; // 0x48
- const uint8_t RELAY_PIN = D0; // GPIO 16
- // Instance of ADS1115
- DFRobot_ADS1115 ads(&Wire);
- // Variable to hold pressure value
- int pressure_mV;
- void setup() {
- Serial.begin(115200); // Initialize serial communication
- // Initialize I2C
- Wire.begin();
- // Setup ADS1115
- ads.setAddr_ADS1115(PRESSURE_SENSOR_I2C_ADDRESS); // Set I2C address 0x48
- ads.setGain(eGAIN_TWO); // Configure for pressure range suitable
- ads.setMode(eMODE_SINGLE); // Single shot mode
- ads.setRate(eRATE_128); // 128SPS
- ads.setOSMode(eOSMODE_SINGLE); // Single conversion
- // Setup relay pin
- pinMode(RELAY_PIN, OUTPUT); // Relay control pin
- digitalWrite(RELAY_PIN, LOW); // Ensure relay off initially
- // Initialize ADS1115
- ads.init();
- delay(100); // Wait for setup
- }
- void loop() {
- if (ads.checkADS1115()) { // Check connection
- // Read pressure in mV from channel 0
- pressure_mV = ads.readVoltage(0);
- Serial.print("Pressure: ");
- Serial.print(pressure_mV);
- Serial.println(" mV");
- // Convert voltage to pressure in bar (assuming voltage corresponds to pressure range)
- // For example, if 0-6.144V covers 0-10 bar, then 1V = 1.63 bar
- float pressure_bar = (pressure_mV / 1000.0) * (10.0 / 6.144); // Adjust as per sensor calibration
- Serial.print("Pressure in bar: ");
- Serial.println(pressure_bar);
- // Control relay based on pressure
- if (pressure_bar >= 1.0 && pressure_bar <= 3.0) {
- digitalWrite(RELAY_PIN, HIGH); // Turn relay on
- } else {
- digitalWrite(RELAY_PIN, LOW); // Turn relay off
- }
- } else {
- Serial.println("ADS1115 Disconnected!");
- // Turn relay off if sensor disconnected
- digitalWrite(RELAY_PIN, LOW);
- }
- delay(1000); // Wait 1 second before next reading
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment