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:
the circuit is this one:
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:
(* 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);
But in the monitoring something is not working correctly:
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;