Introduction: Line Follower Robot With PICO
Before you're capable of creating a robot that can end civilization as we know it, and is able to end the human race. You first must be able to create the simple robots, the ones that can follow a line drawn on the ground, and here is where you'll take your first step towards ending us all >.<
First of all, a line following robot is a robot that capable of following a line on the ground, and this line usually is a black line drawn on a white background or vise versa; and that is because it is easier for the robot to tell the difference between highly contrasting colors, like black and white. Where the robot changes its angle depending on the color it reads.
Supplies
- PICO
- Two-Wheel Drive robot chassis, which have the following:
- Acrylic chassis
- 2 DC motors with wheels and encoders
- Caster wheel with metal standoffs
- 4 channel battery holder
- Some screws and nuts
- On/off switch
- L298N motor driver module
- 2 Line tracker sensors
- 7.4v battery
Step 1: Preparing the DC Motors
You can use the two-wheel-drive " 2WD" chassis to make this project easier, as it saves time and effort when it comes to building your own chassis. Giving you more time to focus on the electronics of the project.
Let's start with the DC motors, as you're going to use the motors to control the movement speed and direction of your robot, depending on the sensors' readings. The first thing to do is to start controlling the motors' speed, which is directly proportional to the input voltage, meaning that you have to increase the voltage to increase the speed and vice versa.
The PWM "Pulse Width Modulation" technique is ideal for the job, as it allows you to adjust and customize the average value that is going to your electronics device (motor). And it works by using the digital signals "HIGH" and "LOW" to create analog values, by alternating between the 2 signals at a very fast rate. Where the "analog" voltage depends on the percentage between the digital HIGH to digital LOW signals present during a PWM period.
Please note that we can't connect PICO directly to the motor, as the motor needs a minimum of 90mA which cannot be handled by PICO's pins, and that is why we use the L298N motor driver module, which gives us the ability to both send enough current to the motors and change its polarity.
Now, let's solder a wire to each of the motor's terminals, following these steps:
- Fuse a little amount of solder on the motor's terminal
- Put the wire tip above the motor terminal and heat it with the soldering iron until the solder on the terminal melts and connects with the wire, then remove the soldering iron and let the connection cool down.
- Repeat the previous steps with the remaining terminals of both motors.
Step 2: Using the L298N Motor Driver Module
The L298N motor driver motor has the ability to boost the signal coming from PICO, and to change the polarity of the current passing through it. Letting you control both the speed and direction that your motors rotate at.
L298N Pin Outs
- DC motor A's first terminal
- DC motor A's second terminal
- Onboard 5v regulator jumper. Remove this jumper if you're connecting motor supply voltage of more than 12v, to not hard the voltage regulator.
- Motor supply voltage in. The maximum is 35v, and don't forget to remove the voltage regulator if you're using more than 12v.
- GND
- 5v output. This output comes from the voltage regulator if it is still connected, and it gives you the ability to power your PICO from the same source as the motor.
- DC motor A enable jumper. If this jumper is connected, the motor will run at full speed either forward or backward. But, if you want to control the speed, just remove the jumper and connect a PWM pin instead.
- In1, it helps with controlling the current's polarity, and thus, the direction of rotation for motor A.
- In2, it helps with controlling the current's polarity, and thus, the direction of rotation for motor A.
In3, it helps with controlling the current's polarity, and thus, the direction of rotation for motor B.
In4, it helps with controlling the current's polarity, and thus, the direction of rotation for motor B.
DC motor B enable jumper. If this jumper is connected, the motor will run at full speed either forward or backward. But, if you want to control the speed, just remove the jumper and connect a PWM pin instead.
DC motor B's first terminal
DC motor B's second terminal
The number of pins that the L298N driver motor has makes it seem difficult to use. But, it is actually quite easy, and let's prove that with a functioning example, where we use it to control the rotation direction of both of our motors.
Connect PICO to the motor driver as follows "you will find the diagram above":
- In1 → D0
- In2 → D1
- In3 → D2
- In4 → D3
The motor's direction is controlled by sending a HIGH and LOW logic value between each couple In1/2 and In3/4 driver pins. For example, if you send HIGH to In1 and LOW to In2 it causes the motor to rotate in one direction and sending LOW to In1 and HIGH to In2 rotates the motor in the opposite direction. But, if you send the same HIGH or LOW signals at the same time to both In1 and In2, the motors will stop.
Don't forget to connect PICO's GND with the battery's GND, and don't remove the Enable A and Enable B jumpers.
You will also find this example's code above.
Attachments
Step 3: Adding PWM to the L298N Driver Module
We now can control the rotation direction of our motors. But, we still can't control their speeds, as we have a constant voltage source that is giving them the maximum power they can take. And to do that, you need two PWM pins to control both of your motors. Unfortunately thou, PICO has only 1 PWM output, which we need to expand by using the PCA9685 OWM module, and this amazing module can expand your PWM from 1 to 16!
PCA9685 Pinouts:
- VCC → This is your logic power, with 3-5v max.
- GND → The negative pin must be connected to the GND to complete the circuit.
- V+ → This pin distributes the power coming from an external power source, it's primarily used with motors that need large amounts of current and need an external power source.
- SCL → Serial clock pin, which you connect to the SCL of PICO.
- SDA → Serial data pin, which you connect to the SDA of PICO.
- OE → Output enable pin, this pin's active is LOW, meaning that when the pin is LOW, all of the outputs are enabled, and when it's HIGH all the outputs are disabled. This is an optional pin, with the default being pulled LOW.
The PCA9685 PWM module has 16 PWM outputs, with each having its own V+, GND, and PWM signal that you can control independently of the others. Each PWM can handle 25mA of current, so be careful.
Now comes the part where we use the PCA9685 module to control the speed and direction of our motors, and this is how we connect PICO to the PCA9685 and L298N modules:
PICO to PCA9685:
- D2 (PICO) --> SDA (PCA9685)
- D3 (PICO) --> SCL (PCA9685)
PCA9685 to L298N:
- PWM 0 (PCA9685) → In1 (L298N), to control the direction of motor A
- PWM 1 (PCA9685) → In2 (L298N), to control the direction of motor A
- PWM 2 (PCA9685) → In3 (L298N), to control the direction of motor B
- PWM 3 (PCA9685) → In4 (L298N), to control the direction of motor B
- PWM 4 (PCA9685) → enableA (L298N), for sending the PWM signal that controls motor A's speed.
- PWM 5 (PCA9685) → enableB (L298N), for sending the PWM signal that controls motor B's speed.
You will find the code for all of these parts attached above.
Step 4: Using the Line Tracker Sensor
The line tracker is pretty straightforward. This sensor has the capability to distinguish between two surfaces, depending on the contrast between them, as in black and white.
The line tracker sensor has two main parts, the IR LED and the photodiode. It can tell the colors by emitting IR light from the LED and reading the reflections that come back to the photodiode, then the photodiode outputs a voltage value depending on the reflected light (HIGH value for a light "shiny" surface, and a LOW value for a dark surface).
The line tracker's pinouts:
- A0: this is the analog output pin, and we use it if we want an analog input reading (0-1023)
- D0: This is the digital output pin, and we use it if we want a digital input reading (0-1)
- GND: This is the ground pin, and we connect it to PICO's GND pin
- VCC: This is the power pin, and we connect it to PICO's VCC pin (5v)
- Potentiometer: This is used to control the sensitivity of the sensor.
Let's test the line tracker sensor with a simple program that turns on an LED if it detects a black line, and turn the LED off if it detects a white surface while printing the sensor's reading on the Serial Monitor.
You will find the code to this test attached above.
Attachments
Step 5: Putting Everything Together
The last thing we need to do is to put everything together. As we have tested all of them individually and all of them are functioning as expected.
We'll keep PICO, the PCA9685 and L298N modules connected as they are. Then, we add the line follower sensors to our existing setup, and it is as follows:
- VCC (all of the line tracking sensors) → VCC (PICO)
- GND (all of the line tracking sensors) → GND (PICO)
- D0 (Right line tracker sensor) → A0 (PICO)
- D0 (Center line tracker sensor) → A1 (PICO)
- D0 (Left line tracker sensor) → A2 (PICO)
This is the final code that will control your car and tell it to follow a line, black line on a white background in our case.