• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PID Line follower robot

Not open for further replies.
I'm trying to do a line following but the problem is there is a T junction in the track. I figured out that I have to reverse one direction of the motor backward and the other is forward, but the library of ada fruit doesn't respond to negative speeds, how would I modify the pid code so it reverses the direction of motors when it finds a T junction.

void loop()
unsigned int sensors[5];
int position = qtrrc.readLine(sensors); //get calibrated readings along with the line position, refer to the QTR Sensors Arduino Library for more details on line position.
int error = 2500 - position;

int motorSpeed = Kp * error + Kd * (error - lastError);
lastError = error;

int leftMotorSpeed = M1_maksimum_speed + motorSpeed;
int rightMotorSpeed = M1_maksimum_speed - motorSpeed;

// set motor speeds using the two motor speed variables above
set_motors(leftMotorSpeed, rightMotorSpeed);

void set_motors(int motor1speed, int motor2speed)
if (motor1speed > M1_maksimum_speed ) motor1speed = M1_maksimum_speed;
if (motor2speed > M2_maksimum_speed ) motor2speed = M2_maksimum_speed;
if (motor1speed < 0) motor1speed = 0;
if (motor2speed < 0) motor2speed = 0;


Without know what hardware you are using I can only guess what hardware you have.
When I made a wheeled robot, I also used skid steering and I used an H bridge so one motor could reverse while the opposite side went forward.
I had 4 motorised wheels so it turned within its own length.
If you are using standard DC motors, you can use components to make a circuit or an IC like L293d up to 1 amp per channel.
If I remember correctly, to go into full reverse, you would need an extra output from the microprocessor for each motor.
You could try just turning one motor off, depending upon the surface, it might be enough to allow the other motor to drag it round.
I'm not an Arduinista which is what I think you are using, so I only half understand your code.
Last edited:


I too am not familiar with your hardware / software, but I note in your code example the lines:-
So I would assume there is a command such as:
to reverse the motor?
Not open for further replies.

Latest threads

EE World Online Articles