Powering LCD and Servo Motor

I have ran into a problem that I do not know the source of. I am trying to run a program that basically conceptually simulates a vending machine. I don’t know if I do not have enough power or really what is happening but attached is a picture what the LCD screen looks like when I try to run the program. (Yes the contrast is all the way up) It seems like the LCD is just not being powered enough. It looks like the LCD is trying to display but doesn’t have enough juice to do the whole thing.

I will also post my schematic and my coding.

Can anyone help? or lead me in the right direction?

#include <LiquidCrystal.h>
#include <Servo.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Servo myServo;
const int coinInsertPin = 6;
const int selection1Pin = 7;
const int selection2Pin = 8;
const int greenLedPin = 13;
const int blueLedPin = 9;
int totalAmount = 0;
int prevTotalAmount = 0;
int change = 0;
int coin[] = {1, 5, 10, 25, 50, 100};
int coinSwitchState = 0;
int prevCoinSwitchState = 0;
int selection1SwitchState = 0;
int selection2SwitchState = 0;
int insert;
int value = 0;
void setup (){
 lcd.begin(16,2);
 pinMode(coinInsertPin, INPUT);
 pinMode(selection1Pin, INPUT);
 pinMode(selection2Pin, INPUT);
 pinMode(greenLedPin, OUTPUT);
 pinMode(blueLedPin, OUTPUT);
 myservo.attach (10);
 
}
void loop(){
 myServo.write (90);
 lcd.clear ();
 lcd.setCursor(0,0);
 lcd.print("Insert Coins");
 if (totalAmount < 25) {
   coinSwitchState = digitalRead(coinInsertPin);
   if (coinSwitchState != prevCoinSwitchState) {
     if (coinSwitchState == LOW) {
       value = random (6);
       insert = coin[value];
       totalAmount = prevTotalAmount + insert;
       lcd.clear ();
       lcd.setCursor(0,0);
       lcd. print("Inserted Coin");
       lcd.setCursor(0,1);
       lcd.print("Value is: ");
       lcd.print (insert);
       lcd.print ("c");
       delay (2000);
       lcd.clear();
       lcd.setCursor(0,0);
       switch(insert){
         case 1:
         lcd.print("Reject Coin");
         lcd.setCursor(0,1);
         lcd.print ("Insert Coins");
         break;
         case 5: 
         lcd.print ("Total Amount");
         lcd.setCursor(0,1);
         lcd.print ("Inserted is: ");
         lcd.print (totalAmount);
         lcd.print ("c");
         break;
         delay (2000);
         lcd.clear();
         lcd.setCursor(0,1);
         lcd.print ("Insert Coins");
         break;
         case 10:
         lcd.print ("Total Amount");
         lcd.setCursor(0,1);
         lcd.print ("Inserted is: ");
         lcd.print (totalAmount);
         lcd.print ("c");
         delay (2000);
         lcd.clear();
         lcd.setCursor(0,1);
         lcd.print ("Insert Coins");
         break;
         case 25:
         lcd.print ("Total Amount");
         lcd.setCursor(0,1);
         lcd.print ("Inserted is: ");
         lcd.print (totalAmount);
         lcd.print ("c");
         delay (2000);
         lcd.clear();
         lcd.setCursor(0,1);
         lcd.print ("Insert Coins");
         break;
         case 50:
         lcd.print("Reject Coin");
         lcd.setCursor(0,1);
         lcd.print ("Insert Coins");
         break;
         case 100:
         lcd.print("Reject Coin");
         lcd.setCursor(0,1);
         lcd.print ("Insert Coins");
         break;
       }
     }
   }
 }
 if (totalAmount >= 25){
   change = totalAmount - 25;
   lcd.clear ();
   lcd.setCursor(0,0);
   lcd.print ("Please Make");
   lcd.setCursor (0,1);
   lcd.print ("A Selection");
   selection1SwitchState = digitalRead(selection1Pin);
   selection2SwitchState = digitalRead(selection2Pin);
   if (selection1SwitchState == HIGH) {
     digitalWrite (greenLedPin, HIGH);
     digitalWrite (blueLedPin, LOW);
     myServo.write (10);
   }
   if (selection2SwitchState == HIGH) {
     digitalWrite (greenLedPin, LOW);
     digitalWrite (blueLedPin, HIGH);
     myServo.write (170);
   }
   lcd.clear ();
   lcd.setCursor(0,0);
   lcd.print ("Please Take Your");
   lcd.setCursor (0,1);
   lcd.print ("Test Form");
   delay (2000);
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Your Change Is:");
   lcd.setCursor(0,1);
   lcd.print(change);
   lcd.print("c");
   delay (3000);
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Thank You");
   delay (2000);
 }
}

Photos of a project do not give enough detail to comment on wiring connections. You need to draw a proper schematic diagram and post a photo of the drawing.

It looks like you are powering the servo from the Arduino and that is likely to be the problem. The servo should have a separate power supply with the servo GND connected to the Arduino GND.

...R

Robin2: You need to draw a proper schematic diagram and post a photo of the drawing.

I have a proper schematic image attached to the original post in the thread. If you don't see it there let me know and I'll repost it.. Thanks

royello99:
I have a proper schematic image attached to the original post in the thread.

Sorry, my mistake.

Your schematic does not indicate what sort of power source you have - how many amps at 5v?

Does the LCD behave itself if you disconnect the servo from the power supply? If it does then I suspect you need more amps.

If the LCD still misbehaves when the servo is disconnected I wonder if you ever got it to work properly.

I am not expert on LCDs - the one I have is altogether wrong for an Arduino as it needs a negative 1.5v to make it light. And they are a PITA as they need so many connections.

…R

The only power source I am using so far is the 5V output from the arduino board. Taking your advice, I am going to see if the screen works without the servo... meaning give the servo its own power source possibly from a 9v battery.

I will let you know the results

Thanks

royello99: The only power source I am using so far is the 5V output from the arduino board.

The schematic is rather fanciful then? It clearly shows an external 5v power supply in the top left corner.

Useful advice relies on accurate input data.

Yes, you need to give the servo its own power supply - with a common GND with the Arduino. But you should be able to verify that that is the problem simply by disconnecting the servo.

...R