Hello!
I am sure someone has maybe posted about this before, I am new and am just looking for a little help.
I would like to use an Arduino to run a DC Motor for a set duration, then turn off for a set duration and keep this as a loop.
Example would be power on motor for 45 seconds, turn motor off for 5 mins, turn motor on for 45 seconds, then motor off for 5 mins. Keep doing this as a loop.
Could someone please help and/or point me in the right dirction?
Thank you,
Chris
Your real design decisions are mostly going to be around your microcontroller - DC motor connection. An arduino generally cannot push out enough current to run a DC motor, so you're going to need some form of control circuit. Bill has a series of videos about different options there... here's a sampling:
- https://forum.dronebotworkshop.com/2017/controlling-dc-motors-with-the-l298n-h-bridge-and-arduino/
- https://forum.dronebotworkshop.com/2019/control-large-gearmotors-with-pwm-arduino
- You can also use devices that bill shows here, https://forum.dronebotworkshop.com/2019/mosfets-and-transistors-with-arduino-2, depending on your needs.
- In some scenarios, even the options discussed here https://forum.dronebotworkshop.com/2020/control-ac-devices-with-arduino-safely-relays-solid-state-switches are appropriate.
I am not going to offer an opinion between these options without knowing more about the current involvements/motor sizes that we are talking about.
The code is actually relatively simple. What you have described here is a simple two-state system with alternating timers. The output of the system is a pin on the microcontroller, HIGH for "run the motor", LOW for "stop the motor".
const int MOTOR_PIN = 11; // <-- This will need to be changed to your chosen control pin
const long long int MOTOR_ON_TIME_IN_MS = 45 * 1000;
const long long int MOTOR_OFF_TIME_IN_MS = 5 * 60 * 1000;
long long int nextChange; // time, in milliseconds, where the next change on/off will occur
bool motorOn; // track if the motor is currently on or off.
void setup() {
pinMode(MOTOR_PIN, OUTPUT);
motorOn = true;
nextChange = millis() + MOTOR_ON_TIME_IN_MS;
digitalWrite(MOTOR_PIN, HIGH);
}
void loop() {
if (millis() > nextChange) {
if (motorOn) {
motorOn = false;
nextChange = millis() + MOTOR_OFF_TIME_IN_MS;
digitalWrite(MOTOR_PIN, LOW);
} else {
motorOn = true;
nextChange = millis() + MOTOR_ON_TIME_IN_MS;
digitalWrite(MOTOR_PIN, HIGH);
}
}
delay(100); // optional... but idle a bit here (1/10th of a second) before restarting the loop
}
This code _can_ be shortened up significantly. But I hope it is clear what each step is doing. I would suggest running this code on an arduino, but instead of hooking up a motor to pin 11, connect an LED(+resistor), or set MOTOR_PIN to pin 13 and then the built-in LED on an arduino will allow you to see the code in action, even with no other devices connected.
"A resistor makes a lightbulb and a capacitor makes an explosion when connected wrong"
"There are two types of electrical engineers, those intentionally making antennas and those accidentally doing so."
@jker thank you very much for the quick reply and the willingness to help!
I am looking to power a small DC Motor, it is this one from Amazon. I am using this as a vibration source to help some material settle as it burns (I am building a cold smoker for BBQ reasons). Thus the reason I need the motor to run for small durations intermittently.
https://www.amazon.com/gp/product/B07X6M8KQ4/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1
The code does make sense that you have shown, thank you for providing. I can test with an LED and try it out.