i2c read giving same number (after crash?)

Hi, I have an Arduino Nano controlling a DRV8825 stepper motor driver board, the Arduino itself is controlled via i2c from an RPI. There are two end-stops for the stepper motor, these are connected to the Arduino.

When I move the motor back and forth between the stops (via i2c commands every couple of seconds) - everything works fine for a while.

Then I think it crashes. The Arduino does not respond to any commands. I still see the Arduino as an i2c device from the RPI. When I send i2c_request s I get the same numbers / parameters back every time, no matter what read command I send.

Once I press the reset button on the Arduino everything works again.

Any ideas? All code attached.

Motor_Code_v2.2.ino (2 KB)

loop.ino (1.58 KB)

setup.ino (851 Bytes)

commands.ino (1.04 KB)

Hi, I can not spot a bug in the code, but there are a few things that got my attention.

A Raspberry Pi with a Arduino Nano and a stepper motor, those three together can already give a lot of problems.

Is the current for the stepper motors somehow influencing the Arduino Nano ? Perhaps with a ground current ? Can you tell how that is connected ?

How did you connect the GND of the Raspberry Pi to the GND of the Arduino ? The I2C has three wires: SDA, SCL and GND.

The Raspberry Pi runs at 3.3V and the Arduino Nano at 5V. How did you connect a 3.3V I2C bus to a 5V I2C bus ? How long are the wires for the I2C bus ? Do you use a cable for the I2C bus ? Do you use pullup resistors and what is their value ? It is possible that the I2C bus is stuck, so you have to get that right.

In the sketch, you don't have to include stdio.h and stdlib.h. Please remove those.

In the i2c_receive() function there is a question if the bytes should be read.
There is no need to read the bytes. The I2C works with packages of data. You can ignore the data and the next time you receive a new package.

In the sketch you do a while-loop until the stepper motor has reached the targe position. During that, it is hard to do other things. You break out of that while-loop if a new command was given via I2C. That makes the code harder to understand.
Would accelStepper make the sketch easier ?

I put all of your code in a single file. It is less than 300 lines, that's not so bad.
When you want to have smaller files, then I suggest to keep the global variables, the setup() and the loop() in the main *.ino file and move functions into other files.

When I turn on the all the warnings in the settings, then it gives warnings for the i2c_request() function. Did you forget a few "break ; " ? I think you always return 3 bytes and only 0x01 and 0x02 have a break.

Why did you choose the I2C bus to communicate with the Arduino ? When the Arduino board is connected with the USB bus, then you can use the Serial bus. There is a library to communicate using Python: GitHub - PowerBroker2/pySerialTransfer: Python package to transfer data in a fast, reliable, and packetized form.