Hall sensor and stepper motor Arduino code help.

Thread Starter

rory_malone

Joined Mar 31, 2020
16
Hello,

I am a bit stuck with an Arduino project that I am working on and am wondering if anyone could give me some advice ? :)

I have a lazy Suzanne bearing that I wish to rotate between two specific places. I have a stepper motor on the side of the bearing pushing it in one direction I also have 2 linear hall sensors and two magnets mounted beneath the bearing to let the stepper motor know where it is, but I cannot figure out how to code the hall sensors correctly.

I wish the two hall sensors to be the start point and the end point of the bearings journey. The motor will have to rotate until one of the magnets triggers a hall sensor then the motor will have to reverse its direction in the opposite way until it reaches the other hall sensor where another magnet will trigger it to then send the motor back in the other direction again.

I am using linear hall sensors so they read HIGH until a magnet is near them and then they read LOW, but only when the magnet is near, so I would need some sort of condition in the code that keeps the motor going in the opposite direction until it meets the other hall sensor.

Currently I have only been able to switch the direction of the motor by holding a magnet beside the hall sensor. ( not very good I know :(. )
I also have latching hall effect sensors, but I feel that the linear ones would be better for this project

Any help on this would be greatly appreciated.

Thank you !

-Rory


Code Below
--------------------------------------
const int LedPin = 7;
const int SensorPin1 = 2;
const int SensorPin2 = 3;
int SensorValue1 = 0;
int SensorValue2 = 0;
#include <AccelStepper.h>

#define FULLSTEP 4

#define motorPin1 8 // Blue - 28BYJ48 pin 1
#define motorPin2 9 // Pink - 28BYJ48 pin 2
#define motorPin3 10 // Yellow - 28BYJ48 pin 3
#define motorPin4 11 // Orange - 28BYJ48 pin 4


// The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48
AccelStepper stepper1(FULLSTEP, motorPin1, motorPin3, motorPin2, motorPin4);


void setup()
{

Serial.begin(9600);
pinMode (SensorPin1,INPUT);
pinMode (SensorPin2,INPUT);


stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(800.0);



}


void loop()
{

SensorValue1=digitalRead(SensorPin1);

Serial.println(SensorValue1);

if(SensorValue1==LOW) {
stepper1.moveTo(20000);
stepper1.run();}
else {
stepper1.moveTo(-20000);
stepper1.run();}

}
 
Top