Hall Effect Sensor and Motor Control

Hey everyone,
I am looking at turning on a motor when it receives information from an accelerometer to do with a particular coordinates received. Then I wanted to have the motor move until it reaches a position that is defined by a hall effect sensor reaching a magnet. Although, I havent been able to find too much code around using hall effect sensors and motors around. Wondering if anyone has worked with something like this or would have any suggestions on how I’d go about coding something like this!

I used a reed switch + stepper motor in a project quite a while ago. The idea was to use the reed switch to ‘zero’ the motor, so I knew exactly where it was pointing.

Reed switched / hall sensors can act as DIGITAL_INs. So the basic logic went something like this:

  • Step forward until the DIGITAL_IN is high
  • Count the number of forward steps required for the pin to go low again
  • Step back half as many steps.

You should now be roughly centered over your reed switch.

Aaah @beardedinventor thank you!
Here is what I’ve put together so far:

`// These values may be different for your servo
const SERVO1_MIN = 0.00;
const SERVO1_MAX = 0.1;

const SERVO2_MIN = 0.00;
const SERVO2_MAX = 0.1;
// may not need this as servo is just turning in different directions

hardware.pin7.servo1.configure(PWN_OUT_.002_0.0)
//period of motor turning at .002 seconds this means how long the motor is on for
// as it is at 0% high, could mean that if turn it to a negative it turns in reverse
hardware.pin8.servo2.configure(PWN_OUT_.002_0.0)
hardware.pin6.halleffect.configure(DIGITAL_IN)
//configure hall effect1 as in

servo1 <- hardware.pin7;
servo2 <- hardware.pin8;
halleffect1 <- hardware.pin6
halleffect2 <-hardware.pin5

function.sleep()
{
hallState = digitalRead(halleffect1)
//if sleep function is called and the magnet isn’t aligned at low (south)
if (halleffect1 == LOW) {
// if the hall effect is not high then turn motor on
// low is when it is at the south pole of the magnet?
hardware.pin7.servo1.write(PWN_OUT_0.025_1);
//set servo to move up - in required direction for .0025 seconds
// set to one so it is high in the positive direction

until halleffect1 == HIGH
hardware.pin7.servo1.write(PWN_OUT_0.0_0.0)
//turn motor off once get to hall effect
}
else {
hardware.pin7.servo1.write(PWN_OUT_.0.0_0.0)
//motor is off if it is already at south of hall effect

if (halleffect2 == LOW) {
//if the other side of the ball is low then needs to get high, need to move ball
// therefore, if it was high, it wouldn’t move
hardware.pin8.servo2.write(PWN_OUT_0.025_1) ;
// set to one so that it is high in positive direction

//until
hardware.pin5.halleffect2.write(HIGH)
}
else {
hardware.pin8.servo2.write(PWN_OUT_0.0_0.0)

// do it again in half a second:
imp.wakeup(1.0, Sweep);
} Sweep()`

Although, I’m not too sure about the part in the code where I count the number of stops and telling the motor to stop when the hall effect sensor has reached the north of the magnet. I took the hall effect sensor being at the south of the magnet as signalling it was ‘low’. I’m not too sure if that was an appropriate assumption but.

Thanks for your help!