stepper motor+arduino motor shield R +LCD display problems working simultaneosly

Hello all,
i am a newbie in the field of arduino. i was trying to control a stepper motor(with 27:1 gear ratio)(details of the motor link) with 3 buttons (1 to move it clockwise a specified number of steps, another to move it anti clockwise the same number of steps and another button to take the stepper back to the home postion from the current position).The motor is used to tilt an L bracket that could carry a load.The aim is to tilt the bracket and the load its holds by 5 degrees every time a button is pressed.Currently I have succeeded in getting all the 3 buttons do their specified job… However when LCD(LCM1602C) is used along with this set up a problem arises. even the basic hallo word program fails .All i get is some weird special characters. I am pretty sure the connections are fine… as the motor part alone (LCD part of code commented)and the LCD part alone works perfectly fine.

Could some one kindly advice me on how to proceed.

i am also posting my code along with the post.
Kind regards

#include <Stepper.h>                                  // Including the stepper library
#include <LiquidCrystal.h>                             // Including the LCD library
const int stepsPerRevolution = 200;                     // Depends on the specific motor
// Has to be modified if the motor is altered
const int homeButton = 7;                                // Naming the arduino pins
const int cwSwitch = 19;                                    // Naming the arduino pins
const int ccwSwitch = 14;                                    // Naming the arduino pins
const int pwmA = 3;                                          // Naming the arduino pins
const int pwmB = 11;                                          // Naming the arduino pins
const int brakeA = 9;                                          // Naming the arduino pins
const int brakeB = 8;                                           // Naming the arduino pins
const int dirA = 12;                                             // Naming the arduino pins
const int dirB = 13;                                              // Naming the arduino pins
boolean switchStatecw = LOW;                                       // Delcaring the clockwise rotation switch state and initializing it
boolean switchStateccw = LOW;                                       // Delcaring the counter-clockwise rotation switch state and initializing it
boolean switchStatehomeButton = LOW;                                 // Delcaring the counter-clockwise rotation switch state and initializing it
long stepcounter = 0;                                                 // Delcaring variable to count the steps rotated-hels to determine the current position

//LiquidCrystal stagelcd(1, 10, 2, 4, 5, 6);                        // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
Stepper stageStepper(stepsPerRevolution, dirA, dirB);                 // Initialization of the stepper library

void setup() {                                                      // The setting up function which sets the environment for the repeat loop
  while (!Serial);                                                 //to delay the serial interface untill the terminal window is opened
  // the Direction pins solely determine the motor rotation sense
  pinMode(pwmA, OUTPUT);                                         // Setting the pulse width modulation pin of A channel to output mode
  pinMode(pwmB, OUTPUT);                                        // Setting the pulse width modulation pin of B channel to output mode
  pinMode(brakeA, OUTPUT);                                     // Setting the brake pin of A channel to output mode
  pinMode(brakeB, OUTPUT);                                    // Setting the brake pin of B channel to output mode
  pinMode(homeButton, INPUT);                                // Setting the home button pin to input mode
  pinMode(cwSwitch, INPUT);                                 // Setting the clockwise rotation switch pin to input mode
  pinMode(ccwSwitch, INPUT);                               // Setting the counter-clockwise rotation switch pin to input mode

  digitalWrite(pwmA, HIGH);                              // Setting the PWM A pin to ON state
  digitalWrite(pwmB, HIGH);                             // Setting the PWM B pin to ON state
  digitalWrite(brakeA, LOW);                           // Disabling the brake of A channel
  digitalWrite(brakeB, LOW);                          // Disabling the brake of B channel
  Serial.begin(9600);                                // initialize the serial port:
  //stagelcd.begin(16, 2);                          // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
  //  stagelcd.print("hello, world!");

  // Printing on to the serial monitor

  Serial.println("Enter Custom number of steps to override the button function :");
  Serial.println("positive  number for clockwise rotation and negative number for counterclockwise rotation");


  stageStepper.setSpeed(50);                         // best practise - set speed for multiple steps
}   // End of setup function

void loop() {    // Code that keeps repeating

  switchStatecw = digitalRead(cwSwitch);      // Interruptiong the continuous loop to get external input
  switchStateccw = digitalRead(ccwSwitch);   // Interruptiong the continuous loop to get external input
  switchStatehomeButton = digitalRead(homeButton);   // Interruptiong the continuous loop to get external input
  if (Serial.available())                   // This block of if state ment is to give the option of roationg
    //the motor a custom number of steps from the serial interface
  {
    Serial.println("current step is");
    Serial.println(stepcounter);
    int serialStep = Serial.parseInt();
    stageStepper.step(serialStep);
    stepcounter = stepcounter + serialStep;
    Serial.println("updated  step is");
    Serial.println(stepcounter);
  }
  if (switchStatecw == HIGH)
  {
    stageStepper.step(74);
    //accestagestepper.move(74);
    stepcounter = stepcounter + 74;
    Serial.println("updated step is");
    Serial.println(stepcounter);

  }
  if (switchStateccw == HIGH)
  {
    stageStepper.step(-74);
    stepcounter = stepcounter - 74;
    Serial.println("updated step is");
    Serial.println(stepcounter);

  }

  if (switchStatehomeButton == HIGH)
  {
    if (stepcounter >= 0)
    {
      stageStepper.step(-stepcounter);
      Serial.println("moving counter clockwise to Zero");
      stepcounter = 0;
      Serial.println("current position is");
      Serial.println(stepcounter);
    }
    else
    {
      stageStepper.step(-stepcounter);
      Serial.println("moving clockwise to Zero");
      stepcounter = 0;
      Serial.println("current position is");
      Serial.println(stepcounter);

    }
  }
  //   // set the cursor to column 0, line 1
  //  // (note: line 1 is the second row, since counting begins with 0):
  //  stagelcd.setCursor(0, 1);
  //  // print the number of seconds since reset:
  //  stagelcd.print(millis() / 1000);
}  //End of Loop

Have you checked to see if you are using the same I/O pins on the shield and LCD.

The LCD uses digital pins 12,11,5,4,3,2 and if I read the specs for the motor shield right it uses channel A 12,3,9,A0 and channel B 13,11,8,A1. There seems to be some overlap in pin usage.

hello ribbonman if you see the code i have initialized the lcd object with this command //LiquidCrystal stagelcd(1, 10, 2, 4, 5, 6); thus avoiding the comon usage of pins.So all the pins being used for the motor remains excludsivr to the motor. I have also tird avoiding the pin 1 for LCD but still the result remains the same.

I did see your pin assignments but the data sheet has the pins listed as below and wondered if there was a reason they are stating which pins to connect the lcd too?

Also when using serial comm. pin 0,1 can only be used for serial comm and nothing else so maybe that could be a problem.

I'm new to this also but just finished a project with a touch screen and had to move pins around to accommodate so I thought somebody to talk through problems with could be helpful.

Pin 1 is connected directly to ground. Pin 2 is connected directly to +5V. (NOTE: This, in turn, comes directly from the +5V output of the Arduino Uno.) Pin 3 is connected to the middle pin of the 10k potentiometer, and is used to adjust the contrast of the LCD. Pin 4 (RS or “register select”) is connected to pin 12 on the Arduino Uno. Pin 5 (RW or “read/write”) is connected directly to ground. By doing this, we’re saying that we’re only going to write to the LCD, not read anything from it. Which is fine. We just want to put text onto the LCD, not read anything from it anyway. Pin 6 (EN or “enable”) is connected to pin 11 on the Arduino Uno. Pins 7 – 10: Not connected. Pin 11 on the LCD is connected to pin 5 on the Arduino Uno. Pin 12 on the LCD is connected to pin 4 on the Arduino Uno. Pin 13 on the LCD is connected to pin 3 on the Arduino Uno. Pin 14 on the LCD is connected to pin 2 on the Arduino Uno. Pin 15 is connected to one end of a 1k resistor, and the other end of the resistor is connected to 5V. Pin 16 is connected directly to ground.

Hi,

i have initialized the lcd object with this command //LiquidCrystal stagelcd(1, 10, 2, 4, 5, 6);

Pin 1 is part of the Arduino comms/programming pair of pins 0 and 1.

You will need to avoid pins 0 and 1.

What model Arduino are you using?

Thanks… Tom… :slight_smile:

hello all, i am using arduino Uno. I tried what you suggested and still the result is the same.Again i was able to find out that the problem is not the motor and the lcd being used simultaneosly, but the serial monitor and the LCD being used together. When I comment the Serial.begin(9600); line every thing is running fine.

Could someone shine some light on this matter. LCD and serial monitor .. how can they be used in the same program. is it possible to print something onto both ..or can only one function at a time.

thanks taby

Hi, What did you try?

If you moved the LCD connection from pin1 to anther pin, did you edit the LCD pin parameter in your code?

Please draw a circuit diagram for us, reverse engineer what you have. A picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :)

Hello all,

i was trying to redo the whole wiring and it worked. The 0 and 1 pins were avoided and now the LCD and serial monitor has started functioning simultaneously. Thanks a lot for your help. I would like to know of some tool to make electrical drawings for my project report. Kindly suggest me some tool so that i can document the connections on the report for the project.(All this is part of a college project ). Now I am able to make the motor motions and get the LCD display for 2 buttons (clockwise motion and counter clockwise motion) However the home button is still unable to display on the LCD. (The LCD goes blank basically once home button is pressed and further presses of the other 2 buttons also don't display anything)

Could you kindly help on that bit. the final code and a pic of the connection is given below. Any suggestions to improve the code is welcome.

#include <Stepper.h>                                  // Including the stepper library
#include <LiquidCrystal.h>                             // Including the LCD library
const int stepsPerRevolution = 200;                     // Depends on the specific motor
float stepToanglefactor = 0.06666667;                   //  To convert the steps to angle turned.
// Has to be modified if the motor is altered
const int homeButton = 15;                                // Naming the arduino pins
const int cwSwitch = 19;                                    // Naming the arduino pins
const int ccwSwitch = 16;                                    // Naming the arduino pins
const int pwmA = 3;                                          // Naming the arduino pins
const int pwmB = 11;                                          // Naming the arduino pins
const int brakeA = 9;                                          // Naming the arduino pins
const int brakeB = 8;                                           // Naming the arduino pins
const int dirA = 12;                                             // Naming the arduino pins
const int dirB = 13;                                              // Naming the arduino pins
boolean switchStatecw = LOW;                                       // Delcaring the clockwise rotation switch state and initializing it
boolean switchStateccw = LOW;                                       // Delcaring the counter-clockwise rotation switch state and initializing it
boolean switchStatehomeButton = LOW;                                 // Delcaring the counter-clockwise rotation switch state and initializing it
float stepcounter = 0.0;                                                 // Delcaring variable to count the steps rotated-hels to determine the current position

LiquidCrystal stagelcd(7, 10, 2, 4, 5, 6);                        // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
Stepper stageStepper(stepsPerRevolution, dirA, dirB);                 // Initialization of the stepper library

void setup() {                                                      // The setting up function which sets the environment for the repeat loop
  while (!Serial);                                                 //to delay the serial interface untill the terminal window is opened
  Serial.begin(9600);
                                                                  // the Direction pins solely determine the motor rotation sense
  pinMode(pwmA, OUTPUT);                                         // Setting the pulse width modulation pin of A channel to output mode
  pinMode(pwmB, OUTPUT);                                        // Setting the pulse width modulation pin of B channel to output mode
  pinMode(brakeA, OUTPUT);                                     // Setting the brake pin of A channel to output mode
  pinMode(brakeB, OUTPUT);                                    // Setting the brake pin of B channel to output mode
  pinMode(homeButton, INPUT);                                // Setting the home button pin to input mode
  pinMode(cwSwitch, INPUT);                                 // Setting the clockwise rotation switch pin to input mode
  pinMode(ccwSwitch, INPUT);                               // Setting the counter-clockwise rotation switch pin to input mode

  digitalWrite(pwmA, HIGH);                              // Setting the PWM A pin to ON state
  digitalWrite(pwmB, HIGH);                             // Setting the PWM B pin to ON state
  digitalWrite(brakeA, LOW);                           // Disabling the brake of A channel
  digitalWrite(brakeB, LOW);                          // Disabling the brake of B channel
                                  // initialize the serial port:
  stagelcd.begin(16, 2);                          // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
  stagelcd.print("stage horizontal");
  delay(1000);
  stagelcd.clear();
  stagelcd.setCursor(0, 0);

  // Printing on to the serial monitor

  Serial.println("Enter Custom number of steps to override the button function :mutipples of 75 to have same effect as a button press");
  Serial.println("positive  number for clockwise rotation and negative number for counterclockwise rotation");


  stageStepper.setSpeed(50);                         // best practise - set speed for multiple steps
}   // End of setup function

void loop() {    // Code that keeps repeating

  switchStatecw = digitalRead(cwSwitch);                // Interruptiong the continuous loop to get external input
  switchStateccw = digitalRead(ccwSwitch);             // Interruptiong the continuous loop to get external input
  switchStatehomeButton = digitalRead(homeButton);   // Interruptiong the continuous loop to get external input
  if (Serial.available())                           // This block of if state ment is to give the option of roationg
                                                  //the motor a custom number of steps from the serial interface
  {
    Serial.println("current Angle is");
    Serial.println(stepcounter*stepToanglefactor);
    int serialStep = Serial.parseInt();
    stageStepper.step(serialStep);
    stepcounter = stepcounter + serialStep;
    Serial.println("updated  angle is");
    Serial.println(stepcounter*stepToanglefactor);
    stagelcd.print("Custom angle:");
    stagelcd.setCursor(0, 1);               // set the cursor to column 0, line 1
                                           // (note: line 1 is the second row, since counting begins with 0):
    stagelcd.print(stepcounter*stepToanglefactor);
    stagelcd.setCursor(0, 0);
  }
  if (switchStatecw == HIGH)
  {
    stageStepper.step(75);
    stepcounter = stepcounter + 75;
    Serial.println("updated angle is");
    Serial.println(stepcounter*stepToanglefactor);
    stagelcd.clear();
    stagelcd.print("Updated angle:");
     stagelcd.setCursor(0, 1);               // set the cursor to column 0, line 1
                                              // (note: line 1 is the second row, since counting begins with 0):
    stagelcd.print(stepcounter*stepToanglefactor);
    stagelcd.setCursor(0, 0);
    delay(500);                                   //functioning as a debouncer of the switch -reads only one switch press

  }
  if (switchStateccw == HIGH)
  {
    stageStepper.step(-75);
    stepcounter = stepcounter - 75;
    Serial.println("updated angle is");
    Serial.println(stepcounter*stepToanglefactor);
    stagelcd.clear();
    stagelcd.print("Updated angle:");
     stagelcd.setCursor(0, 1);               // set the cursor to column 0, line 1
                                              // (note: line 1 is the second row, since counting begins with 0):
    stagelcd.print(stepcounter*stepToanglefactor);
    stagelcd.setCursor(0, 0);
    delay(500);                                   //functioning as a debouncer of the switch -reads only one switch press
  }

  if (switchStatehomeButton == HIGH)
  {
    if (stepcounter >= 0)
    {
      stageStepper.step(-stepcounter);
      Serial.println("moving counter clockwise to Zero");
      stepcounter = 0.0;
      Serial.println("current position is");
      Serial.println(stepcounter);
      stagelcd.clear();
      stagelcd.print("Current position:");
      stagelcd.setCursor(0, 1);               // set the cursor to column 0, line 1
                                              // (note: line 1 is the second row, since counting begins with 0):
      stagelcd.print(stepcounter);
      stagelcd.setCursor(0, 0);
      delay(500);                                   //functioning as a debouncer of the switch -reads only one switch press
    }
    else
    {
      stageStepper.step(-stepcounter);
      Serial.println("moving clockwise to Zero");
      stepcounter = 0.0;
      Serial.println("current position is");
      Serial.println(stepcounter);
      stagelcd.clear();
      stagelcd.print("Current position:");
      stagelcd.setCursor(0, 1);               // set the cursor to column 0, line 1
                                              // (note: line 1 is the second row, since counting begins with 0):
      stagelcd.print(stepcounter);
      stagelcd.setCursor(0, 0);
      delay(500);                                   //functioning as a debouncer of the switch -reads only one switch press
    }
  }
  if (stepcounter==0)
  {
    stagelcd.print("stage-horizontal");
    stagelcd.setCursor(0, 0);
    }   
}  //End of Loop

in the current scenario , everytime the serial monitor is closed and opened , the program flow goes into the setup function and therefor the stepcounter variable is reinitialized to zero . Is there a way to retain its previous value .

Hi,
Try ExpressPCB.

Its PCB section is good, but does not produce a Gerber file.
Its Schematic is easy to use, simple and uncluttered.
Example:
arduino-L293.jpg

https://www.expresspcb.com/free-cad-software/

Tom… :slight_smile:

Thanks a lot Tom :) for your valuable advice. :)

Hello all, i have managed to run the code and get the motor working as intended.(all buttons working fine… doing their job… biggest lesson learned … never use any of the default pins used being used by your stepper motor -in my case as i am using the arduino R3 motor shield and Uno board they are 3,8,9,11,12,13,A0 and A1 ) I still have the problem of

“in the current scenario , everytime the serial monitor is closed and opened , the program flow goes into the setup function and therefor the stepcounter variable is reinitialized to zero . Is there a way to retain its previous value-that the variable is not initialized to zeroeverytime the serial monitor is closed and opened.”

Thanks a ton all of you.

Best of light
Taby Tom

Final code

#include <Stepper.h>                                  // Including the stepper library
#include <LiquidCrystal.h>                             // Including the LCD library
const int stepsPerRevolution = 200;                     // Depends on the specific motor
float stepToanglefactor = 0.06666667;                    //  To convert the steps to angle turned.
// Has to be modified if the motor is altered
const int homeButton = 17;                                 // Naming the arduino pins
const int cwSwitch = 19;                                    // Naming the arduino pins
const int ccwSwitch = 18;                                    // Naming the arduino pins
const int pwmA = 3;                                           // Naming the arduino pins
const int pwmB = 11;                                           // Naming the arduino pins
const int brakeA = 9;                                           // Naming the arduino pins
const int brakeB = 8;                                            // Naming the arduino pins
const int dirA = 12;                                              // Naming the arduino pins
const int dirB = 13;                                               // Naming the arduino pins
boolean switchStatecw = LOW;                                        // Delcaring the clockwise rotation switch state and initializing it
boolean switchStateccw = LOW;                                        // Delcaring the counter-clockwise rotation switch state and initializing it
boolean switchStatehomeButton = LOW;                                  // Delcaring the counter-clockwise rotation switch state and initializing it
float stepcounter = 0.0;                                               // Delcaring variable to count the steps rotated-hels to determine the current position

LiquidCrystal stagelcd(7, 10, 2, 4, 5, 6);                              // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
Stepper stageStepper(stepsPerRevolution, dirA, dirB);                  // Initialization of the stepper library

void setup() {                                                       // The setting up function which sets the environment for the repeat loop


  // the Direction pins solely determine the motor rotation sense
  pinMode(pwmA, OUTPUT);                                         // Setting the pulse width modulation pin of A channel to output mode
  pinMode(pwmB, OUTPUT);                                        // Setting the pulse width modulation pin of B channel to output mode
  pinMode(brakeA, OUTPUT);                                     // Setting the brake pin of A channel to output mode
  pinMode(brakeB, OUTPUT);                                    // Setting the brake pin of B channel to output mode
  pinMode(homeButton, INPUT);                                // Setting the home button pin to input mode
  pinMode(cwSwitch, INPUT);                                 // Setting the clockwise rotation switch pin to input mode
  pinMode(ccwSwitch, INPUT);                               // Setting the counter-clockwise rotation switch pin to input mode

  digitalWrite(pwmA, HIGH);                              // Setting the PWM A pin to ON state
  digitalWrite(pwmB, HIGH);                             // Setting the PWM B pin to ON state
  digitalWrite(brakeA, LOW);                           // Disabling the brake of A channel
  digitalWrite(brakeB, LOW);                          // Disabling the brake of B channel
  while (!Serial);                                  //to delay the serial interface untill the terminal window is opened
  Serial.begin(9600);                            // initialize the serial port:
  stagelcd.begin(16, 2);                           // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display

  stagelcd.setCursor(0, 0);

  // Printing on to the serial monitor

  Serial.println("Enter Custom number of steps to override the button function :mutipples of 75 to have same effect as a button press");
  Serial.println("positive  number for clockwise rotation and negative number for counterclockwise rotation");


  stageStepper.setSpeed(50);                         // best practise - set speed for multiple steps
}   // End of setup function

void loop() {    // Code that keeps repeating

  switchStatecw = digitalRead(cwSwitch);                // Interruptiong the continuous loop to get external input
  switchStateccw = digitalRead(ccwSwitch);             // Interruptiong the continuous loop to get external input
  switchStatehomeButton = digitalRead(homeButton);   // Interruptiong the continuous loop to get external input
  if (Serial.available())                           // This block of if state ment is to give the option of roationg
    //the motor a custom number of steps from the serial interface
  {
    Serial.println("current Angle is");
    Serial.println(stepcounter * stepToanglefactor);
    int serialStep = Serial.parseInt();
    stageStepper.step(serialStep);
    stepcounter = stepcounter + serialStep;
    Serial.println("updated  angle is");
    Serial.println(stepcounter * stepToanglefactor);
    stagelcd.print("Custom angle    ");
    stagelcd.setCursor(0, 1);               // set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
    stagelcd.print(stepcounter * stepToanglefactor);
    stagelcd.setCursor(0, 0);
  }
  if (switchStatecw == HIGH)
  {
    stageStepper.step(75);
    stepcounter = stepcounter + 75;
    Serial.println("updated angle is");
    Serial.println(stepcounter * stepToanglefactor);
    //stagelcd.clear();
    stagelcd.print("Updated angle:  ");
    stagelcd.setCursor(0, 1);               // set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
    stagelcd.print(stepcounter * stepToanglefactor);
    stagelcd.setCursor(0, 0);
    delay(500);                                   //functioning as a debouncer of the switch -reads only one switch press

  }
  if (switchStateccw == HIGH)
  {
    stageStepper.step(-75);
    stepcounter = stepcounter - 75;
    Serial.println("updated angle is");
    Serial.println(stepcounter * stepToanglefactor);
    //stagelcd.clear();
    stagelcd.print("Updated angle:  ");
    stagelcd.setCursor(0, 1);               // set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
    stagelcd.print(stepcounter * stepToanglefactor);
    stagelcd.setCursor(0, 0);
    delay(500);                                   //functioning as a debouncer of the switch -reads only one switch press
  }

  if (switchStatehomeButton == HIGH)
  {

    if (stepcounter >= 0)
    {
      stageStepper.step(-stepcounter);
      Serial.println("moving counter clockwise to Zero");
      stepcounter = stepcounter - stepcounter ;
      Serial.println("current position is");
      Serial.println(stepcounter);
      stagelcd.clear();
      stagelcd.print("home            ");
      stagelcd.setCursor(0, 1);               // set the cursor to column 0, line 1
      // (note: line 1 is the second row, since counting begins with 0):
      stagelcd.print(stepcounter);
      stagelcd.setCursor(0, 0);
      delay(500);                                   //functioning as a debouncer of the switch -reads only one switch press
    }
    else
    {
      stageStepper.step(-stepcounter);
      Serial.println("moving clockwise to Zero");
      stepcounter = stepcounter - stepcounter;
      Serial.println("current position is");
      Serial.println(stepcounter);
      stagelcd.clear();
      stagelcd.print("home            ");
      stagelcd.setCursor(0, 1);               // set the cursor to column 0, line 1
      // (note: line 1 is the second row, since counting begins with 0):
      stagelcd.print(stepcounter);
      stagelcd.setCursor(0, 0);
      delay(500);                                   //functioning as a debouncer of the switch -reads only one switch press
    }
  }
  if (stepcounter == 0)
  {
    stagelcd.print("stage horizontal");
    stagelcd.setCursor(0, 0);
  }
}  //End of Loop