Sensors with LCD-Display not working parallel

Hello folks,

I've been looking for a solution for three days now and I don't know, where the mistake ist.

My Hardware: Arduino Leonardo with 2x16 LCD Display with I2C-Adapter and Keystudio Hall Effect Sensor.
Sketch and Code see below.

What I want it to do:
Show on a 2x16 LCD-Display if the Hall Sensor detects a magnet or not and count the number (Cnt) of magnets detected after one by one (measure rotational velocity).

What is working right:
When I use the LCD and the Hall Sensor seperately everything works fine. I can show every text I want on the LCD and for example show a rising counter. I can use the Hall Sensor to count number of magnets and detect if there is a magnet or not and display it on the Serial Monitor.

What is not working right:
When I use the LCD and the Hall Sensor the same time the sensor is allways detecting a magnet when the LCD is updated. So the counter is allways rising when the LCD is getting a new signal.
For example: I tell the LCD lcd.clear() and then the small LED on the Hall Sensor is blinking and the Counter "Cnt" is rising even if there is no magnet. When I put the magnet next to the Hall Sensor the LCD-Display stops updateing.

When I comment out the part "updateDisplay()" the Hall Sensor and the Serial Monitor work great. When I add one of the following lines (so send a signal to LCD I Think)
lcd.clear(); ,
lcd.setCursor(1,0); or
lcd.print(Cnt);
(it doesnt matter which one) the problem starts and I dont get what i expect.

Do you know what I do wrong? Is it the wiring or the code? Ive already changed the LCD and when I use a Photoelectric sensor (KY-010) there is exactly the same problem.

#include <Wire.h>                   // I2C-Library for LCD-Display 
#include <LiquidCrystal_I2C.h>      // Library for LCD-Display

// LCD Display
LiquidCrystal_I2C lcd(0x27,16,2);   // set the LCD address to 0x27 for a 16 chars and 2 line display

// Hall Effect Sensor
#define HALL_SIGNAL_PIN 2
byte hallState = 0;
byte hallValue = 1;         // 1 = no magnet, 0 = magnet
int Cnt = 0;


void setup()
{
  pinMode(HALL_SIGNAL_PIN, INPUT_PULLUP);
  setupDisplay();
  Serial.begin(9600);
}

void loop()
{
  runHallSensor();
  updateDisplay();
  serialPrint();
  delay(250);
}


void serialPrint()
{
  Serial.print("Cnt = ");
  Serial.println(Cnt);
}

void runHallSensor()
{
  hallValue = digitalRead(HALL_SIGNAL_PIN);
  if (hallState == 0 && hallValue == 0)
  {
    hallState = 1;
    Cnt++;
  }
  else if (hallState == 1 && hallValue == 1)
  {
    hallState = 0;
  }
}

void setupDisplay()
{
  lcd.init();
  lcd.backlight();
  lcd.setCursor(2,0);
  lcd.print("Hello World");
  delay(1000);
}

void updateDisplay()
{
  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print(Cnt);
}

C:\Users\xxx\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\variants\leonardo\pins_arduino.h:

#define PIN_WIRE_SDA         (2)
#define PIN_WIRE_SCL         (3)

You have a conflict on pin number 2.

What do you mean with "conflict"? The Pin 2 and SDA Pin are two differnt ones, aren't they? How can they be in conflict?

No, they are the same on Leonardo. Check the docs or use a ohm-meter.

Ooooooh, now I can see it.... Why the hell is someone making two searat SCL / SDA on the board? :smiley: I guess someone because someone knows more about it than me xD

THANK YOU VERY MUCH! I was searching this connection the whole time biut I did not get this before. You saved my week.

Because of the same headers as on Arduino UNO. You are welcome.

Welcome to the forum, BTW. Good Luck!

Jean-Marc

1 Like

So does that mean, that when I use the LCD-I2C-stuff I can not use the INT0 and INT1 for interrupts anymore?

I don't know these details. I am more familiar with ESP8266, ESP32, STM32...

1 Like

ok, thanks a lot

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