Programming Ultimate 2.0 in Arduino

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

1.png

 

(2) Download Makeblock library functions:

https://github.com/Makeblock-official/Makeblock-Libraries/archive/master.zip

Decompress the makeblock folder in the zip to the Arduino default library.Your Arduino library folder should now look like this:

    • Windows 7(on Windows):

[arduino installation directory]\libraries\makeblock\src

[arduino installation directory]\libraries\makeblock\example

….

2.png

    • or like this (on Mac):

[arduino directory]\Contents\Java\libraries\makeblock\src

[arduino directory]\Contents\Java\libraries\makeblock\example

 

(3) Open the Arduino Application. If already open it, you need to restart to see changes.

3.png

1.2. Connect MegaPi 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 driver;

 

(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 COM3.

 

4.png

 

(3) Then we can validate this with Blink.

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

5.png

b. Click Tools > Port, COM3 (You have to choose corresponding COM port except COM1)

c. Choose File > Example > 01.Basics > Blink to open Blink. Then click Upload to upload the program. This will last around 1min.

 

6.png

 

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 driver by yourself:

1.3. How to refresh and recover the firmware

Choose File > Examples > MakeBlockDrive > Firmware_for_MegaPi > Firmware_for_MegaPi, and upload the program to MegaPi.

7.png

1.4. Examples of programming

(brief example of driver application and complicated example of programming)

(1) You should prepare these devices:

      • 1 x encoder DC motor driver;
      • 1 x 25mm DC motor;
      • 1 x MegaPi;
      • 1 x USB B type interface line;
      • 1 x power source (9-12V) (or alkaline battery)

8.png

(2)  Insert encoder DC motor driver to the PORT1 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  burnt.

9.png

(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).

10.png

(4) Open the application program, choose File > Examples > MakeBlockDrive > Me_MegaPiDCMotor > MeMegaPiDCMotorDriverTest.

11.png

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) Choose Tools > Board > Arduino Mega 2560 or Mega ADK.

 

(6) Choose Tools > Port > COM3 (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) Introduction to the functions of  DC motor:

Function Feature

MeMegaPiDCMotor(uint8_t port)

define the port of the motor, and divide them into A and B

void run(int16_t speed)

Motor runs with certain speed

void stop(void)

Stop motor

 

1.5 How MegaPi control 1 encoder motor to rotate clockwise or anticlockwise at one-minute intervals?

(1) You should prepare these devices:

      • 1 x 25 encoder DC motor;
      • 1 x encoder/DC reuse driver;
      • 1 x encoder motor wire;
      • 1 x MegaPi;
      • 1 x USB B type interface cable;
      • 1 x power source

12.png

(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 burnt.

 

(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)

13.png

 

(4) Write program:

 

#include "MeMegaPi.h"

const byte interruptPin =18;
const byte NE1=31;

uint8_t motorSpeed = 100;

long count=0;

unsigned long time;
unsigned long last_time;

MeMegaPiDCMotor motor1(PORT1B);

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) Choose Tools > Board > Arduino Mega 2560 or Mega ADK.

 

(6) Choose Tools > Port > COM3 (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 encoder 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.

 

14.png

 

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.

 

1.6 Explanation of how MegaPi control a stepper motor to rotate

(1) You should prepare these devices:

      • 1 x stepper motor;
      • 1 x stepper motor diver;
      • 1 x MegaPi;
      • 1 x USB B type interface cable;
      • 1 x power source;

15.png

(2) Insert stepper motor driver to the PORT1 of MegaPi. Do align the holes without missing anyone – red pin to red female header, yellow pin to yellow female header.

 

(3) The stepper motor we use is two-phase four-wire stepper motor with four wire: blue, red, green and black. Connect the wires to the ports with this sequence. 

 

(4) Open the application program, choose File > Examples > MakeBlockDrive > Me_MegaPiOnBoardStepper > MegaPiOnBoardStepperTest.

16.png

(5) Choose Tools > Board > Arduino Mega 2560 or Mega ADK.

 

(6) Choose Tools > Port > COM3 (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 stepper motor requires voltage over DC9V (DC12V will be best).

 

NOTE: For this program, you need to the serial port communication on the top right corner of IDE, and then send the numbers 0 to 9 respectively, and the stepper motor will go to the corresponding step number according to the number you sent.

17.png

18.png

 

(8) Features of the DC motor functions:

Function

Feature

MeStepperOnBoard(int slot)

define the port of stepper motor

void setMicroStep(int8_t value)

set microsteps of stepper motor(1, 2, 4, 8, 16, 32)

void setMaxSpeed(float speed)

set the maximum speed of stepper motor

void setAcceleration(float acceleration)

set the acceleration of stepper motor

void enableOutputs(void)

enable stepper motor

void moveTo(long absolute)

move to

void move(long relative)

move

boolean run(void)

run

 

1.7 Introduction of various sensors and components

Link: http://learn.makeblock.com/electronics/

 

2. FAQ of MegaPi

2.1 Where to get the driver package if the computer fails to install it automatically?

Manually install the driver

 

2.2 Why the Port1 – Port4 of MegaPi are anti-reverse?

The Port1 – Port4 of MegaPi are designed as one the row of female header or male header, and they are divided 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 burnt if you connect them reversely.

 

2.3 How much current can the large current driver interface (MOS driver interface) output?

Two interfaces can output current up to DC 12V 10A.

 

2.4 Why the stepper motor get hot?

Considering the usage scenario of stepper 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.

 

2.5 What's the function of the black adjustable potentiometer on the stepper motor?

This potentiometer is for adjusting the current of stepper 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.

 

2.6 Why the master chip reset when MegaPi drives several motors at the same time? How to deal with this problem?

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.

 

2.7 How to weld 2*10 pin header while connecting MegaPi with Raspberry Pi?

MegaPi is not weld to the Raspberry Pi, so you have to weld by yourself. Do avoid short circuit when welding the pins.

 

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

 

2.9 How to get after-sales service if there are some quality problem with MegaPi?

email: tec-support@makeblock.com

 

2.10 Is there any accessories that can work with MegaPi? Where should I buy them?

Encoder motor drivers, stepper motor drivers, Bluetooth, 2.4G, DC motors, encoder motors, stepper motors, Shield, sensors of RJ25, steering engines, structural parts, etc.

 

3. Notice

1. Apply MegaPi voltage DC 6-12V.

2. Prevent short circuits.

3. Prevent the contamination of water, acidity/ alkalinity liquid, or solid debris, etc.

4. Keep away from children and pets.

5. Do not throw away.

 

4. Contact us

email: support@makeblock.com

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.