qwerty
Hi everyone,
Sorry if this is a common topic, but I'm relatively new to this and I'm having some trouble setting up a simple lab. My goal is to use a Raspberry Pi as a Master and an Arduino as a Slave to read temperature data.
Currently, I have a thermistor (B=3950, R=10k, T1=25) connected to pin A0 on the Arduino to read the ADC values. However, I haven't been able to get the communication working correctly. Any advice would be appreciated!
The thermistor is:
[203128%2F9636200%2Fmceclip0]
the circuit is this one:
[203128%2F9636202%2Fmceclip0]
Following the guide to set up the Arduino as a slave, I have uploaded a blank project containing two dummy variables. Below is the code I am using for the OpenPLC project on my Raspberry Pi:
  • The Function Block with math:
[203128%2F9636204%2Fmceclip0]
(* 1. Tensione (assumendo risoluzione 10-bit come Arduino: 1024) *)
v := (INT_TO_REAL(adcVal) * 5.0) / 1024.0;

(* 2. Calcolo Resistenza Rt *)
IF v < 4.99 THEN
    Rt := (R_NOM * v) / (5.0 - v);
ELSE
    Rt := 999.0; (* Valore di errore o fuori scala *)
END_IF;

(* 3. Equazione di Steinhart-Hart (Semplificata) *)
tempK := 1.0 / ( (LN(Rt / R_NOM) / B_PARAM) + (1.0 / T_NOM) 😉;

(* 5. Conversione in Celsius *)
tempC := tempK - 273.15;

tempC := tempC * 100.0;

Out_temp := REAL_TO_INT (tempC);

  • The main program:
[203128%2F9636205%2Fmceclip0]
But in the monitoring something is not working correctly:
[203128%2F9636207%2Fmceclip0]
 
 
 
As a reference for the math code i've used this arduino code (that works):
// Convert analog value of A0 port into digital value 
int adcVal = analogRead(A0);
// Calculate voltage
float v = adcVal * 5.0 / 1024;
// Calculate resistance value of thermistor
float Rt = 10 * v / (5 - v);
// Calculate temperature (Kelvin)
float tempK = 1 / (log(Rt / 10) / 3950 + 1 / (273.15 + 25));
// Calculate temperature (Celsius)
float tempC = tempK - 273.15;
 
Reply 0 0
thiagoralves
I didn’t check your math, but I suspect the issue is that you’re using INT instead of UINT, which makes large reading values appear as negative.  
Reply 0 0
qwerty
I've changed the A0 variable to UINT and the first thing i think is fixed. Now i dont see any changes in the final temperature that stay at the same value. I've also tried to change that uint and stay at 0. Is it possible to have a little help on the math part?[203128%2F9636381%2Fmceclip0]
Reply 0 0
qwerty
(* 1. Voltage Conversion *)
v := (UINT_TO_REAL(adcVal) * 5.0) / 65535.0;

(* 2. Thermistor Resistance (Rt) Calculation *)
IF v < 4.99 THEN
    Rt := (R_NOM * v) / (5.0 - v);
ELSE
    Rt := 10000.0;
END_IF;

(* 3. Steinhart-Hart Equation (Beta Model) *)
(* Formula: 1/T = 1/T_NOM + ln(Rt/R_NOM) / B_PARAM *)
tempK := 1.0 / ( (1.0 / T_NOM) + (LN(Rt / R_NOM) / B_PARAM) );

(* 4. Convert Kelvin to Celsius *)
tempC := tempK - 273.15;

(* 5. Output to Tc with 2 decimal places fixed-point *)
Out_temp := REAL_TO_UINT(tempC * 100.0);
 
I'm finally able to read the correct temperature. I leave here the correct function block if someone need it.
 
 
Reply 1 0
Reply