Quantcast
Channel: MSP430 Technical Forums
Viewing all articles
Browse latest Browse all 2077

Auto Pond Filler Project, Request for Code Guidance/Help

$
0
0

We have a koi pond and I've always wanted to create an auto fill system. I've much more proficient in hardware than I am in software so I'm hoping this project peaks some curiosity and people are willing to help. I've been doing some research and have some ideas so I thought I'd start with an overview and go from there.

 

Project Components:

1)    EHCOTECH DDT-ML-4.5VDC latching solenoid valve

a)    I purchased this valve a long time ago because it was latching which means it won’t require constant power which lends to being able to be run off of solar

B)    In order to operate the valve you need to send a ≥ 50ms pulse. Positive polarity will open the valve while opposite polarity will close the valve.

c)    Operates from 5V which is lower than the standard 12V controllable valves

2)    Adafruit Flow Meter Liquid Flow Meter

a)    Operates from 5V and sends pulses when liquid is flowing

B)    Flow rate pulse characteristics: Frequency (Hz) = 7.5 * Flow rate (L/min)

c)    Pulses per Liter: 450

3)    DRV8838EVM DRV8838 Low voltage H-Bridge Driver

a)    This is needed to drive the solenoid valve and I have it so it makes the project easier to start.

B)    The nice thing about this development board is that it has an onboard 3.3V regulator for the MSP430G2131 onboard and only requires VM (5V from the solar powered battery)

c)    The board also exposed some GPIO pins of the MSP430G2131:

d)      P1.5, P1.6, P1.7, P2.7, and P1.3 is accessible from a pin header.

4)    ESP8266 Module Espressif WiFi Module

a)    My plan is to use this to expose control of the valve as well as water flow rate to a web page.

B)    I’m thinking communication between the ESP8266 and the MSP430 could be with I2C

Project Overview Ideas:

1)    Connect a button to P1.3 (note the DRV8838EVM has a pull-down on this pin as opposed to the Launchpad boards which have a pull-up). A long press of the button opens the valve while a short press closes the valve.

2)    In order to have the ESP8266 also control the valve I’m thinking of using P1.5 for the I2C clock (SCLK) and P1.7 for the I2C data (SDA). This way the valve can be controlled while you’re at the pond or by mobile phone/tablet if that is readily available.

3)    The flow meter can be connected to P1.6

4)    Since the DRV8838 has a simple 2-pin interface, you only need to toggle P1.2 (PH) high for a valve close operation and P1.2 low for an open. Since we are only controlling a valve we don’t need to use the braking function of the DRV8838 so P2.6 (EN) can always be high.

5)    In order to put the DRV8838 in sleep mode to conserve power we need to pull that pin low. Fortunately the EVM has that pin exposed on a header and we can use P2.7 to control the sleep function.

Program Flow Ideas:

1)    My idea is to keep everything in low power mode since the fill operation is only something that is done maybe a few times per week. The button connected to P1.3 can be interrupt enabled but I’m not sure if the I2C communication can be so the ESP8266 could also wake up the system

2)    So if a fill request occurs the following needs to happen:

a)    If the request comes from the button it needs to be debounced and checked that it is a long press. Need to handle the I2C communication if the ESP8266 is making the request.

B)    Once the request is confirmed you next have to turn on the valve by sending the 50ms pulse

c)    Once the valve is open you need to start monitoring the flow meter to both count the pulses and calculate the flow and report this to the ESP8266 (maybe current flow rate and total water added). Ultimately I’d like to add a water sensor to control when to turn off the water but for now we’ll just say it has to run for 5 minutes.

3)    Need to prevent the case that the valve never gets a shut command so perhaps add that into the WDT timer and make that the first command that happens on reboot.

Code Ideas:

 

1)    I’ve seen code that uses the WDT for switch debounce. That seems like a good idea bundled with an interrupt on Port1, something like this:

#pragma vector = WDT_VECTOR
__interrupt void wdt_isr(void)
{
 if (Pressed & S1) // Check if switch 1 is pressed
 {
 if (++PressCountS1 == 47 ) // Long press duration 47*32ms = 1.5s
 {
 P1OUT |= BIT0; // Turn on the P1.1 LED to indicate long press
 }
 }
 
 P1IFG &= ~BIT3; // Clear the button interrupt flag (in case it has been set by bouncing)
 P1IE |= BIT3; // Re-enable interrupt for the button on P1.3
}

Then the Port 1 interrupt could be something like this:

#pragma vector=PORT1_VECTOR
__interrupt void PORT1_ISR(void)
{
 if (P1IFG & BIT3)
 {
 P1IE &= ~BIT3; // Disable Button interrupt to avoid bounces
 P1IFG &= ~BIT3; // Clear the interrupt flag for the button
 if (P1IES & BIT3)
 { // Falling edge detected
 P1OUT |= BIT0; // Turn on P1.0 red LED to indicate switch 2 is pressed
 Pressed |= S2; // Set Switch 2 Pressed flag
 PressCountS2 = 0; // Reset Switch 2 long press count
 }
 else
 { // Rising edge detected
 P1OUT &= ~(BIT0+BIT6); // Turn off P1.0 and P1.6 LEDs
 Pressed &= ~S2; // Reset Switch 2 Pressed flag
 PressRelease |= S2; // Set Press and Released flag
 }
 P1IES ^= BIT3; // Toggle edge detect
 IFG1 &= ~WDTIFG; // Clear the interrupt flag for the WDT
 WDTCTL = WDT_MDLY_32 | (WDTCTL & 0x007F); // Restart the WDT with the same NMI status as set by the NMI interrupt
 }
 else {/* add code here to handle other PORT1 interrupts, if any */}
 if (P1IFG & FLOW) {                           // Flow ometer pulse detected ?
    flowCount++;                                // Increment flow counter
    P1IFG &= ~FLOW;                             // Clear interrupt flag
  }
}

These are slightly modified code snippets from what I've found during my research. I need to add in code for the 50ms pulses and my thoughts are to use the timer to trigger an interrupt after 50ms which can then modify the EN pin (P2.6) to stop the valve as well as put PH (P1.2) at high to close the valve.

 

I'm not sure of the fail safes and how to implement them nor the best way to handle the I2C code with interrupts.

 

Hopefully this wan't too long winded and I've peaked some curiosity from some people who are willing to help this project along.

 

Thanks in advance for anyone's help!

 

George


Viewing all articles
Browse latest Browse all 2077

Trending Articles