Communication with Arduino Mega - Serial or I2C?

Discussion related to "under the hood" OpenMV topics.
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Communication with Arduino Mega - Serial or I2C?

Postby ScottM » Fri Dec 13, 2019 3:43 pm

I'm building a robot that will use an Arduino Mega as the controller and there will be two OpenMV H7 cameras that I need to communicate bidirectionally with. What is the best protocol to use, I2C or Serial? I have searched this forum and found lots of old posts and my own experiments using serial are not producing consistent performance. I'd like to hear what the latest wisdom is. Also, what is the latest firmware for the H7?
User avatar
kwagyeman
Posts: 3719
Joined: Sun May 24, 2015 2:10 pm

Re: Communication with Arduino Mega - Serial or I2C?

Postby kwagyeman » Fri Dec 13, 2019 4:11 pm

Serial is the best to use. I2C is definitely not what you want to use.

Typically folks fail with serial comms because they have difficulty programming things. If you are not new to making two processors talk to each other serial is very easy.
Nyamekye,
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Communication with Arduino Mega - Serial or I2C?

Postby ScottM » Mon Dec 16, 2019 7:42 am

I have serial comm working, but it stops randomly, so I'm leaning towards I2C. I can post my code for Arduino and OpenMV with regards to the serial comm portion.
User avatar
kwagyeman
Posts: 3719
Joined: Sun May 24, 2015 2:10 pm

Re: Communication with Arduino Mega - Serial or I2C?

Postby kwagyeman » Mon Dec 16, 2019 11:38 am

Can you post the code that you think is in error? I know the serial comm code is robust... I've built a robot that uses it to communicate between the OpenMV Cam and an Arduino and it works fine.

Please post the serial comm side snippet and etc.
Nyamekye,
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Communication with Arduino Mega - Serial or I2C?

Postby ScottM » Mon Dec 16, 2019 1:35 pm

As promised, here is my code for the OpenMV.

Code: Select all

# P4 UART3 tx goes to Mega RX1 on pin 19
# P5 UART3 rx goes to Mega TX1 on pin 18

import time
from pyb import UART

uart = UART(3, 9600, timeout_char=1000)

print("Starting test")

for x in range(0, 6):
    print (x)
    uart.write("Cycle # %d \n" % x)   #send text to display on lcd connected to Mega
    time.sleep(1500)

print("Finished")
and for Arduino Mega

Code: Select all


#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

String Data = "";

void setup()
{
  Serial.begin(9600);                  // to display on computer screen
  Serial1.begin(9600);                 // Mega Serial1 uses pins 18 (TX1) and 19 (RX1)
  lcd.begin(20, 4);
  Data = "";
  lcd.clear();
  Serial.println("Program start");     // display in serial window on PC
  DisplayL1("Serial COM test");
}


void loop()
{
  if (Serial1.available()) {
    char character = Serial1.read();  
    if (character == '\n') { 
      Serial.println(Data);
      DisplayL1(Data); 
      delay(10);
      Data = ""; 
    }
    else
    {
      Data.concat(character);
    }
  }
  delay(10);
}


void DisplayL1(String Data) {
  lcd.setCursor(0, 0);                    // position cursor
  lcd.print("                    ");          // clear row by printing 20 spaces
  lcd.setCursor(0, 0);                    // re-position cursor
  lcd.print(Data);                          // display data
}
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Communication with Arduino Mega - Serial or I2C?

Postby ScottM » Mon Dec 16, 2019 1:37 pm

How to I code the OpenMV to read serial data from the Arduino? I want to be able to send commands or data from Arduino to OpenMV, such as for a quick check to see if it's available.
User avatar
kwagyeman
Posts: 3719
Joined: Sun May 24, 2015 2:10 pm

Re: Communication with Arduino Mega - Serial or I2C?

Postby kwagyeman » Mon Dec 16, 2019 3:07 pm

In the other post you just posted to there's an example of that.
Nyamekye,
ScottM
Posts: 70
Joined: Thu May 30, 2019 2:12 pm
Location: Ottawa, Canada

Re: Communication with Arduino Mega - Serial or I2C?

Postby ScottM » Mon Dec 16, 2019 8:59 pm

Yes, I found that post after I made my request. So you see from my code that I am using the Arduino Mega to handle the I2C LCD display. I tried months ago to figure out how to get the OpenMV to display on it but couldn't figure it out. If someone has OpenMV working with a common I2C LCD display, I'd like to get some code for it.

Return to “Technical Discussion”

Who is online

Users browsing this forum: MSN [Bot] and 5 guests