Coin vibration motors or small vibration motors can be used for different purposes, such as an alert instead of a beep. A Vibration Motor Module is sold which includes the required resistors and transistors. They cost around $3-$4 per piece. 10mm and 12mm coin vibration motors are also sold as a component. If you are using the module, you can connect :
———————–
ESP32 |Vibration motor
———————–
3v3 |Vcc
———————–
Gnd |Gnd
———————–
D15 |In
———————–
Use this code :
---
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | int motorPin = D15; void setup() { pinMode(motorPin, OUTPUT ); } void loop() { digitalWrite(motorPin, HIGH); delay(1000); digitalWrite(motorPin, LOW); delay(1000); } |
If you use the “solo” vibration motor, then you need to connect in this way :
You’ll need :
BreadBoard
ESP32
Vibration Motor
Diode Rectifier – 1A 50V
Transistor – NPN BC337
1K Ohm Resistor
Jumper Wires
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | // Include Libraries #include "Arduino.h" #include "Switchable.h" #include "VibrationMotor.h" // Pin Definitions #define VIBRATIONMOTOR_PIN_COIL10 // Global variables and defines // object initialization VibrationMotor vibrationMotor(VIBRATIONMOTOR_PIN_COIL1); // define vars for testing menu const int timeout = 10000; //define timeout of 10 sec char menuOption = 0; long time 0; // Setup the essentials for your circuit to work. It runs first every time your circuit is powered with electricity. void setup() { // Setup Serial which is useful for debugging // Use the Serial Monitor to view printed messages Serial.begin(9600); while (!Serial) ; // wait for serial port to connect. Needed for native USB Serial.println("start"); menuOption = menu(); } // Main logic of your circuit. It defines the interaction between the components you selected. After setup, it runs over and over again, in an eternal loop. void loop() { if(menuOption == '1') { // Vibration Motor - Test Code // The vibration motor will turn on and off for 500ms (0.5 sec) vibrationMotor.on(); // 1. turns on delay(500); // 2. waits 500ms (0.5 sec). change the value in the brackets (500) for a longer or shorter delay in milliseconds. vibrationMotor.off(); // 3. turns off delay(500); // 4. waits 500ms (0.5 sec). change the value in the brackets (500) for a longer or shorter delay in milliseconds. } if (millis() - time0 > timeout) { menuOption = menu(); } } // Menu function for selecting the components to be tested // Follow serial monitor for instrcutions char menu() { Serial.println(F("\nWhich component would you like to test?")); Serial.println(F("(1) Vibration Motor")); Serial.println(F("(menu) send anything else or press on board reset button\n")); while (!Serial.available()); // Read data from serial monitor if received while (Serial.available()) { char c = Serial.read(); if (isAlphaNumeric(c)) { if(c == '1') Serial.println(F("Now Testing Vibration Motor")); else { Serial.println(F("illegal input!")); return 0; } time0 = millis(); return c; } } } |
In the above example, you need :
VibrationMotor.cpp
VibrationMotor.h
Switchable.h
Switchable.cpp
VibrationMotor.cpp :
1 2 3 4 5 6 7 | #include <Arduino.h> #include "VibrationMotor.h" VibrationMotor::VibrationMotor(const int pin) : Switchable(pin) { } |
VibrationMotor.h :
1 2 3 4 5 6 7 8 9 10 11 12 | #ifndef _VIBRATION_MOTOR_H_ #define _VIBRATION_MOTOR_H_ #include "Switchable.h" class VibrationMotor : public Switchable { public: VibrationMotor(const int pin); }; #endif // _VIBRATION_MOTOR_H_ |
Switchable.h :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #ifndef _SWITCHABLE_H_ #define _SWITCHABLE_H_ //Base class for output that can be switched on/off via a single digital pin: class Switchable { public: // Consturcutor accepts pin number for output Switchable(const int pin); // Turn pin on void on(); // Turn pin off void off(); // Toggle pin void toggle(); // dim pin void dim(int dimVal); // Get current state bool getState(); // Set state with bool variable void setState(bool state); private: const int m_pin; //output pin bool m_state;//current state }; #endif // _SWITCHABLE_H_ |
Switchable.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include "Switchable.h" #include <Arduino.h> Switchable::Switchable(const int pin) : m_pin(pin) { // Set pin as output pinMode(m_pin, OUTPUT); // Start state if off off(); } //turn on: void Switchable::on() { digitalWrite(m_pin, 1); //high m_state = true; } //turn off: void Switchable::off() { digitalWrite(m_pin, 0); //low m_state = false; } void Switchable::toggle() { digitalWrite(m_pin, !m_state); //low m_state = !m_state; } // dim pin void Switchable::dim(int dimVal) { analogWrite(m_pin, dimVal); } bool Switchable::getState() { return m_state; } void Switchable::setState(bool state) { digitalWrite(m_pin, state); m_state = state; } |
I got the thing from here :
1 | https://www.circuito.io/app?components=513,8449,360217 |
and kept it here on GitHub.