12 years, 10 months ago.

I need help with converting Arduino code to work with mbed.

Hello all. I am attempting to convert my old arduino temperature probe(datasheet: http://www.atlas-scientific.com/_files/ENV-TEMP.pdf code to work with the mbed. However, with the new mbed code I keep getting negative numbers, for example, -1070304047.

Probe to MBED is as followed:

Quote:

  • Probe -> MBED
  • adc -> p20
  • GND -> GND
  • VCC -> VU (I also tried VOUT)

I am not sure if the vcc is correct or if my math is correct. Why do I receive negative numbers and why is printf("ResTemp: %s\n", theNumberString); not printing. How do I get this to work?

Arduino Code:

float read_temp(void){
   float v_out;   
   float temp;
   digitalWrite(A0, LOW);  
   digitalWrite(2, HIGH);
   delay(2);   
   v_out = analogRead(0);  
   digitalWrite(2, LOW);
   v_out*=.0048; 
   v_out*=1000;              
   temp= 0.0512 * v_out -20.5128;

   return   temp;
}

MBED code:

float tempSensor::getCurrentTempValue()
{
    float v_out;
    float temp;
    
    printf("Temp Probe Warming Up!\n");

    wait(2);
    printf("2 seconds has passed!\n");
    v_out = _resTemp.read();
    v_out*=.0048;
    v_out*=1000;
    temp= 0.0512 * v_out -20.5128;
    
    printf("ResTemp: %d\n", temp);
    
    ostringstream ostr; //output string stream
    ostr << temp; //use the string stream just like cout,
    //except the stream prints not to stdout but to a string.

    string theNumberString = ostr.str(); //the str() function of the stream 
    //returns the string.
    
    printf("ResTemp: %s\n", theNumberString);
    _msExt.sendCommand("resTemp: ", theNumberString);
    
    return temp;
}

2 Answers

12 years, 10 months ago.

Without trying it myself, I am pretty sure you are getting a warning on that code that you are doing something with your printf that you shouldnt be doing: you are printing a float with the '%d' command. Printing floats is done with %f, so replacing that should fix it.

And afaik %s is only meant to be used with an array of chars, not with actual strings, so that doesn't work. Generally you really don't want to be using strings anyway, strings and their function consume enormous amounts of RAM memory, so better stick to char arrays.

Hi Erik. I took your advise and switched my strings to char arrays.

posted by d 0773d 11 Jan 2013
12 years, 10 months ago.

According to the datasheet the temperature in degrees celsius is:

temp = 0.0512 * v_out - 20.5128

With v_out the sensor outputvalue in mV.

The mbed AnalogIn will give you a value between 0 and 1.0 for input voltages between 0V and 3V3. That means you have to multiply the AnalogIn value by 3300 to get the voltage in mV.

Something like this should work:

float tempSensor::getCurrentTempValue()
{
    float v_out;
    float temp;
    
//dont need this delay when the probe is permanently powered
    printf("Temp Probe Warming Up!\n");
 
    wait(2);
    printf("2 seconds has passed!\n");

    v_out = _resTemp.read();
//    v_out*=.0048;
//    v_out*=1000;
//    temp= 0.0512 * v_out -20.5128;

    temp = 0.0512 * (v_out * 3300) - 20.5128;
    
    printf("ResTemp: %f\n", temp);  // use %f for float instead of %d 
    
//    ostringstream ostr; //output string stream
//    ostr << temp; //use the string stream just like cout,
    //except the stream prints not to stdout but to a string.
 
//    string theNumberString = ostr.str(); //the str() function of the stream 
    //returns the string.
    
//    printf("ResTemp: %s\n", theNumberString);
//    _msExt.sendCommand("resTemp: ", theNumberString);
    
    return temp;
}

Note that you may want to measure several times and average to cancel noise.

You still need to fix the string part. Recommend you use char arrays as suggested by Erik. The sprintf will convert floats to a char string.

Hi Wim. I changed my code to your example. Code seems to work well. I also added %.2f to the printf to trim the leading numerical values. I also set the remaining analogin pins to digital to possibly help reduce noise. Also, how many times would you suggest that I measure to help cancel noise?

posted by d 0773d 11 Jan 2013

There has been a lot of discussion on this site regarding noise on the ADC. Noise could come from external sources depending on your environment. The noise is also due to internal sources like the ethernet PHY and the fact that we have only one common GND pin for digital and analog on mbed (pin 1). Averaging 4-5 values should help. You may also want to use a small capacitor (eg 10nF) between the analog inputpin and GND. Place as close as possible to the pin. Averaging and filtering slows down the responsetime but that should not be a problem for your temperature measurement application.

posted by Wim Huiskamp 11 Jan 2013