Help how to Control Solenoid...

Hi I have a arduino project a newbie one. So I wanted to build a door lock that is controlled by Bluetooth module(HC-05) to the arduino. I found this link...

https://create.arduino.cc/projecthub/taifur/arduino-and-android-based-bluetooth-password-lock-a3b941?ref=search&ref_id=ARDUINO%20DOOR%20LOCK&offset=1

And I have used his app..
But I wonder how to use his code to my circuit design???
I wanted to control the solenoid..

What changes do i have to make to his code??

Thanks in advance. :slight_smile:

/* Athor: Md. Khairul Alam Date: 1 September, 2015 This program is for password protected smart door lock */ String inputString = ""; String command = ""; String value = ""; String password = "arduPi"; // this is the password for opening and closing your door // you can set any password you like using digit and symbols boolean stringComplete = false; int motorPin1 = 10; // pin 2 on L293D IC int motorPin2 = 11; // pin 7 on L293D IC int motorEnablePin = 9; // pin 1 on L293D IC int Speed = 100; void setup(){ //start serial connection Serial.begin(9600); // baud rate is 9600 must match with bluetooth //The String reserve() function allows you to allocate a buffer in memory for manipulating strings. inputString.reserve(50); // reserve 50 bytes in memory to save for string manipulation command.reserve(50); value.reserve(50); boolean stringOK = false; pinMode(motorPin1, OUTPUT); pinMode(motorPin2, OUTPUT); pinMode(motorEnablePin, OUTPUT); } void loop(){ // if arduino receive a string termination character like \n stringComplete will set to true if (stringComplete) { //Serial.println(inputString); delay(100); // identified the posiion of '=' in string and set its index to pos variable int pos = inputString.indexOf('='); // value of pos variable > or = 0 means '=' present in received string. if (pos > -1) { // substring(start, stop) function cut a specific portion of string from start to stop // here command will be the portion of received string till '=' // let received string is open=test123 // then command is 'open' command = inputString.substring(0, pos); // value will be from after = to newline command // for the above example value is test123 // we just ignoreing the '=' taking first parameter of substring as 'pos+1' // we are using '=' as a separator between command and vale // without '=' any other character can be used // we are using = menas our command or password must not contains any '=', otherwise it will cause error value = inputString.substring(pos+1, inputString.length()-1); // extract command up to \n exluded //Serial.println(command); //Serial.println(value); // password.compareTo(value) compare between password and value string,if match return 0 if(!password.compareTo(value) && (command == "OPEN")){ // if password matched and command is 'OPEN' than door should open openDoor(); // call openDoor() function Serial.println(" OPEN"); // sent open feedback to phone delay(100); } else if(!password.compareTo(value) && (command == "CLOSE")){ // if password matched and command is 'CLOSE' than door should close closeDoor(); Serial.println(" CLOSE"); // sent " CLOSE" string to the phone delay(100); } else if(password.compareTo(value)){ // if password not matched than sent wrong feedback to phone Serial.println(" WRONG"); delay(100); } } // clear the string for next iteration inputString = ""; stringComplete = false; } } void serialEvent() { while (Serial.available()) { // get the new byte: char inChar = (char)Serial.read(); //Serial.write(inChar); // add it to the inputString: inputString += inChar; // if the incoming character is a newline or a carriage return, set a flag // so the main loop can do something about it: if (inChar == '\n' || inChar == '\r') { stringComplete = true; } } } void openDoor(){ digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, LOW); digitalWrite(motorEnablePin, HIGH); // use following line if you want to change speed, then use millis() instead if delay() //analogWrite(motorEnablePin, Speed); delay(1500); digitalWrite(motorEnablePin, LOW); // off motor } void closeDoor(){ digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, HIGH); digitalWrite(motorEnablePin, HIGH); //analogWrite(motorEnablePin, Speed); delay(1500); digitalWrite(motorEnablePin, LOW); }

Looks like you need to delete all the motor commands in your code and set a digital pin to OUTPUT for controlling the solenoid. The solenoid can be controlled with a relay or FET. All you do is set the pin HIGH or LOW to turn the solenoid on/off. (Unless you're using a PNP transistor or something with reversed logic).

Check out this tutorial on how to control a solenoid with Arduino.

But I wonder how to use his code to my circuit design???

That depends on your mysterious "circuit design". That code expects there to be a motor controller connected to the Arduino, with the motor connected to the controller.

That code runs the motor at full speed. That should be fine for your solenoid. Just connect it like it was a motor.

androidfanboy:
Looks like you need to delete all the motor commands in your code and set a digital pin to OUTPUT for controlling the solenoid. The solenoid can be controlled with a relay or FET. All you do is set the pin HIGH or LOW to turn the solenoid on/off. (Unless you're using a PNP transistor or something with reversed logic).

Check out this tutorial on how to control a solenoid with Arduino.

Can you give me the full code for my circuit sir? I really need some help.I think the code from that site. Is to much to my circuit.

Maybe the code can be shorten??

You don't have a circuit. Maybe when you do I can help more.

androidfanboy:
You don't have a circuit. Maybe when you do I can help more.

circuit is on attachment sir :slight_smile:

Ok, I see it now.

First of all, the schematic shows RX of the HC-05 going to the Arduino RX, which is wrong. RX goes to TX and vice versa.

Secondly, it shows the 12V going to the 5V pin, which will certainly try something. You need to connect it to VIN, not VCC.

All you need to do to control the solenoid via a transistor is set the pin to high or low (depending on the type of traffic being used). Pretty simple.

I get it now, Sorry for the circuit , my fault.

But now I understand how to set the pin 13 only to high and low ??

Can i delete those other pins??? i just need pin 13 to control the solenoid right?

Yes you only need pin 13. You can delete the others.

Pin 13 is connected to an onboard LED that flashes during bootup.
Your door lock could rattle/unlock during bootup if you use that pin.
Better use a different pin, e.g. pin 12.

The solenoid is connected between +12volt and the base of the TIP120.
Should be between +12volt and collector.
R1 goes to the base.

The BT module needs a level shifter on it's RX pin if you want it to last.
It's a 3.3volt device (powered by 5volt).

Pins 0 and 1 are also used by the USB<>Serial chip.
Maybe better to use SoftwareSerial on two different pins.
Leo..

Q1 is completely wrong in your circuit. Copy the correct way from: Arduino Playground - HomePage
(except use a 220 ohm base resistor for a 2N2222 or other non-darlington device, 1k for a darlington is fine)

Hi,
This is what you should have;
NOTE: DO NOT CONNECT 12V to the Vcc or 5V pin of the UNO, you need to connect it to the Vin pin.

Check datasheet for pinouts of TIP120.

Tom... :slight_smile:

Wawa:
Pin 13 is connected to an onboard LED that flashes during bootup.
Your door lock could rattle/unlock during bootup if you use that pin.
Better use a different pin, e.g. pin 12.

The solenoid is connected between +12volt and the base of the TIP120.
Should be between +12volt and collector.
R1 goes to the base.

The BT module needs a level shifter on it's RX pin if you want it to last.
It's a 3.3volt device (powered by 5volt).

Pins 0 and 1 are also used by the USB<>Serial chip.
Maybe better to use SoftwareSerial on two different pins.
Leo..

Hi sir , can i use the 12 pin to control the solenoid? what changes do i need to do?

TomGeorge:
Hi,
This is what you should have;
NOTE: DO NOT CONNECT 12V to the Vcc or 5V pin of the UNO, you need to connect it to the Vin pin.

Check datasheet for pinouts of TIP120.

Tom... :slight_smile:

Thanks for the Circuit sir it really a big help :slight_smile:

kalilinux25:
Hi sir , can i use the 12 pin to control the solenoid? what changes do i need to do?

androidfanboy told you in post#1 to "delete all the motor commands in your code and set a digital pin to OUTPUT for controlling the solenoid."

YOU mentioned pin13 in post#7.

I get the impression that you don't know what you're doing, and are just copying code that you don't understand.
Maybe because you skipped the simple learning stuff.

I suggest you put it all aside, and go through some of the basic examples that come with the IDE.
Then you will understand how to name a pin, and make it an output.

const byte solenoidPin = 12; // pin for solenoid transistor

void setup() {
  pinMode(solenoidPin, OUTPUT); // make the solenoid pin an output
}

Leo..

Wawa:
androidfanboy told you in post#1 to "delete all the motor commands in your code and set a digital pin to OUTPUT for controlling the solenoid."

YOU mentioned pin13 in post#7.

I get the impression that you don't know what you're doing, and are just copying code that you don't understand.
Maybe because you skipped the simple learning stuff.

I suggest you put it all aside, and go through some of the basic examples that come with the IDE.
Then you will understand how to name a pin, and make it an output.

const byte solenoidPin = 12; // pin for solenoid transistor

void setup() {
  pinMode(solenoidPin, OUTPUT); // make the solenoid pin an output

}


Leo..

do i need to remove the delay also sir under the mortor pins?

Wawa:
androidfanboy told you in post#1 to "delete all the motor commands in your code and set a digital pin to OUTPUT for controlling the solenoid."

YOU mentioned pin13 in post#7.

I get the impression that you don't know what you're doing, and are just copying code that you don't understand.
Maybe because you skipped the simple learning stuff.

I suggest you put it all aside, and go through some of the basic examples that come with the IDE.
Then you will understand how to name a pin, and make it an output.

const byte solenoidPin = 12; // pin for solenoid transistor

void setup() {
 pinMode(solenoidPin, OUTPUT); // make the solenoid pin an output
}



Leo..

do i need to delete the delay code sir under motor pins?
I've remove the motorEnable pin sir?

void openDoor(){
  digitalWrite(solenoidPin, HIGH);
  // use following line if you want to change speed, then use millis() instead if delay()
  //analogWrite(motorEnablePin, Speed);
  delay(1500);  
  digitalWrite(motorEnablePin, LOW); // off motor
}

The delay(1500) in the previous motor code was to make the motor run for 1.5s, then turn off. If you need your solenoid to run for XX amount of time, then change the delay. You will probably need it.

androidfanboy:
The delay(1500) in the previous motor code was to make the motor run for 1.5s, then turn off. If you need your solenoid to run for XX amount of time, then change the delay. You will probably need it.

what is the used of motor enable code sir?

/* Athor: Raven P. Tenepre
 *  ThesisProject : Bluetooth Controlled Door Lock
   This program is for password protected smart door lock */
String inputString = "";
String command = "";
String value = "";
String password = "1234"; // this is the password for opening and closing your door
                            // you can set any password you like using digit and symbols
boolean stringComplete = false; 
const byte solenoidPin = 12; // pin for solenoid transistor
int Speed = 100;
void setup(){
  //start serial connection
  Serial.begin(9600);  // baud rate is 9600 must match with bluetooth 
  //The String reserve() function allows you to allocate a buffer in memory for manipulating strings.
  inputString.reserve(50);  // reserve 50 bytes in memory to save for string manipulation 
  command.reserve(50);
  value.reserve(50);
  boolean stringOK = false;
  pinMode(solenoidPin, OUTPUT);
}
void loop(){
  // if arduino receive a string termination character like \n stringComplete will set to true
  if (stringComplete) {
    //Serial.println(inputString);
    delay(100);
    // identified the posiion of '=' in string and set its index to pos variable
    int pos = inputString.indexOf('=');
    // value of pos variable > or = 0 means '=' present in received string.
    if (pos > -1) {
      // substring(start, stop) function cut a specific portion of string from start to stop
      // here command will be the portion of received string till '='
      // let received string is open=test123
      // then command is 'open' 
        command = inputString.substring(0, pos);
      // value will be from after = to newline command
      // for the above example value is test123
      // we just ignoreing the '=' taking first parameter of substring as 'pos+1'
      // we are using '=' as a separator between command and vale
      // without '=' any other character can be used
      // we are using = menas our command or password must not contains any '=', otherwise it will cause error 
        value = inputString.substring(pos+1, inputString.length()-1);  // extract command up to \n exluded
       //Serial.println(command);
       //Serial.println(value);
       // password.compareTo(value) compare between password and value string,if match return 0 
    if(!password.compareTo(value) && (command == "OPEN")){
          // if password matched and command is 'OPEN' than door should open
           openDoor(); // call openDoor() function
           Serial.println(" OPEN"); // sent open feedback to phone
           delay(100);
           }
    else if(!password.compareTo(value) && (command == "CLOSE")){
          // if password matched and command is 'CLOSE' than door should close
           closeDoor();
           Serial.println(" CLOSE"); // sent " CLOSE" string to the phone 
           delay(100);
           }
    else if(password.compareTo(value)){
          // if password not matched than sent wrong feedback to phone
           Serial.println(" WRONG");
           delay(100);
           } 
        } 
       // clear the string for next iteration
       inputString = "";
       stringComplete = false;
    }  
}
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read(); 
    //Serial.write(inChar);
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline or a carriage return, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n' || inChar == '\r') {
      stringComplete = true;
    } 
  }
}
void openDoor(){
   digitalWrite(solenoidPin, HIGH);
   digitalWrite(motorEnablePin, HIGH);
   // use following line if you want to change speed, then use millis() instead if delay()
   //analogWrite(motorEnablePin, Speed);
   delay(1500);  
   digitalWrite(motorEnablePin, LOW); // off motor
}
void closeDoor(){
   digitalWrite(solenoidPin, LOW);
   digitalWrite(motorEnablePin, HIGH);
   //analogWrite(motorEnablePin, Speed);
   delay(1500); 
   digitalWrite(motorEnablePin, LOW);
}

From the code it is used to change the speed of the motor, but in normal motor driver convention the "EN" pin changes direction, and the "PWM" pin changes speed.