Is this possible?

Hi, I'm making a tachometer with infrared sensor and ultrasonic sensor to calculate the distance of a thing using one arduino and a push button. It shows no error, but when I uploaded it, both the serial monitor and the LCD (i2c) wont turn on and shows nothing. Is there anything wrong with the code? or is it with the circuit?

#include <LiquidCrystal_I2C.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
#define infrared 9
#define button 4
const int trigPin = A1;
const int echoPin = A3;
long timer;
int jarak;

int delay1()
{
  int i,j;
  unsigned int count=0;
  for(i=0;i<1000;i++)
  {
    for(j=0;j<1000;j++)
    {
      if(digitalRead(infrared))
        {
          count++;
          while(digitalRead(infrared));
}
}
}
  return count;
}

void setup()
{
  Serial.begin(9600);
  pinMode(echoPin, INPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(infrared, INPUT);
  pinMode(button, INPUT);
    
  lcd.begin(16,2);
  lcd.backlight();
  lcd.print("Mekatronika");
  delay(2000);
  digitalWrite(button, HIGH);
}

void loop()
{
  unsigned int time=0,RPM=0;
  lcd.clear();
  lcd.print("Please Press");
  lcd.setCursor(0,1);
  lcd.print("Button to Start");

  while(digitalRead(button));
  lcd.clear();
  lcd.print("Reading RPM...");
  time=delay1();
  lcd.clear();
  lcd.print("Reading Distance...");
  
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  timer = pulseIn(echoPin, HIGH);
  jarak = timer/58;
  delay(1000);

  RPM=(time*12)/3;
  delay(2000);

  Serial.print(jarak);
  Serial.print(RPM);
  
  lcd.clear();
  lcd.print("RPM=");
  lcd.print(RPM);
  lcd.print("DISTANCE=");
  lcd.print(jarak);
  lcd.print("cm");
  delay(5000);  
}

You are saying that you don't see "Mekatronika" on the LCD?

Have you tested the LCD with example code from the LiquidCrystal_I2C.h library so that you know it is connected correctly and not faulty?

I also suggest you add Serial.println("Starting"); in setup() and see if that appears in serial monitor.

Maybe clearing the LCD in every loop is taking information off the LCD?

If your pushbutton is tied HIGH, this line will hold the sketch here.

Hi! Thanks for replying!

Yes, I don't see the "Mekatronika" on the LCD.

I tried using the default code for the LCD but it won't show up aswell. But I don't think there's a problem with the wiring.... maybe? Or can you help them check it out in case I missed something?

Hi! Thankyou for replying.

What do you mean by tied HIGH? Should I remove the line then or..?

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

1 Like

"Tied high" means if the button is connected in a way that an unpressed button reads HIGH (Vcc).

The line asks: while (button is high/exists/1)

In this situation, there is no point continuing with your own code. If the example code does not work, then it must be that either your wiring is incorrect or the LCD is faulty. Don't waste your time or the forum's time reviewing your own code. If the LCD will not work with the example code, it will not work with your code either.

Disconnect everything except the Arduino and the LCD and go back to the example code from the LiquidCrystal_I2C.h library and get that working. The forum can help you with that if you are not successful.


The power rails in your breadboard have breaks in them. You need to connect them as I have shown in the photo.

Done

OHH okayyy.

I mean that if the button is pressed....... then it will proceed?

Or did i write the wrong code?

Hold on, I tried connecting them all already but it still wouldn't load the LCD test display..... I'm confused.

There could be other things wrong with the circuit, or a faulty LCD.

Try running the i2c scanner sketch to see if the LCD adapter is visible on the i2c bus.

Also, did you try adjusting the contrast pot on the back of the adapter?

When you run the i2c scanner it will verify if your lcd address is correct or not also.

Hi,
If you have a test code for the display, please post a copy of it.
When you run the code, make sure EVERY other bit of hardware is DISCONNECTED.

Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Yes. You have the code correct to read the button. Check your wiring. Here is your simulation.
https://wokwi.com/projects/367249711904395265

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