1

I was designing a circuit that measure distance using HC-SR04 and display it into a LCD.This circuit also have 4 LEDs indicator.But i have a problem.The LEDs doesn't turn on.The LCDs backlight turn on, but it didnt display anything. Btw, i'm using LCD with I2C module.Here is the code: The Schematic

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
#define trigPin 6
#define echoPin 7
int led5cm = 8;
int led7cm = 9;
int led10cm = 10;
int ledoor = 11; //ledoor means LED Out of Range
void setup() { // put your setup code here, to run once: lcd.begin(16,2); lcd.setCursor(0,0);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led5cm, OUTPUT);
  pinMode(led7cm, OUTPUT);
  pinMode(led10cm, OUTPUT);
}
void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  if (distance <= 5)
  { lcd.println("Distance < 5 CM");
    lcd.print("Distance = ");
    lcd.print(distance);
    lcd.print(" CM");
    digitalWrite(led5cm, HIGH);
    digitalWrite(led7cm, LOW);
    digitalWrite(led10cm, LOW);
    digitalWrite(ledoor, LOW);
    lcd.clear();
    delay(200);
  } else if (distance >= 5 || distance <= 7) {
    lcd.println("Distance < 7 CM");
    lcd.print("Distance = ");
    lcd.print(distance);
    lcd.print(" CM");
    digitalWrite(led5cm, LOW);
    digitalWrite(led7cm, HIGH);
    digitalWrite(led10cm, LOW);
    digitalWrite(ledoor, LOW);
    lcd.clear();
    delay(200);
  }
  else if (distance >= 7 || distance <= 10) {
    lcd.println("Distance < 10 CM");
    lcd.print("Distance = ");
    lcd.print(distance);
    lcd.print(" CM");
    digitalWrite(led5cm, LOW);
    digitalWrite(led7cm, LOW);
    digitalWrite(led10cm, HIGH);
    digitalWrite(ledoor, LOW);
    lcd.clear();
    delay(200);
  }
  else if (distance > 10) {
    lcd.println("Out of Range");
    lcd.print("Distance = ");
    lcd.print(distance);
    lcd.print(" CM");
    digitalWrite(led5cm, LOW);
    digitalWrite(led7cm, LOW);
    digitalWrite(led10cm, LOW);
    digitalWrite(ledoor, LOW);
    lcd.clear();
    delay(200);
  }
}

Can you please help me solve this problem? Thank you very much!!

11
  • 2
    All the code in a single line is unreadable. Properly format it. And I don't see any power connections or current limiting resistors for the LEDs.
    – gre_gor
    Mar 16, 2017 at 14:59
  • I'm sorry, i maybe had mistake when pasteing the code.I will edit it.Yes, im forget to add the resistor in the schematic.Only in schematic.
    – deadguy88
    Mar 16, 2017 at 15:03
  • Did you first check if distance was right using Serial.print
    – Sniper
    Mar 16, 2017 at 15:07
  • 1
    @deadguy88 I don't know what you are seeing, but this is not properly formatted.
    – gre_gor
    Mar 16, 2017 at 16:08
  • 1
    Please show us more details about the I2C IO Expander on your LCD. Can you confirm that the pins on the IO Expander is connected to the pins set in your initialization call? Basically, please re-write your question and/or schematic to show us more, as you have asked why the LCD doesn't display, but you have given nearly zero information about the LCD and how it's connected. Also, you noted that the schematic omitted the pull-up resistors, but the only way we can help you is if what you tell us is accurate. Mar 16, 2017 at 18:19

1 Answer 1

1

To make your LCD work, add code like the following in setup(). (Substitute appropriate values in place of LCDcols and LCDrows, or give them appropriate values via const int ... or enum { ... }; statements.)

  lcd.begin(LCDcols, LCDrows);
  lcd.setBacklightPin(3,POSITIVE); // Might not need this, since info appears to be included in lcd declaration
  lcd.setBacklight(HIGH);
  lcd.clear();          // Clear the display
  lcd.home();

To make the LEDs work better, you may need to change the || connectives in some of the if statements to &&. An if condition with terms joined by ||, OR, is true if any of its terms are true; with terms joined by &&, AND, all of its terms must be true for the condition to be true.

For example, the condition (distance >= 5 || distance <= 7) is true if distance is 5 or more, or 7 or less; in other words, is always true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.