Code works only if power is supplied from USB port

Hi everyone

I am interfacing a 4Duino LCD to an Arduino Micro
I am trying to implement the I2C protocol.
I want a variable X to be shown on the 4Duino LCD, incrementing from 0 to 5, then resetting and starting again. I found the code online, quotes are in the code. the variable X is sent to the 4Duino from its master, the Arduino Micro.

this is the code compiled on the A Micro (master):

// Chapter 7 - Communications
// I2C Master
// By Cornel Amariei for Packt Publishing

// Include the required Wire library for I2C
#include <Wire.h>

int x = 0;

void setup() {
  // Start the I2C Bus as Master
  Wire.begin(); 
}

void loop() {
  Wire.beginTransmission(9); // transmit to device #9
  Wire.write(x);              // sends x 
  Wire.endTransmission();    // stop transmitting
 
  x++; // Increment x
  if (x > 5) x = 0; // `reset x once it gets 6
  
  delay(500);
}

and this is the code compiled on the 4Duino 2.4" (slave):

//
#define RESETLINE     30
#define DisplaySerial Serial1

// uncomment to log SPE errors messages to Serial (serial monitor)
#define LOG_MESSAGES

#include "Picaso_Serial_4DLib.h"
#include "Picaso_Const4D.h"

Picaso_Serial_4DLib Display(&DisplaySerial);

/* the following code is taken from I2C_Slave */
#include <Wire.h>
#define led 13
int x = 0;

// routine to handle ESP Serial errors
void mycallback(int ErrCode, unsigned char Errorbyte){
  
  #ifdef LOG_MESSAGES
  const char *Error4DText[] = {"OK\0", "Timeout\0", "NAK\0", "Length\0", "Invalid\0"} ;
  Serial.print(F("Serial 4D Library reports error ")) ;
  Serial.print(Error4DText[ErrCode]) ;
  
  if (ErrCode == Err4D_NAK){  
    Serial.print(F(" returned data= ")) ;
    Serial.println(Errorbyte) ;
  }else{
    Serial.println(F("")) ;
  }while (1) ; /* you can return here, or you can loop */
  
  #else
  /* Pin 13 has an LED connected on most Arduino boards. Just give it a name*/
  #define led 13

  while (1){
    digitalWrite(led, HIGH);   
    delay(200);                
    digitalWrite(led, LOW);    
    delay(200);                
  }
  
  #endif

} /* end of routine to handle Serial errors */



void setup(){
/*Uncomment to use the Serial link to the PC for debugging*/
/*serial to USB port*/
//  Serial.begin(115200) ;        
/* Note! The next statement will stop the sketch from running until the serial monitor is started
    If it is not present the monitor will be missing the initial writes*/
/* wait for serial to be established*/
//    while (!Serial) ;             

  pinMode(RESETLINE, OUTPUT);       /* Picaso reset pin */
  digitalWrite(RESETLINE, 1);       /* Reset Display, using shield */
  delay(100);                       /* wait for it to be recognised */
  digitalWrite(RESETLINE, 0);       /* Release Display Reset, using shield */  
  delay(3000) ;                     /* give display time to startup */

  /* now start display and ESP as Serial lines should have 'stabilised' */
  DisplaySerial.begin(200000) ;     /* Hardware serial to Display, same as SPE on display is set to */
  Display.TimeLimit4D = 5000 ;      /* 5 second timeout on all commands */
  Display.Callback4D = mycallback ;

  Display.gfx_ScreenMode(PORTRAIT) ; /*change manually if orientation change*/
  
  /* put your setup code here, to run once: */
  
  /*code from I2C_Slave*/
  pinMode (led, OUTPUT);
  /* Start the I2C Bus as Slave on address 9 */
  Wire.begin(9);
  /* Attach a function to trigger when something is received. */
  Wire.onReceive(receiveEvent);
  
  /*-------------------*/
  
} /* end Setup **do not alter, remove or duplicate this line**  */

/* from I2C_Slave */
void receiveEvent(int bytes) {
  x = Wire.read();    /* read one character from the I2C */
}

void loop(){
  // put your main code here, to run repeatedly:
  Display.txt_MoveCursor (1,0);
  Display.println ("Current value of");
  Display.println ("variable X is:");
  Display.txt_MoveCursor (4,0);
  Display.println(x);
  

//  Display.println ("This is the second line");
//
//  Display.txt_MoveCursor (6,0);
//  Display.print ("This is the fourth line");
}

If the 4Duino is connected to the arduino only using the SDA and SCL cables, while the power is supplied from the USB port, the code works fine.

If I am feeding 5Vdc to the 4duino Vin pin using the 5V output present on the Arduino Micro, and connecting the GND of the 4duino to the gnd pin on the Micro, the code doesn't work. The 4Duino powers up, but the value on screen don't change.

any good link to look at or any suggestion?
could be the 4Duino requires more current than what is supplied from the A.Micro?

thank you!!

int x = 0;

Why do you need an int to hold a value that will range from 0 to 5?

  Wire.write(x);              // sends x

The write() method does NOT take an int. What happens to the value that you pass, when the function doesn't accept the type you pass?

If you are powering both devices from USB cables, then there is a common ground, through the PC. When you power one with batteries, and connect ONLY SDA and SCL, there is NO common ground, so, of course, the device is not going to get data correctly.

Hi,

If I am feeding 5Vdc to the 4duino Vin pin using the 5V output present on the Arduino Micro, and connecting the GND of the 4duino to the gnd pin on the Micro, the code doesn't work. The 4Duino powers up, but the value on screen don't change.

Vin on the 4duino is not the 5V input pin, it is the power in for 7 to 12V, it feeds the 4duino onboard linear 5V regulator.
You are feeding 5V to the regulator input, which it cannot use to product 5V for the 4duino.

You need to feed the 5V from the Arduino to the 5V pin of the 4duino.
However your micro may not be able to supply the current needed by the 4duino.

Tom.. :slight_smile:

Note that 'x' should be 'volatile' in the receiver side since it can be changed at any time.

volatile byte x = 0;