The line follower, and collision detection robot was developed as part of a school project and was constructed to complete a path designed with a black line and stop before objects. This robot is a fun way to get started with working with embedded systems! It is a fairly simple program and circuit to construct, but it allows one to learn a great deal about electronics.
The RobotThe robot has various components and sensors that allow the robot to function properly. These include the Texas Instruments MSP430 F5529 LaunchPad, photoresistors, an infrared proximity sensor, an h-bridge, and two motors. Don't worry if you do not know what each of these are because it will be explained more.
The first thing is the brain of the robot, which is the TI Microcontroller. This is what will be programmed in order to make all the components work together.
Next are the photoresistors, which are also known as light dependent resistors (LDR). It is a variable resistor that increases resistance with increased light intensity, so in essence, it can be used as a light sensor. However, in this scenario, the photoresistors will be used to detect different colors. It often helps to keep a light near it, so it the resistor will change variably based on how much light is being reflected back from the color that is in front of it. This is why it is better to have LEDs above the photoresistors.
The next component is the infrared proximity sensor, which is used to prevent the robot from colliding with objects in front of it. This works by outputting a specific voltage based on the distance of an object in front of it. IR sensors can also be used for line following as a substitute for the photoresistors, but the specific assignment had a requirement of using the photoresistors for line detection.
The H-bridge is the most important component to control the motors properly. It allows voltage to be applied to a load in any direction, allowing us to turn the motors either forward or backward. The two dc motors are used to make the robot go forward, stop, turn, and go backwards. The data sheet for the h-bridge can be found HERE.
Below are pictures of the robot, the hardware is not put together in a sturdy or permanent way because it was made first as a test.
Programming the robot is much simpler than the wiring. In general, because the two photoresistors are pointing downward at the front of the robot by each wheel, we know that the line would have to be between the two.
- When both resistors detect white, it should go forward
- When the left resistor detects black and the right resistor detects white, it should turn left
- When the right resistor detects black and the right resistor detects white, it should turn right
If we wanted the, robot to stop on a red line, we would make the robot stop if both resistors detected red.
Similarly, if we wanted the robot to stop at a certain distance away from an object, we would make the robot stop if it reached a certain threshold. This may seem incredibly simple, but it is a bit more than just that because we have to determine those thresholds. This is why we use the serial port to display the current values of both photoresistors and the IR sensor. Within void setup()
, we should add the following:
analogReadResolution(12);
Serial.begin(9600);
This sets 4096 different levels of light and distance the photoresistors and the IR sensor can detect, respectively.
Next, the motors must be set as outputs. There should be 2 logic pins connected to each motor, which makes a total of 4 outputs. These will be digitalWrite
pins. The following must also be added within void setup()
to properly set the motors as outputs:
pinMode(29, OUTPUT); //MOTOR 1
pinMode(30, OUTPUT);
pinMode(32, OUTPUT); //MOTOR 2
pinMode(31, OUTPUT);
I connected Motor 1 to pins 29 and 30, and Motor 2 to pins 32 and 31. Notice that the inputs are not set here. This is because there is no need to do so when using analog inputs.
Next, we should display the values from the sensors to the serial port. This is done by adding the following within void loop()
:
int photo1 = analogRead(23);//photoresistor1 is connected to analog pin 23
int photo2 = analogRead(24);//photoresistor2 is connected to analog pin 24
int ir = analogRead(25);//IR sensor is connected to analog pin 25
Serial.print(photo1);
Serial.print(" ");
Serial.print(photo2);
Serial.print(" ");
Serial.println(ir);
**Note that all the sensors are connected to analogRead
pins on the microcontroller.
The values must be printed to the serial port in order to determine the thresholds for each of the sensors. The first 2 numbers shown in the serial port should be different when the photoresistors are put over white and black. Record the two numbers and find a good middle point for the threshold to determine whether or not the resistor is over black or white. Similarly, do this with the third number displayed on the serial port with the IR sensor.
**Note that these values may change based on the environment's lighting
After this, it is simply one giant "if" statement that is seen in the following format:
if (photo1 > 2900 && photo2 > 2200 && ir < 2000)
{
digitalWrite(29, HIGH);//HIGH for forward
digitalWrite(30, LOW); //LEFT MOTOR GOES FORWARD
digitalWrite(32, HIGH);//HIGH for forward
digitalWrite(31, LOW); //RIGHT MOTOR GOES FORWARD
}
else if (photo1 < 2900 && photo2 > 2200 && ir < 2000)
{
digitalWrite(29, HIGH);
digitalWrite(30, LOW); //LEFT MOTOR GOES FORWARD
digitalWrite(32, LOW);//HIGH for forward
digitalWrite(31, HIGH); //RIGHT MOTOR GOES BACKWARDS
}
The if statement goes on, but this is the general format. The numbers 2900 and 2200 are my thresholds for determining if the photoresistors are over black or white, and the 2000 is the threshold if an object is too close to the robot.
**Note that 2900, 2200, 2000 are the threshold values I determined based on the light conditions in the environment my robot is (your values may be differ from mine)
The first "if" statement says that if both the photoresistors are detecting white and there isn't an object in front of it, then both motors will move forward. The second statement says that if the right photoresistor detects black and the left one detects white, then it will turn right. Turning signal 1 HIGH and signal 2 LOW for one motor will make the motor go one direction, but if you turn signal 2 HIGH and signal 1 LOW, it will go in the opposite direction.
Turning Right: To turn right, the left motor should go forward and the right motor should either be stopped or go backwards.
Turning Left: To turn left, the right motor should go forward and the right motor should either be stopped or go backwards.
- Making one motor go forward and the other stopped will make the robot pivot about the non-moving motor.
- Making one motor go forward and the other backwards will make the robot fully rotate about the center of the robot.
Within my program, I made the robot fully rotate about the center of the robot, but either case will work for this robot to function properly. To stop, you can turn both the signals of each motor to LOW, it should look like the following:
else if (ir > 2000)
{
digitalWrite(29, LOW);
digitalWrite(30, LOW); //LEFT MOTOR STOPS
digitalWrite(32, LOW);
digitalWrite(31, LOW); //RIGHT MOTOR STOPS
}
This is what should occur if there is an object right in front of the robot. If you wanted to include a part where the robot stops for a different colored line, an additional threshold would need to be designated, where the photoresistors give a value between the threshold for black and the threshold for white.
After completing all the scenarios what the photoresistors and the IR sensor can detect, you'll be done! For powering the robot, you can either connect it straight to a computer or use a portable battery charger. After understanding how the program works a bit, it becomes much easier.
Comments