Using Analog Read() and Converting to Temp in Mbed
nine years, 1 calendar month 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 lawmaking to work with the mbed. Notwithstanding, with the new mbed code I continue 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 non 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); non printing. How do I get this to work?
Arduino Code:
bladder read_temp(void){ float v_out; float temp; digitalWrite(A0, Low); digitalWrite(2, Loftier); filibuster(2); v_out = analogRead(0); digitalWrite(2, Low); v_out*=.0048; v_out*=1000; temp= 0.0512 * v_out -xx.5128; return temp; }
MBED code:
bladder tempSensor::getCurrentTempValue() { bladder v_out; float temp; printf("Temp Probe Warming Upwards!\due north"); wait(ii); printf("2 seconds has passed!\n"); v_out = _resTemp.read(); v_out*=.0048; v_out*=k; temp= 0.0512 * v_out -20.5128; printf("ResTemp: %d\north", temp); ostringstream ostr; //output string stream ostr << temp; //employ the string stream just similar cout, //except the stream prints not to stdout but to a cord. cord theNumberString = ostr.str(); //the str() function of the stream //returns the string. printf("ResTemp: %southward\northward", theNumberString); _msExt.sendCommand("resTemp: ", theNumberString); return temp; }
ii Answers
9 years, 1 month ago.
Without trying it myself, I am pretty sure you are getting a warning on that code that y'all are doing something with your printf that y'all shouldnt be doing: you are printing a float with the '%d' command. Press floats is done with %f, and then replacing that should prepare it.
And afaik %s is only meant to be used with an array of chars, non with actual strings, then that doesn't work. Generally y'all 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.
nine years, one calendar month 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 volition give yous a value between 0 and 1.0 for input voltages between 0V and 3V3. That ways you have to multiply the AnalogIn value by 3300 to become the voltage in mV.
Something like this should work:
bladder tempSensor::getCurrentTempValue() { float v_out; bladder temp; //dont demand this filibuster when the probe is permanently powered printf("Temp Probe Warming Up!\n"); await(ii); printf("2 seconds has passed!\n"); v_out = _resTemp.read(); // v_out*=.0048; // v_out*=k; // temp= 0.0512 * v_out -xx.5128; temp = 0.0512 * (v_out * 3300) - 20.5128; printf("ResTemp: %f\n", temp); // use %f for float instead of %d // ostringstream ostr; //output cord stream // ostr << temp; //utilize the cord stream just like cout, //except the stream prints not to stdout simply to a string. // string theNumberString = ostr.str(); //the str() office of the stream //returns the cord. // printf("ResTemp: %south\due north", theNumberString); // _msExt.sendCommand("resTemp: ", theNumberString); return temp; }
Note that you may desire to measure out several times and boilerplate to cancel racket.
Y'all notwithstanding demand to set up the string part. Recommend you use char arrays every bit suggested by Erik. The sprintf will convert floats to a char string.
Source: https://os.mbed.com/questions/245/I-need-help-with-converting-Arduino-code/
Postar um comentário for "Using Analog Read() and Converting to Temp in Mbed"