Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Here's an example of how to control a vibration motor's speed using a potentiometer with an Arduino, without using an H-bridge. In this example, we will use an NPN transistor (e.g., 2N2222) to control the motor speed.
- Wiring:
- Connect the vibration motor's positive (+) terminal to the transistor's collector (C).
- Connect the vibration motor's negative (-) terminal to the power supply ground (GND).
- Connect the transistor's emitter (E) to the power supply ground (GND).
- Connect a resistor (1kΩ recommended) between the transistor's base (B) and the Arduino's PWM pin (e.g., pin 3).
- Connect the potentiometer's middle pin to the Arduino's analog pin A0.
- Connect the potentiometer's other two pins to the power supply voltage (VCC) and ground (GND) respectively.
- Now, you can use the following code to control the vibration motor's speed using the potentiometer:
- This code reads the potentiometer value, maps it to the motor speed range (0-255), and sets the vibration motor's speed accordingly. Make sure the transistor and resistor values are appropriate for the motor's voltage and current requirements. Adjust the connections and pin numbers as necessary to match your hardware setup.
- */
- #include <Arduino.h>
- const int MOTOR_PIN = 3; // Transistor base connected to Arduino pin 3 through a 1kΩ resistor
- const int POT_PIN = A0; // Potentiometer connected to Arduino analog pin A0
- void setup() {
- pinMode(MOTOR_PIN, OUTPUT); // Set the motor pin as output
- }
- void loop() {
- int potValue = analogRead(POT_PIN); // Read the potentiometer value (0-1023)
- // Map the potentiometer value to the motor speed range (0-255)
- int motorSpeed = map(potValue, 0, 1023, 0, 255);
- // Set the vibration motor speed
- analogWrite(MOTOR_PIN, motorSpeed);
- // Optional: Add a small delay between readings
- delay(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement