ON/OFF Timer switch for small DC motor

Hello!

I'm a newbie to Arduino and electronics in general and I would really appreciate your help. I have found many guides on how to make a timer with Arduino but none that came close enough to what I need.

I'm currently working on a DIY fog machine project (based on this tutorial) involving an e-cigarette and a 3V DC aquarium air pump.

So far, so good. I have successfully created the mini fog machine and I even managed to correctly wire it to a push button and a DC adaptor, such that the pump would turn on as long as I pressed the button. Now I would like to replace the button with a timer switch so that the pump is on for exactly 3 minutes once I upload the code into the Arduino.

However, I am lost as to how I should wire and code such a thing. Any advice on this?

Thanks!

vs

Since you are trying to "control a dc motor using arduino", that's what you should look for using your favourite search engine :slight_smile:

My guess is you are going to need an npn-transistor, a resistor around 10-100 Ohm and about 5 lines of code.

lg, couka

Thank you couka!
I already have a 2N2222 Transistor and a 330 Ohm resistor. Do those save me a trip from the shop?

If the pump doesn't pull too much current it could work with those components, it's worth a try. Keep an eye on the transistor, it shouldn't get too hot to touch. If it does, you have to get a higher-current transistor.

lg, couka

Hello again all!

I’ve been researching how to do the coding and wiring and I’m stuck. Here’s a fritzing diagram of what I have so far (attached). The Arduino is powered via USB, the power socket is 5V, 2.25A, the transistor is 2N2222A and the resistor is 10KOhms as I figured the amperage might be too high for the 330ohm one I had.

The code I’m using is as following:

void setup() {
  pinMode(12,OUTPUT);     //Channel A Direction Pin Initialize
}

void loop() {
  
  analogWrite(3, 0);      //Channel A Speed 0%
  delay(100);             //100ms Safety Delay
  digitalWrite(12, HIGH); //Channel A Direction Forward
  analogWrite(3, 255);    //Channel A Speed 100%
  delay(180000);            //3 minute Delay
  analogWrite(3, 0);      //Channel A Speed 0%

  exit(0)
  
  
}

This setup is not working (code compiles but motor doesn’t turn on) and I’m not experienced enough to figure what’s wrong.
Any advice is greatly appreciated! Thanks!

vs

Did you connect the arduino GND and motor power GND?

Also, not sure what's going on in your code. You cannot set the direction of the motor with that setup. I guess you used some code for an h-bridge module.

lg, couka

Before you go any further and damage something, you need a flyback diode across the motor.

But the reason it's not working is probably the lack of common between the transistor / battery ground and the Arduino ground.

ardy_guy:
Before you go any further and damage something, you need a flyback diode across the motor.

+1, sorry for not mentioning that.

lg, couka

Thanks couka and ardy!

The only wire connected to my Arduino is the 12pin you see in the diagram, connected to the resistor which is in turn connected to the transistor Base. I just added a wire connecting the Arduino Ground pin and the transistor and power grounds. Tested it with the same code as in the previous post, got nothing more than a slight noise.

Motor ground is connected to the transistor Emitter. Transistor Collector is connected to the ground.

I don't have a flyback diode at hand. Could I use an LED instead considering the relatively low voltages I'm working with? And how would I wire the diode "across the motor"?

The code is adapted from the Basic Control example. Original example below for reference:

void setup() {
  pinMode(12,OUTPUT);     //Channel A Direction Pin Initialize
}

void loop() {
  
  analogWrite(3, 0);      //Channel A Speed 0%
  delay(100);             //100ms Safety Delay
  digitalWrite(12, HIGH); //Channel A Direction Forward
  analogWrite(3, 255);    //Channel A Speed 100%
  delay(1000);            //1 Second Delay
  
  analogWrite(3, 0);      //Channel A Speed 0%
  delay(100);             //100ms Safety Delay
  digitalWrite(12, LOW);  //Channel A Direction Reverse
  analogWrite(3, 255);    //Channel A Speed 100%
  delay(1000);
  
}

Thank you for being so helpful!

vodka_sailor:
Motor ground is connected to the transistor Emitter. Transistor Collector is connected to the ground.

The other way around, if I understand your description correctly, could you provide a clear sharp image of your setup?

A LED probably won't survive very long.

lg, couka

Setup: fritzing file and .PNG of setup attached. I switched the transistor E and C around as per your advice.

Right, I’ll go buy the diode. Could anyone draw a line showing where it should go, please?

Thanks

vs

edit: fritzing file didn’t upload for some reason; hopefully the screenshot is clear enough.

Diode goes literally across the motor contacts, but unintuitively backwards, with the cathode (silver stripe) on the positive. Have a look at this page at Sparkfun, near the bottom.

Hello again all.

I think I’m almost there. There’s just one thing that I can’t figure out:
If you look at the diagram attached, you’ll see I added a diode. Ardy recommended a flyback diode but I did not find any of that specific kind - instead, I got a bag of mixed diodes and hoped I’d find something similar enough.

When I tried to use the setup attached with a rectifier diode, the motor didn’t turn on and I sensed a burning smell from the transistor, which became extremely hot.
However, when I tried the exact same setup except for replacing the diode with a LED, it all worked. Granted, I didn’t keep it on for more than a few seconds.

I figured I should stop and seek advice before I do any damage. The code seems to work; I just need to confirm what I did wrong with the electronics. I will get a higher current transistor ASAP, but what about the diode? Is it fine as it is? If it needs to be changed, can I replace the flyback diode with anything from this mixed bag (datasheet attached) ?

Code for reference:

int motorPin = 9;
void setup () {
   
 pinMode (motorPin, OUTPUT ); 
}
void loop () {
 digitalWrite (motorPin, HIGH); 
 delay(180000); 
 digitalWrite (motorPin,LOW);
 delay(180000);
}

Thanks!
vs

10k base resistor too high.