I'm not from electronics background so I don't know what should be the connections. My Hall sensor has 4 pins(AO, GND, +, DO)
So, I have connected hall sensor pins to Arduino nano pins in this order(connection)
AO(Sensor) <-> A4(Arduino) [Changed from A0 to A4]
DO(Sensor) <-> D4(Arduino) [Changed from D0 to D4]
+(Sensor) <-> 5V(Arduino)
GND(Sensor) <-> GND (Arduino)
What should be pseudo code? So that if the magnet passes close to the sensor. Arduino should get the output & I'll blink the led on Arduino
This below is my code that I have written so far
int sensorPin = 4; // this is for sensor in analog pin 4
int buttonPin = 4; // we are looking for digital 4
int val;
bool flag = false;
void setup() {
// put your setup code here, to run once:
pinMode(buttonPin, INPUT); // defines digital pin 4 as input for LED
Serial.begin(9600);
}
void loop() {
int reading = analogRead(sensorPin);
float voltage = reading * 5.0;
voltage /= 1024.0;
if(voltage > 3.0) {
Serial.print(voltage); Serial.println("volts");
}
val = digitalRead(buttonPin); // read data from digital Pin
if(val == HIGH) // when the sensor detects a magnetic field
{
flag = !flag;
if(flag){
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
} else { // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
}
//delay(1000); // wait for a second
}
delay(100);
}
Expected output: It should turn On/Off the LED when voltage goes HIGH
Known issues: It works sometime. It doesn't work sometime. Also it works only when magnet is kept clone. Doesn't detect when I'm waving magnet close to the sensor