Need help with code Arduino nano, qtr-8rc and l298n

after uploading this code, the motor 1 is moving backwards only and nothing more than that is happening, here is the line follower code :

#include <QTRSensors.h>

QTRSensors qtr;
const uint8_t SensorCount = 8;
uint16_t sensorValues[SensorCount];

float Kp = 0.2; 
float Ki = 0;
float Kd = 0.1;

int P;
int I;
int D;
int lastError = 0;
boolean onoff = false;

const uint8_t maxspeeda = 255; // Max speed for motor A (PWM range: 0-255)
const uint8_t maxspeedb = 255; // Max speed for motor B
const uint8_t basespeeda = 100; // Base speed for motor A
const uint8_t basespeedb = 100; // Base speed for motor B

// Define motor control pins
int aPhase = 9;    // Motor A direction pin
int aEnable = 6;   // Motor A PWM pin (Enable pin)
int bPhase = 5;    // Motor B direction pin
int bEnable = 3;   // Motor B PWM pin (Enable pin)

int buttonCalibrate = 17; // Calibration button pin
int buttonStart = 2;      // Start button pin

void setup() {
  Serial.begin(9600);
  qtr.setTypeRC();
  qtr.setSensorPins((const uint8_t[]){10, 11, 12, 14, 15, 16, 18, 19}, SensorCount);
  qtr.setEmitterPin(7); // LEDON pin
  
  // Set motor control pins as output
  pinMode(aPhase, OUTPUT);
  pinMode(aEnable, OUTPUT);
  pinMode(bPhase, OUTPUT);
  pinMode(bEnable, OUTPUT);
  
  delay(500); // Delay for stability
  
  boolean Ok = false;
  while (!Ok) { 
    if (digitalRead(buttonCalibrate) == HIGH) {
      calibration(); 
      Ok = true;
    }
  }
  forward_brake(0, 0); // Stop motors at the beginning
}

void calibration() {
  digitalWrite(4, HIGH); // Pin 4 can be used to indicate calibration mode
  for (uint16_t i = 0; i < 400; i++) {
    qtr.calibrate(); // Calibrate the sensors
  }
  digitalWrite(4, LOW);
}

void loop() {
  if (digitalRead(buttonStart) == HIGH) {
    onoff = !onoff; // Toggle on/off state
    delay(1000); // Debounce delay
  }

  if (onoff) {
    PID_control(); // Execute PID control if on
  } else {
    forward_brake(0, 0); // Stop motors if off
  }
}

void forward_brake(int speedA, int speedB) {
  digitalWrite(aPhase, HIGH); // Set direction for Motor A
  digitalWrite(bPhase, LOW);  // Set direction for Motor B
  
  // Set PWM speed for motors
  analogWrite(aEnable, speedA);
  analogWrite(bEnable, speedB);
}

void PID_control() {
  uint16_t position = qtr.readLineBlack(sensorValues); // Read the line position
  int error = 3500 - position; // Calculate error

  P = error; // Proportional term
  I += error; // Integral term
  D = error - lastError; // Derivative term
  lastError = error; // Update last error
  
  int motorSpeed = P * Kp + I * Ki + D * Kd; // Calculate motor speed adjustment

  int motorSpeedA = basespeeda + motorSpeed; // Speed for motor A
  int motorSpeedB = basespeedb - motorSpeed; // Speed for motor B
  
  // Constrain motor speeds within limits
  motorSpeedA = constrain(motorSpeedA, 0, maxspeeda);
  motorSpeedB = constrain(motorSpeedB, 0, maxspeedb);
  
  forward_brake(motorSpeedA, motorSpeedB); // Set motor speeds
}

Using L298N, Arduino Nano and QTR-8RC

Many problems involving Arduino and L298N are resolved with the correct power supply. Show a fully labeled wiring diagram of your project.

Lipo 11.1v 3S 40C (positive terminal) ---> 12V L298n
Lipo 11.1v 3S 40C (negative terminal) ---> GND L298n

Two N20-12v-600rpm motors connected to L298n (OUT1, OUT2, OUT3, OUT4).
[ENA, IN1, IN2, IN3, IN4, ENB] pins of L298n are connected to D2, D3, D4, D5, D6, D7 of Arduino Nano.( Ignore the pins in the code because i changed it later but after modifying pins in the code it was still not working.)

Arduino Nano VIN pin --> 5v of L298n.
Arduino Nano GND pin --> GND of L298n.

QTR-8RC VIN pin ---> 5v of Arduino Nano.
QTR-8RC GND pin ---> GND of Arduino Nano.

QTR-8RC Emitter Pin ---> Pin 7 of Arduino Nano.
QTR-8RC Sensor Pins ---> Pin(10, 11, 12, 14, 15, 16, 18, 19) of Arduino Nano.

I am ignoring everything you have posted until you post exactly what you have. All errors are human-caused. Memorize that.

Verify everything, make a detailed drawing, post a picture, post the correct sketch.

"Nano" points to a whole family of electrically very different boards.

If you mean a classic Nano v3, then D6 and D7 are analogue input only.
Not possible to use those pins as output, and they also don't have internal pull up.

D13 can be a problem when used as input, because it has a LED/resistor directly attached.
Leo..

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.