Line Following Robot Using Morph3DBot
by mh99 in Workshop > 3D Printing
45 Views, 1 Favorites, 0 Comments
Line Following Robot Using Morph3DBot
This Instructable shows how I turned the modular System Morph3DBot into a fully functional line-following robot powered by a Raspberry Pi Pico and IR sensors.
The Morph3DBot platform is a fantastic base for this project: it's modular, beginner-friendly, and requires minimal tools. I've added a custom IR sensor mount (printable file included) and a MycroPython-based firmware that you can upload an tinker with using Thonny.
Supplies
Morph3DBot kit (3D-printed parts: if you dont have a full kit available, you can only print the following parts using the Morph3DBot-Instructable)
Printables:
- 3x Cube 1x1
- 2x LBracket 10x7
- 1x Mount 9v Battery
- 1x Mount Ball
- 1x Mount Breadboard
- 1x Mount HBridge
Electronics:
- 1 x 9V Block Battery
- 1 x 9V Battery Clip
- 1 x Table Tennis Ball
- 1 x Micro Breadboard with 25 slots
- 1 x HBridge L298N
- 2 x Gearmotor
- 2 x Infrared Distance Sensor
- 20 x Breadboard cable male/female (20 cm)
- 20 x Breadboard cable male/male (20 cm)
Other:
- 2 x Wheels
- 2 x M3 x 8 mm Screw
- 40 x Lego Technic Pin 6279875 - 2780
Parts not included in the Morph3dBot-Kit:
- 1x Raspberry Pi Pico
- 1x compatible USB cable (for programming)
- 2x Alternative IR-Sensor mounts
- 1x Breadboard (a standard breadboard cut down to 20 columns will do, the kit does not include a compatible one)
- Black tape for the line (e.g. electrical tape)
- a white surface/table/board to test your robot on (a big piece of paper will work too)
Software:
- Thonny IDE
- MicroPython firmware for Raspberry Pi Pico
- Line-follower script (provided in this Instructable)
Tools:
- 3D printer (for the sensor mount)
- Screwdriver
- Saw (for shortening the breadboard)
- Small power-bank (optional)
Print the Parts, Gather the Electronics
If you dont already have a full set available, print and assemble one (or just the specific parts i mentioned above) following the Morph3DBot Instructable.
I made a custom IR-Sensor-Mount based on the original project, that better suited the projects needs. You can download it here:
Set Up Your PC
I used MicroPython to program the Raspberry Pi Pico. Here are the steps i followed to get the software up and running:
Flash MicroPython onto the Pico:
- Download the official MicroPython firmware for your Pico
- Hold the BOOTSEL button on the Pico and plug it into your PC via USB.
- It will appear as a USB drive. Drag and drop the MicroPython .uf2 firmware file onto it.
- The Pico will reboot automatically.
Install Thonny: Download and install from thonny.org.
Install required libraries via Thonny's package manager (Tools -> Manage packages):
- picozero
- time
Change the interpreter: In the bottom right corner of the Thonny window, you will see the interpreter used to run the code you write in Thonny. Change it from local to your Raspberry Pi Pico.
You should now be able to upload a program to your Pico. You can test this by pasting the following lines of code into thonnys editor-window and press the green arrow-button on the top.
If everything worked out, the onboard-LED of your Pico should now blink.
Prepare Your Pico
The Raspberry Pi Pico will sit on top of the robot on a breadboard for easy and flexible wiring. Unfortunately, the included 17x10 breadboard is too small for the Pico, the standard 30x10 breadboard included in most starter kits is too large for the Morph3DBot platform. I could not find a suitable breadboard that fits the Picos 20-Pins-length.
My Solution: Take a standard breadboard and cut it down to 20 columns. I peeled off the foam on the bottom, pushed out the metal rail on the 21st row and cut the board down using a model saw.
If your Pico does not have header pins yet, solder them accordingly.
Build Your Robot Base
Connect three cubes from the set using the Lego Technic Pins. Attach the Gearmotors in the middle and stabilize the robot using the Ball Mount.
Run the motor cables through the base cubes, to keep them from hanging down.
Attach the H-Bridge with the mount above the gearmotors.
Stick the breadboard onto the breadboard mount using the adhesive (or glue them). Plug the Pico onto the breadboard and fixate the mount on top of the cubes using the Lego Pins.
Wiring
Connect the Pico, HBridge and the motor using the jumper wires.
Gearmotor:
Connect the cables from the left motor to OUT1/OUT2 and the right motor to OUT3/OUT4 on the H-Bridge and screw them down.
Battery:
Connect the battery-cable to GND/12V
Pico:
ENA > Pico GP28 (PWM)
IN1 > Pico GP27
IN2 > Pico GP26
IN3 > Pico GP22
IN4 > Pico GP21
ENB > Pico GP20 (PWM)
Power to the Pico:
You can choose to connect the Pico to an external power source/bank via USB. This can prevent issues with inrush current, that can lead to the Pico not powering on at all when connecting the 9V battery.
But if you got a fresh battery, you can wire the 5V/GND of the H-Bridge up to the VSYS/GND of your Pico.
Test Your Motors (optional)
Test your wiring with this short little sketch before moving on.
The wheels should now spin for three seconds.
Please make sure that both wheels are moving. If not, check your wiring. You can also check, if your motors are working by powering it directly with the 9v battery.
If one or both of the motors are moving in the wrong direction, you can switch the values for IN1&2 (left motor) or IN3&4 (right motor) in the sketch.
Connect IR Sensors
Step 7: Connect the IR Sensors
Screw the two IR sensor modules to the custom brackets. Stick them to the L-shaped brackets and attach them to the robot using Lego Pins. The sensors need to be as close to the ground as possible.
Connect both the VCC and ground-Pins to the respective Pins on the Pico. Attach the DC-Outputs of the sensors to GP0 (left sensor) & GP1 (right sensor).
Connect the Battery and check if the control led's of the sensors turn on. If not, check your wiring and power-bank.
Upload the Line Following Script
Upload the main linefollower.py script to the Pico via Thonny. The simple logic of it is:
- If both of the sensors do not detect the black line: robot drives forwards
- If the left sensor detects the line: robot turns left (Left wheel stops, right wheel keeps moving)
- If the left sensor detects the line: robot turns right (Right wheel stops, left keeps moving)
- If both sensors detect the line: stop (and wait for help)
Save the file as main.py on the Pico via the Save-Icon on top. This makes the skript start automatically on power-up.
Downloads
Built a Test Track and Test Your Robot
Now its time to build a track for your robot!
Use black electrical/insulation tape on a white, flat surface (e.g. white paper or a table). This gives the best contrast for the IR sensors. The line should be at least 2–3 cm wide — too thin and the sensors will struggle to follow it reliably. Start with gentle curves, or the robot will loose the track and wander off.
Stick your electrical tape on your surface and slowly stretch the tape in one direction to form a smooth curve. If your surface is big enough, you can go for a full circle.
Now calibrate the IR sensors by turning the potentiometer with a screwdriver. Adjust the sensors so that the second indicator light is on when the sensor is over the white surface, and off when the sensor is over the line.
Let It Run
Now its time for the moment of truth:
Set your robot onto your test track and make sure, that the line is between both sensors. Connect the 9V battery (and your power-bank).
The robot should now move along your track 🤖🚀 Congratulations!