Ultimate 2.0—Arduino programming

Arduino is an open-source electronics platform based on easy-to-use hardware and software. It’s intended for anyone making interactive projects. The Arduino development environment makes it easy to write code and upload it to the I/O board. You can use Arduino language (C\C++) to interace with Arduino hardware. We provide a complete Arduino using environment.
MegaPi is compatible with Arduino Mega 2560, so you can develop program with Arduino IDE. We suggest you install Makeblock program library if using Makeblock’s electronic modules.

1. Arduino programming

1.1 Set Arduino
(1) Get Arduino IDE and Makeblock program library
Login Arduino’s official website to download Arduino IDE: https://www.arduino.cc/en/Main/Software
You should download the installation package for corresponding operating system (Subject to the latest version)
Decompress the makeblock folder in the zip to the Arduino default library:
  • Windows 7: “[arduino installation directory]\libraries”Your Arduino library folder should now look like this (on Windows):
      [arduino installation directory]\libraries\makeblock\src
      [arduino installation directory]\libraries\makeblock\example
      ….
    or like this (on Mac):
     [arduino directory]\Contents\Java\libraries\makeblock\src
     [arduino directory]\Contents\Javalibraries\makeblock\example
     …
    or similarly for Linux.
(3).Open the Arduino Application. If already open it, you need to restart to see changes.
2.2. Connect MegPi to the computer
Materials: computer, MegaPi, USB B type interface line, 9-12V power source, etc.
(1)Connect MegaPi with the computer by USB line, then the computer will automatically install MegaPi drive;
(2) Open device manager and you will see the port number of MegaPi. The port number may be different in different computer. In the picture the port number is COM9. 
(3) Then we can validate this with Blik.
a. Open Arduino IDE, click Tools->Board and choose Arduino Mega 2560 or Mega ADK (There will be slight difference among different Arduino IDE versions, so you should choose Mega 2560 option.)
b. Click Tools->Port, COM9 (You have to choose corresponding COM port except COM1)
c. Click File->Example->01.Basics->Blink to open Blink. Then click “Upload” to upload the program. This will last around 1min;
d. After that you will see the blue LED light on MegaPi flash at one-minute intervals.
(4) How to get and install driver if the program doesn’t recognize the driver?
You can install the diver by yourself.
Windows : Download
Mac OSX : Download
2.3. How to refresh and recover the firmware
File->Examples->MakeBlockDrive->example->Firmware_for_MegaPi->Firmware_for_MegaPi,Upload to MegaPi.
2.4. Examples of programming (brief example of driver application and complicated example of programming)
(1) You should prepare these devices:
1x encoder DC motor drive; 1x25mm DC motor; 1xMegaPi; 1xUSB B type interface line; 1x power source (9-12V) (or alkaline battery)
(2) Insert encoder DC motor driver to the PROT1 of MegaPi. Do align the holes without missing anyone – red pin to red female header, yellow pin to yellow female header, or the main control board and driver may get burn.
(3) Connect the terminal of DC motor (Orange) to the green contact 1A+ and 1A- on MegaPi(1B+ and 1B can be connected to the other motor).
(4)Open the application program,click File->Examples->MakeBlockDrive->example->Me_MegaPiDCMotor->MeMegaPiDCMotorDriverTest
This is the example of operation 4 motors on Port1 and Port2 at the same time. Users can change program depend on the situation.
 
(5) Click Tools->Board, and choose “Arduino Mega 2560 or Mega ADK”
(6) Click Tools->Port, COM9 (choose corresponding COM except COM1)
(7) Click “Upload” to upload the program (around 1min). Then plug in 12V power adapter and turn on the switch, and the DC motor will start to run.
Introduction to the functions of  DC motor:
Function                                                                 //   Feature
MeMegaPiDCMotor(uint8_t port)                  //define the port of the motor, and devide them into A and B 
 void run(int16_t speed)                                   //Motor runs with certain speed     
void stop(void)  
2.5 How MegaPi control 1 encoder motor to rotate clockwise or anticlockwise at one-minute intervals?
(1) You should prepare these devices:
1x 25 encoder DC motors;
1x encoder/DC reuse diver;
1x encoder motor wire;
1xMegaPi; 1xUSB B type interface cable;
1x power source
(2)Insert encoder DC motor driver to the PROT1 of MegaPi. Do align the holes without missing anyone – red pin to red female header, yellow pin to yellow female header, or the main control board and driver may get burn.
(3) Connect the encoder driver wire to the driver (the white port), and the other side connected to the encoder motor (don’t contact the white sensor on the encoder motor and black swivel plate)
(4) Write program
#include “MeMegaPi.h”
const byte interruptPin =18;    
const byte NE1=31;                 
long count=0;
unsigned long time;
unsigned long last_time;
MeMegaPiDCMotor motor1(PORT1B);   
uint8_t motorSpeed = 100;
void setup()
{
    pinMode(interruptPin, INPUT_PULLUP);
    pinMode(NE1, INPUT);
    attachInterrupt(digitalPinToInterrupt(interruptPin), blink,RISING);   
    Serial.begin(9600);   
}
void loop()
{
    motor1.run(motorSpeed);       // value: between -255 and 255
    time =millis(); 
    if(time-last_time>2000)    
    {
          Serial.println(count);
          last_time=time;
   }
}
void blink()
{
    if (digitalRead(NE1)>0)   
    count++;
    else
    count–;
}
NOTE: This program controls the motor to rotate clockwise and calculate the pulse number – the pulse number is made by the external interrupt. Then check whether it is rotating clockwise or anticlockwise through the high/low power the pin. The pulse number will be printed every 2 seconds.
(5) Click Tools->Board, choose “Arduino Mega 2560 or Mega ADK”
(6) Click Tools->Port, COM9 (choose corresponding COM except COM1)
(7) Click “Upload” to upload the program (around 1min). Then plug in 12V power adapter and turn on the switch, and the DC motor will start to run. 
(8) Click the serial port communication on the top right corner of IDE, then you will see the pulse number.
Each port has its corresponding pin:
motorport    interruptPin    EN1     
port1B          18                   31
port2B          19                   38
port3B          3                     49
port4B          2                     A1
You can write your own program according to the positive/negative pulse number, so that you can precisely control the speed and angles of the motor and achieve the encoding process. 
2.6 Explanation of how MegaPi control a stepping motor to rotate
(1)  You should prepare these devices:
1x stepping motor;
1x stepping motor diver;
1xMegaPi;
1xUSB B type interface cable;
1x power source
(2)Insert stepping motor driver to the PROT1 of MegaPi. Do align the holes without missing anyone – red pin to red female header, yellow pin to yellow female header.
(3) The stepping motor we use is two-phase four-wire stepping motor with four wire: blue, red, green and black. Connect the wires to the ports with this sequence. 
(4) Open the application program, click
File->Examples->MakeBlockDrive->example->Me_MegaPiOnBoardStepper->MegaPiOnBoardStepperTest
(5) Click Tools->Board, choose “Arduino Mega 2560 or Mega ADK”
(6) Click Tools->Port, COM9 (choose corresponding COM except COM1)
(7) Click “Upload” to upload the program (around 1 min). Then plug in 12V power adapter and turn on the switch;(DC motor can move with voltage DC6-12V while the stepping motor requires voltage over DC9V (DC12V will be best)
Features of the DC motor functions:
Function                                                                  Feature
MeStepperOnBoard(int slot)                         define the port of stepping motor
void setMicroStep(int8_t value)                    set microsteps of stepping motor(1, 2, 4, 8, 16, 32)
void setMaxSpeed(float speed)                    set the maximum speed of stepping motor
void setAcceleration(float acceleration)       set the acceleration of stepping motor
void enableOutputs(void)                              enable stepping motor
void moveTo(long absolute)                              move to  
void move(long relative)                                      move
boolean run(void)                                                 run
2.7 Introduction of various sensors and components

2. FAQ of MegaPi

3.1 Where to get the driver package if the computer fails to install it automatically?
A:Manually install the driver
Windows : Download
Mac OSX : Download
3.2 Why the Port1 – Port4 of MegaPi are anti-reverse?
A: The Port1 – Port4 of MegaPi are designed as one the row of female header or male header, and they are devided into red or yellow color, which can avoid connecting reversely. The red port is for the input/output of large current, while the yellow port is for I/O pin. The master chip or the driver module may get burn if you connect them reversely.
3.3 How much current can the large current driver interface (MOS driver interface) output?
A: Two interfaces can output current up to DC12V10A.
3.4 Why the stepping motor get hot?
A: Considering the usage scenario of stepping motor, the driver current was adjusted to the larger one, so the motor may get hot. Please contact the cooling fin and the driver module well, or the master chip may get burn.
3.5 What’s the function of the black adjustable potentiometer on the stepping motor?
A: This potentiometer is for adjusting the current of stepping motor driver. The default position is on the middle, but you can turn it up or down. When potentiometer turned up, the chip will get hotter. So remember to dissipate heat with larger cooling pin or in good cooling conditions.
3.6 Why the master chip reset when MegaPi drives several motors at the same time? How to deal with this problem?
A: When the motor starts to run or rotate clockwise/anticlockwise, the power consumption will be enormous. In this situation, the current from the power source will be insufficient, which leads to the result of low voltage and the reset of master chip. You should use stronger power source or adjust the program to avoid the motor rotating  clockwise/anticlockwise frequently.
3.7 How to weld 2*10 pin header while connecting MegaPi with Raspberry Pi?
A: MegaPi is not weld to the Raspberry Pi, so you have to weld by yourself. Do avoid short circuit when welding the pins.
3.8 Raspberry Pi resets when connected to Raspberry Pi and drive high power device. How to deal with this problem?
When supplying  power to Raspberry Pi  through MegaPi and controlling motors at the same time, the voltage will be unstable and Raspberry Pi will reset. Just supply power to Raspberry Pi directly.
3.9 How to get after-sales service if there are some quality problem with MegaPi?
A: tec-support@makeblock.com
3.10 Is there any accessories that can work with MegaPi? Where should I buy them?
A: Encoder motor drivers, stepping motor drivers, Bluetooth, 2.4G., DC motors, encoder motors, stepping motors, Shield, sensors of RJ25,  steering engines, structural parts, etc.

3. Notice

4.1. Apply MegaPi voltage DC 6-12V
4.2. Prevent short circuits
4.3 Prevent the contamination of water, acidity/ alkalinity liquid, or solid debris, etc.
4.4. Keep away from children and pets 
4.5. Do not throw away

4. Contact us

tec-support@makeblock.com

24 thoughts on “Ultimate 2.0—Arduino programming”

  1. Hi, Arduino IDE 1.6.9 can not compile the MegaPiOnBoardStepperTest sketch due some errors. could you please check the code?

    Here is the first error msg;

    MegaPiOnBoardStepperTest:49: error: no matching function for call to ‘MeStepperOnBoard::moveTo(int)’

    stepper.moveTo(0);

    Thank you, Leo

    1. I just downloaded the latest library update by YanMinge. And now I am able to compile and upload the stepper example. THX

  2. Hi admin,
    I am trying to drive a simple motor with megapi, but I need your help:
    1. I have downloaded latest library, I have copied it into Arduino’s library folder and it appeaars on the list of libraries successfully, however when I selected the firmware for megapi it doesn’t compile, I have selected: File->Examples->MakeBlockDrive->Firmware_for_mBlock>MegaPi_firmware

    Error message:
    /Applications/Arduino.app/Contents/Java/libraries/makeblock/examples/Firmware_For_mBlock/megapi_firmware/encoders.ino: In function ‘void encodersInit()’:
    encoders:7: error: ‘class MeEncoderOnBoard’ has no member named ‘GetIntNum’
    attachInterrupt(encoders[0].GetIntNum(), isr_process_encoder1, RISING);
    ^
    encoders:8: error: ‘class MeEncoderOnBoard’ has no member named ‘GetIntNum’
    attachInterrupt(encoders[1].GetIntNum(), isr_process_encoder2, RISING);

    1. Hi Christian,

      I have compiled the firmware for MegaPi and it works ok.
      On the Arduino software, have you choose the Arduino/Genuino Mega or Mega 2560 under Board option?

      If the board is correct, which version Arduino software do you use?

  3. The Makeblock app for iOS does not recognize the firmware if I upload the referenced megapi firmware to run the self-balancing robot via bluetooth.

    If I use the standard firmware from the Makeblock PC app (MBLOCK), the ultrasonic works, the gyro works, but the synching between the tilt and the motor correction to keep it balanced is way off and it just falls over. Any thoughts?

    1. Hi Tom,

      Yes, you need upgrade firmware for Ultimate robot first with mBlock software before control the robot with Makeblock APP.
      As for the other issue, is it possible for you to take a video to show the problem and send it to tec-support@makeblock.com

  4. Yes, sure. It will be a few days. I have some business keeping me otherwise occupied and tore down the robot. I am going to start over and will then send you an update. If the same problem persists, I will send a video. Thank you for your response!

  5. OK. So I rebuilt the self-balancing robot and got it to balance. I realized that I did not cross-wire the encoded motors to their decoders. However, still having a a problem. Robot moved very slow and falls over easily. I sent an email with a link to a video. Please take a look and advise…

    1. Hi Tom,

      Where did you send the email to? We checked the email and can’t find your email. Please make sure you have sent to tec-support@makeblock.com
      Besides, as for this issue, please change the batteries have a check.According to our test and research, it is suggested to use rechargeable Li-ion battery or rechargeable nickel-metal hydride, nickel-cadmium which can be bought from amazon or local shop. Or Alkaline battery with good quality like Energizer, DURACELL.

  6. Hi,

    Can you please provide the Arduino code for the detecting Robot?
    I have the ultimate 2.0 MegaPi Board. I Need the complete Firmware to try out all.
    And i delete the Firmware 🙁

    Regards,
    KK

    1. Hi Kevin,

      Please refer to this link to install the Arudino software and Makeblock library.
      You can find the source code of firmware for MegaPi in the Makeblock library.

  7. Hello
    I’m using a megapi and encoder dc motor. I want that read a encoder values properly by using megapi but i don’t have any idea about megapi, i’m new in that area. Please help me. the code which you wrote, it is not working. there is a error which is Arduino:1.8.1 (Windows 10), Kart:”Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)”

    In file included from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeMegaPi.h:65:0,

    from C:\Users\Asusnb\AppData\Local\Temp\arduino_modified_sketch_571230\Me_Megapi_encoder_pid_pos.ino:1:

    C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeStepperOnBoard.h:73:0: warning: “SLOT_1” redefined

    #define SLOT_1 1

    ^

    In file included from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/Me7SegmentDisplay.h:70:0,

    from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeMegaPi.h:42,

    from C:\Users\Asusnb\AppData\Local\Temp\arduino_modified_sketch_571230\Me_Megapi_encoder_pid_pos.ino:1:

    C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MePort.h:108:0: note: this is the location of the previous definition

    #define SLOT_1 SLOT1

    ^

    In file included from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeMegaPi.h:65:0,

    from C:\Users\Asusnb\AppData\Local\Temp\arduino_modified_sketch_571230\Me_Megapi_encoder_pid_pos.ino:1:

    C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeStepperOnBoard.h:74:0: warning: “SLOT_2” redefined

    #define SLOT_2 2

    ^

    In file included from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/Me7SegmentDisplay.h:70:0,

    from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeMegaPi.h:42,

    from C:\Users\Asusnb\AppData\Local\Temp\arduino_modified_sketch_571230\Me_Megapi_encoder_pid_pos.ino:1:

    C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MePort.h:109:0: note: this is the location of the previous definition

    #define SLOT_2 SLOT2

    ^

    In file included from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeMegaPi.h:65:0,

    from C:\Users\Asusnb\AppData\Local\Temp\arduino_modified_sketch_571230\Me_Megapi_encoder_pid_pos.ino:1:

    C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeStepperOnBoard.h:75:0: warning: “SLOT_3” redefined

    #define SLOT_3 3

    ^

    In file included from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/Me7SegmentDisplay.h:70:0,

    from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeMegaPi.h:42,

    from C:\Users\Asusnb\AppData\Local\Temp\arduino_modified_sketch_571230\Me_Megapi_encoder_pid_pos.ino:1:

    C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MePort.h:110:0: note: this is the location of the previous definition

    #define SLOT_3 SLOT3

    ^

    In file included from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeMegaPi.h:65:0,

    from C:\Users\Asusnb\AppData\Local\Temp\arduino_modified_sketch_571230\Me_Megapi_encoder_pid_pos.ino:1:

    C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeStepperOnBoard.h:76:0: warning: “SLOT_4” redefined

    #define SLOT_4 4

    ^

    In file included from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/Me7SegmentDisplay.h:70:0,

    from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeMegaPi.h:42,

    from C:\Users\Asusnb\AppData\Local\Temp\arduino_modified_sketch_571230\Me_Megapi_encoder_pid_pos.ino:1:

    C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MePort.h:111:0: note: this is the location of the previous definition

    #define SLOT_4 SLOT4

    ^

    In file included from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeMegaPi.h:65:0,

    from C:\Users\Asusnb\AppData\Local\Temp\arduino_modified_sketch_571230\Me_Megapi_encoder_pid_pos.ino:1:

    C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeStepperOnBoard.h:77:0: warning: “SLOT1” redefined

    #define SLOT1 SLOT_1

    ^

    In file included from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/Me7SegmentDisplay.h:70:0,

    from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeMegaPi.h:42,

    from C:\Users\Asusnb\AppData\Local\Temp\arduino_modified_sketch_571230\Me_Megapi_encoder_pid_pos.ino:1:

    C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MePort.h:104:0: note: this is the location of the previous definition

    #define SLOT1 (1)

    ^

    In file included from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeMegaPi.h:65:0,

    from C:\Users\Asusnb\AppData\Local\Temp\arduino_modified_sketch_571230\Me_Megapi_encoder_pid_pos.ino:1:

    C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeStepperOnBoard.h:78:0: warning: “SLOT2” redefined

    #define SLOT2 SLOT_2

    ^

    In file included from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/Me7SegmentDisplay.h:70:0,

    from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeMegaPi.h:42,

    from C:\Users\Asusnb\AppData\Local\Temp\arduino_modified_sketch_571230\Me_Megapi_encoder_pid_pos.ino:1:

    C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MePort.h:105:0: note: this is the location of the previous definition

    #define SLOT2 (2)

    ^

    In file included from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeMegaPi.h:65:0,

    from C:\Users\Asusnb\AppData\Local\Temp\arduino_modified_sketch_571230\Me_Megapi_encoder_pid_pos.ino:1:

    C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeStepperOnBoard.h:79:0: warning: “SLOT3” redefined

    #define SLOT3 SLOT_3

    ^

    In file included from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/Me7SegmentDisplay.h:70:0,

    from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeMegaPi.h:42,

    from C:\Users\Asusnb\AppData\Local\Temp\arduino_modified_sketch_571230\Me_Megapi_encoder_pid_pos.ino:1:

    C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MePort.h:106:0: note: this is the location of the previous definition

    #define SLOT3 (3)

    ^

    In file included from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeMegaPi.h:65:0,

    from C:\Users\Asusnb\AppData\Local\Temp\arduino_modified_sketch_571230\Me_Megapi_encoder_pid_pos.ino:1:

    C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeStepperOnBoard.h:80:0: warning: “SLOT4” redefined

    #define SLOT4 SLOT_4

    ^

    In file included from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/Me7SegmentDisplay.h:70:0,

    from C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MeMegaPi.h:42,

    from C:\Users\Asusnb\AppData\Local\Temp\arduino_modified_sketch_571230\Me_Megapi_encoder_pid_pos.ino:1:

    C:\Users\Asusnb\Desktop\arduino-1.8.1\libraries\makeblock\src/MePort.h:107:0: note: this is the location of the previous definition

    #define SLOT4 (4)

    ^

    Me_Megapi_encoder_pid_pos:31: error: stray ‘\226’ in program

    count–;

    ^

    exit status 1
    stray ‘\226’ in program

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.

    1. Hi Mert

      The program is ok, we tested it here with Arduino 1.8.1 and it compiles success. You may change a PC have a test.

    1. The main board of the Ultimate 2.0 is the MegaPi, if you want to change the main board, you may need to rewrite the whole firmware.
      Besides, some electronic modules are special for MegaPi which won’t work with the Orion board.

Leave a Reply

Your email address will not be published. Required fields are marked *