I need to implement a circuit that measures the temperature of a light bulb with a 100K NTC thermistor. In python I need to register the measured temperature and the instant of time in which the registration is made, considering a sampling rate of 20 samples per second .
Then I need to do several measurement and data acquisition tests that must be during intervals of 60 seconds .
So far my python code that logs the data is as shown below. Regarding that code I should mention the following:
- Since the measurement must be done for 1 minute and the sampling rate is 20 samples per second, I considered that the amount of data to acquire in that time corresponds to 20 samples * 60 seconds = 1200 data in total, which is the length of the python code list (
numPoints = 1200
) - For the time variable developed in the Python code, I made a list and a simple for that will generate 1200 data in an interval of 50, this interval value is due to the fact that, due to the requested sampling rate, every 50 ms I acquire data, so my time goes from 0 ms to 60,000 ms, so the iterator i multiply it by 50 (
d1 = i*50
)
import sys, time, serial
import numpy as np
from datetime import datetime
time.sleep(3) # Generar un retardo 3ms
numPoints = 1200 # Cant de datos
with serial.Serial('COM8', 9600) as ser:
def getValues():
ser.write(b'g')
tiempo = np.arange(0, 60000, 50)
arduinoData = ser.readline().decode('ascii','ignore')
arduinoData_2 = arduinoData.strip(',\r\n')
return tiempo, arduinoData_2
registro = open("exp1_punto22_v3.txt", "w")
header = "Tiempo, T°"
registro.write(header + "\n")
for i in range(0, numPoints):
data = getValues()
d1 = i*50 #tiempo
d2 = data[1] #datos arduino
datos = str(d1)+ ", " + str(d2) + "\n"
registro.write(datos)
registro.close()
The arduino code for the NTC is shown below and I must mention that:
- To make the measurement according to the requested sampling, in the Arduino code I made an if condition that allows me to trigger the measurement only in 50 ms intervals, for that I generated two variables, time1 and time2, where one corresponds to the function
millis()
, and that will be subtracted from the other variable, in this way, when the subtraction is equal to 50, a measurement is triggered, and the variable of time2 takes the value of time1 at that moment, in order to be able to fulfill the condition again and continue acquiring data every 50 ms.
#define outputPin 6
#define zerocross 2
#define termistorPin A0
#define termistorNominalRes 100000
#define termistorNominalTemp 25
#define termistorBValue 4100
#define VoltageDividerResistor 100000
int outVal = 0, ntc, error;
float termistorRes = 0.0, temp, ref;
float steinhart;
unsigned long tiempo1 = 0, tiempo2 = 0;
void setup() {
Serial.begin(9600);
tiempo1 = millis();
tiempo2 = 0;
}
void loop() {
Serial.println(temp);
if (tiempo2 - tiempo1 == 50){
tiempo1 = tiempo2;
termistorRes = ((float)analogRead (termistorPin)* VoltageDividerResistor)/(1023 - (float)analogRead (termistorPin));
steinhart = termistorRes / termistorNominalRes; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= termistorBValue; // 1/B * ln(R/Ro)
steinhart += 1.0 / (termistorNominalTemp + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
temp=steinhart;
}}
Currently the code on the arduino does not output any value.
Note : Any error related to the RBDdimmer library or another, ignore it, since this piece of code is part of a larger one related to the power control of the bulb, and that was not included in this post.
The correct way to run a process at intervals in Arduino without using interrupts is:
In each
loop
you compare the current time against the initial one. When working with milliseconds you should always calculate the intervals by subtraction and compare by greater (or less) equal, as the case may be.The time is taken at the beginning of each cycle. Each execution within
if
it is at regular intervals (50 ms), regardless of whether a particular loop takes more or less time than the previous ones.It is also recommended to execute only one
millis()
per loop, to ensure that all calculations refer to the same base.