Running 24 volts motor through arduino

Dear all, greetings of the day!

Hope this message finds everyone in good health. I am writing this message becasue I need some assistance with a project I am working on. The below code is going to turn ON and OFF a 24 V motor that is connected through 2 relay modules to a power supply. +24 V turns the motor in forward direction and -24 in reverse. I am using Arduino MKR 1310 WAN and three hall sensors.

Hall sensor 1 would start moving the motor when a magnet comes in front of it. The motor continues to move even if I take off the magnet and the motor would continue to move until I bring a magnet to sensor no. 2 that would stop the motor forward movement. I repeated it several times and I face no problem.

But when I check the reverse motion, sometimes it works, sometimes not, sometimes the motor gives forward movement. For instance, with the command [ if (digitalRead(BUTTON) == HIGH && !stopped) ] in which I actually give a pulse from arduino, because right now I am not using a button. So, I give a pulse from Vcc to pin 9 and the motor would start reverse rotation. But when I try to stop it by bringing the magnet to the sensor 3, sometimes it would stop and sometimes it will make the motor start moving in forward direction rather than stopping it. Can some please assist me that what am I doing wrong?

P.S: Today, I used the same code to run on Arduino UNO and I was surprised to see that when I brought the magnet to sensor 1 it started the motor and when I got it away it stopped the motor which should not have happened. I didn't see this issue on MKR 1300 board.

Your kind suggestion would highly be appreciated. Thank you very much
Bilal

#define hallSensor1 3                                                    // digital pin 3 as input
#define hallSensor2 4                                                    // digital pin 4 as input
#define hallSensor3 2                                                    // digital pin 2 as input
#define relayPin1 6                
#define relayPin2 7
#define BUTTON 9
int state1 = 0, state2 = 0, state3 = 0;                                                                     
bool stopped = false;
bool cycleRelease = false;
         
void setup() {
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);      
  pinMode(hallSensor1, INPUT);
  pinMode(hallSensor2, INPUT);
  pinMode(hallSensor3, INPUT);
  pinMode(BUTTON, INPUT);     
}
void loop(){
   
  state1 = digitalRead(hallSensor1);
  state2 = digitalRead(hallSensor2);
  state3 = digitalRead(hallSensor3);
    
  if (state1 == 0 && !cycleRelease) {        
     digitalWrite(relayPin1, HIGH);                                    // motor forward ON
    }
  if (state2 == 0) {
     digitalWrite(relayPin1, LOW);                                     // motor turns OFF
    }

  if (digitalRead(BUTTON) == HIGH && !stopped){
     digitalWrite(relayPin2, HIGH);                                     // start motor in reverse direction  
     stopped = true;
    } 

  if (state3 == 0 && stopped) {
    digitalWrite(relayPin2, LOW);                                      // turn motor OFF
    stopped = false;
    cycleRelease = true;
    }

    if (state1 == 1){
      cycleRelease = false;
    }
}

I think that a schematic of the wiring and data sheets for the Hall Effect sensors would help us to help you.

Many Hall effect sensors have NPN open collector outputs, try pinMode (INPUT_PULLUP), the pin will read LOW when activated. Post a link or part number for the sensors. How is the button wired?

groundFungus:
I think that a schematic of the wiring and data sheets for the Hall Effect sensors would help us to help you.

Thank you for the response.
Yes, I can do that. just in quick words. I gave 5V and ground from my Arduino board to 3 sensors and the 2 channel relay. Additionally, I have 3 data wires for 3 sensors and 2 signal pins for a relay are connected to the board.

bilal40:
Thank you for the response.
Yes, I can do that. just in quick words. I gave 5V and ground from my Arduino board to 3 sensors and the 2 channel relay. Additionally, I have 3 data wires for 3 sensors and 2 signal pins for a relay are connected to the board.

No, you can not express a schematic in words. It does convey some information, but for a multitude of reasons, it is a severely limited method. Please make a pencil and paper diagram and post a clear image of it. Please also supply the links or part numbers for the actual sensors, that was requested.

JCA34F:
Many Hall effect sensors have NPN open collector outputs, try pinMode (INPUT_PULLUP), the pin will read LOW when activated. Post a link or part number for the sensors. How is the button wired?

I need to check the sensor because it is inside a box and I can’t see what specific type sensor it is. but I see three wires coming out. 1 Vcc, 2 GND, 3 signal pin. Generally, I made a program to check the out, so, it gives 1 as output but when I bring a magnet, it turns to 0.

We need to see exactly how the sensor and everything else is connected.

Attached is the schematic of the setup I used. Today, I tried to power the relay from another source using JD-VCC pin. I don't know it worked for 1 or 2 times, now the Arduino board when I connect, the windows couldn't recognize it.

For the hall effect sensor, I am attaching the link to its datasheet as well. I am using the UA package.

https:://docs.rs-online.com/e64f/0900766b8106a158.pdf

What coil voltage are the relays specified for? You are powering them on 24V.

Looks like OP is powering relay module from 3.3V VCC, which, of course, won't work.
And the HESs are LINEAR and connected to digital inputs. Oh my!

aarg:
What coil voltage are the relays specified for? You are powering them on 24V.

For 5 volts. Which I am providing from Arduino.
Actually with 24 volts what I am doing, making +24 V and -24 volts. When common wire from relay is high and the other is low, I have +24 volts. If common of relay 2 is high and relay one is low i have -24 volts. If both are high or low motor won't start.

JCA34F:
Looks like OP is powering relay module from 3.3V VCC, which, of course, won't work.
And the HESs are LINEAR and connected to digital inputs. Oh my!
https://docs.rs-online.com/e64f/0900766b8106a158.pdf

Sorry, I didn't understand can you please elaborate it a bit? Am I connecting it wrong pins on Arduino? And I am giving 5 volts from Arduino not 3.3 volts.

Instead of : if (digitalRead(BUTTON) == HIGH && !stopped){

try : if ( (digitalRead(BUTTON) == HIGH) && (!stopped) ) {

GRuser:
Instead of : if (digitalRead(BUTTON) == HIGH && !stopped){

try : if ( (digitalRead(BUTTON) == HIGH) && (!stopped) ) {

Yes sure. Thank you for the suggestion. :slight_smile:

You should really power the relays from a separate 5V power supply, by removing the JD-VCC jumper and connecting the pin to an independent power supply.

aarg:
You should really power the relays from a separate 5V power supply, by removing the JD-VCC jumper and connecting the pin to an independent power supply.

Yes, I am doing it now. However, what do you think about what I did before i.e. connecting all three sensors and relay to the Arduino 5V was a bad idea? Was it affecting the data of the sensors? Because, sometimes, the motor would run as expected but sometimes it would do unexpected things. After all, the rotation and stopping of the motor are dependent on the data of the sensors.
And can I keep the sensors powered from the Arduino? Because I tried to power them from another supply but I didn’t get the expected results. I think the max current a sensor draws is 10 mA each.

bilal40:
Yes, I am doing it now. However, what do you think about what I did before i.e. connecting all three sensors and relay to the Arduino 5V was a bad idea? Was it affecting the data of the sensors? Because, sometimes, the motor would run as expected but sometimes it would do unexpected things. After all, the rotation and stopping of the motor are dependent on the data of the sensors.
And can I keep the sensors powered from the Arduino? Because I tried to power them from another supply but I didn't get the expected results. I think the max current a sensor draws is 10 mA each.

Yes, it's a bad idea. You haven't responded to the comment that someone made, that you are using analog Hall sensors and reading them digitally...

Linear HESs give an analog signal from 0 to VCC volts, NOT a HIGH/LOW state, the voltage point where the MCU switches from HIGH to LOW and vice versa depends on whether the signal voltage is rising or falling, will not be well defined and could vary from pin to pin. You have a switch in your code, (#define BUTTON 9) but not in your diagram, how is that switch wired?

JCA34F:
Linear HESs give an analog signal from 0 to VCC volts, NOT a HIGH/LOW state, the voltage point where the MCU switches from HIGH to LOW and vice versa depends on whether the signal voltage is rising or falling, will not be well defined and could vary from pin to pin. You have a switch in your code, (#define BUTTON 9) but not in your diagram, how is that switch wired?

Ohhh my bad. I should have studied the datasheet first and then started working on it. Will be very careful next time. Thank you for your valuable comments. Yeah, I haven’t incorporated the button yet. I will be working on that later. First, I need to make the motor working by using the sensors. But today I made a switch, made the LED turned on and off, then did the debouncing of the switch as well. So, the button part is almost ready, but I have to work on the sensors. I think the wrong use of sensors burned two of my MKR boards, the power light turns ON but I can’t flash anything there. Windows couldn’t recognize it. :frowning:

And ANOTHER thing :slight_smile: , you are powering the HESs with 5V and connecting the signal to 3.3V (I think) inputs. Those HESs MAY be the kind you need, I can't say, 'cause I don't know how your project / device is supposed to work.