0

I have this code that is working perfectly but I want my device to start vibrating too (right now only the led is working)

On a Wemos Battery Module I have simply connected the vibration motor on the same PIN of the LED and it works perfectly.

Now I want to do the same on an Beetle ESP32 Microcontroller DFR0575. I used the same code But I can't make the module vibrating (The led light is working perfectly)

this is the code:

 #include <BLEAdvertisedDevice.h>
   #include <BLEDevice.h>
   #include <BLEScan.h>

   const int PIN = 2;
   const int CUTOFF = -50;


   void setup() {
     pinMode(PIN, OUTPUT);
     BLEDevice::init("tkr");
   }

   void loop() {
     BLEScan *scan = BLEDevice::getScan();
     scan->setActiveScan(true);
     BLEScanResults results = scan->start(1);
     int best = CUTOFF;
     for (int i = 0; i < results.getCount(); i++) {
       BLEAdvertisedDevice device = results.getDevice(i);
       int rssi = device.getRSSI();
       if (rssi > best) {
         best = rssi;
       }
     }
     digitalWrite(PIN, best > CUTOFF ? HIGH : LOW);
   }
  • Are you connecting the vibration motor directly to a digital output pin? Or are you using some kind of driver (for example a transistor)? – chrisl Sep 30 at 10:12
  • In Wemos Battery Module I connect the vibration in the digital output pin. But the same things does not work on the Beetle ESP32 Microcontroller DFR0575 I don't know why.. – Alex x Sep 30 at 10:14
  • What type of Battery Module do you have? Can you provide a link? Googling, I found different battery shields for the Wemos. Please also say, to which pin you connected the vibration motor exactly on both boards. – chrisl Sep 30 at 11:14
  • Wemos Battery Module (amazon.com/gp/product/B07M9X7HS6/…) connected on PIN 16 Working ---- Beetle ESP32 Microcontroller DFR0575 connected on PIN 2 NOT WORKING – Alex x Sep 30 at 11:54
  • why are you asking this question again? .... edit your original question with additional information instead of reposting ... arduino.stackexchange.com/questions/68964/… – jsotola Sep 30 at 14:31
0

It is generally very dangerous to drive any motor directly from a digital output pin. Motors often need way more current, than the digital pin can provide. Based on the answer to this question, an ESP8266 can provide 20mA or 12mA depending on current direction (may be a bit different for an ESP32, but not substantially). Drawing more current, than the pin can provide, can lead to the destruction of the output hardware of the pin. It is not easy to say, when this will happen, because it depends not only on the current draw, but also on time and fabrication variation.

Also a motor is an inductive load, which means, that fast changes (for example turning on and off) will induce a reverse voltage in the motor coils. These can easily be very high relative to the supply voltage and thus destroy you output pin or even the complete microcontroller.

I cannot give you a full reasoning, why it works with the first board, but not with the second. I think, it has to do with the above.

You should always do 2 things, when driving motors with a microcontroller:

  1. Use a motor driver circuit: You can either buy a ready to use motor driver or you can build one yourself. If you only want to turn the motor in one direction (as with a vibration motor), all you need is a MOSFET, which will act as a switch between the motor and the power supply. This switch get's controlled by the Arduino's output pin (which only needs to supply a very low current for switching). You can google "MOSFET as a switch". Be sure to use a MOSFET, that can provide the needed current and that is good in saturation for the used gate voltage (the working voltage of the microcontroller).

  2. Use a flyback diode: The reverse induction voltage can also hurt MOSFETs. You want to let these voltage spikes quickly plenish by letting the current flow to ground. You can do this with a flyback diode. That is a diode, that bridges drain and source of the MOSFET. It is oriented so that block for normal operation (the normal positive supply voltage). When the MOSFET get's turned off, a great negative voltage will build up in the motor, but since the diode is conducting in this way, the voltage will safely flow to ground, so that the induction voltage cannot hurt the other electronic parts.

  • Thank you very much i'll try. the only problem I have is that I found a lot of Mosfet on amazon but i don't know what to buy. – Alex x Sep 30 at 12:33
  • I need something very small... – Alex x Sep 30 at 12:48
  • You already have to categories, where the MOSFET has to fit in: You search a MOSFET which can conduct some hundred mA (depending on your motor, you may just measure the real value) without a headsink (which means the maximum current has to be clearly above this value). Also it's threshold voltage should be clearly less than 3.3V (which is the voltage of ESP boards). And it should be small, so a SMD part is the best fit. Just use one with these conditions. – chrisl Oct 1 at 20:29

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.