DC Motor help (two wires)

Hi guys
some I’m taking apart an old device, and trying to hook it up to the Particle Photon.
(thought why not hack it up to get to terms with the IOT eh).

The motor that is already in the device is a DC motor, with a Red Cable and Black Cable.
Just wondering if any tutorials out there to connect and control where its wired using just the two wires?
PS, its just a standard motor that opens and closes a flap

Thanks in advance
Dave

@dspalton, do a search for “motor control” in the Community and you’ll see lots of topics popping up! The key thing to know is that you cannot drive the motor directly from a pin on the Photon since it can’t provide enough current. :smile:

hi mate
ill have a look thank you.
got a PhoBot board here too so hoping that helps.
Got it working with a stepper motor and the tutorial, but that motor has 4 wires lol

1 Like

You can drive it with a standard H-Bridge. This way you can use two control lines to drive the motor in both directions.

There are a zillion of these on eBay.

I’m guessing i can’t use the phobot for it?

Not sure what a phobot is.

dave

@dspalton, looks like you probably can…

cool.
just need to find a tutorial now haha

very new here…

@dspalton, you may have better luck finding one on Arduino and just apply it to your photon.

actually i have used the code from the stepper tutorial
just connected to M4 on the board

just need to work how to control online now haha

ok so this is the code in using

#include "PhoBot/PhoBot.h"

PhoBot p = PhoBot(12.0, 12.0);

void setup() {
}

void loop() {
    forward(500, 2);
    reverse(100, 2);
}

void forward(int numSteps, int period) {
    for (int i = 0; i < numSteps; i++) {
        p.setMotors("M3-F-100");
        p.setMotors("M4-B-100");
        delay(period);
        p.setMotors("M3-F-100");
        p.setMotors("M4-F-100");
        delay(period);
        p.setMotors("M3-B-50");
        p.setMotors("M4-F-50");
        delay(period);
        p.setMotors("M3-B-50");
        p.setMotors("M4-B-50");
        delay(period);
    }
}

void reverse(int numSteps, int period) {
    for (int i = 0; i < numSteps; i++) {
        p.setMotors("M3-B-100");
        p.setMotors("M4-B-100");
        delay(period);
        p.setMotors("M3-B-100");
        p.setMotors("M4-F-100");
        delay(period);
        p.setMotors("M3-F-50");
        p.setMotors("M4-F-50");
        delay(period);
        p.setMotors("M3-F-50");
        p.setMotors("M4-B-50");
        delay(period);
    }
}

I need to hack it up to Just run off M4, not M3, and to be able to reverse as well.
Im not really good enough to be able to modify it myself.
But having seen it actually working, moving the motor, really happy

Dave

I hadnt seen that phobot board before. it uses a TB6612FNG dual H-Bridge motor controller. it will drive a single stepper or 2 DC motors. I have built some motor controllers with the same chip before, its pretty indestructible!

there are some examples here, https://github.com/simonmonk/PhoBot although not very well documented!

your best bet will be to study the schematic of the phobot and have a read of the adafruit tutorials for their boards with the same chip ( https://www.adafruit.com/products/2448 and https://www.adafruit.com/products/1438 ) Once you have an understanding of how the motors are driven you can write your own code

Been trying to read up over the last few days but for the love of me can’t figure out how to get the motor to reverse using the code above on just M4

Dave

I just noticed in the loop section above you have 500, 2 and the code is for stepper not dc motors having said that it should still give you an output in both directions

Try changing to 5, 2000 and watch the motor should do 4sec in each direction

Let me know how it goes

the code is showing M3 which isn’t connected at all.
i think thats where the problem is?

Dave

Try this, it should run the motor fwd, for 2 sec, then back for 2 sec. then stop for 2 sec

#include "PhoBot/PhoBot.h"

PhoBot p = PhoBot(9.0, 6.0);
      
 void setup() {
 }
      
      
void loop() {
p.setMotors("M4-F-100");
delay(2000);
p.setMotors("M4-B-100");
delay(2000);
p.setMotors("M4-S");
delay(2000):
}

getting the error

motorbig.cpp:14:12: error: expected ‘;’ before ‘:’ token
p.setMotors(“M4-F-100”);
^

figured it i think

sorry my quick typing and not checking properly.

Sometimes the place where the error is pointing gets messed up

I guess you found it, at the end of the last delay line.