Hello! Apologies if this is the wrong category, first time
In the last few days I've been trying to figure out an issue with my code, here's some background and then an explanation of the problem:
Master: Arduino Uno
Slave: 0x50 (a custom microcontroller MVLD)
Here's a diagram explaining how it is all connected:

Background:
Trying to read Temperature from a custom sensor that's connected to the MVLD - I have a device that sits on the i2c bus, and can monitor it, so if I write to the i2c bus it can detect it, if I read from it it'll also detect it (aardvark i2c), I'm using a python code in order to communicate with the serial port (using PySerial), and then I send the data from the serial port to the i2c bus using the arduino code, for example if I wanted to set the current I can do that using the python code and it works. The issue is when I try to read, it just won't send the bytes that I am reading to the Serial port so I could later print it out in my python code (I know I am reading it because of the i2c monitor device, and it is working)
Not sure if I gave a clear enough explanation, but TLDR: I'm accessing data on the microcontroller (MVLD) using the i2c bus, and I'm trying to print out what I'm reading to the serial port, so I can then read it in my python code.
Arduino code:
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
pinMode(LED_BUILTIN, OUTPUT);
pinMode(SCL, INPUT_PULLUP);
pinMode(SDA, INPUT_PULLUP);
Serial.println("Arduino setup");
}
void loop() {
unsigned char message[20];
bool isRead = false;
int mIdx = 0;
if (Serial.available())
{
Serial.print("Arduino Received : ");
// int mIdx = 1;
while (Serial.available())
{
message[mIdx] = Serial.read();
Serial.print(message[mIdx]);
Serial.print(" ");
mIdx++;
if ((mIdx == 1) && (message[0]) == 0x02)
{
isRead = true;
}
if (isRead && mIdx >= 3)
break;
delay(5);
}
message[mIdx] = '\0';
Serial.println("Arduino " + message[0]);
Serial.println("Arduino read");
Wire.beginTransmission(0x50); // transmit to device #9
Wire.write(message + 1, mIdx - 1);
Wire.endTransmission();
Wire.requestFrom(0x50, 3, true);
while (Wire.available()) {
byte readBytes = Wire.requestFrom(0x50, 3);
Serial.print(readBytes, HEX);
Serial.print(" ");
delay(1000);
// This while loop works, but it doesn't seem to be printing the bytes to the serial port, reading works
}
delay(100);
if (Wire.available()) {
while (Wire.available() <= 3) {
delay(1);
}
Wire.readBytes(message, 3);
delay(100);
Serial.write(message, 3);
}
delay(100);
}
delay(100);
}
Python code that I use to access the temperature sensor, and then read the data from it:
checksum = (slave_id + temperature_cmd_id) # Set the checksum for the Temperature
write_structure = [enable_writing, temperature_cmd_id, checksum] # Create the write structure for the Temperature
write_structure_bytearray = bytearray(write_structure) # Turn the write structure into bytes
serial_instance.write(write_structure_bytearray) # Write the bytes to the serial port
while True:
for j in range(2):
temperature_line = serial_instance.readline() # Read the data from the serial instance
print(temperature_line.decode('utf').replace('Arduino led', '').strip()) # Decode data using 'utf' decode and remove unneccessary text
FYI I am a beginner with arduino and this is my first project, so please try and explain the answers in a tad bit more detail so I could understand them ![]()
If I didn't give a clear enough explanation or there's something missing, please let me know so I can try and explain it properly!
Thanks!