Line Following Robot Using Morph3DBot

by mh99 in Workshop > 3D Printing

45 Views, 1 Favorites, 0 Comments

Line Following Robot Using Morph3DBot

PXL_20260417_130446388.jpg

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:

  1. 3x Cube 1x1
  2. 2x LBracket 10x7
  3. 1x Mount 9v Battery
  4. 1x Mount Ball
  5. 1x Mount Breadboard
  6. 1x Mount HBridge

Electronics:

  1. 1 x 9V Block Battery
  2. 1 x 9V Battery Clip
  3. 1 x Table Tennis Ball
  4. 1 x Micro Breadboard with 25 slots
  5. 1 x HBridge L298N
  6. 2 x Gearmotor
  7. 2 x Infrared Distance Sensor
  8. 20 x Breadboard cable male/female (20 cm)
  9. 20 x Breadboard cable male/male (20 cm)

Other:

  1. 2 x Wheels
  2. 2 x M3 x 8 mm Screw
  3. 40 x Lego Technic Pin 6279875 - 2780


Parts not included in the Morph3dBot-Kit:

  1. 1x Raspberry Pi Pico
  2. 1x compatible USB cable (for programming)
  3. 2x Alternative IR-Sensor mounts
  4. 1x Breadboard (a standard breadboard cut down to 20 columns will do, the kit does not include a compatible one)
  5. Black tape for the line (e.g. electrical tape)
  6. a white surface/table/board to test your robot on (a big piece of paper will work too)

Software:

  1. Thonny IDE
  2. MicroPython firmware for Raspberry Pi Pico
  3. Line-follower script (provided in this Instructable)

Tools:

  1. 3D printer (for the sensor mount)
  2. Screwdriver
  3. Saw (for shortening the breadboard)
  4. 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:

  1. Download the official MicroPython firmware for your Pico
  2. Hold the BOOTSEL button on the Pico and plug it into your PC via USB.
  3. It will appear as a USB drive. Drag and drop the MicroPython .uf2 firmware file onto it.
  4. The Pico will reboot automatically.

Install Thonny: Download and install from thonny.org.

Install required libraries via Thonny's package manager (Tools -> Manage packages):

  1. picozero
  2. 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.

from picozero import pico_led
pico_led.blink()

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

PXL_20260417_131317518.jpg

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

PXL_20260417_131146389.jpg
robot_Steckplatine.png

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.

from machine import Pin, PWM
import time

#Pin Setup (left motor)
pwm1 = PWM(Pin(28))
in1 = Pin(27, Pin.OUT)
in2 = Pin(26, Pin.OUT)
#Pin Setup (right motor)
in3 = Pin(22, Pin.OUT)
in4 = Pin(21, Pin.OUT)
pwm2 = PWM(Pin(20))

# Set PWM Frequency
pwm1.freq(1000)
pwm2.freq(1000)

# Set direction
in1.value(0)
in2.value(1)
in3.value(0)
in4.value(1)

# Spin the wheels
pwm1.duty_u16(60000)
pwm2.duty_u16(60000)

#Pause after 3 secs
sleep(3)
pwm1.duty_u16(0)
pwm2.duty_u16(0)

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

PXL_20260417_130849489.jpg
PXL_20260417_131429431.jpg
robot_Steckplatine2.png

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:

  1. If both of the sensors do not detect the black line: robot drives forwards
  2. If the left sensor detects the line: robot turns left (Left wheel stops, right wheel keeps moving)
  3. If the left sensor detects the line: robot turns right (Right wheel stops, left keeps moving)
  4. 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

PXL_20260417_130419226.jpg

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

apo1w4.gif

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!