I have found a temperature sensor that might fit my project. However the data come as PWM and I lack knowledge about how to measure PWM in my UNOs. The sensor is an SMT172-TO220.
Here is a link to the data sheet:
https://store.comet.bg/download-file.php?id=13478
I read that the output frequency is some 1 - 4 kHz when supplied with 5 volts. Sounds like possible to me. Is there any library available for this kind of device?
Google "arduino measure duty cycle" for examples. Top hit.
Multiple hits when I Google "SMT172 site:arduino.cc".
Thanks a lot Wawa and jremington! I found the sensor deep down on the sigth of a Swedish electronics supplier and didn't even dream that the sensor would be known like this. The library is now downloaded.
@Wawa
I managed to incorporate that library. Selected the demo but it compiles with errors.
First the code of demo, then the error report:
/*
Demo sketch for the SMT172 library. This sketch will output to serial
Connect the output of the SMT172 to pin 8 (Input Capture Pin of timer 1
Timer 2 is set up in phase correct PWM and output a duty cycle of
10.98% ~-45 C on pin 3 and a duty cycle of 92.94% ~130 C on pin 11
Connect pin 8 to pin 3 or pin 11 to check the working if no SMT172 is available
*/
#include<arduino.h>
#include <SMT172.h>
uint32_t LastSensorUpdate;
//The setup function is called once at startup of the sketch
void setup() {
Serial.begin(115200);
pinMode(8, INPUT);
Serial.println(F("Demo sketch SMT172"));
// The following code fragment sets up phase-correct PWM on pins 3 and 11 (Timer 2).
// The waveform generation mode bits WGM are set to to 001 for phase-correct PWM.
// The other bits are the same as for fast PWM.
pinMode(3, OUTPUT);
pinMode(11, OUTPUT);
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM20);
// TCCR2B = _BV(CS22); // Output frequency: 16 MHz / 64 / 255 / 2 = 490.196 Hz
TCCR2B = _BV(CS21); // Output frequency: 16 MHz / 8 / 255 / 2 = 3921.569 Hz
OCR2A = 237; // Output A duty cycle: 237 / 255 = 92.94% = 129.58 C on pin 11
OCR2B = 28; // Output B duty cycle: 28 / 255 = 10.98% = -45.06 C on pin 3
// set accuracy:
/*
while (SMT172::setError(0.002) == 2){
Serial.println(F("****** Sensor disconnected ******"));
delay(250);
}
*/
}
// The loop function is called in an endless loop
void loop() {
// read the sensor every 250 millisecond
if ((unsigned long) (millis() - LastSensorUpdate) >= 250) {
LastSensorUpdate = millis();
SMT172::startTemperature();
repeat:
switch (SMT172::getStatus()) {
case 0: goto repeat; // O Dijkstra, be merciful onto me, for I have sinned against you :)
case 1: Serial.print(F("Measuring time [ms]: "));
Serial.println(SMT172::getTime() * 1e3, 2); // convert to milliseconds
Serial.print(F("Sensor frequency [Hz]: "));
Serial.println(SMT172::getFrequency(), 2);
Serial.print(F("Duty cycle [%]: "));
Serial.println(SMT172::getDutyCycle() * 100, 2);
Serial.print(F("Temperature [C]: "));
Serial.println(SMT172::getTemperature(), 2);
Serial.print(F("Error [mK]: "));
Serial.println(SMT172::getError() * 1000, 2);
Serial.println();
break;
case 2: Serial.println(F("**** Sensor disconnected ****"));
Serial.println();
}
}
}
Error:
C:\Users\User\Documents\Arduino\SMT172\examples\SMT172_demo\SMT172_demo.ino: In function 'void loop()':
SMT172_demo:48: error: too few arguments to function 'bool SMT172::startTemperature(uint8_t)'
SMT172::startTemperature();
^
In file included from C:\Users\User\Documents\Arduino\SMT172\examples\SMT172_demo\SMT172_demo.ino:10:0:
C:\Users\User\Documents\Arduino\libraries\SMT172/SMT172.h:71:6: note: declared here
bool startTemperature(uint8_t oversampling);
^
SMT172_demo:51: error: 'getStatus' is not a member of 'SMT172'
switch (SMT172::getStatus()) {
^
SMT172_demo:54: error: 'getTime' is not a member of 'SMT172'
Serial.println(SMT172::getTime() * 1e3, 2); // convert to milliseconds
^
SMT172_demo:56: error: 'getFrequency' is not a member of 'SMT172'
Serial.println(SMT172::getFrequency(), 2);
^
SMT172_demo:58: error: 'getDutyCycle' is not a member of 'SMT172'
Serial.println(SMT172::getDutyCycle() * 100, 2);
^
SMT172_demo:62: error: 'getError' is not a member of 'SMT172'
Serial.println(SMT172::getError() * 1000, 2);
^
exit status 1
too few arguments to function 'bool SMT172::startTemperature(uint8_t)'
Thanks a lot! I cleaned up some folders, including the user/Documents/Arduino from SMT172's and tried again using Your link in #2. This bloody Win10 that dumps downloads all over the place so being in the Arduino IDE the incorporate library doesn't find them…..
Now I have a nice, stable and precise reading of the temperature using the demo sketch.
Also, thanks to jremington