2

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

1
  • I guess you might have a bouncing button. Have a search for "debouncing a switch", there are lots of different solutions. The magnet has nothing to do with the LED, the analog reading is not used to trigger the LED in this code. Commented Aug 21, 2017 at 12:47

2 Answers 2

2

The board you have has two outputs - AO and DO (note, those are the letter O not the number zero - for output).

You can connect AO to any of the analog inputs on your board if you want to read a discrete analog value from the sensor.

You can connect DO to any of the digital inputs (or analog ones since they are also digital inputs) if you just want a simple "Something is there" signal.

The sensitivity of the "something is there" is controlled by the trimmer on the board.

The simplest way of doing what you want is to use DO and adjust the trimmer to suit. Then the sensor looks, to all intents and purposes, like a switch or a button (but you don't need the pull up / pull down resistor). So you can leave AO disconnected.

Note that using D0 (zero) and D1 (one) on the Arduino is discouraged since they are connected to the USB interface and using them can affect your ability to upload sketches. It also means you cannot use any serial communication in your program.

2
  • yes done also please see my code. It works & doesn't work. Can you tell me why this strange behaviour. Detection should be very quick right? Commented Aug 21, 2017 at 12:26
  • Not with a 100ms delay in there it won't be - it will be slow.
    – Majenko
    Commented Aug 21, 2017 at 12:31
0

Someone will probably post a comment saying this is not a code writing service, but I get the impression that you just need pointers, so here they are.

In the IDE you have an example on how to blink the LED, called blink, if you haven't already make yourself familiar with that.

I think there is an example on how to read from the analog pin, if not its int value = analogRead(A0);

Now print that value to the Serial monitor and see what happens when you start waving the magnet around. The value will change say for 400 odd to 800 odd.

Now write an if statement that turns on the LED when the value you read is greater than (>) a certain value.

If you need any help on the code you need to write look in the Help->Reference section of the IDE, you might find the Bold words especially relevant :)

If you get stuck, post the code of how far you have got and people will help.

1
  • Hi @CodeGorilla can you please verify my code & what's wrong with the code. It's not behaving as expected Commented Aug 21, 2017 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.