Problems with Motor shield v2.3 when transfering data via i2c between mega and esp32

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
ryszka
 
Posts: 1
Joined: Wed Feb 15, 2023 2:55 pm

Problems with Motor shield v2.3 when transfering data via i2c between mega and esp32

Post by ryszka »

Hi, i have an motor shield v2.3 for controlling 4 dc motors.
In addition to that i have a i2c connection between my mega 2560 and esp32 dev kit c v4 on (Mega sda 20 and scl 21).
The esp32 is connected to an BANNED 4 controller via bluetooth in order to transmit
the controller values to the mega to controll the motors.

My i2c connection works fine and i receive the data from my esp32 but as soon
as I want to run a motor I dont receive any data from my esp32 and i cant controll the motor.

The motors movement works also as soon as i remove the i2c connection from the esp32.

Mega code :

Code: Select all

#include <PS2X_lib.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <SoftwareSerial.h>
#include <Wire.h>

const int I2C_ADDRESS = 0x10;
const uint8_t SDA_PIN = 20; // choose a different pin for SDA
const uint8_t SCL_PIN = 21; // choose a different pin for SCL
const int I2C_CLOCK = 400000; // I2C clock frequency in Hz




// create an instance of the PS2X class
PS2X ps2x;

Adafruit_MotorShield AFMS_TOP(0x63); 
Adafruit_DCMotor *motor_top = AFMS_TOP.getMotor(1);
Adafruit_DCMotor *motor2_top = AFMS_TOP.getMotor(2);

Adafruit_MotorShield AFMS(0x60); 
Adafruit_DCMotor *motor = AFMS.getMotor(1);
Adafruit_DCMotor *motor4 = AFMS.getMotor(4);
Adafruit_DCMotor *motor3 = AFMS.getMotor(3);
Adafruit_DCMotor *motor2 = AFMS.getMotor(2);
bool startMotor4 = false;

void setup() {
  Serial.begin(115200); // start serial communication with Serial Monitor
   AFMS.begin();
  AFMS_TOP.begin();


  motor->setSpeed(255);
  motor4->setSpeed(255);
  motor2->setSpeed(255);
  motor3->setSpeed(255);
  motor_top->setSpeed(90);
  motor2_top->setSpeed(255);
  Wire.begin(I2C_ADDRESS);
  Wire.onReceive(receiveEvent);

  Wire.setClock(I2C_CLOCK);
  // initialize serial communication
  
  // start serial communication with ESP32
  

 
  
}

void loop() {
 

}

void receiveEvent(int byteCount) {
  if (byteCount == 5) {
    int lStickY = Wire.read();
    int r1 = Wire.read();
    int r2 = Wire.read();
    
    // as soon as this code block appears
    // then the prints below are no more shown
    if(r1) {
      Serial.println("r1");
      motor->run(FORWARD);
      motor4->run(FORWARD); 
    } else if(r2) {
      motor->run(BACKWARD);
      motor4->run(BACKWARD);
    } else {
      motor->run(RELEASE);
      motor4->run(RELEASE); 
    }
    // end of problematic block
    
    int cross = Wire.read();
    int circle = Wire.read();
    

    delay(10); // Add delay


    Serial.print("LStickY: ");
    Serial.println(lStickY);
    Serial.print("R1: ");
    Serial.println(r1);
    Serial.print("R2: ");
    Serial.println(r2);
    Serial.print("Cross: ");
    Serial.println(cross);
    Serial.print("Circle: ");
    Serial.println(circle);

    
  }
}
esp32

Code: Select all

#include <PS4Controller.h>
#include <Wire.h>

const int I2C_ADDRESS = 0x10;
const int I2C_CLOCK = 400000; // I2C clock frequency in Hz


void setup() {
  Wire.begin();
  Wire.setClock(I2C_CLOCK);
  Serial.begin(115200);
  mySerial.begin(115200);
  
  PS4.begin("e8:61:7e:47:83:30"); // replace with your PS4 controller's MAC address
  PS4.setLed(0, 255, 0); // set LED to green
}

void loop() {
  int lStickY = PS4.LStickY();
  int r1 = PS4.R1();
  int r2 = PS4.R2();
  int cross = PS4.Cross();
  int circle = PS4.Circle();
  Serial.println("foop");

  Wire.beginTransmission(I2C_ADDRESS);
  Wire.write(lStickY);
  Wire.write(r1);
  Wire.write(r2);
  Wire.write(cross);
  Wire.write(circle);
  Wire.endTransmission();

  delay(50);
}
Image

Am I doing or understanding something wrong?
Attachments
mega_i2c.JPEG
mega_i2c.JPEG (138.46 KiB) Viewed 159 times
mega_esp32.JPEG
mega_esp32.JPEG (125.47 KiB) Viewed 159 times

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: Problems with Motor shield v2.3 when transfering data via i2c between mega and esp32

Post by adafruit_support_bill »

Once you call "Wire.begin(I2C_ADDRESS);", you are putting the Mega into i2c 'slave' mode. At that point is no longer a master and no longer capable of sending commands to the motors.

There are ways of handling both Master & Slave at the same time. See this page: http://www.gammon.com.au/i2c

Scroll down to the section: "Communicating with other devices"

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”