Hello, I have been tasked with creating an Arduino code that we can use to open and close 4-5 different valves independently remotely from the Arduino dashboard. I have very limited coding experience and have been trying to piece together parts of code I have found on the web, but I do not believe I am going down the right path. I will include a list of specifics down below but if someone can help me write the code that would be much appreciated.
-the valves we are using are 24vac
-we were planning on using a ESP32 NodeMCU board but can switch if needed
-We believe that we will have to use a relay system between the board and the valve
Welcome to the Arduino forum. A good start for the project, but continue describing what you want to create.
The use of relays for AC is mandatory. How often will the valves operate? How much time will they be open? How many valves will you have and how many will be open at one time? How far will a valve be from the relay?
Have you decided on which Arduino board you will use? Have you decided on how you will power the Arduino and the relays?
the valves will be operated anywhere between once a week and daily. Just depends on which tests need to be ran. I think there will be 5 valves and the amount open at one time once again depends on which tests need ran. We have the power figured out, already have sensors hooked up to Arduino boards with power being supplied from an outlet hooked up to a 24V connector and a 5V connector running to the ESP32-30 pin boards. We do not know for sure what board we want to use for this project and are open to suggestions.
With the board you have chosen, how much testing have you done for any of the sensors and any of the relays? You must develop your project one step at a time for each type of sensor and relay.
if you try a web search (amazon, ebay, etc) for ESP32 relay board you will get plenty of links to PCBs with onboard ESP32 microcontroller and 4/6/8 relays etc - this would save you a lot of wiring and possible poor connections
in post 1 you mention control from a dashboard
will your board have access to a local WiFi?
if not the ESP32 could run an access point with web server - a web client could access the web server (e.g. from a mobile phone) to view sensor data and control relays
if you are planning to operate in industry recommend you use modules designed for such environments with suitable power supplies
recommend you edit your post - select each the code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and the forum software will display it correctly.
#include "arduino_secrets.h"
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/a64abdf4-f7e2-44c4-8ef3-5d0bc83f3066
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
bool valveOpen;
CloudTemperature outsideAirTemp;
CloudTemperature waterTankTemp;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
#include "OneWire.h"
#include "DallasTemperature.h"
//define the pin that all the temp sensors are connected to
#define ONE_WIRE_BUS 17
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress sensor1 = { 0x28, 0xBB, 0xF6, 0x45, 0xD4, 0x70, 0x45, 0xF2 }; //outside air temp
DeviceAddress sensor2 = { 0x28, 0x6C, 0x62, 0x80, 0xE3, 0xE1, 0x3C, 0x64 }; //tank water temp
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
pinMode(14, OUTPUT);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Your code here
delay(500);
//obtains water temp at heat pump outlet
sensors.requestTemperatures();
outsideAirTemp = sensors.getTempF(sensor1);
waterTankTemp = sensors.getTempF(sensor2);
if (valveOpen) {
// If the boolean is true
//HIGH: COM and ON has continuity
//LED on relay lights up
//chilling mode: will cool water in the tank
digitalWrite(14, HIGH);
} else {
// If the boolean is false
//LOW: NC and COM have continuity
//LED is off
//ground loop mode: water runs through ground loop
digitalWrite(14, LOW);
}
}
/*
Since ValveOpen is READ_WRITE variable, onValveOpenChange() is
executed every time a new value is received from IoT Cloud.
*/
void onValveOpenChange() {
// Add your code here to act upon ValveOpen change
}
@farmafield as an idea instead of a function for each valve you should consider just one or perhaps two functions that will operate, open or close, any of the valves.
You pass arguments to the function defining the valve number and the direction.
I don't know how critical it is but you may also want to consider feedback from each valve to indicate the state of the valve which could be used in the logic of the previously mentioned functions. This could be something as simple as a couple of limit switches, a single byte can contain the state 8 switches for example.
@farmafield Paul is right you have to show us what you have already, I would think most important is how and what values you are receiving at the Arduino there has to be a protocol.
This is not the answer to your problem it is an idea to play with, a byte with 8 bits can control 8 valves depending if the bit is 1 or 0, the bits have values of 1,2,4,8,16,32,64,128 so these values will toggle the respective valve(s) on or off. It might be hard to visualize this so here is a snippet to run in the serial monitor that I hope will help you understand the concept.
Entering one of the above values will toggle a bit on or off, some of the snippet is drawn from previous forum code examples.
int input;
int output;
void setup() {
Serial.begin(115200);
}
void valve_control(int input){
Serial.print("Binary input = ") ;
for (int bit = 7; bit >= 0; bit--)
{
Serial.print(bitRead(input, bit));
}
Serial.println();
output=output ^ input ;
Serial.print("Binary output = ") ;
for (int bit = 7; bit >= 0; bit--)
{
Serial.print(bitRead(output, bit));
}
Serial.println();
}
void loop() {
while (Serial.available () > 0){
input=Serial.parseInt();
valve_control(input);
}
}