To use Raspberry Pi to communicate with mBot Mega, drive its motors, and obtain the output data of its sensors, you need to update the firmware of mBot Mega on mBlock 5 to the online firmware first and then connect it to Raspberry Pi.
For details about how to update the firmware, see Updating the Firmware of mBot Mega.
Prepare the Raspberry Pi:
Here are some steps to successfully connect mBot Mega to Raspberry Pi:
The first connection method:GPIO method
Connect Raspberry Pi and MegaPi with a cable or 2.54mm female header connector (see the pictures for your reference)
Tip: When you connect Raspberry Pi and MegaPi with a 2.54mm female header connector, you only need one battery pack to power both of them.
First in your Raspberry Pi, disable the login prompt from Desktop > Menu > Preferences > Raspberry Pi Configuration.
- If you are using raspberry 3 B+, since the Bluetooth function takes up the ttyAMA0 port, you have two ways to solve this problem.
a. Disable the pi3 bluetooth and restore UART0/ttyAMA0 over GPIOs 14&15
b. Switch pi3 blutooth function to use the mini-UART(ttyS0) and restore UART0/ttyAMA0 over GPIOs 14&15.
- Here, I disable the pi3 bluetooth as an example
a. Search for pi3-disable-bt in file /boot/overlays/README, it will show you how to disable the bluetooth; if you want switch the bluetooth to mini-UART(ttyS0), you can search for pi3-miniuart-bt.
b. Modify the file /boot/config.txt. At the end of the file, add the following content.
#Enable uart
enable_uart=1
dtoverlay=pi3-disable-bt
c. Reboot Raspberry Pi
d. Open the Terminal and input the following command:
sudo systemctl disable hciuart
e. Now you can use ttyAMA0 as UART over GPIOs 14&15
- install python library for Makeblock
# pip3 install makeblock
- enter the initial code for Python
from time import sleep
from makeblock import MegaPi,SerialPort
megapi = MegaPi.connect(SerialPort.connect("/dev/ttyAMA0"))
- compile your code in Python
The second connection method: USB cable connection method
a. Connect mBot Mega to the computer and update it to the online firmware (47.01.101) on mBlock.
Tips: When you connect Raspberry Pi and MegaPi with a cable, you need to use two different battery packs to power each of them, respectively.
Please connect mBot Mega to the computer first and update to the online firmware on mBlock.
- install python library for Makeblock
# pip3 install makeblock
- enter the initial code for Python
from time import sleep
from makeblock import MegaPi,SerialPort
megapi = MegaPi.connect()
# or megapi = MegaPi.connect(SerialPort.connect("/dev/ttyUSB0"))
- Tip: When using USB connection, there are two ways to connect to the USB serial port.
Method 1: Do not specify the serial port—: MegaPi.connect(), MegaPi.connect() connects to the USB serial port by default.
Method 2: Specify the serial port: MegaPi.connect(SerialPort.connect("/dev/ttyUSB0")), not necessarily ttyUSB0, subject to actual conditions. - compile your code in Python
Some related content for reference:
Open source schematic of Megapi:
To control the sensors and motors on mBot Mega with Raspberry Pi, you need to install the corresponding library first.
- To control the obstacle avoidance and line following sensors and the impact switches, use the library: https://gist.github.com/xeecos/ceeb8fd83cc15b4e83b713bb75a982fd
- To control the RGB LED module, use the library: https://gist.github.com/xeecos/0a326e03f44633fed726867b0e71a3fe
- To control the motors, use the library: https://gist.github.com/xeecos/5fa6cb5876a8c9449562d8026942fff1/revisions
Comments
Thanks for the post, have couple questions:
1. How different it is to connect Raspberry Pi 4B to Mega?
2. I bought the makeblock robotic arm addon pack and wonder if it can also be controlled by Pi through Mega? Any library I can use?
Thanks a lot.
Please sign in to leave a comment.