Python Forum

Full Version: sensor code that starts heater
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
i want to start the heater with the sensor, when the temperature is 0 dеgrees the heater start when the temperature becomes +3 degree heater stop.is it possible?
Can you tell me whats the sensor to buy and how i switch it in raspberry.I have relays 10A the heater is 1000w.
Thank you
Hi, check this out:
https://github.com/szazo/DHT11_Python
As you can see in the example code, the "signal" pin is connected to pin 14 of raspberry pi.
The DHT11 has 3 pins though, the GND pin should be connected to GND, and VCC can be either connected to 3.3V or 5V (according to ebay description of the module).

The sensor:
https://www.ebay.co.uk/itm/DHT11-Tempera...2527049987
(Jun-04-2019, 02:36 PM)michalmonday Wrote: [ -> ]Hi, check this out:
https://github.com/szazo/DHT11_Python
As you can see in the example code, the "signal" pin is connected to pin 14 of raspberry pi.
The DHT11 has 3 pins though, the GND pin should be connected to GND, and VCC can be either connected to 3.3V or 5V (according to ebay description of the module).

The sensor:
https://www.ebay.co.uk/itm/DHT11-Tempera...2527049987


I did not understand anything,where i put the dagrees?
So the DHT11 is the sensor you could use. That's it:

[Image: 2MShDqG.jpg]

It has 3 metal pins. VCC, signal and GND. VCC and GND pins are used solely for powering the sensor. Signal pin is used to actually receive information about temperature from it.

Here's a pinout of the raspberry pi:

[Image: raspberry-pi-15b.jpg]

So the 3 metal pins from the sensor should be connected to the appropriate metal pins on the raspberry pi.
VCC of the sensor -> any of the 3.3V or 5V pins of Raspberry Pi
GND of the sensor -> any of the GND pins of RPi
Signal pin of the sensor -> GPIO 14 of RPi

After connecting it this way you can get the temperature from the sensor by manipulating pin 14 of the Raspberry Pi which is done by the library I mentioned. In this example code it is shown how it can be done:
import RPi.GPIO as GPIO
import dht11
import time
import datetime

# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()

# read data using pin 14
instance = dht11.DHT11(pin=14)

while True:
    result = instance.read()
    if result.is_valid():
        print("Last valid input: " + str(datetime.datetime.now()))
        print("Temperature: %d C" % result.temperature)
        print("Humidity: %d %%" % result.humidity)

    time.sleep(1)

Notice that it imports dht11, so you need to download and put the dht11.py in the same folder as your main python file will be or put it in the python/Lib/site-packages/ folder.

Here's example of what your code could look like (I didn't test it though because I don't have that sensor).

import RPi.GPIO as GPIO
import dht11
import time

# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()

# read data using pin 14
instance = dht11.DHT11(pin=14)

# pin 4 will be used to control the relay
relay_pin = 4
GPIO.setup(relay_pin, GPIO.OUT)

ON = 1
OFF = 0

def switch_relay(state):
    '''You may want to invert state here if your relay is normally high
    when its' input pin is low.'''
    GPIO.output(relay_pin, state)

while True:
    result = instance.read()
    if result.is_valid():
        if result.temperature <= 0: 
            switch_relay(ON)
        elif result.temperature >= 3:
            switch_relay(OFF)
            
    time.sleep(1)