Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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.
- Components:
- Arduino board (e.g., Arduino Uno)
- Vibration motor with a shaft (3V-5V)
- NPN transistor (e.g., 2N2222 or BC547)
- Resistor (e.g., 1kΩ)
- Diode (e.g., 1N4001)
- Power supply (for the vibration motor, if required)
- I2C LCD display (16x2)
- 10kΩ potentiometer (optional, for adjusting the tumbling duration)
- 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.
- Wiring for the I2C LCD display:
- Connect the SDA pin to the Arduino's A4 pin.
- Connect the SCL pin to the Arduino's A5 pin.
- Connect the VCC pin to the Arduino's 5V pin.
- Connect the GND pin to the Arduino's GND pin.
- Wiring for the potentiometer (optional):
- Connect one of the outer pins to the Arduino's 5V pin.
- Connect the other outer pin to the Arduino's GND pin.
- Connect the middle pin (wiper) to the Arduino's A0 pin.
- Arduino code:
- 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.
- */
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- const int motorPin = 9;
- const int potPin = A0;
- // Set the I2C address of the LCD display (usually 0x27 or 0x3F)
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- unsigned long startTime = 0;
- unsigned long elapsedTime = 0;
- unsigned long tumblingDuration = 0;
- bool motorState = false;
- void setup() {
- pinMode(motorPin, OUTPUT);
- pinMode(potPin, INPUT);
- lcd.init();
- lcd.backlight();
- lcd.setCursor(0, 0);
- lcd.print("Tumbler Control");
- // Set the initial tumbling duration (5 minutes)
- tumblingDuration = 5 * 60 * 1000;
- }
- void loop() {
- elapsedTime = millis() - startTime;
- // Turn the motor ON and OFF based on the tumbling duration and rest period (1 minute)
- if (elapsedTime >= tumblingDuration) {
- motorState = false;
- digitalWrite(motorPin, LOW);
- if (elapsedTime >= tumblingDuration + 1 * 60 * 1000) {
- startTime = millis();
- }
- } else {
- motorState = true;
- digitalWrite(motorPin, HIGH);
- }
- // Read the potentiometer value (optional) to adjust the tumbling duration
- int potValue = analogRead(potPin);
- tumblingDuration = map(potValue, 0, 1023, 1 * 60 * 1000, 60 * 60 * 1000); // Map potentiometer value to 1 minute - 1 hour
- // Update the LCD display with the remaining time
- unsigned long remainingTime = (tumblingDuration - elapsedTime) / 1000;
- int remainingMinutes = remainingTime / 60;
- int remainingSeconds = remainingTime % 60;
- lcd.setCursor(0, 1);
- lcd.print("Time: ");
- lcd.print(remainingMinutes);
- lcd.print(":");
- if (remainingSeconds < 10) {
- lcd.print("0");
- }
- lcd.print(remainingSeconds);
- if (motorState) {
- lcd.print(" ON ");
- } else {
- lcd.print(" OFF");
- }
- // Wait for 500 milliseconds before updating the display and motor state
- delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement