How do i add an ln(x) eqation?

I need to code y = -29.14ln(x) + 203.83 to measure the resistence of an ntc and then convert it to celcius. Y is temperature and x is resistens from ntc. But i have no idea how arduino works and its for a school project. Can somebody help me?
Ntc (between 30 C and 40 C ) = 315 Ohm
Resistor = 320 Ohm

Sorry


for my englisch

1 Like

Hi, @wirbrauchenbass
Welcome to the forum.

Have you Googled;

arduino natural log

Tom.. :smiley: :+1: :coffee: :australia:

Checking Arduino Reference - Arduino Reference I don¨t see any function for the natural logarithm.

Let's try Oncle Google.
Voila!
Arduino natural logarithm – Yahoo Sökresultat

thanks for the help, i asked some classmates and now i understand it !

You want to evaluate:
y = - 29.14 x ln(x) + 203.83

Analysis:
1. Assume x = 320.
2. ln(x) is called Natural Logarithm which means "at what power (say: z) e(2.718) is to be raised" so that ez = 320.

3. If we use Scientific Calculator, we find that z = 5.77.
4. As a result:

 y = -29.14 x 5.77 + 203.83
===> y = 35.69

5. Let us see how we can do it using the Microcontroller of the Arduino UNO Board.
we have:
===> y = -29.14 x ln(320) + 203.83
===> y = -29.14 x loge320 + 203.83
===> y = -29.14 x (log10320 / log10e) + 203.83 //e = 2.718
6. Arduino Code:

float y = -29.14 * (log10(320)/log10(2.718)) + 203.83

Here, *float* is the "data type", and it is used to 
declare a variable which is expected to store a 
floating point number (the number having integer part and fractional part).

7. Arduino UNO Sketch

void setup()
{
  Serial.begin(9600);
  float y = -29.14 * (log10(320)/log10(2.718)) + 203.83;
  Serial.println(y, 2); //shows: 35.72;  2-digits after decimal point
}

void loop(){}

An alternative to actually coding the equation in the sketch is to pre-calculate the temperature for all possible values from analogRead() and use that as a lookup table in the code. Much faster than doing the actual math in the code, and if there is no other need for floating point numbers the code can end up being much smaller.

I think, the task is given to the school children to test that they remember the transformation of log-base (logab = log10b / log10a).

1 Like

hi @GolamMostafa,
the project is to make an Temperature-based exercise meter, which would have to be run on arduino, would this be sutible for that, i do understand the math and all but right know this is the code i have wrote (the notes are in dutch tho..)

#include <math.h>

// Pinnummers
// GND -> weerstand
// 5V -> ntc
const int sensorPin = A0;
const int buzzerPin = 9;

// Variabelen
float previousTemperature = 0;
unsigned long previousTime = 0;
bool buzzerActivated = false;

void setup() {
  // Seriële communicatie initialiseren
  Serial.begin(9600);

  // Buzzer pin als uitgang instellen
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  // Lees de waarde van de ntc
  int sensorValue = analogRead(sensorPin);

  // Converteer de ntc-waarde naar een waarde tussen 0 en 1
  float x = map(sensorValue, 0, 1023, 0, 1);

  // Bereken temperatuur tegenover de weerstand van de ntc: y = -29.14ln(x) + 203.83 , deze formule hebben we uit excel gehaald. Met een meting van R^2 = 0.9965
  float temperature = -29.14 * log(x) + 203.83;

  // Controleer of de temperatuur binnen 1 minuut van 34 naar 38 stijgt
  unsigned long currentTime = millis();
  if (temperature > 34 && temperature < 38) {
    if (!buzzerActivated) {
      // Start de timer
      previousTime = currentTime;
      previousTemperature = temperature;
      buzzerActivated = true;
    } else {
      // Controleer of de vereiste tijd is verstreken (dus 1 minuut)
      unsigned long elapsedTime = currentTime - previousTime;
      if (elapsedTime >= 60000) {
        // Vereiste tijd is verstreken, activeer de buzzer (als de temperatuur te hard is gestegen binnen een minuut)
        digitalWrite(buzzerPin, HIGH);
      }
    }
  } else {
    // Reset de buzzer en de timer, want de temperatuur is niet te hard gestegen binnen de minuut (herhaal de meting) 
    digitalWrite(buzzerPin, LOW);
    buzzerActivated = false;
  }

  // Stuur de temperatuurwaarde naar de seriële monitor, zodat de temperatuur is aftelezen op het scherm
  Serial.print("Temperatuur: ");
  Serial.println(temperature);
}
  // Blink Without delay, want anders blijft hij niet constant door meten maar stopt het zo'n 1000 milli's met meten en dit kan een verschil uitmaken

i would like to add that a buzzer needs to make a noise when the themperture that the ntc reads rises to fast in the time span of one minute. so how would i incorperate the equation( float y = -29.14 * (log10(320)/log10(2.718)) + 203.83 ) into the code i have already got? and is there anything in the code that isn't correct?

its for biofysica which is a part of my physics lessons

oke i sort of tested the code and i'm pretty sure there is al lot wrong with it, but i cant really tell what . i'll try to incorperate the formula you provided

the thing is that the code is going to have to print the temperture that occures with different resistens, i measured the resistence over the NTC which is 320 Ohm at 36 degrees celcius, and that was our aiming point. so i added a resistence in paralel net to it (of 320 Ohm). Because the arduino UNO can take an absolute maximum of 40mA, so we have to make sure we're under that. so that means that i have to confert the resistence the NTC measures and make it degrees celcius. thats why i think i cant use the formula you profided, since it is set to have the same outcome every time, and i need to measure the 'body temperture' so it becomes an exercise meter. i'm going to look if i can change it a little bit and then see if it has a normal outcome.

What is the threshold temperature which when exceeded the buzzer will be activated?

Hi, @wirbrauchenbass

Have you Googled;

arduino ntc thermistor code

You will find many tutorials on how to use your NTC.

Thanks.. Tom.. :smiley: :+1: :coffee: :coffee: :coffee::coffee: :australia:

You may wish to check the documentation for this function.

hi thom,
i have, but in that case i got no further then the steinhart-hart equation. Which in this case is not really what i need. also because i have no idee how to get the coëficcients that match with my ntc.

how i dont really understand arduino or coding, could you please explain?

From the reference
" Returns

The mapped value. Data type: long."

the buzzer will be activated when the temperture rises to fast, so for examle within one minute it the skin temperture rises from 34 to a temperture which lays 3 degrees higher. why not to 37 or 38? because every person has a diverend skin temperture.
part of the project is : Minimum demonstrate working principle of measuring the
temperature change per unit of time.

sorry that i might sound stupid but i still dont understand

how would i add a buzzer into here? (the photo)

"long is an integer datatype.