Ultrasonic sensor and value from serial monitor on LCD_I2C

Somebody know how to get the value that de ultrasonic sensor send to my arduino on the lcd_I2C?

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
int trigPin = 9;    // TRIG pin
int echoPin = 8;    // ECHO pin

float duration_us, distance_cm;

void setup()
{
  Serial.begin(9600); //Serial baud rate 
lcd.begin (16,2);
  lcd.init(); // initialize the lcd
  lcd.backlight();

lcd.home ();
lcd.print("Hello");
delay(2000);
lcd.clear();
  // configure the trigger pin to output mode
  pinMode(trigPin, OUTPUT);
  // configure the echo pin to input mode
  pinMode(echoPin, INPUT);
}

void loop()
{ // generate 10-microsecond pulse to TRIG pin
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10000);
  digitalWrite(trigPin, LOW);

  // measure duration of pulse from ECHO pin
  duration_us = pulseIn(echoPin, HIGH);

  // calculate the distance
  distance_cm = 0.017 * duration_us;

  // print the value to Serial Monitor
  Serial.print("distance: ");
  Serial.print(distance_cm);
  Serial.println(" cm");

  delay(500);
  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());
      }
    }
}

Welcome to the forum

lcd.home ();
lcd.print("Hello");

If this prints Hello on the LCD, then have you tried printing the distance_cm variable in loop() instead ?

yes , i tried that and it doesn't show something on my lcd.

Post the sketch showing what you tried

You mean the wiring diagram?

I actually meant the sketch but the schematic would be interesting too

this is what i tried. I'm just a beginner but this is my topic for school and i like to work with arduino so I will try till it works. I appreciate that you are helping me to find my solution

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
int trigPin = 9;    // TRIG pin
int echoPin = 8;    // ECHO pin

float duration_us, distance_cm;

void setup()
{
  Serial.begin(9600); //Serial baud rate 
lcd.begin (16,2);
  lcd.init(); // initialize the lcd
  lcd.backlight();

lcd.home ();
lcd.print(distance_cm);
delay(2000);
lcd.clear();
  // configure the trigger pin to output mode
  pinMode(trigPin, OUTPUT);
  // configure the echo pin to input mode
  pinMode(echoPin, INPUT);
}

void loop()
{ // generate 10-microsecond pulse to TRIG pin
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10000);
  digitalWrite(trigPin, LOW);

  // measure duration of pulse from ECHO pin
  duration_us = pulseIn(echoPin, HIGH);

  // calculate the distance
  distance_cm = 0.017 * duration_us;

  // print the value to Serial Monitor
 lcd.print(distance_cm);
delay(500);
      }

Have you ever seen anything printed on the LCD ?

yes the program works now i think i will just add a button that when i push it the ultrasonic sensor only measure then. Thanks a lot for your help

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
int trigPin = 9;    // TRIG pin
int echoPin = 8;    // ECHO pin

float duration_us, distance_cm;

void setup()
{
  Serial.begin(9600); //Serial baud rate 
lcd.begin (16,2);
  lcd.init(); // initialize the lcd
  lcd.backlight();

lcd.home ();
lcd.print(distance_cm);
delay(2000);
lcd.clear();
  // configure the trigger pin to output mode
  pinMode(trigPin, OUTPUT);
  // configure the echo pin to input mode
  pinMode(echoPin, INPUT);
}

void loop()
{ // generate 10-microsecond pulse to TRIG pin
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10000);
  digitalWrite(trigPin, LOW);

  // measure duration of pulse from ECHO pin
  duration_us = pulseIn(echoPin, HIGH);

  // calculate the distance
  distance_cm = 0.017 * duration_us;
delay(5000);
  // print the value to Serial Monitor
 lcd.print(distance_cm);
delay(5000);
lcd.clear();
      }

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