Using TemperUSB sensor to trigger motor

I have a Temper2USB device that can plug into the USB port of my Raspberry Pi. I am looking to write a code that when the temperature sensor detects a change of 1C, it turns on the motor for a period of time. I am having issues with writing a code that detects the sensor I want (the Temper2 has an internal and external sensor of which I only care about the external one) but also turn on the motor every time a temperature change of 1C has occurred. Any help would be greatly appreciated!

1 Like

Hi Alex
Some info here

1ºC is pretty tight. Is that what you need?

If this sensor is a thermocouple type the internal temp has to be known to calculate the cold junction compensation. I have a thermocouple interface board which reports both int and ext temps. If they are both the same at switch on it usually means the reported ext temp is going to be fairly accurate, for a while at least.

If you are only interested in a CHANGE of 1ºC and not the actual temperature the absolute accuracy is not that important.

Going to be a pretty busy little motor I think.
Cheers Bob

1 Like

Hello Robert,

Thanks for the reply! Yes I am mainly interested in the change of 1ºC (it would be nice to have the program record the temperatures it activates the motor though that’s not entirely necessary). Once the 1ºC temperature change occurs, I want to get the motor to run for 10 seconds and then to stop.

I’ll admit I’m not much of a programming wiz so I’m definitely swimming in water that’s too deep for me here.

1 Like

Hi Alex
The RPI gurus (which I am not) might need a bit more info than that. For instance is this temp change up or down or both.
Like I said a very busy motor.
Cheers Bob

1 Like

Hi Robert
The temp change would be either up or down but it should be occurring only in one direction at a time (I will be heating or cooling the sensor). Probably the biggest issue I have been having is for the raspberry pi to recognize the inputs from the sensor. If I open up a text file it will show the readings but that seems to be the only way I get readings from it.

1 Like

You should start by defining the task in terms that are programmable. For instance, does " a temperature change of 1C" mean a current temperature that is 1 degree Celsius or more different than some pre-determined number, or does it mean a temperature difference of 1 degree Celsius or more from the temperature as it was at some previous time? If it’s a change from some previous time, what is that time interval? If you need to turn on a motor then it seems likely that you only want to detect a temperature increase or decrease, not both. Is that so, or do you need to detect both an increase and a decrease, and if you are detecting both an increase and a decrease do you need to do the same action in both cases, or are the actions different?

You should show the code you are currently using and describe exactly what the issue with that code is. A syntax error during compilation is going to be quite different than a logic problem in the code. Also, if the problem is actually in detecting the sensor then you need to describe the hardware setup you have and what procedure you are using to do the detecting.

1 Like

Hi Jeff,

Sorry I didn’t do a very good job of explaining what I am doing. I was hoping to use the same code for two experiments, one in which I will be increasing the temperature and another where I will decrease the temperature. In either case the action will be the same. The code should take a temperature at time 0 and then at every step degree change from that time (either up or down) run the motor for X seconds.

The temperature sensor is plugged into the USB port and I can get it to work when a text window is open but putting into my code to only read the external temp given (it gives an internal temp of the usb dongle itself and an external from the probe connected to it) is what’s tripping me up

1 Like
import hid
import time

# Open the TEMPer2 device
device = hid.device()
device.open(0x0c45, 0x7401)

# Read the initial temperature
device.write([0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00])
result = device.read(8)
temp = result[2] + (result[3] / 256.0)
prev_temp = temp

# Continuously read the temperature and check for changes
while True:
    # Read the current temperature
    device.write([0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00])
    result = device.read(8)
    temp = result[2] + (result[3] / 256.0)
    
    # Check if the temperature has changed by 1 degree Celsius
    if abs(temp - prev_temp) >= 1.0:
        prev_temp = temp
        
        # Turn on the motor for 5 seconds
        # Code to turn on the motor goes here
        time.sleep(5)
        
        # Code to turn off the motor goes here
        
    # Wait for 1 second before reading the temperature again
    time.sleep(1) 
1 Like

Do you have a link to where you got this code from?

I think that the code is designed to detect an increase in temperature of at least 1 degree Celsius since the last time a change was detected. If there has been a change then run the motor for five seconds and set the current temperature as the new base temperature. That is, you are trying to control the rate of temperature increase, not the actual increase.

You haven’t indicated what the problem with that code is, and I guess that’s because you do not have any information about what it is doing. You should include some output to the display to show what temperature reading you are getting. Then you will be able to see whether or not it is changing. Currently, the code won’t do anything, even if the termperature does change, so you won’t know whether it works or not.

1 Like

Hi Alex,

Sweet project!

It sounds like you want to set up a P controller (Proportional, with a 1-degree deadband)

I’ll make some psuedo-code changes around your logic statements to roughly do what you are after (this isnt code)

# Calculate the difference
temperature_delta = temp - prev_temp

# Statically define the control constant, this effects how long the motor will run for
K = 1

if abs(temperature_delta ) >= 1.0:
    prev_temp = temp
    
    # Calculate control value
    u = K * temperature_delta 

    # Turn on the motor
    

    # Code to turn on the motor goes here
    time.sleep(u)
    
    # Code to turn off the motor goes here

Let us know if this is something that you are after, you could also add logic to reverse the motor if the value is negative!
Liam

1 Like