Morph3DBot Multi-Mode Robot: Line Following, Obstacle Avoidance & Light Hunting With a Raspberry Pi Pico
by mh99 in Workshop > 3D Printing
62 Views, 1 Favorites, 0 Comments
Morph3DBot Multi-Mode Robot: Line Following, Obstacle Avoidance & Light Hunting With a Raspberry Pi Pico
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 MicroPython-based firmware that you can upload and tinker with using Thonny.
Supplies
Morph3DBot kit (3D-printed parts: if you don't 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
- 1x Mount LEDRing
- 1x Mount UltrasonicSensor
- 1x Mount TwoSensors
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
- 2 x Photoresistor Breakoutboard
- 2 x Photoresistor Breakoutboard
- 1 x LEDRing 12Pixel
- 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 custom 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)
- Flashlight for testing the light-following mode
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
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 and select the correct COM port.
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 GP22 (PWM)
IN1 > Pico GP21
IN2 > Pico GP20
IN3 > Pico GP19
IN4 > Pico GP18
ENB > Pico GP17 (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.
[optional] Test Your Motors
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
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 (3-8mm is recommended).
Connect both the VCC and ground pins to the respective pins on the Pico. Attach the digital output (labeled DO) 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.
Use your black electrical/insulation tape and stick it onto a white, flat surface on which the robot should drive. Place both IR sensors above the tape.
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.
[Optional] Upload the Line Following Script
If you just want to build a line following robot, use this software. Otherwise you can skip this step.
Upload the main line-following-robot.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 right 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 sketch start automatically on power-up.
Downloads
[optional] Build a Test Track and Test Your Robot
If you just want to build a line following robot, use this software. Otherwise you can skip this step.
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 lose 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.
Set your robot onto your test track and make sure, that the line is between both sensors. Connect the 9V battery (or your power-bank).
The robot should now move along your track 🤖🚀 Congratulations!
Add Ultrasonic Sensors for Obstacle Detection
Now its time to enhance the robots functionality with an ultrasonic sensor:
To prevent the robot from crashing into obstacles, we add an HC-SR04 ultrasonic distance sensor to the front of the chassis. Mount the sensor at the front of the robot, facing forward. Make sure the two "eyes" are unobstructed.
Now connect 5V/GND to the Pico and the remaining pins as follows:
Echo > GPIO3
Trig > GPIO2
Add Two LDR-Sensors for Light Hunting
Two LDRs (light-dependent resistors) let the robot detect and "hunt" light sources - useful for interacting with it using a flashlight - We can now program the robot to hunt the light, while staying in the tape marked area and not bumping into anything, thanks to the ultrasonic sensor.
Attach the LDR-Module to the front of the robot, facing forward.
Wire up both the 5V/GND-Pins to the pico, and use GPIO26 & 27's ADC functionality to read the brightness levels
Left LDR → GP27 (ADC1)
Right LDR → GP26 (ADC0)
You can test the wiring by printing the readings to the console:
This should print out a value between 0–65535 - brighter environment should result in a lower reading. You can use that to tweak the light_treshold in the final software accordingly.
Adding the NeoPixel RGB LED Ring
A 12-LED NeoPixel ring gives the robot visual feedback about its current state – is it staying inside the lines, hunting light, or avoiding an obstacle?
Attach the module where ever you like and connect it to 5V/GND and GPIO28 for data (sometimes labeled di for data in)
data > GPIO28
Please also add the library "neopixel" from the library manager
Upload the New Software:
With all sensors mounted, it's time to upload the extended firmware. The new code adds three new behaviors on top of line following:
What the new code does:
- Obstacle avoidance – The ultrasonic sensor fires regulary. If something comes closer than ~10 cm, the LED ring turns blue and the robot performs a 180° turn.
- Line avoidance – Whenever an IR sensor detects a black line, the LED ring turns green and the original line-following logic kicks in (steering left/right depending on which IR sensor sees the line).
- Light hunting – When no line and no obstacle are detected, the robot reads both LDRs. If the area is darker than a threshold, the LED ring turns red and the robot drives toward the brighter side ("light hunting" at higher speed).
- Idle – If none of the above triggers, the LED ring shows white and the robot rolls forward at normal speed.
The three sensors (IR left, IR right, ultrasonic echo) use interrupts so the robot reacts immediately rather than waiting for the next loop iteration.
You can tweak the pin configuration as well as the following parameters in the code:
- speed – base PWM duty (raise until both motors reliably start)
- hunting_speed – higher speed used during light hunting
- us_safety_distance – obstacle detection distance in cm
- light_threshold – switch into the "hunting" mode, may need to be tweaked depending on your environment
Now it's time to see all the behaviors come together. Place the robot on a white surface and, if you like, frame the area with your black insulation tape — the robot should treat the tape as a boundary and stay inside it rather than crossing over. Put an obstacle in its path: it should detect it early, flash the LED ring blue, and turn around by roughly 180°. Finally, grab a flashlight and shine it from the side — the robot should pick up the brighter direction and start chasing the light. If all three reactions work, your Morph3DBot is fully alive!
And that's it — you've taken a modular 3D-printed chassis and turned it into a robot that follows lines, dodges obstacles, and hunts light, all driven by a single Raspberry Pi Pico. Congratulations!
🤖🚀