These are the hall effect sensor i have tested out.
Meanwhile, I am playing with my new nano board. But the magnet I have here is not strong enough. It need to be very close to the sensor (like 1cm). Gotto order new
rare earth magnet from dx.com then.
Simple code to output the analog in reading, and if less then the threshold, light the LED.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to const int analogOutPin = 9; // Analog output pin that the LED is attached to const int ledPin = 13; int sensorValue = 0; // value read from the pot int outputValue = 0; // value output to the PWM (analog out) int threshold = 515; void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); pinMode(ledPin, OUTPUT); } void loop() { // read the analog in value: sensorValue = analogRead(analogInPin); // map it to the range of the analog out: //outputValue = map(sensorValue, 0, 1023, 0, 255); // change the analog out value: //analogWrite(analogOutPin, outputValue); if(sensorValue < threshold) digitalWrite(ledPin, HIGH); else digitalWrite(ledPin, LOW); // print the results to the serial monitor: Serial.print("sensor = " ); Serial.print(sensorValue); Serial.println(""); // wait 2 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(2); } |
This sensor in red board is a
linear hall effect sensor.
Next, I try on the black board one. According to dx.com
comment, this is a digital output and 10k resister is required between the 5v & signal pin.
After placing the resistor in between, i getting reading of 1023 (off magnet) & 15 (with magnet). It is analog but the value only 2 state, so like the comment said, it did behave like digital. I can then use a
map function to turn the value into either 0 or 1.
Next, I tried another sensor that looks exactly the same like above, except the packaging label with “analog hall sensor”. with the same circuit (with resistor removed) and code, this is what i observed:
- without magnet, ouput reading at 522
- with magnet place in direction of the top of board, output reading range between 522- 770
- with magnet place in direction of the bottom of board, output reading range between 522-413
Interesting! seems like it can tell at which direction the magnet is coming from.