BME 553 IOT Lab
Program -06
Object::
To interface sensor with Arduino/Raspberry Pi and write a program to
turn ON/OFF Solenoid valve when sensor data is detected.
Outcome:
Able to control Solenoid valve with help of microcontroller and sensors.
Components Required
Arduino UNO
Solenoid valve
5 Volt Single-channel relay module
USB cable for unloading the code
Jumper wires
12-volts DC power supply
breadboard
What is a Solenoid Valve
Just like the valves used in your homes for controlling the flow of the water
a solenoid valve is used to serve the same purpose.
The main difference between an ordinary valve and a solenoid valve is that
a solenoid valve can operate automatically.
You can program it to work automatically according to the given instructions. you
can also make Smart Irrigation System
In this project, we use an Arduino UNO microcontroller for controlling the
solenoid valve.
Mainly it is used in the industries. Inside the solenoid valve,
there is a coil of insulated copper wire which turns into an electromagnet
when you supply the power to it.
The valve is open for the flow of liquid when the coil produces a magnetic
field around it and cuts the flow when there is no power.
The inner mechanism returns to its home position in the absence of
electricity.
A solenoid valve looks like this. As you can see at the upper part there is a
coil inside the part which works on electromagnetism.
How Does it Work?
The solenoid valve is used for controlling the flow of any liquid. In this article, we
show you the interfacing of the solenoid valve with Arduino and you can make
your own projects from it.
We use a relay module here if you don’t know how to use a relay module with
Arduino then check it first
The solenoid valve can open and close automatically at a regular time interval of
1 second. Another one is the solenoid lock which is used in the lock system it
also operates with a program.
You can change it by modifying the code. Just complete the circuit and then
upload the given code to the Arduino UNO.
Code
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
So first we will need a variable for the Arduino pin:
int solenoidPin = 4; //This is the output pin on the Arduino we are using
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
Next we need to set the Arduino pin to act as an output:
int solenoidPin = 4; //This is the output pin on the Arduino we are using
void setup() {
// put your setup code here, to run once:
pinMode(solenoidPin, OUTPUT); //Sets the pin as an output
}
void loop() {
// put your main code here, to run repeatedly:
}
Ok, now all of the pins are set to outputs. Next we can write some code in the loop to switch
a relay on and off:
int solenoidPin = 4; //This is the output pin on the Arduino we are using
void setup() {
// put your setup code here, to run once:
pinMode(solenoidPin, OUTPUT); //Sets the pin as an output
}
void loop() {
// put your main code here, to run repeatedly:
Code:::
digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
delay(1000); //Wait 1 Second
digitalWrite(solenoidPin, LOW); //Switch Solenoid OFF
delay(1000); //Wait 1 Second
}
Upload The Code And Test
Now that all of the code has been written it can be uploaded to your Arduino!
Click “Upload” button in the top left corner of the Arduino IDE and it should
upload without any issues. After a few seconds the solenoid will start opening and
closing