Code failure to control TB6612FNG motor driver

Hello friends,

i am new at arduino and i just tried to control 2 dc motors by an arduino nano. first motor go forward and reverse, second is only forward. and i have a potansiometer to set motor speeds.

i used 9 and 10 pins to use pwm and my code is like below.

my first question is that why the code words so slow about input signals, i read from serial port the changings and i can see changing pot level exact time when i changed it. but i take 5V from arduino pins and thouched to inputs to get signal when i changed ths signal it takes time maybe 10 second or more, and also my input signals changes when i do notinhg and watching. i thought it is because parazites but i dont know how to solve it.

isnt it right to use 5V on arduino to make Input signals.

and can you check my code because my project doesnt works correct motors doesnt works fine.

Thanks.

//motor A connected between A01 and A02
//motor B connected between B01 and B02

bool ileriButton = false; 
bool geriButton = false;   
bool kaziButton = false;      

int STBY = 8; //standby
int pwmValue = 0;

//Kontrol
int analogPin = 3;
int val = 0;   
int ileri = 12;
int geri = 2;
int kazi = 3;

//Motor A
int PWMA = 9; //Speed control
int AIN1 = 4; //Direction
int AIN2 = 5; //Direction

//Motor B
int PWMB = 10; //Speed control
int BIN1 = 6; //Direction
int BIN2 = 7; //Direction

void setup(){
  TCCR1B = TCCR1B & 0b11111000;
pinMode(ileri, INPUT);
pinMode(geri, INPUT);
pinMode(kazi, INPUT);
  
pinMode(STBY, OUTPUT);

pinMode(PWMA, OUTPUT);
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);

pinMode(PWMB, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);

Serial.begin(9600);
}

void loop(){

  ileriButton = digitalRead(ileri);
  geriButton = digitalRead(geri);
  kaziButton = digitalRead(kazi);
  
val = analogRead(analogPin);   // read the input pin

    if (ileriButton == HIGH){
      move(1, val/4, 1); //motor 1, full speed, left
      Serial.print("Motor 1 ileri, Hız: ");
      Serial.print(val/4);
  Serial.println();
  delay(200);
     }
     if (geriButton == HIGH){
      move(1, val/4, 0); //motor 1, full speed, left
      Serial.print("Motor 1 geri, Hız: ");
      Serial.print(val/4);
  Serial.println();
  delay(200);
     }
     if (kaziButton == HIGH){
      move(2, val/4, 1); //motor 1, full speed, left
            Serial.print("Motor 2 kazıma, Hız: ");
      Serial.print(val/4);
  Serial.println();
  delay(200);
     }
      if (ileriButton == LOW && geriButton == LOW && kaziButton == LOW){
      stop();
            Serial.print("Motorlar duruyor ");
  Serial.println();
  delay(200);
     }

}

void move(int motor, int speed, int direction){
//Move specific motor at speed and direction
//motor: 0 for B 1 for A
//speed: 0 is off, and 255 is full speed
//direction: 0 clockwise, 1 counter-clockwise

digitalWrite(STBY, HIGH); //disable standby

boolean inPin1 = LOW;
boolean inPin2 = HIGH;

if(direction == 1){
inPin1 = HIGH;
inPin2 = LOW;
}

if(motor == 1){
digitalWrite(AIN1, inPin1);
digitalWrite(AIN2, inPin2);
analogWrite(PWMA, speed);
}else{
digitalWrite(BIN1, inPin1);
digitalWrite(BIN2, inPin2);
analogWrite(PWMB, speed);
}
}

void stop(){
//enable standby
digitalWrite(STBY, LOW);
}

i thought it is because parazites but i dont know how to solve it.

Parasites? You have got to be kidding.

How ARE your switches wired? You are not using the internal pullup resistors, so you MUST be using external resistors. Show how the switches are actually wired, and explain why you want to use more hardware (external resistors) than is needed.

  ileriButton = digitalRead(ileri);

It's generally a good idea to use Pin in the name of variables that hold pin numbers, and State in the name of variables that hold pin states. Using Button in the name of variables that hold switch pin numbers or switch pin states is not a good idea.

Thanks for your reply,

i am so sorry it was just because i didnt put the 10k resistors to input pins, as i say i am new :slight_smile:


i add the picture of my board. i connect 5v directly the input pins. as i say i didnt do it before so i am probably wrong but i wanna fix it.

i connect potansiometer to a3

other pins like below. i write directly pin numbers tu understand easily,

I used 4 5 pins to direction motor 1, 6 7 pins to direction motor2

as you see pins 2, 3 and 12 inputs.
when i touch the 5v on board to that pins it works, i can read from serial port screen, when i release the signal, it doesnt change maybe 4 seconds,

i thought maybe program cycle is too much but it cant be 4 second and potansiometer is changing exact time i can see it, so i didnt understand why when i release 5vd from input pins it doesnt change its condition at the same time. and also somehow when i didnt touch anything i can see a short time inputs comes then goes, just a moment and pess or when i give one of input, the other ones comes and goes also.

pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(12, INPUT);
  
pinMode(8, OUTPUT);

pinMode(9, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);

pinMode(10, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);

pins-arduino.jpg

when i touch the 5v on board to that pins it works, i can read from serial port screen, when i release the signal, it doesnt change maybe 4 seconds,

That's because you are not pulling the pin LOW. Add a 10K resistor between the pin and ground, to pull the pin LOW instantly when you remove the wire.