Advertisement
microrobotics

Vibration Motor 3V Haptic Feedback

Apr 18th, 2023
2,708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Here's a simple Arduino code to control a 3V vibration motor using a transistor for haptic feedback. In this example, we'll use an NPN transistor like the 2N2222 or BC547.
  3.  
  4. Components:
  5.  
  6. Arduino board (e.g., Arduino Uno)
  7. Vibration motor (3V)
  8. NPN transistor (e.g., 2N2222 or BC547)
  9. Resistor (e.g., 1kΩ)
  10. Diode (e.g., 1N4001)
  11. External power supply (3V) for the vibration motor (optional)
  12. Wiring:
  13.  
  14. Connect the Arduino's digital pin 9 to the transistor's base through the 1kΩ resistor.
  15. Connect the transistor's collector to the positive terminal of the vibration motor.
  16. Connect the transistor's emitter to the ground (GND) of the Arduino.
  17. Connect the diode across the motor terminals, with the cathode connected to the positive terminal and the anode connected to the negative terminal. This diode protects the circuit from reverse voltage spikes.
  18. Connect the negative terminal of the vibration motor to the ground (GND) of the Arduino.
  19. Optionally, if you are using an external power supply for the motor, connect the positive terminal of the power supply to the positive terminal of the motor and the negative terminal to the Arduino GND.
  20. Arduino code:
  21.  
  22. This code will turn the vibration motor ON and OFF every second, creating a simple haptic feedback pattern. You can modify the code to create different haptic feedback patterns depending on your specific requirements.
  23. */
  24.  
  25. const int motorPin = 9; // Connect the transistor's base to this pin through a 1kΩ resistor
  26.  
  27. void setup() {
  28.   pinMode(motorPin, OUTPUT);
  29. }
  30.  
  31. void loop() {
  32.   // Turn the vibration motor ON
  33.   digitalWrite(motorPin, HIGH);
  34.   delay(1000); // Keep the motor ON for 1 second
  35.  
  36.   // Turn the vibration motor OFF
  37.   digitalWrite(motorPin, LOW);
  38.   delay(1000); // Keep the motor OFF for 1 second
  39. }
  40.  
  41.  
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement