Hall effect sensor arduino help !

Thread Starter

rory_malone

Joined Mar 31, 2020
25
Hello,

I am trying to get this action to work with a the AccelStepper library. The movement can be seen here in this video at 22:43
Screen Shot 2020-05-30 at 13.01.28.pngScreen Shot 2020-05-30 at 13.01.36.png

I have two linear hall sensors and a 28BYJ-48 stepper motor. But I am really stuck on the code for this.

Any help would be greatly appreciated
Thank you !

-Rory
 

KeithWalker

Joined Jul 10, 2017
3,063
I sat through the video but I still don't know exactly what you are trying to do, how you are trying to do it or what is not working. A brief explanation of what you are trying to do with a circuit diagram and a description of what is not working would be much more informative. Then we may be able to help you.
Regards,
Keith
 

Thread Starter

rory_malone

Joined Mar 31, 2020
25
Yes ,sorry I will include a more detailed description now.

I am trying to have two hall sensors as two limit switches. The circut I have looks like this https://www.circuito.io/app?components=9240,9312,9312,9442,11021. but i am using a ULN2003 driver board instead of the Easy Driver.

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 am using the accelstepper library.

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();}

}
 

KeithWalker

Joined Jul 10, 2017
3,063
If you are using a linear sensor, it would be better to connect it's output to an analog pin. Then you can adjust the threshold level to suit your setup.
e.g.:

if (analogRead (A3) > 453) // 453 is just a random threshold level that I chose.

Regards,
Keith
 

Thread Starter

rory_malone

Joined Mar 31, 2020
25
Hi Keith,

Thank you very much for your advice !
I hooked up the stepper up with the two linear hall sensors on the analog inputs, but am still quite stuck as to how I can get the motor to continue in the opposite direction even when the hall sensor. I have written the code like this

const int SensorPin1 = A0;
const int SensorPin2 = A1;
int SensorValue1 = 0;
int SensorValue2 = 0;

int directionPlus = 0;
int directionMinus = 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


// Define two motor objects
// 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(50.0);



}


void loop()
{

SensorValue1=analogRead(A0);
SensorValue2 =analogRead(A1);



if(SensorValue1<500) {
stepper1.moveTo(-300);
stepper1.setSpeed(600);
stepper1.run();}

else {
stepper1.moveTo(300);
stepper1.setSpeed(600);
stepper1.run();}


This would only be for one of the sensors, but it dosent seem to be working, The motor just rotates in the same direction not matter what the input. I also think that this is the wrong way to go about coding this. Maybe something with switchstates might be better. Would you have any advice on this ?
Thank you.

Rory

p.s. For some reason when I call serial.print the motor slows down significantly. Very perculiar
 

KeithWalker

Joined Jul 10, 2017
3,063
Hi Keith,


}


void loop()
{

SensorValue1=analogRead(A0);
SensorValue2 =analogRead(A1);



if(SensorValue1<500) {
stepper1.moveTo(-300);
stepper1.setSpeed(600);
stepper1.run();}

else {
stepper1.moveTo(300);
stepper1.setSpeed(600);
stepper1.run();}


This would only be for one of the sensors, but it dosent seem to be working, The motor just rotates in the same direction not matter what the input. I also think that this is the wrong way to go about coding this. Maybe something with switchstates might be better. Would you have any advice on this ?
Thank you.

Rory

p.s. For some reason when I call serial.print the motor slows down significantly. Very perculiar
First, have you made sure that the software will rotate your motor in both directions? Once you have confirmed that, change the "else" statement to another "if" to reverse the motor depending on the value of "SensorValue2".
The motor slows down because the slow serial interface transmissions become part of the motor pulse timing.
Regards,
Keith
 

Thread Starter

rory_malone

Joined Mar 31, 2020
25
Oh sorry yeah the motor will only rotate clockwise no matter what minus values I include before the moveTo commands. Even if I remove the if statements and just have

stepper1.moveTo(3000);
stepper1.setSpeed(600);
stepper1.run();

no matter if i have minus or not it only rotates in the same direction. But in this nearly identical sketch it rotates in both directions, depending on if i put in a plus or minus.


void setup()
{
// 1 revolution Motor 1 CW
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(50.0);
stepper1.setSpeed(200);
stepper1.moveTo(2048);


}


void loop()
{
//Change direction at the limits
stepper1.moveTo(-300);
stepper1.run();


}
 

KeithWalker

Joined Jul 10, 2017
3,063
I have not used the library "AccelStepper.h" so I am not familiar with the commands.
I suggest that you develop your program in easy stages and test each stage as you go. Write a simple program that turns the motor in one direction. test it. Add a sensor so that the motor will run until the sensor detects the magnet. Test it.
Write another separate program that makes the motor run in the opposite direction. Test it. Add the other sensor. Test it. When both programs will run, combine them into one program.
Regards,
Keith
 
Top