OH137 Hall Effect Switch

Hi All

I have used OH137 hall effect switches successfully in the past using Leo Bodnar boards, making things like non-contact 'gearboxes' for Race sims

I want to know how to use them with Arduinos. I have no specific project in mind, this is simply trying to learn. I have connected one OH137 and an LED in the following way

and have looked on the web for some code and found this


int hallSensorPin = 6;     
int ledPin =  2;    
int state = 0;          
void setup() {
  pinMode(ledPin, OUTPUT);      
  pinMode(hallSensorPin, INPUT);     
}
void loop(){
  
  state = digitalRead(hallSensorPin);
  if (state == LOW) {        
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    digitalWrite(ledPin, LOW); 
  }

  
Serial.begin(9600);
Serial.println(state);
}

The LED is working fine, but clearly something is up with either the sketch, circuit or both! Even using neodymium magnets I can't get any response from the hall effect sensor

Can anyone advise of how to get the sensor to work with an Arduino?

Cheers

Les

pinMode(hallSensorPin, INPUT_PULLUP);

Why is this in loop(), Les? Think: when have you ever seen this function in loop()?

1 Like

Thanks, that resolved it

As for the Serial.begin (9600), as you point out never - I have put it in it's rightful location

Cheers

Les

Great, but do you understand why INPUT_PULLUP was needed?

As I understand it it it forces the default state to be high or low to make the input easier for the Arduino to read

It is because the sensor has an open collector output.


You need to provide a path for the collector current to flow to get a HIGH or LOW voltage response from the sensor.

In fact if you still have some switching instability you could add a 10K or lower pullup resistor.

Tom.. :smiley: :+1: :coffee: :australia:

Thanks, nice explanation!

I tested out the sketch with the hardware yesterday, and it was totally stable, so it looks like the resistors aren't essential, however I may try it again today with the resistor to see if there is any difference. Would the resistor also potentially improve reliability?

Les

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