Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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.
- Components:
- Arduino board (e.g., Arduino Uno)
- Vibration motor (3V)
- NPN transistor (e.g., 2N2222 or BC547)
- Resistor (e.g., 1kΩ)
- Diode (e.g., 1N4001)
- External power supply (3V) for the vibration motor (optional)
- Wiring:
- Connect the Arduino's digital pin 9 to the transistor's base through the 1kΩ resistor.
- Connect the transistor's collector to the positive terminal of the vibration motor.
- Connect the transistor's emitter to the ground (GND) of the Arduino.
- 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.
- Connect the negative terminal of the vibration motor to the ground (GND) of the Arduino.
- 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.
- Arduino code:
- 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.
- */
- const int motorPin = 9; // Connect the transistor's base to this pin through a 1kΩ resistor
- void setup() {
- pinMode(motorPin, OUTPUT);
- }
- void loop() {
- // Turn the vibration motor ON
- digitalWrite(motorPin, HIGH);
- delay(1000); // Keep the motor ON for 1 second
- // Turn the vibration motor OFF
- digitalWrite(motorPin, LOW);
- delay(1000); // Keep the motor OFF for 1 second
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement