Welcome to EnviroDIY, a community for do-it-yourself environmental science and monitoring. EnviroDIY is part of WikiWatershed, a web toolkit designed to help citizens, conservation practitioners, municipal decision-makers, researchers, educators, and students advance knowledge and stewardship of fresh water. New to EnviroDIY? Start here

Logging to SD Card More than Two Decimal Places

Home Forums Mayfly Data Logger Logging to SD Card More than Two Decimal Places

This topic contains 2 replies, has 2 voices, and was last updated by  Drew 3 weeks, 5 days ago.

  • Author
    Posts
  • #13114

    Drew
    Participant

    Hello,

    I am trying to read a voltage from a low-cost turbidity sensor, and log that to an SD Card. It is set as a float value (it is <float turbvoltage> in the code below, about 43 lines down when counting spaces), so only outputs to two decimal places when logging to the SD Card (near the end of the code string). I would like to get it to output three decimal places when logging, but I have not seen anything out there on how to do that. I know there is a way to get it to output to more decimal places on the serial monitor with the Serial.Print function, but that value does not log to the SD Card and nothing else I have tried works. I will paste my code below (I apologize if it is a little messy, I am somewhat new to this):

    If anyone could help, it would be greatly appreciated.

  • #13115

    Shannon Hicks
    Keymaster

    I edited your post to move the sketch code into code snippet box. Next time, use the “Add Code Snippet” button at the top of the text editor box when you’re writing a post or comment. It makes it much easier to read, especially for long sketches.

    It’s very easy to add an integer or characters to a string, you did it correctly with the line 263: data += samplenum
    However, that only works for integers and characters (or words). But you can’t do that with floating point numbers. To add a floating point number to a string, the best way to do that is with a separate generic function you can paste into the very bottom of any of your sketches:

    So then anytime you want to add floating point numbers (like batteryvoltage and turbvoltage) to your data string, just use these lines:

    Notice that you can adjust the number of significant digits (numbers after the decimal point) by changing the value for precision.
    You can see an example of how I did all this in the Sleeping Mayfly Logger example on the sample code page:

    Sleeping Mayfly logger example

    Also something to note, in line 269 you have 5.0 in the formula for the board voltage, but that should be 3.3 since the Mayfly board voltage is 3.3, and not 5 like in most other Arduino boards. Are you powering your turbidity sensor with 3.3v or 5v, and what is the output voltage range of your sensor? Feeding anything greater than 3.3v back into the analog input pin (A0 in your code) will damage the Mayfly, so you should either use a voltage divider or some other method to protect the Mayfly from excessive input voltage on the analog input.

    Also, another fun trivia fact, if you simply want to print a float to the serial monitor, you don’t have to use the special function above, it’s only for adding floats to strings. To print a float to the serial monitor, just do this:

    Serial.print(voltage);

    But if you wanted to see 4 significant digits, do this:

    Serial.print(voltage,4);

    The default precision for printing a number in Arduino is only 2 significant digits. So if you want anything more than that, just put a comma followed by the number of places you want to see. It’s a handy trick to know, but not mentioned widely enough that it is common knowledge.

    • #13123

      Drew
      Participant

      Hello,

      Thank you for the quick reply and the information. This was exactly what we were looking for, and now we are getting the data that we need. Thank you for your help.

You must be logged in to reply to this topic.