GLCD & digitalwrite

I trying to enable a display on and off in my software code.

I have used a NPN/PNP configuration to drive the Vcc and backlight on the display. After placing a diode on the Vcc line to stop feedback which caused the PNP not to switch off I confirmed the hardware functioned correctly.

However when the Arduino powers up I see nothing on the output pin and hence the display never powers up. It appears the routine gets stuck until I force power to the display. Then the code runs without any issues and I can toggle the output.

I’ve attached a breakdown of the code to show what I’m trying to do.

My question before I have to look for a hardware solution, is there a way to force the pin high before the glcd initialises?

Thanks in advance

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif


#include <glcd.h>
#include <glcd_Config.h> // display config
#include <fonts\newrombig.h> // new times roman big font



/* ints output */
int Pin = 51;



void setup() {
  pinMode(Pin, OUTPUT); //set output
  digitalWrite(Pin, HIGH);       // turn on pullup resistors   
  /*Display setup*/
  GLCD.Init(NON_INVERTED);   // initialise the library, non inverted writes pixels onto a clear screen
}



void loop() 
  digitalWrite(Pin, HIGH);       // turn on display  
  GLCD.SelectFont(newrombig); // new times roman font big
  GLCD.ClearScreen(); //Clear the full screen  
  GLCD.GotoXY(50,20);  // position cursor
  GLCD.Puts("HELLO"); // print text 
  delay(1000);
  GLCD.ClearScreen(); //Clear the full screen  
  GLCD.GotoXY(50,20);  // position cursor
  GLCD.Puts("NEED"); // print text 
  delay(1000);
  GLCD.ClearScreen(); //Clear the full screen  
  GLCD.GotoXY(50,20);  // position cursor
  GLCD.Puts("HELP"); // print text 
  delay(1000);
  GLCD.ClearScreen(); //Clear the full screen  
  GLCD.GotoXY(50,20);  // position cursor
  GLCD.Puts("PLEASE"); // print text 
  delay(1000);
  digitalWrite(Pin, LOW);       // turn off display
  delay(2000);
  

 }

is there a way to force the pin high before the glcd initialises?

Force what pin high?

  pinMode(Pin, OUTPUT); //set output
  digitalWrite(Pin, HIGH);       // turn on pullup resistors

Probably not related to your problem but an OUTPUT pin doesn't have pull-up resistors to turn on.

I have used a NPN/PNP configuration to drive the Vcc and backlight on the display

That's a bit, ambiguous. How about a schematic of your "NPN/PNP" configuration.

Please excuse the hand sketch, also ignore the A marking on the display just refer to the numbers which line up with the pins on my Arduino Mega.

So the switch circuit works correctly when I manually put 5v to the 1k resistor to drive the NPN transistor. The circuit also work fine once the main code is running. However Pin 51 will not go HIGH with initial power on of the Arduino with the display connected and the code doesn’t seem to run until power is applied to the display.

My question is can I set Pin 51 high before the GLCD is initialized as it looks like something in that is stopping it from working.

drt1979: However Pin 51 will not go HIGH with initial power on of the Arduino with the display connected and the code doesn't seem to run until power is applied to the display.

You've measured the pin to ensure it isn't at a high state?

You could put a delay in after you set the pin high initially to see if there is a timing issue.

Yes I have probed the pin and it is LOW and I have also tried a delay between the pin going high and GLCD command.

Does the Arduino do any initialization before running the commands in the start-up menu? It does look like the Arduino is not running through the code until the display is powered.

If I write something to the display in the startup function. I can power up the arduino and then plug in the display a fews seconds later and it displays the message in start-up. Since I can't see a buffer for the display I came to the conclusion that there is some initialization for the display that is not running until the display is powered on. I'm maybe way off the mark here!

If the display is powered on start-up everything runs fine and I can toggle the display on and off using pin 51.

Does the Arduino do any initialization before running the commands in the start-up menu?

The Arduino doesn't have a start-up menu, nor can it run any commands. Using proper terminology is going to be essential to solving your problem.

If I write something to the display in the startup function. I can power up the arduino and then plug in the display a fews seconds later and it displays the message in start-up.

There is no startup function, so this doesn't make sense. You should not be plugging stuff into the Arduino while it is running. That makes as much sense as pounding on it with a hammer.

If the display is powered on start-up everything runs fine and I can toggle the display on and off using pin 51.

How is the display being powered?

Sorry for the wrong terminology, instead of startup I mean the setup code for the Arduino. Does the Arduino do any initialization of the libraries before running the setup code?

The code does not appear to run when the display is plugged in without power to the Vcc of the display on power up. I do call the digitalwrite code to put Pin 51 logic high before calling any command for the display. However Pin 51 does not go HIGH.

The display Vcc and backlight is powered from the 5volts on the Arduino through the transistor configuration provided in the sketch above. If connect the base of the NPN transistor through the resistor to 5 volts the display powers up and I can toggle Pin 51 HIGH and LOW.

Does the Arduino do any initialization of the libraries before running the setup code?

Constructors are called. Then, init() is called to set up the hardware. Then, setup() is called to do your set up stuff. The constructors should not do anything that requires hardware to be in any known state. The Init() method of the glcd class seems to be dealing with the hardware, though it's hard to be sure without looking at the code.

drt1979: The code does not appear to run when the display is plugged in without power to the Vcc of the display on power up.

How do you know if code is not running if the display's backlight isn't on? You could start toggling pin 13 or something to get a heartbeat indicator. Or use Serial.prints() to debug.

drt1979: If connect the base of the NPN transistor through the resistor to 5 volts the display powers up and I can toggle Pin 51 HIGH and LOW.

It sounds like you have a wiring problem, not a code issue.

drt1979, I am the co-author of the glcd library and am the one who was maintining the glcd library, However, I am no longer maintaining the glcd library. If you are interested, I have created a new project called openGLCD which is a fork of the GLCDv3 code. openGLCD has many bug fixes, new features, and improvements including backlight API functions. openGLCD is licensed as GPL v3 as opposed to GLCDv3 being under the more liberal LGPL 2.1+ license. If you are ok using GPL v3 for your project, you may want to take a look switching to openGLCD. Here is the link to the openGLCD project page: https://bitbucket.org/bperrybap/openglcd/wiki/Home

There are API example sketches for each of the API functions. The functions of interest for backlight control are in in the device control section and called: - OnBacklight() - OffBacklight() The needed circuit is in the comments of the example sketch.

To enable it you have to configure the backlight pin in the appropriate config file.

--- bill

Many thanks Bill.

I've compiled with your OpenGLCD library and assigned the backlight to the same pin I was using to switch the power to the display. However the issue is the same. So I'm convinced that it's due to the display having nothing on Vcc on power-up.

However I have changed my wiring so the the switched supply only feeds the backlight and use the 5v direct from the Arduino for Vcc on the display. Then using the display off command combined with backlight off is giving the result I'm looking for.

Great, glad you were able to get it up and going. If you are used to using GLCDv3, make sure to read the documentation about migration from GLCDv3 as you will need to make some adjustments to your existing code or turn on GLCDv3 compatibilty mode. You also may want to convert your font declaration over to the way openGLCD now does it, to allow the font to work on the non AVR platforms - should you decide to go there.

If using Off() there is no need to do anything else since Off() is the same as doing both: OffDisplay(); // turn off display pixels OffBacklight(); // turn off backlight

All these API functions are documented in the html documentation, as well as in each API example sketch.

--- bill