Electronic Miter box! Control a Stepper Motor with a Keypad

//Electronic Miter box! Control a Stepper Motor with a Keypad

DIY Electronic miter box!  Controlling a Stepper motor with an Arduino and a Keypad


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.


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.


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!


#include "AccelStepper.h" // AccelStepper Library
#include <Keypad.h>  // 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] = {
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
  do {
  u8g.drawHLine(0, 15, 84);
  u8g.drawVLine(50, 16, 38);
  u8g.drawHLine(0, 35, 84); 
  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':
      case '2':

      case '3':

      case '4':

      case '5':

      case '6':

      case '7':

      case '8':

      case '9':

      case '0':

      case '*':

      case '#':


void checknumber(int x){
  if (firstnumber == 99) { // Check if this is the first number entered
    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
      String displayvalue = (String(firstnumber) + String(secondnumber));

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


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

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

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

void calculatedistance() {  // Used to create a full number from entered numbers

    if (thirdnumber == 99 && secondnumber == 99 && firstnumber != 99) {
    if (secondnumber != 99 && thirdnumber == 99) {
    if (thirdnumber != 99) {
    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
  currentposition = String(z);
  do {
    u8g.drawHLine(0, 15, 84);
    u8g.drawVLine(50, 16, 38);
    u8g.drawHLine(0, 35, 84); 
    u8g.drawStr(0, 10, "ENTER DISTANCE");
    u8g.drawStr(62, 29, "MM");
    u8g.drawStr(4, 46, "cur-pos");
  while( u8g.nextPage() ); 
void resetnumbers() {  // Reset numbers for next entry

void drawnokiascreen(String y) {

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


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.

By | 2018-02-23T17:06:25-05:00 February 10th, 2016|Tutorials|


  1. Sean August 25, 2020 at 6:34 am - Reply

    Great project, helped set up my ballscrew stepper job.

    How would you inplement a limit on the value entered. E.g. not allow the stepper to move if its over 500mm?

  2. Sean August 21, 2020 at 7:56 am - Reply

    Hi brainy bits and other programmers reading this thread,

    How can I add the stepper motor to home to a reference point automatically on start up in this code with the use of a limit switch, and with out the use of EEROM.

    Thanks in advance

  3. pctrung May 9, 2020 at 9:43 am - Reply

    Very nice project with useful !!! I have made it and got problem that when I enter 700 mm it revert direction while up to 600 mm is another direction. Can you tel me about how to settle it

  4. pctrung May 6, 2020 at 11:04 am - Reply

    Very nice project !! and really need your help when I extend number enter by 4 digit instead 3 digit as your tutorials, it working fine and in the code change to INT calculatemove = (z*50), the problem is when i enter the number more than 700, the direction will be revert, can you help me to settle it

  5. Craig March 24, 2020 at 9:23 am - Reply

    Hi im new to arduino and have very little knowledge, did anyone manage to rewrite the code for a home sensor and to use the 16×2 screen with ic2
    Any help would be hugely appreciated

  6. Mike January 16, 2020 at 5:49 pm - Reply

    I have just your found your great website and videos. I would like to replicate your electronic miter saw stop, however, I am new to Arduino programming and design. Since you no longer offer the hardware kits can you provide a parts list for this project?


  7. Gerry May 21, 2019 at 1:18 pm - Reply

    my post seems to have disappeared so I’ll try again. Your code works well, but I am making a variable length antenna using a stepper motor, I could do with some help to get the motor to extend a 10mtr length of copper strip. I have tried to modify your code without success.
    Thanks for the projects.

    • parisa September 6, 2019 at 3:35 am - Reply

      hi, can you launched sensor fluxgate flc100 magnetometr with ardunio and conected to pc wiht bleuthos? thack you very much

  8. Gerry Stoelwinder May 21, 2019 at 12:34 pm - Reply

    I am attempting to make an antenna similar to a steppir, I watched your tutorial and thought I could modify your code to suit, I failed. I need to have a linear movement of about 10mtrs. The last time I attempted programming was in 1982 with a ZX81 computer, didn’t do too well then either. I would appreciate some help or a point in the right direction.
    Thanks for the original code, I keep trying.

  9. Tinel May 12, 2019 at 6:18 am - Reply

    Thanks for the project. Interesting. If you can change it to enter 4 numbers. Thank you. I can not modify it. I admit I’m a beginner.

  10. jan claerhout April 28, 2019 at 10:21 am - Reply

    hi Brainy Bits
    i’m trying to make a positioning system with a lead screw for witch i used mainly your code. it works quite well, but an input over 84 makes the motor run in the wrong direction. do you have any idea what the reason for this may be and what i can do to make it work right?

    many thanks

    • Patrick November 7, 2019 at 7:19 pm - Reply

      Try to replace the “int z” in the movestepper function to “long Z”

  11. marco January 3, 2019 at 3:35 pm - Reply

    Hello Mister Brainy-Bits,
    I need to add a home position in this project with a switch….
    can you help me?

  12. Jim December 19, 2018 at 5:26 pm - Reply

    Another question. Is it possible to mount a L239d motor shield on a MEGA 2560 and still use the 4×4 keypad and LCD?

  13. Jim December 19, 2018 at 5:23 pm - Reply

    What driver do you recommend for a NEMA 23 stepper motor?.7.4v, 1Amp per phase

    • Carl Blickhan January 30, 2019 at 8:37 pm - Reply

      I built this using a longs NEMA34 stepper and a longs 2H microstep driver. the only thing I changed in the code is INT calculatemove = (z*3007) ; . this makes the gear train move in meters rather than MM. the problem is everything works fine until I enter any number over 10. then it will rotate the opposite direction or not move the proper distance. any help would be great.

  14. Manoj October 11, 2018 at 12:08 pm - Reply

    Hi I want the motor move in degrees position & in continuous motion with rev & fwd option. I also want Home switch from where I have started rotation.

  15. Fandi August 21, 2018 at 9:17 am - Reply

    Hi. I have tried this project. I use a 4×4 keypad. there are 4 buttons that have not been used, namely the A, B, C & D button. it’s a pity if this button is not used. if I want to make the A button function as the home position button. can you help me add the home button function in this program.

    Thank You…

  16. Jonty June 12, 2018 at 2:46 am - Reply

    Hi Brainy Bits,
    I have watched all of your videos on youtube and am inspired by your work. I want to make a miter box much like the tigerstop and the same as neo7cnc on youtube. However I want to use mm and be able to insert a decimal place (eg 1.7mm), and I also want to use a 16×2 LCD I2C screen with just 4 wires. How do I change the code so that it is able to do this?

    Any help would be greatly appreciated
    Cheers, Jonty

  17. Vlad May 23, 2018 at 7:08 pm - Reply

    Hi, I’m trying to do program in inches with decimal inputs from keypad, but unsuccessfully, maybe you have something similar and can you show me the code. Thank you in advance.

    • Uli Libal January 13, 2020 at 11:18 am - Reply

      RElative simple -most steppers have 200 stps per turn – I have spindle which needs 78 steps per mm (1mm /turn Spindle with production tolerances) so you simply have to do the maths…

  18. Georgios April 20, 2018 at 12:27 pm - Reply

    Very nice project!!!But what happen with decimal numbers movement? If for example i want to move the stepper motor 1.5 mm?
    Thanks in advance.

  19. Robert March 19, 2018 at 8:18 am - Reply

    Hi, this project code can you convert for 4×3 keypad and for simple 16×2 LCD? i use I2C for LCD so just use 4 wire.like this can be use whit UNO. thanx

Leave A Comment