Program mBot Mega with Raspberry Pi in Python

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.

mceclip0.png

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)

mega-Raspberry_Pi-07.png

mega-Raspberry_Pi-08.png

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.

mega-Raspberry_Pi-02.png

  • 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.

mega-Raspberry_Pi-03.jpg

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

mega-Raspberry_Pi-04.jpg

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.

mega-Raspberry_Pi-06.png

mega-Raspberry_Pi-05.png

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:

http://t.hk.uy/kPk 

http://t.hk.uy/kPn

 

To control the sensors and motors on mBot Mega with Raspberry Pi, you need to install the corresponding library first.

 

 

 

Was this article helpful?
6 out of 9 found this helpful

Comments

1 comment
  • 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.

    0

Please sign in to leave a comment.