What can I do to change the turning direction of a DC motor automatically?

It may be the most stupid question you have seen,as I am a rookie.(already fall on your knees :fearful:)

If I got a DC motor like in the video

What can I do to let the DC motor changes its turning direction automatically after a few seconds so to form a loop?
And what stuffs do I need?

(fall on your knees again.)

You need what's called an H-bridge, and there are a number of chips and chips on boards that do that.

The oldest and best known are the L293 and L298, Google those, and there are loads of motor driver chips of differing voltage and current specs here at Pololu.

edit.... also look here in the Playground.

This method won’t work with the motor in the video, but if you got a motor that can run on the arduino I/O pins, this little bit of code i wrote will work:

(you need to create variables for pin3 and pin4 and then use pinMode(pin3, OUTPUT); pinMode(pin4, OUTPUT); in the setup)

boolean flip = false;

void step() {
      if(flip) {
      digitalWrite(pin3, HIGH);
      digitalWrite(pin4, LOW);
    } else {
      digitalWrite(pin3, LOW);
      digitalWrite(pin4, HIGH);
  flip = !flip;

I copied this from a massive program i wrote so it might not work, but i could fix it if you told me the error.

all you need to do is call the ‘step();’ method in your loop after a delay and it will switch directions.
Your motor MUST be able to handle the voltage and amperage an arduino provides, but most DC motors can to my knowledge.

if you got a motor that can run on the arduino I/O pins, this little bit of code i wrote will work:
Your motor MUST be able to handle the voltage and amperage an arduino provides, but most DC motors can to my knowledge.

On the contrary.... very few motors will consume as little as 40mA which is max an i/o pin can provide, 20-30 rather for safety.

You really don't want to do it this way, it's asking to pop something.

Use an h-bridge,.

Never, EVER connect any inductive load like a standard motor directly to an Arduino pin, not even if it only takes as little as 5 mA.
It will create back EMF, which is a very effective way of destroying that port.

Anybody advising to do this without even hinting to protection measures for this effect, should be very ashamed and at least apologize to anyone who has followed this straight wrong advise, or is considering to do so.

JimboZA's answer, to use a H-bridge, is one way to do this.
Doing it that way will also enable you to control the speed of the motor but you don't need to do that.
If you have no plans to do that, you could also use a pair of relays.
The first one, a DPDT type, would control the direction.
The second one (could also be a DPDT type) would control power.
First set the direction, then switch power.
If the motor is running, stop it first, switch direction and then power it again.

Be aware that a relay also is an inductive load, and that they are likely to draw more current than can be handled by an Arduino pin.
That means you should use transistors, some resistors and diodes to get this done safely.
These parts are however more likely to be available than a H bridge.

Personally i would use the H bridge, i've got some available at home.
But this shows there's more than a single solution to do this.
Just to be more complete, the mentioned L293 and L298 aren't the most efficient ones available, and aren't very suitable for low power motors combined with low power supplies.
Read less than 9 volts as low power.

Thanks a lot and I have never thought there are so much passionate reply! (Oh my tear!)
And I think H-bridge will work!

Then,when I check about H-bridge,I foound there are some on my motor shield.
Like on this page:Using DC Motors | Adafruit Motor Shield | Adafruit Learning System
There is a photo on the top of the page,
should I just connect the wire of the motor to get the function o change the turning direction of a DC motor?
Or,is there more stuff should I do?

You haven't mentioned you had a motorshield.
Is it the one shown on that Adafruit site ?
Connecting the wire will not do anything.
You need to control the motor, and that motorshield, stacked on your Arduino is your hardware to do that.
You need to use your Arduino and program it in such way that the motorshield knows what to do.
So read that Adafruit page again.
It tells you what to do.
And it shows the code, ready to be copied.
Use that and see what happens.