B4R Question calculation different values B4R / Arduino

Michael1968

Active Member
Licensed User
Hi all,
I am working on a project to measure/calculate temperature from a NTC resistor using MCP3208.
the arduino code works but i have a problem to write it in B4R.

I get different values.
Arduino Code:
float Rmess = 47;
float a, b, c, Rn;

Rn = 1000;
a = 0.003358;
b = 0.0002242;
c = 0.00000261;

  // start sampling
  uint16_t raw = adc.read(MCP3208::Channel::SINGLE_0); --> 3915 (3154mv)


  float Rt = Rmess * ((4096.0 / (4096 - raw)) - 1); -->1016.60
  float v = log(Rt / Rn); -->0.02
  float erg = (1 / (a + b * v + c * v * v)) - 273.15; -> 24.32

...and this is the B4R Code
B4X:
Dim Rmess,V,erg As Float
    Dim RT As Float
    Dim a,b,c,rn As Float
   
    rn = 1000
    a = 0.003358
    b = 0.0002242
    c = 0.00000261
   
    Rmess=47
   
    RT = Rmess * ((4096.0 / (4096 - 3915)) - 1)  '-->1016.6022
   
    v= RT/rn
    v = Logarithm(V,10) '-->0.0072

    erg = (1 / (a + b * v + c * v * v)) - 273.15 '--> 24.54

any idea?

i have also testet to use the Arduino Code inline but i get an Error at the point
float v = log(Rt / Rn);
is called

thx for help
 

thetahsk

Active Member
Licensed User

Try this and dont't mix log and log10 function.

B4X:
Dim Rmess,V,erg As Float
    Dim RT As Float
    Dim a,b,c,rn As Float
    Dim Math_Euler As Float=2.7182818284590452354
    rn = 1000
    a = 0.003358
    b = 0.0002242
    c = 0.00000261
       Rmess=47
       RT = Rmess * ((4096.0 / (4096 - 3915)) - 1)
    Log("RT=",RT)
    v= RT/rn
    v = Logarithm(v,Math_Euler)    
    Log("v=",v)
    erg = (1 / (a + b * v + c * v * v)) - 273.15
    Log("erg=",erg)
 
Upvote 0

Similar Threads

Top