Display pin state on LCD display

Hi

I am very new to programming and have very basic knowledge, and had a simple query.

I am wanting to display on an LCD display the state of one or two pins (specifically 12 and 13). I am using the Arduino to switch on and off some lasers. I have added an LCD display (16x2) and can get text to display and on this display wanted to show the state of the pins (high or low) on the display so you can easily see if the lasers are on.
I am hoping this is a simple one line of code to do this along the lines of lcd.print (XXX)

Any help much appreciated.

Welcome to the forum

Read the state of the pins into 2 variables using digitalRead(). Print the value of the variables to the screen

If you want to you can even use digitalRead(pinNumber) as a parameter for the print command

#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

hd44780_I2Cexp lcd;

void setup()
{
    Serial.begin(115200);
    lcd.begin(20, 4);    
    
    // use just digital read to print 1 or 0
    lcd.print("pin 12 state = ");
    lcd.print(digitalRead(12));
    
    // use the ternary operator to print LOW or HIGH ( or whatever you want)
    lcd.setCursor(0,1);    
    lcd.print("pin 13 state = ");
    lcd.print(digitalRead(13) ? "HIGH" : "LOW");
}

void loop()
{

}

The ternary operator

Thanks. Ive run this and it works fine on my Arduino/screen!

I will train integrating this into the other code I have got.

thanks so much!

1 Like

just as an FYI,
While the code that was present will "work" it is abusing the API.
i.e. it use internal knowledge of the code implementation that is not assured based on the documentation of digitalRead() since there is no guarantee that HIGH is 1 or true and low is 0 or false

According the Arduino documentation digitalRead() returns HIGH or LOW and the type and value of those symbols is unspecified.
This is ridiculously unfortunate but arduino.cc has over the years refused to deal with this issue.

The proper use of the ternary given the Arduino API documentation would be:

lcd.print((digitalRead(13) == HIGH) ? "HIGH" : "LOW");

--- bill

1 Like

I integrated the code into the other code I had and of course it doesn't work (my lack of knowledge).

The code I have is to set up an Arduino to control a laser or lasers, by switching on pins 13, 12, 11 etc.

Should I copy the complete code here (its maybe a bit long)?

Yes, post the complete code. Please read the forum guidelines to see how to properly post code and some information on making a good post.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Also, we need an explanation of what " it doesn't work " means. What does the code actually do and how does that differ from what you want?

A wiring diagram may also be helpful. Written descriptions of circuits are always more ambiguous than a drawing. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

Ive been trying now to integrate the bits of code I have without success (lack of knowledge!)
I am buiding a light sheet microscope and as part of the build it uses an arduino board to control the lasers. I was trying to add an LCD display to indicate the laser state (off or on), which is triggered via pin 13 (or 12 etc) to go high or low.

The basic code for the Arduino (which works fine is here:

https://valelab4.ucsf.edu/svn/micromanager2/trunk/DeviceAdapters/Arduino/AOTFcontroller/AOTFcontroller.ino

I wanted to insert the following code to give an output display of the pin state (12 and 13)

       lcd.setCursor(0,0);    
       lcd.print("488 lsr:");
       lcd.print((digitalRead(12) == HIGH) ? "ON" : "OFF");
                    
     // use the ternary operator to print LOW or HIGH ( or whatever you want)
      lcd.setCursor(0,1);    
      lcd.print("533 lsr:");
      lcd.print((digitalRead(13) == HIGH) ? "ON" : "OFF");

I added the libraries also:

#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

Thanks

What happened when you did ?

IF I install the code from the weblink, it runs fine. And the Mircomanager software can control the pins require.d If I add in the code, it just doesn't work at all (either controlling the pins or giving a display output). Theres a quirk here in that the display also seems to only display on the left hand half.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.