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 Arduino IED.

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\Java\libraries\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