Advertisement
microrobotics

Vibration Motor Shaft Rotary Tumbler

Apr 18th, 2023 (edited)
1,116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Here's an Arduino code for a simple rotary tumbler using a vibration motor with a shaft. This code turns the motor ON and OFF with an adjustable tumbling duration and rest period. It also displays the remaining time on an I2C LCD.
  3.  
  4. Components:
  5.  
  6. Arduino board (e.g., Arduino Uno)
  7. Vibration motor with a shaft (3V-5V)
  8. NPN transistor (e.g., 2N2222 or BC547)
  9. Resistor (e.g., 1kΩ)
  10. Diode (e.g., 1N4001)
  11. Power supply (for the vibration motor, if required)
  12. I2C LCD display (16x2)
  13. 10kΩ potentiometer (optional, for adjusting the tumbling duration)
  14.  
  15. Connect the Arduino's digital pin 9 to the transistor's base through the 1kΩ resistor.
  16. Connect the transistor's collector to the positive terminal of the vibration motor.
  17. Connect the transistor's emitter to the ground (GND) of the Arduino.
  18. 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.
  19. Connect the negative terminal of the vibration motor to the ground (GND) of the Arduino.
  20. 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.
  21.  
  22. Wiring for the I2C LCD display:
  23.  
  24. Connect the SDA pin to the Arduino's A4 pin.
  25. Connect the SCL pin to the Arduino's A5 pin.
  26. Connect the VCC pin to the Arduino's 5V pin.
  27. Connect the GND pin to the Arduino's GND pin.
  28. Wiring for the potentiometer (optional):
  29.  
  30. Connect one of the outer pins to the Arduino's 5V pin.
  31. Connect the other outer pin to the Arduino's GND pin.
  32. Connect the middle pin (wiper) to the Arduino's A0 pin.
  33. Arduino code:
  34.  
  35. With this code, the rotary tumbler will turn ON and OFF based on the tumbling duration and rest period. The tumbling duration can be adjusted using a potentiometer. The remaining time and motor state are displayed on the I2C LCD. If you don't want to use a potentiometer, you can set a fixed tumbling duration in the setup() function.
  36. */
  37.  
  38. #include <Wire.h>
  39. #include <LiquidCrystal_I2C.h>
  40.  
  41. const int motorPin = 9;
  42. const int potPin = A0;
  43.  
  44. // Set the I2C address of the LCD display (usually 0x27 or 0x3F)
  45. LiquidCrystal_I2C lcd(0x27, 16, 2);
  46.  
  47. unsigned long startTime = 0;
  48. unsigned long elapsedTime = 0;
  49. unsigned long tumblingDuration = 0;
  50. bool motorState = false;
  51.  
  52. void setup() {
  53.   pinMode(motorPin, OUTPUT);
  54.   pinMode(potPin, INPUT);
  55.  
  56.   lcd.init();
  57.   lcd.backlight();
  58.   lcd.setCursor(0, 0);
  59.   lcd.print("Tumbler Control");
  60.  
  61.   // Set the initial tumbling duration (5 minutes)
  62.   tumblingDuration = 5 * 60 * 1000;
  63. }
  64.  
  65. void loop() {
  66.   elapsedTime = millis() - startTime;
  67.  
  68.   // Turn the motor ON and OFF based on the tumbling duration and rest period (1 minute)
  69.   if (elapsedTime >= tumblingDuration) {
  70.     motorState = false;
  71.     digitalWrite(motorPin, LOW);
  72.  
  73.     if (elapsedTime >= tumblingDuration + 1 * 60 * 1000) {
  74.       startTime = millis();
  75.     }
  76.   } else {
  77.     motorState = true;
  78.     digitalWrite(motorPin, HIGH);
  79.   }
  80.  
  81.   // Read the potentiometer value (optional) to adjust the tumbling duration
  82.   int potValue = analogRead(potPin);
  83.   tumblingDuration = map(potValue, 0, 1023, 1 * 60 * 1000, 60 * 60 * 1000); // Map potentiometer value to 1 minute - 1 hour
  84.  
  85.   // Update the LCD display with the remaining time
  86.   unsigned long remainingTime = (tumblingDuration - elapsedTime) / 1000;
  87.   int remainingMinutes = remainingTime / 60;
  88.   int remainingSeconds = remainingTime % 60;
  89.  
  90.   lcd.setCursor(0, 1);
  91.   lcd.print("Time: ");
  92.   lcd.print(remainingMinutes);
  93.   lcd.print(":");
  94.   if (remainingSeconds < 10) {
  95.     lcd.print("0");
  96.   }
  97.   lcd.print(remainingSeconds);
  98.  
  99.   if (motorState) {
  100.     lcd.print(" ON ");
  101.   } else {
  102.     lcd.print(" OFF");
  103.   }
  104.  
  105.   // Wait for 500 milliseconds before updating the display and motor state
  106.   delay(500);
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement