Hi,
From the datasheet.
Tom...
Hi, this may be what you are trying for.
I have done it with very little coffee booster, but hope I got it all.
Thanks for the reply DrDiettrich and TomGeorge
I was sooo blind... TomGeorge you are right!
I switched between Vs and Vss pin.... that was the reason I get so much low voltage on the motor.
So stupid mistake.....
Thank you so much. I really appreciate!
Now, I'm going to use latching relays to change the direction of the motor.
Hi,
Glad it resolved your problems.
Now, I'm going to use latching relays to change the direction of the motor.
Why??
Tom..... ![]()
Hi, TomGeorge
Because I cannot make my code work.
I tried so many time in different way and it wasn't moving properly.
It seems like Arduino is not giving continuous signal so I decide to use latching relay between L298n and arduino to give continuous signal.
I tried the code below, it's only moving during the sensor triggered.
Sensor should change the direction, not moving the motor.
I will try other code, since we changed the digital pin 0 to analog pin 5
Thank you!
//set pin number
int Sensor = 13;
int EnM1 = 5;
int EnM2 = 6;
int EnM3 = 9;
int EnM4 = 10;
const int motor1A= 19;
const int motor1B = 4;
const int motor2A = 2;
const int motor2B = 3;
const int motor3A = 7;
const int motor3B = 8;
const int motor4A = 11;
const int motor4B = 12;
// Variables
int sensordetect;
void setup() {
//Set pin I/O
pinMode(Sensor, INPUT_PULLUP);
pinMode(EnM1, OUTPUT);
pinMode(EnM2, OUTPUT);
pinMode(EnM3, OUTPUT);
pinMode(EnM4, OUTPUT);
pinMode(motor1A, OUTPUT);
pinMode(motor1B, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2B, OUTPUT);
pinMode(motor3A, OUTPUT);
pinMode(motor3B, OUTPUT);
pinMode(motor4A, OUTPUT);
pinMode(motor4B, OUTPUT);
}
// forward direction
void forward()
{
analogWrite(EnM1, 255);
analogWrite(EnM2, 255);
analogWrite(EnM3, 255);
analogWrite(EnM4, 255);
digitalWrite(motor1A,HIGH);
digitalWrite(motor2A,HIGH);
digitalWrite(motor3A,HIGH);
digitalWrite(motor4A,HIGH);
digitalWrite(motor1B,LOW);
digitalWrite(motor2B,LOW);
digitalWrite(motor3B,LOW);
digitalWrite(motor4B,LOW);
}
//reverse direction
void reverse()
{
analogWrite(EnM1, 255);
analogWrite(EnM2, 255);
analogWrite(EnM3, 255);
analogWrite(EnM4, 255);
digitalWrite(motor1B,HIGH);
digitalWrite(motor2B,HIGH);
digitalWrite(motor3B,HIGH);
digitalWrite(motor4B,HIGH);
digitalWrite(motor1A,LOW);
digitalWrite(motor2A,LOW);
digitalWrite(motor3A,LOW);
digitalWrite(motor4A,LOW);
}
void loop() {
sensordetect = digitalRead(Sensor);
if (sensordetect == HIGH) {
reverse();
}
else {
forward();
}
}
Hi,
const int motor1A= 19;
make
const int motor1A= A0;
I think.. Tom.. ![]()
TomGeorge:
Hi,const int motor1A= 19;make
const int motor1A= A0;I think.. Tom..
Hi,
Seems like both way working
I found a problem.
Arduino PINs that I set to Motor B (4,3,8,12), are sending 5V. Motor A PINs(A5,2,7,11) are sending 0 V, which means reverse.
It should be other way because I'm not sending 5V to Sensor PIN (13). It should be forward.
Also, If I send a 5V to Sensor pin (13).
Same voltage coming out from same pin.
it should change... why not??
anyone know why????
My arduino PIN number 2 is sending 5 V
Hi,
I tried the code below, it's only moving during the sensor triggered.
Sensor should change the direction, not moving the motor.
You are relying on the sensor STATE to CONTROL motor direction.
You should be using CHANGE in sensor state to CHANGE motor direction.
Tom... ![]()
TomGeorge:
Hi,You are relying on the sensor STATE to CONTROL motor direction.You should be using CHANGE in sensor state to CHANGE motor direction.
Tom...
wow fast reply.
I found sensor PIN (13) is always gives 5V. If I put this to ground, arduino change reverse to forward.
I got an idea. I'll use CHANGE instruction.
Thank you.
void loop() {
sensordetect = digitalRead(Sensor);
if (sensordetect == HIGH) {
state = !state;
reverse();
}
else if (sensordetect == LOW) {
forward();
}
}
shouldn't it work?? hmmm
Hi,
In your code the only thing that makes you motor go in reverse is the sensor being ON, when the motor reverses it comes of the sensor so it turns OFF, so your motor goes forward.
What is supposed to happen when the motor reverses, what stops it , what starts it.
Can you, using point form, describe the action of your machine of walking up stairs, what starts it what stops it etc.
Tom... ![]()
Hi,
What is Point form...
SW ON( 9V arduino powers up) -> moving forward -> reach top floor, inductive sensor detect metal piece -> reverse -> reach bottom Manually SW off
Maybe I should add some button to start.?
Also, if I use boolean, can I save the previous state? so it keep moves reverse even the senser turns off?
Hi,
To keep it simple, just turning the Arduino ON and OFF to start and stop the process will be enough.
You need to code in this sort of order.
Assuming you are at the bottom of the stars.
You have to have a variable , call it FWDflag, it will set the direction of you motor, it gets changed when the sensor is activated.
Switching power OFF to STOP it then ON the START sets the flag in SETUP and starts the FWD motion.
Tom... ![]()
Hi,
Point 7 is probably the main part that need to be right as it senses when the Sensor has gone from OFF to ON.
Tom... ![]()
I don't know... not working...
it's worse...lol even if I put my sensor PIN (13) to ground, just same
always all Motor B stay HIGH, A stay LOW
//set pin number
int Sensor = 13;
int EnM1 = 5;
int EnM2 = 6;
int EnM3 = 9;
int EnM4 = 10;
const int motor1A= A5;
const int motor1B = 4;
const int motor2A = 2;
const int motor2B = 3;
const int motor3A = 7;
const int motor3B = 8;
const int motor4A = 11;
const int motor4B = 12;
// Variables
int sensordetect;
int FWDflag;
void setup() {
//Set pin I/O
pinMode(Sensor, INPUT_PULLUP);
pinMode(EnM1, OUTPUT);
pinMode(EnM2, OUTPUT);
pinMode(EnM3, OUTPUT);
pinMode(EnM4, OUTPUT);
pinMode(motor1A, OUTPUT);
pinMode(motor1B, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2B, OUTPUT);
pinMode(motor3A, OUTPUT);
pinMode(motor3B, OUTPUT);
pinMode(motor4A, OUTPUT);
pinMode(motor4B, OUTPUT);
int FWDflag = 1;
}
// forward direction
void forward()
{
analogWrite(EnM1, 255);
analogWrite(EnM2, 255);
analogWrite(EnM3, 255);
analogWrite(EnM4, 255);
digitalWrite(motor1A,HIGH);
digitalWrite(motor2A,HIGH);
digitalWrite(motor3A,HIGH);
digitalWrite(motor4A,HIGH);
digitalWrite(motor1B,LOW);
digitalWrite(motor2B,LOW);
digitalWrite(motor3B,LOW);
digitalWrite(motor4B,LOW);
}
//reverse direction
void reverse()
{
analogWrite(EnM1, 255);
analogWrite(EnM2, 255);
analogWrite(EnM3, 255);
analogWrite(EnM4, 255);
digitalWrite(motor1B,HIGH);
digitalWrite(motor2B,HIGH);
digitalWrite(motor3B,HIGH);
digitalWrite(motor4B,HIGH);
digitalWrite(motor1A,LOW);
digitalWrite(motor2A,LOW);
digitalWrite(motor3A,LOW);
digitalWrite(motor4A,LOW);
}
void loop() {
sensordetect = digitalRead(Sensor);
if (sensordetect == HIGH) {
FWDflag = 0;
if (FWDflag == 0) {
reverse();
}
else if (FWDflag == 1) {
forward();
}
}
}
Hi,
Try this.
//set pin number
int Sensor = 13;
int EnM1 = 5;
int EnM2 = 6;
int EnM3 = 9;
int EnM4 = 10;
const int motor1A = A5;
const int motor1B = 4;
const int motor2A = 2;
const int motor2B = 3;
const int motor3A = 7;
const int motor3B = 8;
const int motor4A = 11;
const int motor4B = 12;
// Variables
int sensordetect;
int FWDflag;
int oldsensor;
void setup() {
//Set pin I/O
pinMode(Sensor, INPUT_PULLUP);
pinMode(EnM1, OUTPUT);
pinMode(EnM2, OUTPUT);
pinMode(EnM3, OUTPUT);
pinMode(EnM4, OUTPUT);
pinMode(motor1A, OUTPUT);
pinMode(motor1B, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2B, OUTPUT);
pinMode(motor3A, OUTPUT);
pinMode(motor3B, OUTPUT);
pinMode(motor4A, OUTPUT);
pinMode(motor4B, OUTPUT);
FWDflag = 1;
oldsensor = 0;
}
// forward direction
void forward()
{
analogWrite(EnM1, 255);
analogWrite(EnM2, 255);
analogWrite(EnM3, 255);
analogWrite(EnM4, 255);
digitalWrite(motor1A, HIGH);
digitalWrite(motor2A, HIGH);
digitalWrite(motor3A, HIGH);
digitalWrite(motor4A, HIGH);
digitalWrite(motor1B, LOW);
digitalWrite(motor2B, LOW);
digitalWrite(motor3B, LOW);
digitalWrite(motor4B, LOW);
}
//reverse direction
void reverse()
{
analogWrite(EnM1, 255);
analogWrite(EnM2, 255);
analogWrite(EnM3, 255);
analogWrite(EnM4, 255);
digitalWrite(motor1B, HIGH);
digitalWrite(motor2B, HIGH);
digitalWrite(motor3B, HIGH);
digitalWrite(motor4B, HIGH);
digitalWrite(motor1A, LOW);
digitalWrite(motor2A, LOW);
digitalWrite(motor3A, LOW);
digitalWrite(motor4A, LOW);
}
void loop() {
sensordetect = digitalRead(Sensor);
if (sensordetect == 1 && oldsensor == 0) // check to see if sensor changed state
{
FWDflag = 0;
oldsensor = 1;
}
if (FWDflag == 1) // Sets Motor direction
{
forward();
}
else
{
reverse();
}
}
It compiles, but I don't have anything setup to try it.
Tom.... ![]()
Same thing happening
I don't understand
code seems ok but it's not working haha weird
simple if else statement was kindda worked though...
maybe I'll use a latching relay on pin 13
Hi,
What exactly happens with my code, from turning it ON?
I goes FWD, get to activate the sensor, then what?
Tom.... ![]()
Hi,
Try this code.
It just runs the motors fwd and rev in a 5 second continuous cycle.
Let it run for about 30Seconds.
//set pin number
int Sensor = 13;
int EnM1 = 5;
int EnM2 = 6;
int EnM3 = 9;
int EnM4 = 10;
const int motor1A = A5;
const int motor1B = 4;
const int motor2A = 2;
const int motor2B = 3;
const int motor3A = 7;
const int motor3B = 8;
const int motor4A = 11;
const int motor4B = 12;
// Variables
int sensordetect;
int FWDflag;
int oldsensor;
void setup() {
//Set pin I/O
pinMode(Sensor, INPUT_PULLUP);
pinMode(EnM1, OUTPUT);
pinMode(EnM2, OUTPUT);
pinMode(EnM3, OUTPUT);
pinMode(EnM4, OUTPUT);
pinMode(motor1A, OUTPUT);
pinMode(motor1B, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2B, OUTPUT);
pinMode(motor3A, OUTPUT);
pinMode(motor3B, OUTPUT);
pinMode(motor4A, OUTPUT);
pinMode(motor4B, OUTPUT);
FWDflag = 1;
oldsensor = 0;
}
// forward direction
void forward()
{
analogWrite(EnM1, 255);
analogWrite(EnM2, 255);
analogWrite(EnM3, 255);
analogWrite(EnM4, 255);
digitalWrite(motor1A, HIGH);
digitalWrite(motor2A, HIGH);
digitalWrite(motor3A, HIGH);
digitalWrite(motor4A, HIGH);
digitalWrite(motor1B, LOW);
digitalWrite(motor2B, LOW);
digitalWrite(motor3B, LOW);
digitalWrite(motor4B, LOW);
}
//reverse direction
void reverse()
{
analogWrite(EnM1, 255);
analogWrite(EnM2, 255);
analogWrite(EnM3, 255);
analogWrite(EnM4, 255);
digitalWrite(motor1B, HIGH);
digitalWrite(motor2B, HIGH);
digitalWrite(motor3B, HIGH);
digitalWrite(motor4B, HIGH);
digitalWrite(motor1A, LOW);
digitalWrite(motor2A, LOW);
digitalWrite(motor3A, LOW);
digitalWrite(motor4A, LOW);
}
void loop() {
sensordetect = digitalRead(Sensor);
if (sensordetect == 1 && oldsensor == 0) // check to see if sensor changed state
{
FWDflag = 0;
oldsensor = 1;
}
if (FWDflag == 1) // Sets Motor direction
{
forward();
}
else
{
reverse();
}
}
When the sensor is activated, and the relay closes, does that pull pin 13 high or low?
If pin13 is pulled HIGH or 5V then you need to change
pinMode(Sensor, INPUT_PULLUP);
to
pinMode(Sensor, INPUT);
and put a 10K resistor from pin13 to gnd.
Tom.... ![]()
Hi, Tom
sorry for late reply.
Both of your code acts like this.
All the Motor B HIGH, from the beginning, A LOW->
when during the Sensor ON, PIN 13 LOW. However, still all the Motor B stays HIGH, A LOW
However, very simple if else below shows
All Motor B HIGH from the beginning, A LOW -> during the sensor ON, B LOW and A HIGH.
I don't understand......
void loop() {
sensordetect = digitalRead(Sensor);
if (sensordetect == HIGH) {
reverse();
}
else {
forward();
}
}
sensor connection is like this
PIN 13 is always giving 5V so I connected to Ground(when the sensor ON)
I also tried with 5V other than Ground but did not work.