ESP32, Relay not working

Hello, I have the following problem with the ESP32 board, I try to activate a relay from Bluetooth, but the relay does not activate, I made a measurement to verify that voltage arrived through PIN 12, and the signal it generates is 3.3v, the relay is of 3v, and even so, with this signal, the relay does not work.

What could be happening?

Code:

//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif

BluetoothSerial SerialBT;

int pinOut = 12;
int maleta = 23;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
  pinMode(pinOut, OUTPUT);
  digitalWrite (pinOut, LOW);
  delay (20);
  
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }

  
  if (SerialBT.available())
  {

   
    char data = ((byte) SerialBT.read());
    switch (data)
    {
      case 'P':
        digitalWrite (pinOut, HIGH);
        //Hacemos un delay de 1 segundo
        delay (1000);
        digitalWrite (pinOut, LOW);
        break;

      case 'b':
        digitalWrite(maleta, HIGH);
        delay(800);
        digitalWrite(maleta, LOW);
        break;
      default : break;
    }
    Serial.println(data);
  }
  delay(20);
}

Diagram

The relay needs 120mA and the ESP32 can give 12mA. The relay is also a inductive load which can cause damage to other electronics.

I understand, then how could I activate a relay from the ESP32? thnks

With a transistor or a mosfet to drive the relay. You also need a flyback diode to protect the electronics.
There are relay modules with everything on the module, but they are often for 5V, to be used with a Arduino Uno.

Where do you get the 120mA 3.3V from ?
I don't know if the ESP32 board will be happy of you use its 3V3 pin to pull 120mA out of it.

What is your project ?
It might be possible to drive the load with a single mosfet, or maybe a driver module could be a solution. I hope you are not trying to use that relay to switch the mains voltage.

An opto-isolated relay.

Here's a Relay Board with the required buffer electronics built-in.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.