Programming 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:

On 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
sudo pip install megapi
  • enter the initial code for Python
 from megapi import *

 bot = MegaPi()

 bot.start() #if using usb cable, need to call bot.start('/dev/ttyACM0')
  • compile your code in Python

Wiring:

Here are some steps to successfully connect mBot Mega to Raspberry Pi:

        a. Connect mBot Mega to the computer and update it to the online firmware (47.01.101) on mBlock.

        b. Connect Raspberry Pi and MegaPi with a cable or 2.54mm female header connector (see the pictures for your reference)

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

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.

mega-Raspberry_Pi-07.png

mega-Raspberry_Pi-08.png

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?
1 out of 1 found this helpful

Comments

0 comments

Please sign in to leave a comment.