ESP32 Relay operation. Help needed please

Good evening, I am using a NodeMCU-32S and running the code below. I cant seem to get the Realy to operate when the corred BLEAddress is found. The Serial Print output certainly indicates when the correct BLEAddress is forun, but the Relay does not toggle. Coudl someone help please

Here is the code;

#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <BLEAddress.h>
#include <BLEDevice.h>
#include <Ticker.h>
 
String Adresse = "A8:42:E3:57:98:5E"; // Bluetooth MAC to watch for
const int RelayPin = 22;             // pin of relay to swiwtch
int Delay = 15;                      // delay after which the relay will be switched when BLE device is out of range
 
int DelayCounter = 0;
Ticker Tic;
static BLEAddress *pServerAddress;
BLEScan* pBLEScan ;
int scanTime = 30; //In seconds
 
 
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
{
    void onResult(BLEAdvertisedDevice advertisedDevice) // when BLE device is found
    {
      Serial.print(advertisedDevice.getAddress().toString().c_str()); // show MAC of BLE
      if (advertisedDevice.getAddress().equals(*pServerAddress))      // compare MAC
      {
        Serial.print(" Monitored Address");                           // monitored MAC found
        digitalWrite (RelayPin, 0);                                  // switch relay
       DelayCounter = 0;                                            // reset delay
        advertisedDevice.getScan()->stop();                           // stop scanning
      }                                                              // Found our server
      Serial.println("");
    }
};
 
void SekundenTic()  // once per second
{
DelayCounter++;  // time counter
if (DelayCounter >= Delay) digitalWrite (RelayPin, 1); // switch off after delay
}
 
void setup() 
{
  pinMode (RelayPin, OUTPUT);
  digitalWrite (RelayPin, 1);
  Serial.begin(115200);
  Serial.println("");
  Serial.println("Starte BLE Scanner");
  pServerAddress = new BLEAddress(Adresse.c_str());
  BLEDevice::init("");
  pBLEScan = BLEDevice::getScan(); // create new scan
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true); // active scan uses more power, but get results faster
  Tic.attach( 1,SekundenTic);
}
 
void loop()
{
  pBLEScan->start(scanTime);
  delay(1000);      // scan every 1s for devices
}

Thank you Peter

Serial Print Output;

16:53:43.081 -> a8:42:e3:57:98:5e Monitored Address
16:53:44.105 -> 76:16:2e:7b:da:12
16:53:44.105 -> a8:42:e3:57:98:5e Monitored Address
16:53:45.128 -> 52:cc:83:f2:0a:c1
16:53:45.128 -> a8:42:e3:57:98:5e Monitored Address
16:53:46.149 -> 6e:ed:da:3d:c7:24
16:53:46.149 -> a8:42:e3:57:98:5e Monitored Address
16:53:47.179 -> a8:42:e3:57:98:5e Monitored Address
16:53:48.250 -> 67:e2:0f:dc:fc:95
16:53:48.250 -> 0d:b3:d5:4e:8e:96
16:53:48.250 -> a8:42:e3:57:98:5e Monitored Address
16:53:49.270 -> 0d:b3:d5:4e:8e:96
16:53:49.270 -> a8:42:e3:57:98:5e Monitored Address
16:53:50.293 -> 6e:ed:da:3d:c7:24
16:53:50.293 -> 77:e8:34:c5:a6:49
16:53:50.293 -> 11:4e:11:16:14:cb
16:53:50.340 -> a8:42:e3:57:98:5e Monitored Address
16:53:51.367 -> 11:4e:11:16:14:cb
16:53:51.367 -> a8:42:e3:57:98:5e Monitored Address
16:53:52.389 -> 52:cc:83:f2:0a:c1
16:53:52.389 -> 76:16:2e:7b:da:12
16:53:52.389 -> 46:c6:c6:5b:39:00
16:53:52.389 -> a8:42:e3:57:98:5e Monitored Address
16:53:53.416 -> a8:42:e3:57:98:5e Monitored Address
16:53:54.439 -> 6e:ed:da:3d:c7:24
16:53:54.439 -> a8:42:e3:57:98:5e Monitored Address
16:53:55.461 -> a8:42:e3:57:98:5e Monitored Address
16:53:56.478 -> 67:e2:0f:dc:fc:95

Please post schematics.

Attach an led and series resistor to the pin instead of the relay. Does it light up and go out as expected?

1 Like

the ESP32 uses 3.3V logic - make sure you have a relay which accepts 3.3V logic signals
many small relays used with Arduinos use 5V logic
post a link to your relay or a photo

https://www.haljia.com/products/haljia-6pcs-1-channel-relay-board-relay-module-with-optocoupler-isolation-support-high-and-low-level-trigger-universal-high-profermance-1-channel-relay-expansion-board-compatible-with-arduino-5v

looks like a 5V relay will probably have problems attempting to switch it with 3.3V logic
if you power it from 5V and connect the IN to 3.3V does it switch?

3.3v operates the relay

did you try the LED test suggested by @PaulRB in post 4?

Not yet, the LEDs are in the Garage and its 5 below freezing at the moment Brrrrrr

  • The 1 channel relay board schematic below will probably be nearly identical to yours.

  • To properly control this module, you should add a driver transistor between the ESP and the relay module.

  • Do not suggest you use the same power supply on both the relay module and the ESP.

  • Alternatively you can add a jumper from the ESP GND to the center pin of the HIGH/LOW jumper block.

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