USA
Toll Free: 1-800-676-6123
How to Control an Actuator with a Keypad

How to Control an Actuator with a Keypad

Guest Writer
Guest Writer
PA Engineer

Sometimes our projects need some added security. This can be the case in a variety of different applications such as door-lock mechanisms, prevention of unsafe or unauthorized use of machinery or getting even more niche, escape room props. In this article we will take you through a keypad actuator assembly and go over how you can password-protect the operation of your linear actuator.

Here is what we will be using:

Wiring Your Actuator

First, we will start with the wiring. In order to be able to extend and retract your actuator, we will need to use both channels of our 2-channel relay module. This way, when Relay One is active, current will flow one direction and when Relay Two is active, current will flow the opposite direction. If neither or both relays are active, no current will flow to the actuator. To accomplish this, we will need to make the following connections.

Relays to the Actuator and the Power Supply

  • +12V to NC1 (normally closed terminal on relay one) (white wire).
  • -12V to NO1 (normally open terminal on relay one) (black wire).
  • NC1 to NC2 (blue jumper wire).
  • NO1 to NO2 (green jumper wire).
  • COMMON1 to Actuator (brown actuator wire).
  • COMMON2 to Actuator (blue actuator wire).

Arduino to the Keypad and the Relay Module

  • Connect Pins 1-8 on the keypad to pins 2-9 on the Arduino (in that order).
  • Arduino Pin 10 to IN1 on relay module.
  • Arduino Pin 11 to IN2 on relay module.
  • Arduino 5V to VCC on relay module.
  • Arduino GND to GND on relay module.
  • Buzzer anode (longer lead) to pin 12 (optional).
  • Buzzer cathode (shorter lead) to GND (optional).
    Arduino to the Keypad and the Relay Module

    Coding Your Project

    Now that we have made all our connections, we are ready for the code. The purpose of this code is to read the inputs from a keypad, look for the correct 5-digit input and operate our relays accordingly.  There is also code for an optional buzzer to provide feedback.  If you do not wish to use the buzzer, you can simply not attach it and leave the code as is. If you need to use the buzzer pin for something else, delete or comment-out all the code used for the buzzer or “beep” functions.

    In the code below, you will find comments on almost every line (light gray text that follows “//”). These comments will describe what is happening in the sketch, as well as modifications you can make. We will also break down a few important sections here for a more thorough explanation.

    Use our Arduino microcontrollers for unlimited control options of your actuator!

    Setup Code

    In our setup code, we will define our buzzer and relay pins as outputs. The buzzer will start off LOW and the relays will start HIGH. This will cause them to all be inactive when we first power our Arduino.  We will also run the “retract ()” function once so that the Arduino know the correct state of the actuator.

     void setup() //runs once on startup
    {
    digitalWrite(buzzer, LOW);//deactivates buzzer
    digitalWrite(relay1,HIGH);//deactivates relay1
    digitalWrite(relay2,HIGH);//deactivates relay2
    for(int i=10;i<14;i++)
    {
      pinMode(i,OUTPUT);//sets pins 10 - 13 as outputs
    }
    
    
    Serial.begin(9600);//Starts the serial monitor at 9600 baud rate (for debugging only)
    retract();//retracts the actuator on startup if it is not already. comment this out if you do not want the actuator to retract on startup
    Serial.println("READY");//lets us know the serial monitor is running
    }
    

    Keypad Code

    For this sketch we are using the Keypad.h library. This library contains the functions we are using to receive inputs from our keypad. In order to use this library, we need to make a two-dimensional array to map out the characters of our keypad. This can be achieved by, first, defining the number of rows and the number of columns there are on the keypad. Then, we make our array with each character on the keypad. Our keypad has four rows, four columns and eight output pins. There is one pin for each row and one pin for each column. We show this in our code by making a “rowPins” array that contains the digital inputs connected to the row pins and a “colPins” array that contains the digital inputs connected to the column pins. When we press a key, we connect one of the row pins with one of the column pins. For example, if we press button 2, we are creating a closed circuit between row pin one and column pin two.

     char customKey; //characters input by pressing keypad
    const byte ROWS = 4; //four rows
    const byte COLS = 4; //four columns 
    
    char keys[ROWS][COLS] = { //layout your "keymap" here
      {'1', '2', '3', 'A'},
      {'4', '5', '6', 'B'},
      {'7', '8', '9', 'C'},
      {'*', '0', '#', 'D'}
    };
    byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
    byte colPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad
    Keypad customKeypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); //initialize an instance of class NewKeypad 

    Passcode Code

    In this example for secure linear actuator control, our passcode is 5-digits long, so we define “Passcode_Length” as “6”. This is because we require one extra space for a null character. If you want to change the passcode length, simply change the 6 to a number that is greater than your desired code length by one. After that, change the value of “Passcode” to whatever characters you would like (set to “12345” by default).

    The character associated with each key pressed will be stored in the “Input” array. Once that array contains 5 characters, it will compare the value of “Input” and “Passcode” to see if we have the correct passcode. If the values are equal, our code will tell the Arduino to either extend or retract the actuator (depending on the current state of the actuator). If the passcode is incorrect, the buzzer pin will go high three times rapidly and then go low. In either case, the “clearInput()” function will be called afterwards to clear the Input array and make room for a new entry.

    Extending and Retracting Code

    We have two very similar functions in this code “void extend()” and “void retract()”.  When called, void extend() will write Relay One low, making it active. This will close a circuit and apply a positive voltage to the actuator. The relay will remain active for the time assigned to “const int extendTime”. (set to 25,000 milliseconds by default). The void retract() function will do the exact same thing, except it will use relay two to reverse the voltage instead of relay one.

    void extend()//extend the actuator
    {
      longBeep();
      Serial.println("EXTENDING...");
      digitalWrite(relay2,HIGH);//makes sure relay2 is not active
      digitalWrite(relay1,LOW);//activates relay1
      delay(extendTime);
      digitalWrite(relay1,HIGH);//deactivates relay1
      Serial.println("DONE EXTENDING");
      extended = true; //tells the arduino that the actuator is extended 
      longBeep();
    }
    
    void retract()//extend the actuator
    {
      longBeep();
      Serial.println("RETRACTING...");
      digitalWrite(relay1,HIGH);//makes sure relay1 is not active
      digitalWrite(relay2,LOW);//activates relay2
      delay(retractTime);
      digitalWrite(relay2,HIGH);//deactivates relay2
      Serial.println("RETRACTING DONE");
      extended = false; //tells the arduino that the actuator is retracted
      longBeep();
    }

    Finishing Touches

    After we have made all our connections and uploaded our code, we should have a functional password-protected actuator control system.  If you are having trouble when you first set it up, try using the serial monitor tool in the Arduino IDE. This can be extremely helpful in determining whether your issues are being caused by code, wiring or faulty components.

    This code can also be modified beyond just changing the passcode.  You may want to consider swapping out your relay board with a MegaMoto shield so that you can have speed control or using an actuator with hall effect feedback for position control.

    We hope this article was helpful! If you have any questions about this, any other remote controlled linear actuator, or want to share your project with us, please feel free to call or email us.

    
        
      
    

    Please wait...

    ...