Hey there,
I try to get the signal of an ultra sonic sensor on an lcd-Display.
The Program is geting compiled and played on the Arduino, but the lcd only Shows:
Distance:
0 cm|||||
can you spot the mistake in the code? I can't find it.
#include <LiquidCrystal.h>
#include "SR04.h"
#define TRIG_PIN 5
#define ECHO_PIN 4
SR04 sr04 = SR04(ECHO_PIN, TRIG_PIN);
long dist;
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
void setup() {
lcd.begin(16,2);
lcd.print("Distance:");
}
void loop() {
dist = sr04.Distance();
lcd.setCursor(0, 1);
lcd.print(sr04.Distance());
lcd.println(" cm");
delay(1000);
}
try something like
void loop() {
dist = sr04.Distance();
lcd.setCursor(0, 1);
lcd.print(dist);
lcd.print(" cm ");
delay(1000);
}
ps:
this is not an "installation & troubleshooting" topic.
Edit your first post titel and move it to
programming questions / display
I tried, but the lines are just moved to the right and the distance still constant at 0 cm
post a pictures of your setup.
all modules and connections must be visible.
post the updated sketch with my modifactions.
Check them very carefully (!!!).
post a picture of the printout on the LCD. It MUST differ from your previous post.
the lines are gone now, the output is still at 0 cm
Here is the sketch right now
#include <LiquidCrystal.h>
#include "SR04.h"
#define TRIG_PIN 5
#define ECHO_PIN 4
SR04 sr04 = SR04(ECHO_PIN, TRIG_PIN);
long dist;
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
void setup() {
lcd.begin(16,2);
lcd.print("Distance:");
}
void loop() {
dist = sr04.Distance();
lcd.setCursor(0, 1);
lcd.print(dist);
lcd.print(" cm ");
delay(1000);
}
So do you know if that variable is not 0?
Try using Serial print statements to see the value.
Or just set dist equal to 42, or whatever, and see if that prints.
Do you have a sketch that just gets and prints the temperature to the serial monitor to test your hardware?
a7
I tested yout idea and the Serial Monitor also gives out 0.
I just got different Values out, after adding an extra power source, but the Values are wrong.
alto777
January 21, 2023, 3:03pm
11
lukegaming911:
I tested yout idea
OK, so that means, so far as you have shared here, that you have never gotten a plausible return value from the sensor.
The problem is not displaying a number on the LCD. Did you try setting dist to 777?
Go back to an example sketch that you did not write and did not mess up, and see how to get a good reading from the sensor.
Oh, good. Are they plausible giving what the sensor is sensong?
a7
No I held my Hand directly in front of te sensor it gave out following values.
I solved the problem, the ultra sonic sensor was defect, the code was correct.