Electronic Miter box! Control a Stepper motor with an Arduino and Keypad

Got Questions? Click here and post them in the Brainy-Bits Forum to get help!

OVERVIEW

In this tutorial we will see how to move a stepper motor to an exact position that we enter on a keypad.

 

Plus we will display some information like current position and the entered distance on the keypad on a Nokia 5110 LCD module.



SCHEMATIC

Arduino control stepper motor with keypad

There’s quite a bit of connections needed for this project, which is why we are using a MEGA2560 R3.

 

Keypad connections:

Pin 22, 24, 26, and 28 of the MEGA are connected to the Rows Pins of the Keypad.

Pin 31, 33 and 35 are connected to the Columns Pins of the Keypad.

 

Nokia 5110 LCD connections:

Pin 3, 4, 5, 6, 7 of the MEGA are connected to Pins CLK, DIN (or mosi), DC, CE (or sce) and RST.

Pin 11 of the MEGA is connected to the LED pin of the Nokia.

The GND and VCC Pins of the Nokia are connected to GND and 3.3V pins of the MEGA.

 

Easy Driver connections:

Pin A0 and A1 of the MEGA are connected to Pins STEP and DIR of the Easy Driver.

Another GND pin of the MEGA is connected to the GND pin of the Easy Driver.



THE CODE

We are using 3 Libraries in this tutorial, “AccelStepper” to control the stepper motor, “Keypad” to map the keys of the keypad and “U8glib” to draw the information on the nokia 5110 LCD.

You can find download links for all these libraries at the bottom of this tutorial page.

 

As always you can have a look at the tutorial video for more information.

 

/* Arduino Control Stepper with Keypad and LCD

Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!

*/

#include  // AccelStepper Library
#include   // Keypad Library
#include "U8glib.h"  // U8glib for Nokia LCD

// Variables to hold entered number on Keypad
volatile int firstnumber=99;  // used to tell how many numbers were entered on keypad
volatile int secondnumber=99;
volatile int thirdnumber=99;

// Variables to hold Distance and CurrentPosition
int keyfullnumber=0;  // used to store the final calculated distance value
String currentposition = "";  // Used for display on Nokia LCD


// Keypad Setup
const byte ROWS = 4; // Four Rows
const byte COLS = 4; // Four Columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {22, 24, 26, 28}; // Arduino pins connected to the row pins of the keypad
byte colPins[COLS] = {31, 33, 35, 37}; // Arduino pins connected to the column pins of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );  // Keypad Library definition


// U8glib Setup for Nokia LCD
#define backlight_pin 11
U8GLIB_PCD8544 u8g(3, 4, 6, 5, 7);  // Arduino pins connected to Nokia pins:
                                    // CLK=3, DIN=4, CE=6, DC=5, RST=7
                                    
                                    
// AccelStepper Setup
AccelStepper stepper(1, A0, A1);  // 1 = Easy Driver interface
                                  // Arduino A0 connected to STEP pin of Easy Driver
                                  // Arduino A1 connected to DIR pin of Easy Driver
                                  


void setup(void) {
  
  //  Light up the LCD backlight LEDS
  analogWrite(backlight_pin, 0);  // Set the Backlight intensity (0=Bright, 255=Dim)
    
  //  AccelStepper speed and acceleration setup
  stepper.setMaxSpeed(1500);  // Not to fast or you will have missed steps
  stepper.setAcceleration(400);  //  Same here
  
  // Draw starting screen on Nokia LCD
  u8g.firstPage();
  do {
  u8g.drawHLine(0, 15, 84);
  u8g.drawVLine(50, 16, 38);
  u8g.drawHLine(0, 35, 84); 
  u8g.setFont(u8g_font_profont11);
  u8g.drawStr(0, 10, "ENTER DISTANCE");
  u8g.drawStr(62, 29, "MM");
  u8g.drawStr(4, 46, "cur-pos");
  }
  while( u8g.nextPage() );
  
}


void loop(){
  
  char keypressed = keypad.getKey();  // Get value of keypad button if pressed
  if (keypressed != NO_KEY){  // If keypad button pressed check which key it was
    switch (keypressed) {
      
      case '1':
        checknumber(1);
      break;
        
      case '2':
        checknumber(2);
      break;

      case '3':
        checknumber(3);
      break;

      case '4':
        checknumber(4);
      break;

      case '5':
        checknumber(5);
      break;

      case '6':
        checknumber(6);
      break;

      case '7':
        checknumber(7);
      break;

      case '8':
        checknumber(8);
      break;

      case '9':
        checknumber(9);
      break;

      case '0':
        checknumber(0);
      break;

      case '*':
        deletenumber();
      break;

      case '#':
        calculatedistance();
      break;
    }
  }

}

void checknumber(int x){
  if (firstnumber == 99) { // Check if this is the first number entered
    firstnumber=x;
    String displayvalue = String(firstnumber);  //  Transform int to a string for display
    drawnokiascreen(displayvalue); // Redraw Nokia lcd
    
  } else {
    if (secondnumber == 99) {  // Check if it's the second number entered
      secondnumber=x;
      String displayvalue = (String(firstnumber) + String(secondnumber));
      drawnokiascreen(displayvalue);

    } else {  // It must be the 3rd number entered
      thirdnumber=x;
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
      drawnokiascreen(displayvalue);

    }
  }
}

 
void deletenumber() {  // Used to backspace entered numbers
  if (thirdnumber !=99) {
      String displayvalue = (String(firstnumber) + String(secondnumber));
      drawnokiascreen(displayvalue);

    thirdnumber=99;
  } 
  else {
    if (secondnumber !=99) {
      String displayvalue = String(firstnumber);
      drawnokiascreen(displayvalue);

      secondnumber=99;
   } 
   else {
     if (firstnumber !=99) {
       String displayvalue = "";
       drawnokiascreen(displayvalue);

       firstnumber=99;
      }
    }
  }
}
  
void calculatedistance() {  // Used to create a full number from entered numbers

    if (thirdnumber == 99 && secondnumber == 99 && firstnumber != 99) {
      keyfullnumber=firstnumber;
      movestepper(keyfullnumber);
    }
    
    if (secondnumber != 99 && thirdnumber == 99) {
      keyfullnumber=(firstnumber*10)+secondnumber;
      movestepper(keyfullnumber);
    }
    
    if (thirdnumber != 99) {
      keyfullnumber=(firstnumber*100)+(secondnumber*10)+thirdnumber;
      movestepper(keyfullnumber);
    }
    
    resetnumbers(); // Reset numbers to get ready for new entry
  } 


void movestepper(int z) {  //  Move the stepper

  int calculatedmove=((z*1600)/80);  //  Calculate number of steps needed in mm
  stepper.runToNewPosition(calculatedmove);
  currentposition = String(z);
  u8g.firstPage();
  do {
    u8g.drawHLine(0, 15, 84);
    u8g.drawVLine(50, 16, 38);
    u8g.drawHLine(0, 35, 84); 
    u8g.setFont(u8g_font_profont11);
    u8g.drawStr(0, 10, "ENTER DISTANCE");
    u8g.drawStr(62, 29, "MM");
    u8g.drawStr(4, 46, "cur-pos");
    u8g.setPrintPos(57,47);
    u8g.print(currentposition);       
  }
  while( u8g.nextPage() ); 
}
                
void resetnumbers() {  // Reset numbers for next entry
  firstnumber=99;
  secondnumber=99;
  thirdnumber=99;
} 
  

void drawnokiascreen(String y) {

    u8g.firstPage();
    do {
      u8g.drawHLine(0, 15, 84);
      u8g.drawVLine(50, 16, 38);
      u8g.drawHLine(0, 35, 84); 
      u8g.setFont(u8g_font_profont11);
      u8g.drawStr(0, 10, "ENTER DISTANCE");
      u8g.setPrintPos(0,29);
      u8g.print(y);  // Put entered number on Nokia lcd    
      u8g.drawStr(62, 29, "MM");
      u8g.drawStr(4, 46, "cur-pos");
      u8g.setPrintPos(57,47); 
      u8g.print(currentposition);  //  Display current position of stepper
    }
      while( u8g.nextPage() );

}



TUTORIAL VIDEO



DOWNLOAD

Copy the above Sketch code in your Arduino IDE software to program your Arduino.

 

Used Libraries:

Download the AccelStepper library here:  AccelStepper Library Download Link

Download the Keypad library here:  Keypad Library Download Link

Download the U8glib library here: U8glilb Library Download Link

 

Once downloaded, just extract the content of the zip files inside your “arduino\libraries\” folder.

9 Comments

ARTUR - Feb 03, 2017

Hello. Why analog pins A0 -A1 are used to control the stepper motor driver? After all, the protocol involves the use of a digital signal STEP-DIR.

Brainy-Bits - Feb 03, 2017

ARTUR: Analog pins (A0,A1,A2…) can be used in Analog or Digital depending on how you access them (DigitalRead, AnalogRead) in this case they are used as Digital Inputs.

The only reason I used A0 and A1 instead of a standard digital pin, is that their location on the Mega made it cleaner and easier to run the jumper wires to, that’s all. :)

musaab - Feb 03, 2017

Hi,

I am planning to do the same project but instead of entering the distance using a keypad, I want to make the user able to enter the required distance from a labview gui.

Does anyone have an idea that can help me on how to change the value of a variable in arduio from labview graphical language? is that possible?!

It would help me a lot,

thanks a lot in advance.

aniruddha - Feb 03, 2017

why the stepper motor rotates in the opposite direction when the input through keypad is given around 270mm………plzzzz help me out with this

aril - Feb 03, 2017

how to make 3 motor stepper at once?

Michael - Feb 03, 2017

I want to do a recall of last number I put in on A how hard is that to get it to recall that number again and move the same directions so I pushed 10 millimeter
each time I push A it will jump 10 millimeter. or 5 millimeter and I put. A recall it will jump 5 more millimeter.

Tony Baker - Feb 03, 2017

Same problem as @Aniruddha . Entering a high value (based on this setup, or personal distance calculations) causing the motor to run in reverse.
Ex: from 0 position and entering 170mm (my calculation is z*1600/8), the stepper runs from 0 to -170 instead of 0 to 170.
Any help would be greatly appreciated.
Thanks, Tony

Tony Baker - Feb 03, 2017

@Aniruddha
There is a fix to the problem where the stepper moves in a negative direction due to high value inputs. The problem is the size of the variable ‘int’.

Replace this:
int calculatedmove=((z*1600)/80);
with this:
long calculatedmove=((z*1600L)/80);

Now you should get proper forward/backward movements.

Tony

Brian - Feb 03, 2017

I’m very new at this, so please bear with me. Can this set up be used to control a gate actuator by exchanging out the stepper motor for a gate actuator?

Got Questions? Click here and post them in the Brainy-Bits Forum to get help!

Leave a comment

Comments have to be approved before showing up