I2C read function

I have a main python file and an additional library file in which I have functions definitions.

I need to call these functions in the main python file. The device address and register address are the arguments of the function. Both are 1 byte registers.

I am not sure what is the error in the attached screenshot. I think it is related to arguments in the functions which are not properly defined or extracted in the function definitions.

Please don’t post pictures of text. Post the code and the associated error message using the forum’s “Preformatted text“ option.

1 Like

I assume the error you refer to are the underlined variables.

Apart from the suggestion of pasting the actual code as text it would really help to also include the error reported, in this case by I guess hovering over them in the IDE/editor or the result of running the code.

However the error in this case is most probably that not all code is indented to the same level.
The while loop in particular is flush with the left-hand side which causes python to interpret the function as finished and therefore the following code is seen as global where DEVICE_ADDR and REG_ADDR are not defined.

If you indent all code from the while loop and down one level, i.e. 4 spaces, it should probably work as intended.

1 Like

Yes, I also think that the error is with underlined variables.

Now I also understand that the code is not indented to same level.

The issue is fixed after having all the code indent to same level. Thanks for help.