Introduction: Control a Solenoid With a D1 Mini Micro-controller and Push Button

About: Arduino, Alexa, etc.

In this tutorial I show you how to control a pull type solenoid switch with a push button using the D1 mini.

Solenoid switches have a wide range of uses from water/air valves to electrical relays. A pull type solenoid could be used as part of a locking mechanism with a button used to retract the pin from a barrel or frame.

Step 1: Parts Required

The parts you will require are as follows:

It is important that the solenoid used for this project is rated for the D1 Mini's voltage supply and also that its power requirements do not exceed the maximum available. The Solenoid used here is rated at 4.5V and uses a maximum power of 110mA so should be perfect.

Step 2: Schematic

Follow the schematic shown

Step 3: Arduino Sketch

First define the 2 pins used for the solenoid and switch respectively.

#define SOLENOID D5
#define switchPin D3

In the setup declare the solenoid pin as output, the switch pin and input_pullup

void setup()
{
Serial.begin(9600);
pinMode(SOLENOID,OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
}

Loop and check status of the switch pin, setting the solenoid pin to high or low depending.

void loop() {
int btn_Status = HIGH;
btn_Status = digitalRead (switchPin);
if (btn_Status == LOW)
{
digitalWrite(SOLENOID,HIGH); //change valve state
}
else
{
digitalWrite(SOLENOID,LOW); //change valve state
}
}

The sketch for this tutorial can be downloaded here.

Step 4: Test

Upload your sketch to the D1 mini and test by pressing and releasing the push button. The pin should retract when the button is held and extend when it is released.

Further details for this and more projects and tutorials may be found at cabuu.com

Make it Move

Participated in the
Make it Move