LCD (I2C) as an serial monitor.

Goodday all,

I have an question about an little project i’m making for my school.
I’m just a starting user, and ive got an little background with programming and building cerquit boards.

What is my project?:
I want to put the output of my Optical senor (I can read it out on the serial monitor) on my LCD with an I2C chip.

This is the code ive got so far.

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);

const int led = 13;
int tcrt;

void setup(){
    // set up the LCD's number of columns and rows: 
  lcd.init();
  lcd.backlight();
  // initialize the serial communications:
  Serial.begin(9600);
  pinMode (led, OUTPUT);
}

void loop(){
  tcrt = analogRead(A0);
  Serial.println(tcrt);
  analogWrite(led, tcrt/4);
  // when characters arrive over the serial port...
  if (Serial.available()) {
    // wait a bit for the entire message to arrive
    delay(100);
    // clear the screen
    lcd.clear();
    // read all the available characters
    while (Serial.available() > 0) {
      // display each character to the LCD
      lcd.write(Serial.read());
    }
  }
}

I hope you guys can help me.

Tnks,

Jeffrey

Hi Jeffrey

First of all, please change the quote tags to code tags in your post. It makes it easier for people to read and understand your code :slight_smile: The code tags are the “#” button above the row of smileys.

I can read it out on the serial monitor

Your program is doing what you expect when you output to serial monitor, is that right?

This is the code ive got so far.

Tell us what help you need. Does the code compile OK? If so, what happens when it runs, and how is that different from what you need?

All the best

Ray

Hi ray,

When I open the serial monitor on my pc, the LCD goes on and off.
I can read the value's on my pc screen but not on the lcd.

I can't see what i'm doing wrong.

Jeffrey

P.s. thanks for the advice :), im fairly new to this forum

Serial.println(tcrt);

Just add

lcd.write(tcrt);
  if (Serial.available()) {
    // wait a bit for the entire message to arrive
    delay(100);
    // clear the screen
    lcd.clear();
    // read all the available characters
    while (Serial.available() > 0) {
      // display each character to the LCD
      lcd.write(Serial.read());
    }
  }

And this code is only writing to the LCD the characters you enter in serial monitor. So you may not need it any more.

The first thing to note is that opening the serial monitor on most Arduinos will reset the board and restart the program so some effect on the LCD is to be expected. What happens if you print some text to the LCD ? Does it work ?

If I type something in the serial monitor, then i dont got any feed on my LCD.

Also adding
lcd.write(tcrt);
Didn't give any results.

Set cursor position on LCD first?

LegendJeffrey:
If I type something in the serial monitor, then i dont got any feed on my LCD.

Also adding
lcd.write(tcrt);
Didn't give any results.

Maybe you should test your wiring again ?

Is your LCD working at all? Even with another sketch?

As I asked in my previous post

What happens if you print some text to the LCD ? Does it work ?

Got it fixed.
But now i’m getting crazy figures instead of nummers.

This is what i see:

And this is the code i’m using:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);

const int led = 13;
int tcrt;

void setup(){
    // set up the LCD's number of columns and rows: 
  lcd.init();
  lcd.backlight();
  // initialize the serial communications:
  Serial.begin(9600);
  pinMode (led, OUTPUT);
}

void loop(){
  tcrt = analogRead(A0);
  Serial.println(tcrt);
  lcd.write(tcrt); 
  analogWrite(led, tcrt/4);
}

Allready thanks for the help so far!
This is an great community

LegendJeffrey:
Got it fixed.
But now i'm getting crazy figures instead of nummers.

That image doesn't really help, try to print something on your LCD.. Maybe you should correct the contrast..

My lcd works just normal, when i try to talk trough it by the serial monitor.

Here is an better picture:

In loop(), instead of lcd.write(tcrt);, try the following, to move the cursor back to home position each time and print the value as a number string, rather than as binary codes:

lcd.home();
lcd.print(tcrt);
delay(1000); // for testing only, wait 1 s before updating LCD with next number

Hackscribble:
In loop(), instead of lcd.write(tcrt);, try the following, to move the cursor back to home position each time and print the value as a number string, rather than as binary codes:

lcd.home();

lcd.print(tcrt);
delay(1000); // for testing only, wait 1 s before updating LCD with next number

Thank you so much!
Its finally working!

Thanks all!

So, what was wrong ?

The code, now ive chanced some things it works perfect!
Also with other senors :slight_smile:

OK, don't tell us what you changed then.