2way communication between Arduino and Python to send float

Hi,
I’m trying to find a library which enable me to send float in both way(Arduino and Raspi). I prefer the library using I2C or SPI, not USB serial ( cus I want to debug through Arduino’s serial plotter )

Do you know such a library? I’ve been trying to find for 2 days but never found.

Did you try to search the internet?

Or what about:

Thank you for the fast reply!
Yes, I checked them out. However, they can't send "float" to each other

I can't really get the point on that request. You don't need a library other than "Wire.h".

Once you have established an I2C connection between Arduino and RPI (or whatever), you can send any data you want, so the problem could just be on how to let the devices properly exchange data, by defining a common protocol especially because a "float" data can be slightly different between different achitectures.

In this case, you could just either convert a float value into a pre-formatted string (e.g. if values are less than 1000 and you need 4 decimals, its 8 bytes like "###.####") with dtostrf(), or use the binary 4-bytes Arduino float format for I2C communication (i don't know if RPI floats follow the same bytewise structure, you just need to investigate it, and make an RPI-side conversion if needed).

Is this what you are asking for?

Python has a standard library named ctypes which allows you to handle 32 bit floats from Arduino / C.

1 Like

Thank you for the reply!!
Yes, I know the important things are "Wire.h" and the way the devices exchange the data. However, the mile stones will be as follows, and they are so daunting. That's why I need a library.

1.converting a float into 4bytes and send them in order. (from Arduino to RPI)
2.reconstructing 4bytes into float (RPI finally receives the float)

  1. converting a float into 4bytes and send them in order. (from RPI to Arduino)
  2. reconstructing 4bytes into float (Arduino finally receives the float)

The problem is I don't know how to do that in each step.

Hm, please keep in mind an Arduino float is actually stored as a 4-bytes value, so if you know how send bytes (i.e. byte arrays) over I2C with Wire.write(), you have everything you need. You just specify a pointer to the float you want to send, followed by the number of bytes (4). E.g., if you have a float representing a temperature to be sent to RPI, be like:

Wire.write((uint8_t *) &temp, sizeof temp);

When RPI receives that packet it just needs to rebuild the float, but I can't help you with that bc I've never used RPI, sorry.

A simpler way is converting float to a string, as I said before, with a fixed structure to be interpreted back to a float.

If it's a temperature and don't need a lot of decimals, you could convert it to an integer (so just a couple of bytes, a simpler object to pass over) by multiplying the value by 10 and keep just the integer part:

  float temp = 24.351;
  int temp10 = temp*10; // 243

The receiver just needs to convert it back to decimal/float or whatever dividing it by 10.

Anyway, just google a bit, you can find some useful hints like THIS, or even video tutorials like THIS.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.