Programming mBot 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.

 

Arduino programming

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

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) If the driver is not automatically installed on your computer, you can install it manually.

Refresh and recover the firmware

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

7.png

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

 

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.

 

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

 

Introduction of various sensors and components

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

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.

 

Contact us

Mail to: service@makeblock.com

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

Comments

0 comments

Please sign in to leave a comment.