,

Convert from float to byte/bit Arduino

Posted by

Today we will learn how to convert from float to byte/bit on Arduino, going from a floating point number or integer to a byte or bit. Recently I made a project where I needed to do exactly that, I had a float value and needed to convert it to binary to show it on LEDs.

We will use the Arduino IDE and no special library is needed, just everything that comes with it.

But first, let’s understand a little about the numbering systems in Arduino, more precisely the float type. The official documentation says that floats are made up of 32 bits (4 bytes) and can vary numerically between 3.4028235E+38 and -3.4028235E+38. In the specific case of the example in the link above, I was using a DHT11 sensor whose temperature “t” is shown in float.

There are other types of data in Arduino, which you can research for yourself. Let’s look at the technique I used:

The problem and the solution

In the project in question, I needed to read a temperature from the DHT11 sensor and convert it to a format that could connect LEDs in binary form. For example the number 26 would be equal to 011010 (6 bits). This is in 6 bits and just the integer number 26, without being a float. This was the objective of my code: turn on LEDs according to the temperature presented by the sensor.

The solution is in the function below, complete. I’ll explain it step-by-step later.

for (int i = 0; i < 6; i++) {


      bitsresultados[i] = bitRead(int(round(t)), i); //This is where the magic happens, I get the "t" value (temperature),
      // round it, convert it to integer and extract every bit of its binary equivalent. The bits are then used to enter
      // each charlieplexed LED bit value
      
    }

The for loop makes six iterations, from 0 to 5; in each iteration a bit of the bitRead() function is read. The variable bitsresultados[] is an array whose size is the same as the for loop, six bits. This is where the bits that the function will extract will be stored.

The “magic” happens in the line below, where I first round the variable “t” (remove decimal places). Then convert it to an integer (int) and then apply the bitRead function to extract each bit individually.

bitsresultados[i] = bitRead(int(round(t)), i);

The function of the for loop is to make the above line happen six times to extract each individual bit.

The results

If the variable “t” carries a temperature of 26.3ºC, for example, we will first have a conversion to 26.0 and then to 26 integers. Then the bitRead function will extract each bit, which in this case looks like this:

  • bitsresultados[0]= 0
  • bitsresultados[1]= 1
  • bitsresultados[2]= 0
  • bitsresultados[3]= 1
  • bitsresultados[4]= 1
  • bitsresultados[5]= 0

Having each bit inside bitsresultados[] I was able to write functions. They converted these values ​​to activate or not activate digital outputs. If you want to delve deeper into the bitRead function, here is the official documentation.

This was another quick and useful tip on using Arduino, follow the blog to stay up to date with more news.

One response

Leave a Reply

Your email address will not be published. Required fields are marked *