Help to program and ky-003 module

i have a program and it works, but insted of contact1pin, I want to youse the hallsenor magic switch KY-003, but if I delete contctpin1 and put in the hall sensor it doesn't work.
how do I program it so I can youse the high and low?
i have try to do this

// Defined pin-numrene for motorerne and concact
const int motor1ForwardPin = 2;
const int motor1BackwardPin = 3;
const int motor2Pin = 4;
const int contact2Pin = 6;
const int enA = 7;
const int enB = 8;
const int motor2pinback = 7;
const int hallsensorpin = 9;
int halldigital;

void setup() {
  // put pin-mode for motorer og contact
  pinMode(motor1ForwardPin, OUTPUT);
  pinMode(motor1BackwardPin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(contact2Pin, INPUT_PULLUP);
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(motor2pinback, OUTPUT);
  pinMode(hallsensorpin, INPUT) ;

  
  // motor 1 forward, until contact 1 hits
  int halldigital = digitalRead(hallsensorpin);
  while (digitalRead(halldigital) == LOW) {
    digitalWrite(motor1ForwardPin, HIGH);
    digitalWrite(motor1BackwardPin, LOW);
  digitalWrite(motor2Pin, HIGH);
  digitalWrite(motor2pinback, LOW);
  digitalWrite(enA, HIGH);
  digitalWrite(enB, HIGH);
  }



  // motor 1 drivning bag until concact 2 har hit
  while (digitalRead(contact2Pin) == LOW) {
    digitalWrite(motor1ForwardPin, LOW);
    digitalWrite(motor1BackwardPin, HIGH);
  }

  // Stop motor 1 and 2 when contact 2 are push
  
  digitalWrite(motor1ForwardPin, LOW);
  digitalWrite(motor1BackwardPin, LOW);
   digitalWrite(motor2Pin, LOW);
  


}


void loop() {
}```

this program works with the contact1pin.

// Defined pin-numrene for motorerne and concact
const int motor1ForwardPin = 2;
const int motor1BackwardPin = 3;
const int motor2Pin = 4;
const int contact1Pin = 5;
const int contact2Pin = 6;
const int enA = 7;
const int enB = 8;
const int motor2pinback = 7;

void setup() {
// put pin-mode for motorer og contact
pinMode(motor1ForwardPin, OUTPUT);
pinMode(motor1BackwardPin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(contact1Pin, INPUT_PULLUP);
pinMode(contact2Pin, INPUT_PULLUP);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(motor2pinback, OUTPUT);

// motor 1 forward, until contact 1 hits
while (digitalRead(contact1Pin) == LOW) {
digitalWrite(motor1ForwardPin, HIGH);
digitalWrite(motor1BackwardPin, LOW);
digitalWrite(motor2Pin, HIGH);
digitalWrite(motor2pinback, LOW);
digitalWrite(enA, HIGH);
digitalWrite(enB, HIGH);
}

// motor 1 drivning bag until concact 2 har hit
while (digitalRead(contact2Pin) == LOW) {
digitalWrite(motor1ForwardPin, LOW);
digitalWrite(motor1BackwardPin, HIGH);
}

// Stop motor 1 and 2 when contact 2 are push

digitalWrite(motor1ForwardPin, LOW);
digitalWrite(motor1BackwardPin, LOW);
digitalWrite(motor2Pin, LOW);

}

void loop() {
}

forgot to say that the item I programming need to run forward with both motor until the item hits the magnet KY-003 and then motor 1 goes backward while motor 2 keep going forward until the item hits concact 2 then it all stops.

why not just change contact1Pin from 5 to 9?

is the polarity of both devices the same, HIGH/LOW?

add serial prints to show the values input to the code

the connects on pin 5 needs to been removed from the program if the ky-003 can work.
I think ky-003 is a analog and the output is voltage.

why? is some other device on pin5? can't it be ignored?

then shouldn't it be connected to an analog pin, A0-A5 and use analogRead() to read the voltage?

Not the one I just found googling.

It should be a simple replacement, you still need an INPUT pin, and the HIGH/LOW may be inverted to what your code expects.

a7

I try just to replace it, and it doesn’t work. The right program with the 2 contacts work so the ithem go forward until the 1 contacts it hit and then back.
If I replace the first contacts with the magnetic switch then the Ithem drive forward and keep going past it, it doesn’t matter if I chance it to high or low.
I think there is something with the code for the magnetic switch.
I’m new at the arduino programming.

Hi, @have13

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
Using the original setup on the contactPin that works.

Can you please post a diagram just showing how you connected the KY-003 sensor.

We need to know this so we can give you definitive answers.

Please do not do a Fritzy diagram, DRAW IT.

Do you have a DMM? (Digital MultiMeter)

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

You need to add a pullup resistor on your signal pin.
The KY-003 is indeed digital, contrary to some suggestions here, but it's signal is open collector, hence the need for the pullup.
There is an LED and a small resistor tied from positive rail to the signal on some KY-003, which in most instances is enough.
Be aware the hall effect is polarity sensitive with respect to the magnet.
So, your signal will be low with the hall under the influence of the magnet.

KY003

Included below is a pdf on the module which uses the A3141 hall effect switch and in there, you will find some simple Arduino code to get you started.
KY-003.pdf (387.7 KB)

why not configure the pin an INPUT_PULLUP to use the internal pull-up resistor?

As you would be aware, sometimes this pullup is too weak.
Why complicate the situation over the addition of a 1 cent resistor?

At this stage irrelevant as Op did not get back with the exact module in use which may already have a resistor and LED which would in most instances, be sufficient.

In the link I gave, there was code which should prove one way or the other.

why not just first suggest a trivial code change ??

....."fixing" with software is not always a be-all-end-all solution.
As already noted, the Op has not replied so ......who knows?

Software doesn't require a 1 cent resistor which requires the purchase of 300 resistors. It's like s/w debounce versus a h/w fix. S/W is less than 1 cent.

It was just plug and play and high/low signal that needs to be changed.
I have a question about the speed of the 2 motors, before on the same program it run fast, but now it run slow.
It should be like that, that the motor run as fast as possible.
Now I don’t have any thing to adjust the speed, so I’m thinking that it’s is running as fast as possible like if I just take the motor and put on power with out any other things on, so it the arduino driver built up like that?

with some drivers, if you forget to provide the separate power for the motor, it uses the 5V from the arduino and the motors run slower, if at all.

I youse 7,2 volt supply on the motor driver and from that I provided the power to the uno board so every thing is 7,2 volt

i have now try to put the motor straight to the battery and it drives much faster with out the Arduino, i also have try to put 5 volt on uno board and 7,2 volt on the motor driver same result as the 7,2 volt on motor driver and from there to uno bard.
how do I get the motors to drive faster with the bord?

Hi,
What motor driver are you using?
Can you please post a link to specs/data?
Is it one of thses?


If so have you looked at the spec for the IC, L298n?

Two BJTs conduct when the motor is powered.
With both dropping 1.2V and 1.35V saturated, that's 2.55V you will loose from the 7.2V of your supply.

( 2.55 / 7.2 ) * 100 = 35% of available potential lost to the 298.

If you want to improve that figure, you need to look for a MOSFET device not a BJT device.

Tom... :smiley: :+1: :coffee: :australia:

Yes it’s that one. But if I put 12 v on it’s the same as 7.2v, same speed.
If I buy mosfet, I’m I going to take the en pin an youse as signal to make the motors running like I what to?