TWI (send float)

Hi Guys,

I am trying to send a float value over an I2C connection.

I have an I2C communication working perfectly and I can easily send a string from one Arduino to another.

As a float in a 32 bit number and i2c transmits 8 bit packets i cannot simply pass the float value to Wire.Send().

I believe the solution may lie with bit shifting of by using pointers to read each byte of the float individually. I would then need to reconstruct the float value.

I hope you can help.

Many thanks,


There are at least two ways to crack a float.

Are you familiar with unions?

How about pointers?

Hi Bradly,

Many thanks for your response.

I have a basic understanding of pointers. I don't know anything about unions. Sounds like i need to hit the books.

I tried something like this last night but had no joy.

char[4] temp;

temp[0] = roll;
temp[1] = *(&roll + 1);
temp[2] = *(&roll + 2);
temp[3] = *(&roll + 3);


Hi Bradly,

I'm sure Bradly says hi back! :stuck_out_tongue:

Does this help?

float MyFloat;
byte* MyFloatPtr;

MyFloat = 3.1415;

MyFloatPtr = (byte*) &MyFloat;

Wire.Send( MyFloatPtr[0] );
Wire.Send( MyFloatPtr[1] );
Wire.Send( MyFloatPtr[2] );
Wire.Send( MyFloatPtr[3] );

Thanks for your reply "Bradly" :-[.

I can't test the code at the moment but it looks good.

Could i not just pass the pointer to Wire.send().

For example

float MyFloat;
byte* MyFloatPtr;

MyFloat = 3.1415;

MyFloatPtr = (byte*) &MyFloat;

Wire.Send( MyFloatPtr);

I will let you know how i get on.



I believe that would send the low byte of the address of MyFloat. You need to dereference the pointer to get the first byte...

Wire.Send( [glow]*[/glow]MyFloatPtr );

But that only sends the first byte. It needs to be repeated for the remaining three bytes...

Wire.Send( *MyFloatPtr );  // send first byte
Wire.Send( *MyFloatPtr );  // the second
Wire.Send( *MyFloatPtr );  // third
Wire.Send( *MyFloatPtr );  // fourth and final byte

The code above is functionally equivalent to the earlier version.

Hello it's me again,

I'm still not quite there.

In the master side of the communication i call.

Wire.requestFrom(2, 12).

When i call this method i only get the first byte of the float all the other values as 0xff (tested with logic analyser). i'm not entirely sure how the wire library works but i believe the problem is that i'm calling Wire.send() multiple times. By doing this the bytes are not cued in the buffer before they are requested by the master.

I've just had another look at the following reference page.

And there is another method.

Wire.send(data, quantity)

I haven't had chance to try this yet.

float MyFloat;
byte* MyFloatPtr;

MyFloat = 3.1415;

MyFloatPtr = (byte*) &MyFloat;

Wire.Send( MyFloatPtr, 4 );

I've not used the Wire library so I'm not going to be much more help.

I suspect you can do something like this...

  MyFloat = 3.1415;
  Wire.send( &MyFloat, sizeof(MyFloat) );

To transmit the floating-point number. But I honestly don't know if that works or if there is another problem.