I'd like to trigger my relay to turn on my lamp when the humidity level measured is greater than 85. Here's the code I've been using.

import Adafruit_DHT
import RPi.GPIO as GPIO
import time

sensor = Adafruit_DHT.DHT11

gpio.setup(26, gpio.OUT)

print("Lendo os valores de temperatura e umidade");

   umid, temp = Adafruit_DHT.read_retry(11, 4);

   if umid is not None and temp is not None:
     print ("Temperatura = {0:0.1f}  Umidade = {1:0.1f}n").format(temp, umid);
     print ("Aguarda 2 segundos para efetuar nova leitura...n");

     if umid >= 86:

       gpio.output(26, 1)
       gpio.output(26, 0)


     print("Falha ao ler dados do DHT11 !!!")

I'm getting smalls pulses but not enough to trigger the relay. What should I do? Thanks in advance.

PS: all the pins are correctly connected.

  • Could you explain what you mean by "I'm getting smalls pulses but not enough to trigger the relay."
    – joan
    Nov 28 '18 at 12:47
  • The GPIO pins probably will be unable to supply enough current for the relay coil. The coil can also damage the Pi when the stored field energy is released back through the circuit... Please update your question with the precise electrical circuit used to activate the relay as well as the relay specifications. Nov 28 '18 at 13:52
  • What is the part number of the relay/relay module? If there is no diode built in then you will have major issues.
    – justinjt
    Feb 27 '20 at 8:17

Are you able to read temperature and humidity?

Try this: Adafruit_DHT.read_retry(sensor, 4)

When you connect LED diode instead of relay, will it light?

Do you have 3V or 5V relay?

  • Please don't ask questions in an answer.
    – Ingo
    Feb 26 '20 at 20:56

You need a transistor between the raspberry pi and the relay. Use a reverse bias diode accross the relay coil. Try connecting the relay to 5v. If it's not too beefy the current consumption and voltage dropout should be such that the pi doesn't shutdown and reboot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.