High-Tech Plumbing with 8-bit MCU
Plumbing and electronics seem like quite separate disciplines, but not in this era of technology. Jeff needed to control multiple valves from a distance, so he created a circuit that could be powered and controlled locally, while allowing an external controller to control and monitor it from afar. In this project article, he uses a tiny 8-bit Microchip PIC16 MCU and a latching relay.
Igenerally wouldn’t use the word “plumbing” in the same sentence as “electronics.” But it seems as though these days anything goes. The latest faucets have a touchless feature that uses IR (infrared) to determine whether objects are under the faucet spout, for hands-free control of the water flow. Of course, this only works if you’ve left the hot/cold knobs opened and preset to the temperature you want the water, buy hey, now you have a reason to buy more batteries!
The medical industry has been measuring and dispensing liquids electronically for many years. Peristaltic pumps are a great way to control and measure liquid movement. If you’ve got a hot water heating system in your home, then you probably have zone valves that handle the flow of hot water to selected areas. A zone valve requires energy, however, to remain in an open state, something we’ll talk more about in a bit.
A valve is a device that regulates, directs or controls the flow of a fluid by opening, closing or partially obstructing a passageway. The valve comes in many configurations, such as Gate, Globe, Check, Ball and Butterfly. The Check valve is usually self-controlling—that is, there is no external means of control. The flow, itself, will either push a plug of some kind toward or away from the seat, which opens or closes the flow (Figure 1) .
You are probably most familiar with the Globe valve. It is one of the most commonly used valves around your home. Most faucets use a Globe valve. It has a handle that screws a plug toward a seat to close-off the fluid supply (Figure 2) . You may have a handled Gate valve on the water pipe as it enters your house. This is normally used like an on/off switch. Guillotine-like, it chops through the passageway to block the flow (Figure 3) .
The final two valve types—Butterfly and Ball—are similar in nature. The handle of the Butterfly valve rotates a disk on its shaft. When the disk is turned in line with the flow, the fluid can pass. This type of valve was used in many carburetors to control the air/gasoline mixture to the engine. The handle of the Ball valve rotates a ball connected to its shaft. A hole in the ball allows fluid to pass until it is rotated 90 degrees, obstructing the passageway. The Ball valve, while a little more expensive than a globe valve, will not only outlast the Globe valve, but more importantly, will also allow a full unobstructed flow of water through it (Figure 4) .
MOTORIZED BALL VALVES
Where the control of liquid flow is required, a motorized Ball valve can eliminate the need for someone manually turning it on or off. In addition to remote actuation, some models offer feedback, so the status of the valve can be verified remotely. I’ve dealt with U.S. Solid, an American company that offers motorized Ball valves with several different wiring configurations.
There are four basic models: 2-wire, 2-wire auto-return, 3-wire and 5-wire. The 2-wire auto-return model closes automatically upon loss of power. It requires no power to remain closed, but requires continuous power to remain open. The other types of motorized Ball values require power while changing state, but no power to remain in that state.
The 3-wire valve uses a common and two separate wires to open or close the valve. When the valve reaches its requested state, the power is disconnected and the valve remains stationary and unpowered. The 2-wire and 5-wire Ball valves operate identically. The two power wires are polarized. This means that when the voltage is placed across the two wires, the valve will move in one direction, and when power is reversed, it will move in the other direction. The additional three (of five) wires are feedback on the state of the valve. One wire is a common, the second wire is connected to common when the valve is fully opened and a third wire is connected to common when the valve is fully closed. Like the other versions, power is disconnected when the valve reaches its destination. So, the valve requires no current once it reaches its stop.
For this project, I needed to control multiple valves from a distance, so I created a circuit that could be powered and controlled locally, while allowing an external controller to control and monitor it from afar. Since the two power connections would need to be swapped to reverse direction, I would need to use a full-bridge circuit or a DPDT (double-pole double-throw) relay. I chose to use the latter, because there is a benefit to using a latching-type relay, which is designed to remain in its present state without drawing any current.
A latching relay may have one or two coils associated with it. A single-coil latching relay requires the two power connections to be reversed to change the state of the relay, as with the 2-wire motorized Ball valve. The 2-coil latching relay has a common and two coil inputs. One sets the relay, and the other resets it, as in the 3-wire motorized Ball valve. Both types of latching relays require only a short pulse to flip the state of the relay. Power does not and should not be applied continuously.
The relay is DPDT, which allows the contacts to be wired such that the power supply connections are connected to the 2-wire Ball valve with opposite polarities, depending on the state of the relay. All we need to do is pulse the set or reset relay inputs to change the state of the relay. Because the Ball valves require 9VDC to 24VDC, I used a 24VDC supply to keep the current low. 24VDC is one of the common relay voltages, so I used separate transistors to pull the appropriate relay coil to ground when a current is applied to the transistor’s base. This is accomplished by two output bits of a microcontroller (MCU)—one to set the relay, and one to reset it.
The valve motor’s shaft has an attached lever that engages twin micro switches at each extent of the shaft’s travel (Figure 5). When the valve reaches its open state, one switch opens the power connection to the motor and the second switch connects the open feedback wire to the common feedback wire. At the opposite end of travel, when the valve reaches its closed state, one switch opens the power connection to the motor and the second switch connects the closed feedback wire to the common feedback wire.
You’ll note that the feedback is actually three states—the open state, the closed state and the in-between (moving) state, when neither of the output feedback switches are engaged. I read the feedback state using an analog input. With a pull-up resistor on this analog input and neither of the relay status outputs pulled to common, the input will be maximum. Each of the relay status outputs has a resistor connected in series to the analog input. When pulled to common, this resistor and the pull-up resistor on the input create a voltage divider. With a different resistor on each relay status output, its voltage divider produces different voltages. Therefore, the analog input has a different voltage presented according to the state of the relay outputs.
As feedback from this module, it is a bit more difficult to represent three states on a single output pin. The MCU that I used is Microchip Technology’s 8-bit PIC16F18313 MCU. This MCU has a 5-bit DAC that can be used. Other options include a PWM (pulse width modulation) output, or being satisfied with just two states. With two-state feedback, the state of the valve only changes if it has fully closed or opened. With a PWM output, you can set the PWM on/off time as 10%, 50% or 90% to indicate the three states. PWM frequency must be known for the external monitoring device to be able to measure the times. DAC output can simply be set to 100%, 50% or 0%, and the external monitoring device either monitors with an analog input or is satisfied with a two-state status. Note that the 50% output may read as a high or low, depending on how your digital input sees 50% voltage!
To get the latching relay to operate, we need to know the specifications on the switching times. The datasheet for an Omron G6S-type, double-coil, latching relay says a state change requires a minimum of a 10ms pulse width. Timer0 is set up for 10ms. Because this is the main purpose of the MCU, an interrupt is not needed for this function. Therefore, I use a delay loop and just wait for Timer0 to complete its task. (The Timer0 module is an 8-bit timer/counter that is included with all 8-bit PIC MCUs.)
The set and reset tasks are called based on two inputs. The first input is the controlling input from the external device, and has an internal pull-up enabled. Normal operation consists of a logic high indicating an open valve, and a logic low for a closed valve. A CFG input adds an inverted logic option to the operation. This forces the states of both the external control input and external valve status output to use inverted logic. The CFG input can easily change how the valve functions, with respect to its input request and how the valve feedback is reported to the external monitor.
As discussed earlier, the valve feedback is indicated by micro switches that connect the open and closed output wires to common when the valve reaches the open and closed positions. Our circuit adds red and green LEDs to these connections to give the user visual feedback on the state of the valve. These connections also have series resistors connected to the analog input of our micro. It’s important to note that the open and closed valve feedback outputs have resistors connecting them, which will interfere with their off states. A couple of diodes prevent this interaction.
I’ve been a promoter of tiny MCUs since the first ones were introduced in the 90s. With improvements (to all MCUs) of flash for program area and stable internal oscillators, these little guys are easier than ever to use. Many have on-board debugging capability and complete control of the pin routing and configuration. This makes them a great place to start if you’ve been itching to try programming in assembly language. While this isn’t a requirement, the support IDEs enable development in assembly and other higher-level languages.
As shown in the schematic (Figure 6), this project uses both analog and digital I/O. With two pins dedicated to power and ground, six pins are left over for your use. I’ve already discussed four pins used by this circuit (Set, Reset, Invert and Feedback). Now we can look more closely at the two pins used as external control (HCAM) and monitoring of this circuit (HCAM_FB) and a bit about assigning I/O.
Although I’ve chosen to use logic states as the interface with an external controller, these pins could be redefined as any kind of serial interface. One advantage of a serial interface is that any amount of information can be traded (not just logic levels). A disadvantage might be requiring the external controller to consume potentially valuable resources in support of a minor I/O function. It is not until you wish to handle a multitude of Ball valves that the 2-wire communications channel might make sense to talk with any number of devices on the same pair!
To keep this project simple, I used a logic interface. This way, the project can easily tested, with no more than a switch for the external control, and a few LEDs as the external monitor. The MCU’s six I/O pins are the first bits of normal 8-bit GPIO port. Three of these pins are used to program the device. Of those, two are also used as the debug port. The third, *MCLR, can only function as a digital input, whereas all other pins can be either an analog input or a digital I/O. So, you see, some care is necessary in assigning your circuitry to the MCU’s six pins.
I’ve shown in past articles how you might temporarily assign functions that will normally be connected to the debug pins to other I/Os, so that function can be debugged without interference with the debugging process. I assigned the CFG jumper to the input-only pin *MCLR. The jumper, if in the grounded position, must be removed for programming to allow the *MCLR input to function properly. You may notice that the LEDs flicker slightly when they are connected to the debug/programming inputs. If you design some circuit that drives either of the debug/programming inputs, you must add a jumper. The jumper lets you temporarily disconnect the driver, so it can’t prevent the debug/programming of the device the circuit.
PPS INPUT REGISTERS
For those pins that will be used as inputs (either analog or digital), you may redefine which peripheral each one is routed to. This is done through the peripheral pin-select (PPS) input registers. There is a register for each potential input, for instance the UART input RX. The default pin for RX is RA1. This function can be rerouted to any input pin by assigning register RXPPS, a value related to that pin number, RA0-RA5 = value 0-5.
Redefining a peripheral’s output is handled a bit differently. Each output pin has a PPS register associated with it, in this case RAxPPS, where x = pin number or value 0-5. Because RA3 is an input-only pin, there is no RA3PPS register! Each peripheral output has its own designated value. Refer to the PIC16F18313 datasheet for this list . By writing this value into the output-pin register, you reroute the peripheral’s output to your particular pin.
Now you may be thinking, this is a dangerous stunt! An errant program may easily write a value to one of these locations, breaking the routing so the circuit is no longer connected to that peripheral. Well, there is a fix for that possibility, even though a properly coded program will never do this. These registers are protected from being changed by some special coding instructions that must be used to lock or unlock the ability to write to them. There is also a lock bit that allows these to be written to just once on a reset or power-up.
With the Ball valves operating range from 9VDC to 24VDC, you may wish to use this circuit and substitute a different power supply and relay. This design is for the 24VDC power supply, and so with the circuit powered you will find that the 24VDC is applied to relay contacts outputs (J4, pins 4 and 5). You should be able to invert this state using the jumper JP1. Note the labels NO (Normally Open) and NC (Normally Closed). Moving this jumper should cause a pulse to be produced by the MCU. There are separate pulse outputs for the set and reset states of the relay. For the relay to change states, it must see a pulse of at least 10µs applied to the opposite state’s input. When the relay is in its NO state, only a pulse on the “set coil” will close it. When the relay is in its NC state, only a pulse on the “reset coil” will open it. You can check the pulse width with a scope, as shown in Figure 7.
It is important to note that the polarity of the voltage out of the relay contacts should switch with the relay’s state. The polarity of the voltage applied to the Ball valve’s motor determines the direction of the valves rotation, this movement is 90 degrees between its open and closed states. When the valve reaches an extreme, twin micro switches are activated to remove power and to connect the common to a feedback output. You can check this without a valve connected by shorting the common (J4, pin 1) to one of the feedback outputs (J4, pins 2 or 3). Grounding pin 2 will turn on the red LED and cause voltage divider R6/R7 to apply approximately 0.5V to the analog input pin 7. Grounding pin 3 will turn on the green LED and cause voltage divider R6/R9 to apply approximately 2V to the analog input pin 7. With neither pin connected to common, R6 will pull-up the analog pin to 5V.
This circuit is meant to be operated remotely through the 3-wire header, J2. The control input (CFG) has an internal pull-up on it. Therefore, the state changes when this input is connected to ground via JP1. Shorting JP1.1 and JP1.2 should change the state of the relay (Ball valve) and vice-versa. If you’ve tested the valve feedback by shorting the feedback outputs to common, then you should see a change on the HCAM-FB output. For this revision, the HCAM-FB only reflects the last state of the Ball valve; it remains in the last state until a new state has been detected. Although the analog input can detect that the valve is not in either state (normally while it’s moving), this information is not available to the external circuit. As mentioned earlier, this output could be a PWM or DAC signal, where specific outputs would represent different states or a communications channel, such as I2C.
The project is installed on a Ball valve plumbed into a water line is shown in Figure 8. Using an MCU to handle local control and monitoring of any device has certain advantages. It makes control of the device available locally, even if the external controlling device is off-line. The control input can be wire-ORed (parallel) with multiple external controllers, allowing any one to close the valve and still provide feedback to all. The use of a latching relay has the benefit of requiring no power in the idle mode. A simple set/reset pulse can change switch states for minimum power requirements. Local LEDs provide instant feedback about the state of the device. This project was designed specifically for motorized Ball valves, but it has potential uses for other devices, including door locks and remote power control.
You may think that control over the flow of water has few applications, but control can be critical. My friend Tony went away on vacation and came home to a flooded house. The toilet connection in his master bath gave way, and after a full week of endless flow, it flooded his bathroom and the two floors below. As part of a home control system, a monitor on water usage could have turned off water to the house if it found continuous usage exceeding some timeframe.
We’ve all visited a restroom and found all facilities operated without the need for physical touch. The flush and faucet valves are controlled by IR sensors. Recently, I’ve seen ads for these devices available for both residential and business places. The latest is telling Alexa to “Measure out 1 liter of water” through the kitchen faucet! With COVID-19 still around, there seems to be a push for the touchless environment. Too much to think about, so little time.
 Microchip PIC16F18313 MCU datasheet https://ww1.microchip.com/downloads/en/DeviceDoc/40001799F.pdf
Figures 1-4: www.plumbingsupply.com
Hammond Manufacturing | www.hammondmfg.com
Microchip Technology | www.microchip.com
Omron Electronic Components | www.components.omron.com
PUBLISHED IN CIRCUIT CELLAR MAGAZINE • MARCH 2021 #368 – Get a PDF of the issueSponsor this Article
Jeff Bachiochi (pronounced BAH-key-AH-key) has been writing for Circuit Cellar since 1988. His background includes product design and manufacturing. You can reach him at: firstname.lastname@example.org or at: www.imaginethatnow.com.