Attaching an LCD to the VNH5019 Motor Driver

HI,
Scott From Salem.
I am using a VNH5019 Motor Driver shield with a Arduino Uno microcontroller. I want to use a standard LCD 16 by 2 to view real time data. The problem is that I have run out of available digital pins to attach the LCD. I am using analog Pins A2, A3, A4, and A5 as my project data pins. The LCD requires 6 arduino digital pins to make it work. If I read the 5019 motor driver picture correctly it uses up several arduino pins leaving arduino pins 0, 1, 3, 5, 11, and 13 free to use. However I can’t use arduino pins 0 or 1 as they are for tx and rx (I assume), So I have only 4 digital pins available (I need 6). The LCD requires 6 digital pins. I am not using the current sense analog pins 0 and 1. Can I use these as digital pins for this project? If so then how? If not, how would I find the needed 2 digital pins (assuming I can use digital pins 3,5,11, and 13)?
Thank you for your help!
Scott

Hello.

The AVR’s analog pins brought out by the Arduino can be used as digital pins. Does that work for you?

- Ryan

Hi Ryan,
I cut the traces for A0 and A1 on the motor controller board to dedicate them to the Arduino,and tried to use these two pins to get the LCD display to work. No luck. So I tried to use the analog pins on the arduino uno without the motor controllor board attached, and still I could not get the LCD display to work. But when I change back to all digital pins without using A0 and A1 the LCD works fine. Perhaps this will be more clear ind describing what’s going on.

In setting up my Arduino project, I need 2 more digital pins to make my LCD (standard 16 by 2) work. ( I ran out of digital pins using the motor controller) After several hours, I have been unable to get the LCD to work using A0 and A1 as digital pins along with the rest of the digital pins that are free (3,5,11,13) . However the LCD works great if I remove the motor controller (this frees up more digital pins for the lcd) and use only digital pins and no analog pins . Here is borrowed code I modified that does not work… yet.
thank you so much.

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(3, 5, 11, 13, A0, A1);

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  
  // Print a message to the LCD.
  lcd.print("hello, world!");
}

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

Is the code you posted exactly what you are running when you were doing the tests you describe?

If this is the source code for the LiquidCrystal library you are using, it only uses the pins through the pinMode and digitalWrite functions, which should be compatible with pins A0 and A1.

- Ryan

HI Ryan,
Well, I made some progress. I used your great adivce and made A0 and A1 INPUTs, (the LCD works!) but now I ran into another problem. Need a little more help.
I now have the motor controller stacked on the arduino. I am now able to use A0 and A1 as digital pins. However please take a look at the code below. When I hide the pinMode commands for the pinL, pinR, pinU, pinD, the LCD screen prints out great (see code). When I un hide these pinMode commands, the LCD will not print? The LCD is working, nice blue, but no characters. What am I doing wrong. I know the LCD screen works when I hide the pinMode commands. For ease of reading the program, I have left out the main part in the “loop” section. Just want to get that darn LCD working…
Thank you so much.

#include <LiquidCrystal.h>
#include <DualVNH5019MotorShield.h>
   DualVNH5019MotorShield md;

// initialize the library with the numbers of the interface pins
    LiquidCrystal lcd(3, 5, 11, 13, A0, A1);
    
    int pinL = 2;  //analog pin 2.  Input from right photo resistor
    int pinR = 4;  //analog pin 4.  Input from left photo reisitor
    
    int pinU = 3;  // analog pin 3 up photo resistor
    int pinD = 5;  // analog pin 5 down photoresistor
    
    int leftValue = 0;  //left photo resistor value initializing to 0
    int rightValue = 0; // right photo resistor value initializing to 0
    
    int upValue = 0;  //initializing to 0
    int downValue = 0; //initializing to 0
    
    int errorH = 0;      // error value between the two photo resistors.  Horizontal -left/right
    int errorAvgH = 0;   // rolling average:  (errorAvg = errorAvg + errorH)/2  smoothes it out Horizontal
    
    int errorV= 0;        // error value between the two photo resistors.  Vertical
    int errorAvgV = 0;    // rolling average:  (errorAvg = errorAvg + errorV)/2  smoothes it out Vertical
    
    
    int deadband = 50;   //range in which to do nothing 10 = -10 to +10
    void setup() {
      // set up the LCD's number of columns and rows:
     pinMode(A0,INPUT);
     pinMode(A1,INPUT);
     
      lcd.begin(16, 2);
      md.init();
    
    /*
      pinMode(pinL, INPUT);  // designates PinL as input
      pinMode(pinR, INPUT);  // designates PinR as input
      pinMode(pinU, INPUT);  // designates PinU as input
      pinMode(pinD, INPUT);  // designates PinD as input  
     */ 
      
    }

    void loop() {
      // set the cursor to column 0, line 1
      // (note: line 1 is the second row, since counting begins with 0):
      lcd.setCursor(0, 1);

      lcd.print("hello, Ryan!");
      
    }

I am not sure why your LCD is not working, but your comments do not correspond to what you are actually doing. For example, when you write

int pinL = 2;  //analog pin 2.  Input from right photo resistor

you are not assigning pinL to analog pin 2 you are assigning it to digital pin 2. I think you probably want to write:

int pinL = A2;  //analog pin 2.  Input from right photo resistor

- Ryan

Hi Ryan,
Thank you and everyone for helping. I finally figured it out!!!

Thank you so much for helping me get the LCD working. Here is what I was doing wrong. A beginners mistake, but I reviewed the literature and could not find a tutorial or “beware of doing this…” Using two pins with the same number.

  1. I was trying to be safe, and was declaring my 4 analog sensor pins (2 through 5) as input pins e.g. (pinMode(pinL, INPUT) . Big mistake so I learned.

  2. These pins by default are already input pins. By labeling them as input pins via pinMode, the Arduino assumed they were digital pins 2 through 5).

  3. This in turn messed up the actual digital pins needed for the LCD and my motor controller.

  4. So the fix… was just to remove all 4 statements relating to my analog pins, i.e. pinMode(pin, INPUT). In other words, I was inadertantly making my analog pins digital by labeling them as pinMode(pin, INPUT).

  5. The confusion is there are two sets of pins labeled 0 through 5. One analog set, and of course the other set is the digital pins. How does the arduino know which is which when writing the code? I could say: int sensorPin = 2, and also say int ledPin = 2. WOW … two pins with the same number whats up with this? When I write analogRead(sensorPin), the arduino now knows which pin is analog. And for that matter when I write digitalRead(ledPin) it knows it is a digital pin.

So I may be slow, but I finally got it. Thank you so much!!!
Scott from Salem

I am glad you got it working. I don’t think your explanation is completely correct. Running pinMode(5,INPUT) is always going to change digital pin 5 to an input. If you want to change the analog pin 5 to an input, you need to run pinMode(A5,INPUT). I think your code before didn’t work because the LCD library was depending on using digital pin 3 and 5 as outputs, but you were setting them to inputs inadvertently.

- Ryan

Hi Ryan,
I keep learning all the time for sure. I am a little confused you said

But if I do that wouldn’t the analog pin now act as a digital pin? Here is a quote from arduino reference page.

So pinMode(A5,INPUT) does just that… now analog pin 5 is in essence a digital pin and I need analog for the photoresistor.

What am I missing?
Thanks so much,
Scott

HI Ryan,
OK… I think got it… I sure have rocks for brains sometimes. I assumed when I named an analog pin A0 for example, it now converted to a digital pin. I think that is wrong. It still is an analog pin, but it now acts like a digital pin. Like you say pinMode(A0, INPUT), it’s still an analog pin.
Hope this is right.
Thanks
Scott

I think that’s pretty close to right. As you said before, the analog pins are inputs by default, so doing pinMode(A5,INPUT);, at the start of your program, should have no effect.

- Ryan