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);
}

