Connect Orion to Arduino Mega?


#1

I’m brand new to this, and I’ve spent the past couple days trying to figure this out so please excuse my ignorance.

How do I connect an Orion to an Arduino Mega? I have an RJ25 adapter and am using the S1 and S2 pins connected to the Mega’s TX and RX pins. I’ve coded the mega to send a few bytes of (analog) data every 1 second. I can see that the Orion is receiving something (via serial window on my computer) every 1 second, but the characters are all gibberish.

Is there a better way to connect the two? I’ve tried to do an I2C connection but I couldn’t achieve any results. I tried a direct SDA/SCL connection between the two boards, but I don’t know how to code for that on the Orion.


#2

Hi GBabstII,

Actually, we haven’t tried connect Orion to Arduino mega board. This is belong to advanced DIY application, we may not be able to provide all the support. While here we have several suggestions to you for reference:

  1. For the problem that the characters are all gibberish, we suspect if you have set the same baud rate on both Orion, Arduino Mega and the serial monitor.
  2. Please pay attention to that the Orion board and Arduino Mega need use the common GND when connect them via TX and RX.
    Hope you can figure it out.

#3
  1. I have the same baud rate for both. I also tried different baud rates.

  2. I am using a common ground.

I assumed that since I’m receiving gibberish with the same baud rate, the different clock speeds of the arduino mega and orion must be the culprit. If true, I’m going to focus on using I2C to get them to communicate.

What is the arduino code to read the I2C data going into port 6 on the orion?

In other words, how do I directly access SDA/SCL on port 6?

(I have an RJ-25 adapter with wires soldered to the SDA/SCL/GRD holes already.)


#4

Hi Gbastll.

I have connected an Orion card and the MegaPi card using I2C. You have to know that the Megapi is based on the ATmega2560 processor and the Arduino Mega card is based on the same processor. So it means they can be connected (Orion + Arduino Mega) using I2C wiring.

You need this Makeblock wire to connect both cards.
RJ-25 to Dupont (http://store.makeblock.com/rj25-to-dupont-wire-pair)

First thing you have to check is the wire order in the RJ-25, I mean, which one is wire 1, 2,…,6 and identify which one is used for SDC, SCL, GND & 5V
The one I have has these colors:
SCL - RJ25 connector Blue wire.
SDA - RJ25 connector Yellow wire.
GND - RJ25 connector Green wire.
5V - RJ25 connector Red wire.
A7 - RJ25 connector Black wire.
A0 - RJ25 connector White wire.

This diagram may help you (https://www.makeblock.es/public/robot_kit/EL_BASEBOARD/info_z_base_board_scheme.jpg)

At the Orion side. You have to connect the RJ-25 into any port that use SCL and SDA (I use the Port 4) Arduino code -> Wire.begin(4);

Now at the Arduino mega you have also to identify the SCL(20) & SDA(21) pins, GND & 5V, and connect the dupont from the Orion. (there is a problem, both pins ar female, so you have to find some adapter.)

So the connection must be: (be carefull with 5V and GND)

Orion side (RJ25) Arduino Mega Side (Dupont)
SCL - RJ25 Blue wire. -----> SCL (20)
SDA - RJ25 Yellow wire. —> SDA (21)
GND - RJ25 Green wire. —> Any GND
5V - RJ25 Red wire. ----> Any 5V
A7 - RJ25 Black wire. (Not used)
A0 - RJ25 White wire. (Not used)

Hope it helps you.


#5

Thank you kike :slight_smile:

I just ordered the RJ-25 to dupont cable now. Can you share some example code for that setup while I wait for it to be delivered?


#6

Hi GBabstII

Let me explain you my project, it is a Mars rover, the Orion card has the sensors, and the MegaPi card has 4 servos and 6 motors for the wheels. So the MegaPi request to the Orion the sensors data, and the orion thru the requestEvent function, sends back to the MegaPi the data in a 23 chars string.

Of course my project is not finished yet, but on free days I spent some time to it.

You have to know that you need two programs, one for the Orion, and the other for the Arduino Mega, an decide which card must be the “Maincard”.

Here is the full code I wrote for the Orion:

#include <Arduino.h>
#include <MeOrion.h>
#include <Wire.h>
#include <SoftwareSerial.h>

MeUltrasonicSensor us_frente(3);
MeUltrasonicSensor us_espalda(4);
MeHumiture humiture(7);
Me7SegmentDisplay pantallita(6);
float frente;
String frenteS;
float espalda;
String espaldaS;
float humedad;
String humedadS;
float temperatura;
String temperaturaS;
String datos;
int i=0;
int vuelta=0;

void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
pantallita.display(0000);
Serial.begin(9600);
Wire.begin(4);
Wire.onRequest(requestEvent);
Serial.println(“Inicializando Orion…”);
}
void loop()
{
digitalWrite(LED_BUILTIN, LOW);buzzerOn();
delay(100);
humiture.update();
}

void requestEvent()
{
//Serial.println(“Inicio de un request”);
digitalWrite(LED_BUILTIN, HIGH);
frente=us_frente.distanceCm()/2;
frenteS=String(frente,2);
espalda=us_espalda.distanceCm()/2;
espaldaS=String(espalda,2);
humedad=humiture.getHumidity();
//Serial.println(humedad);
humedadS=String(humedad,2);
temperatura=humiture.getTemperature();
//Serial.println(temperatura);
temperaturaS=String(temperatura,2);

if (frenteS.length() > 5) {frenteS = "error";}
if (frenteS.length() < 5) {frenteS = " " + frenteS;}
if (espaldaS.length() > 5) {espaldaS = "error";}
if (espaldaS.length() < 5) {espaldaS = " " + espaldaS;}
if (temperaturaS.length() > 5) {temperaturaS = "error";}
if (temperaturaS.length() < 5) {temperaturaS = "error";}
if (humedadS.length() > 5) {humedadS = "error";}
if (humedadS.length() < 5) {humedadS = "error";}
datos="";
datos += frenteS;
datos += " ";
datos += espaldaS;
datos += " ";
datos += temperaturaS;
datos += " ";
datos += humedadS;
Serial.println(datos);
//Serial.println(datos.length());
pantallita.display(vuelta);vuelta = vuelta + 1;
Wire.write(datos.c_str()); // 5+5+5+5+3 = 23 chars
//Serial.println("Fin del request");
buzzerOff(); 

}

And here is the full code for the MegaPi: Do not pay attenton to the Bluetooth code, it is not working.

#include “MeMegaPi.h”
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <Wire.h>

Servo s1D;
Servo s1I;
Servo s2D;
Servo s2I;
String Datos;
String orden;
MeBluetooth bt(PORT_5);

void setup()
{
Serial.println(“Inicializando…”);
pinMode(LED_BUILTIN, OUTPUT);
Wire.begin();
Serial.begin(9600);
bt.begin(115200);
s1D.attach(60); s1D.write(90);
s1I.attach(61); s1I.write(90);
s2D.attach(62); s2D.write(90);
s2I.attach(63); s2I.write(90);
Serial.println(“Inicializado el Megapi.”);
}
void loop()
{
Serial.println(“Loop inicio”);
if (bt.available()) {orden=btLeer();Serial.println(orden);}
Datos="";
Wire.requestFrom(4, 23);
while(Wire.available())
{
char dato = Wire.read();
Datos += dato;
}
Serial.println(Datos);
digitalWrite(LED_BUILTIN, HIGH);
delay(1500);
digitalWrite(LED_BUILTIN, LOW);
}

String btLeer()
{
Serial.println(“btInicio…”);
String retorno;
retorno=bt.read();
return retorno;
}

The library for the I2C communication is “Wire”, is just the same used for arduino, you can see the library function usage and samples on the arduino site: https://www.arduino.cc/en/Reference/Wire

Good luck.
I will try to upload some photos of the connections I use.


#7

Here are the photos…

I really use the port 8, but in the code I put Wire.begin(4); The number 4 is not related to the port, it must be the same number used in the MegaPi code in the Wire.requestFrom(4, 23);

Wire colors Blue, Yellow, Green, Red, Black & white.

At the MegaPi card, the I2C are te Yellow and the Blue wires, and green to gnd. I do not connect the 5V wire, due to I use external power.

Good luck and hope it helps you.

Kike.


#8

That is a very cool project!

I am trying to use my arduino as a sensor that sends commands to the Orion (which has 4 mecanum wheels with encoder motors). I’ve managed to get the two boards communicating with I2C, but I have a new problem. They send/receive with no issue, but once I connect the motors to ports 1 and 2, the orion “hangs” and stops requesting data from the arduino. Here’s what my code looks like:

It’s just a basic test, to see if my bot will use the byte sent from the arduino to slowly accelerate forward.

#include “MeOrion.h”
#include “SoftwareSerial.h”
#include <Wire.h>

MeEncoderNew motor1(0x09, SLOT1);
MeEncoderNew motor2(0x09, SLOT2);
MeEncoderNew motor3(0x0a, SLOT1);
MeEncoderNew motor4(0x0a, SLOT2);

#define MAXSPEED 180
#define DEFAULTSPEED 100;
typedef struct {
signed int backfront;
signed int leftright;
signed int chinaHat;
}
RCDATA;
RCDATA Rcdata;

int motor1speed=0;
int motor2speed=0;
int motor3speed=0;
int motor4speed=0;
int moveSpeed = DEFAULTSPEED;

unsigned char stop_flag=0;
int Matrix[4]={1,-1,1,-1};//this array is set to motor’s direction

void parseJoystick(int buf[])
{
Rcdata.leftright = buf[0]; //control receive data
Rcdata.backfront = buf[1];
Rcdata.chinaHat = buf[3];
}

void motorprocess()
{
if(Rcdata.chinaHat == 1)
{
Speed_up();
}
else if(Rcdata.chinaHat == 5)
{
Speed_down();
}

motor1speed = moveSpeed*Matrix[0];
motor2speed = moveSpeed*Matrix[1];
motor3speed = moveSpeed*Matrix[2];
motor4speed = moveSpeed*Matrix[3];

if(Rcdata.backfront >= 135 && Rcdata.backfront <= 150) // if in mid_y pos
{
if(Rcdata.leftright >= 105 && Rcdata.leftright <= 120) // if in mid_x pos
{ // begin x_y centered
if(Rcdata.chinaHat == 7)
{
TurnLeft_run(); //Spot Turn To Left
}
else if(Rcdata.chinaHat == 3) //china hat
{
TurnRight_run();//Spot Turn To Right
}
else
{
stop_flag++;
if(stop_flag>2) // if x_y centered and no hat input, stop in place
{
Stop_run(); //Release the keys/Stop motor
stop_flag=0;
}
}
} // end x_y centered, then check x displacement
else if(Rcdata.leftright < 105)
{
Left_run(); // if CH axis moved left, execute left run
}
else if(Rcdata.leftright > 120)
{
Right_run(); // if CH axis moved right, execute right run
}
}
else if(Rcdata.backfront < 135) // check y displaced forward
{
if(Rcdata.leftright >= 105 && Rcdata.leftright <= 120) // if x is centered, go forward
{
Forward_run();
}
else if(Rcdata.leftright < 105) // if stick left and up, go LeftUp
{
LeftUp_run();
}
else if(Rcdata.leftright > 120) // if stick right and up, go RightUp
{
RightUp_run();
}
}
else if(Rcdata.backfront > 150) // check y displaced backward
{
if(Rcdata.leftright >= 105 && Rcdata.leftright <= 120) // repeated as for forward displacement
{
Backward_run();
}
else if(Rcdata.leftright < 105)
{
LeftDown_run();
}
else if(Rcdata.leftright > 120)
{
RightDown_run();
}
}
}

void Forward_run()
{
motor1.runSpeed(motor1speed,0);
motor2.runSpeed(motor2speed,0);
motor3.runSpeed(motor3speed,0);
motor4.runSpeed(motor4speed,0);
}

void Backward_run()
{
motor1.runSpeed(-motor1speed,0);
motor2.runSpeed(-motor2speed,0);
motor3.runSpeed(-motor3speed,0);
motor4.runSpeed(-motor4speed,0);
}

void Right_run()
{
motor1.runSpeed(-motor1speed,0);
motor2.runSpeed(motor2speed,0);
motor3.runSpeed(motor3speed,0);
motor4.runSpeed(-motor4speed,0);
}

void Left_run()
{
motor1.runSpeed(motor1speed,0);
motor2.runSpeed(-motor2speed,0);
motor3.runSpeed(-motor3speed,0);
motor4.runSpeed(motor4speed,0);
}

void LeftUp_run()
{
motor1.runSpeed(motor1speed,0);
motor2.runSpeed(0,0);
motor3.runSpeed(0,0);
motor4.runSpeed(motor4speed,0);
}

void LeftDown_run()
{
motor1.runSpeed(0,0);
motor2.runSpeed(-motor2speed,0);
motor3.runSpeed(-motor3speed,0);
motor4.runSpeed(0,0);

}

void RightUp_run()
{
motor1.runSpeed(0,0);
motor2.runSpeed(motor2speed,0);
motor3.runSpeed(motor3speed,0);
motor4.runSpeed(0,0);
}

void RightDown_run()
{
motor1.runSpeed(-motor1speed,0);
motor2.runSpeed(0,0);
motor3.runSpeed(0,0);
motor4.runSpeed(-motor4speed,0);
}

void TurnRight_run()
{
motor1.runSpeed(-motor1speed,0);
motor2.runSpeed(motor2speed,0);
motor3.runSpeed(-motor3speed,0);
motor4.runSpeed(motor4speed,0);
}

void TurnLeft_run()
{
motor1.runSpeed(motor1speed,0);
motor2.runSpeed(-motor2speed,0);
motor3.runSpeed(motor3speed,0);
motor4.runSpeed(-motor4speed,0);
}

void Speed_up()
{
moveSpeed +=2;
if(moveSpeed>MAXSPEED) moveSpeed=MAXSPEED;
}
void Speed_down()
{
moveSpeed -= 2;
if(moveSpeed<0) moveSpeed=0;
}
void Stop_run()
{
motor1.runSpeed(0,1);
motor2.runSpeed(0,1);
motor3.runSpeed(0,1);
motor4.runSpeed(0,1);
}

void setup() {
delay(10);
motor1.begin();
motor2.begin();
motor3.begin();
motor4.begin();
delay(10);
motor1.setMode(2); //0:I2C_MODE;1:PWM_MODE;2:PWM_I2C_PWM;
motor2.setMode(2);
motor3.setMode(2); //0:I2C_MODE;1:PWM_MODE;2:PWM_I2C_PWM;
motor4.setMode(2);
motor1.runSpeed(0);
motor2.runSpeed(0);
motor3.runSpeed(0);
motor4.runSpeed(0);
Wire.begin();
Serial.begin(9600); // start serial for output
Serial.println(“Begin Orion receive:”);
}

void loop() {
Wire.requestFrom(4, 1); // request 1 bytes from device 4
while (Wire.available()) {
int x = Wire.read(); // receive byte as an integer
int buf[] = {0,x,0,0};
parseJoystick(buf);
Serial.println(buf[1]); // print the integer
}
delay(500);
motorprocess();
}

Here’s the code from the arduino:

#include <Arduino.h>
#include <SoftwareSerial.h>
#include <Wire.h>

void setup() {
Wire.begin(4);
Serial.begin(9600);
Serial.println(“Begin Mega sender test”);
Wire.onRequest(requestEvent);
}

byte x = 0;

void loop() {
Serial.println(“Begin Loop”);
delay(100);
}

void requestEvent()
{
Serial.println(x);
Wire.write(x);
x++;
}

What do you think the problem could be?


#9

Hi Gbastll.

I´ve not seen your code, but, maybe your problem is with the power. My rover hangs when testing motors using the USB power and restarts over and over. Solution, an external power supply. If you do not have one, try your test with one motor and with a max speed of 50.
Happy week end :wink:


#10

Thanks kike,

I tried connecting an external battery (11v) to the motors and orion, and I’m still experiencing the I2C problem. I’ll try posting my problem to the general forum as a separate question.