Hello all.
Are hall effect sensors "latching" switches? If so, is the LED supposed to stay on?
Context:
I'm working on a stepper motor project and I have the stepper motor working the way we want, but I wanted to incorporate some hall effect sensors to use as end-stops. I wired a couple of them up to my project, passed a magnet in front of them and each one responded with a lit up LED when the magnet passed by it. When I uploaded my code, I couldn't get them to work correctly.
Instead of messing with the entire code, I isolated the sensors by running a minimized version of my code with just the basics.....read the sensors then print out the values in the serial monitor.
When I ran this code, both sensors read high right from the start. When I put a magnet in front of them, the LED would come on and the value in the window would show low (like it's supposed to). When I took the magnet away, the LED went off, but the value stayed at 0 (low). The other sensor reacted the same way. The only way I could "reset" them was to re-set the controller.
Below is a photo of these sensors I am using and the code (in case there is something in there that I messed up).
Thank you for your time,
Ron.
/* StepperZ_Project_rev29 */
/* Program to control stepper motor with joystick along with 2 end stops */
#include <AccelStepper.h>
/* ------- Defining Pins --------------------------------- */
const byte stepPin = D1;
const byte dirPin = D2;
const byte enaPin = D5;
const byte upStop = D6;
const byte downStop = D7;
const byte xJoyPin = A0;
/* ------- Pin Variables --------------------------------- */
int xJoy = 0;
int dirVal = -1;
int upStopVal = 1;
int downStopVal = 1;
/* ------- Variables for Calculations & Settings --------- */
long speedVal = 0.0;
long fastSpeed = 50000.0;
long slowSpeed = 100.0;
long accelFast = 500.0;
long accelSlow = 50.0;
long newPos = 0.0;
/* ------- Define the Stepper Driver --------------------- */
AccelStepper xStepper(AccelStepper::DRIVER, stepPin, dirPin);
/* ---------------- SET - UP FUNCTION -------------------- */
void setup()
/* ------------------------------------------------------- */
{
Serial.begin(115200);
xStepper.setMinPulseWidth(10);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enaPin, OUTPUT);
pinMode(xJoyPin, INPUT);
pinMode(upStop, INPUT);
pinMode(downStop, INPUT);
pinMode(enaPin, LOW);
pinMode(upStop, LOW);
pinMode(downStop, LOW);
xStepper.setMaxSpeed(fastSpeed);
xStepper.setAcceleration(accelFast);
xStepper.setSpeed(0);
delay(5);
}
/* ================= LOOP FUNCTION ======================= */
void loop()
/* ======================================================= */
{
xJoy = analogRead(xJoyPin); /* check initial value of joystick and stops */
upStopVal = digitalRead(upStop);
downStopVal = digitalRead(downStop);
Serial.println("");
Serial.print("Stop values are = ");
Serial.print(upStopVal);
Serial.print(" & ");
Serial.println(downStopVal);
delay(1000);
}
