9

How to spend more time writing docs then code?
15m = doc
1m = code

<code>
/* Motor DC + Mosfet IRF520 + Servo
Materials:
IRF520 (Mosfet) + Dc motor + Batery (3V min)
diode (1N4007?)
Changed Tip122 for IRF520: Tip122 has less resistance and produces more heat

Hardware:
ARDUINO = A(port , 5V , GND)
MOSFET = M(A[port] , + , GND)
MOTOR = MOTOR(+ , -)
BATERIA = BAT(+ , -)
DIODO = D(sinal_1 , sinal_2)

MOSFET
M(port) = A(port)
M(+) = D(sinal_1) AND MOTOR(+)
M(GND) = A(GND)

D(sinal_2) = B(+) AND MOTOR(-)

ARDUINO
A(port) = M(port)
5V = null
A(GND) = M(GND) AND BAT(-)

*/

#define motorPIN 8 // A(port)
int loopy = 0; // Loop variable to limit de program

void setup()
{
// Initialize the motor pin as an output:
pinMode(motorPIN, OUTPUT);

}

void loop() // 1 loop == 100ms
{
if (loopy < 10) // Turns motor ON for 1 second
{
digitalWrite(motorPIN, HIGH);
}
else // Turns motor OFF
{
digitalWrite(motorPIN, LOW);
}
}

</code>

Comments
  • 1
    Ups.... And I broke the code.

    CARE TO PLAY A GAME?

    What's missing?
  • 0
    how does loopy counts?
  • 0
    @stecchino

    exactly

    loopy is a increment.

    forgot loopy++;

    still the code doesn't work exactly as I want, fuck
  • 0
    jep, thats's what i meant. What else isnt working exactly ?@GyroGearloose
  • 0
    Well, I had to correct a lot of the code (first was written without trying), I'm a real noob in Arduino and been debugging for one hour just to understand that the problem wasn't the code, but how Arduino handles the loop function (The function that is always running every 100 ms)

    I'll post the correct code next for comparation
  • 0
    /* Motor DC + Mosfet IRF520 + Servo

    Materials:

    IRF520 (Mosfet) + Dc motor + Batery (3V min)

    diode (1N4007?)

    Changed Tip122 for IRF520: Tip122 has less resistance and produces more heat

    Hardware:

    ARDUINO = A(port , 5V , GND)

    MOSFET = M(A[port] , + , GND)

    MOTOR = MOTOR(+ , -)

    BATERIA = BAT(+ , -)

    DIODO = D(sinal_1 , sinal_2)

    MOSFET

    M(port) = A(port)

    M(+) = D(sinal_1) AND MOTOR(+)

    M(GND) = A(GND)

    D(sinal_2) = B(+) AND MOTOR(-)

    ARDUINO

    A(port) = M(port)

    5V = null

    A(GND) = M(GND) AND BAT(-)

    */
  • 0
    #define motorPIN 8 // A(port)

    int loopy = 0; // Loop variable to limit de program without delay(); wich would stop the code

    int loopFINAL = 5; // How many seconds the motor spins

    #define INterval 10 // the interval multiplyer in 100mS to 1 Second, Adjust for a 1 second delay

    // 10 = 1 sec, 1 = 100ms

    void setup()

    {

    // Serial.begin(9600); // Open Serial Port for debug

    pinMode(motorPIN, OUTPUT); // Initialize the motor pin as an output:

    digitalWrite(motorPIN, LOW); // Starts motor off

    }

    void loop() // 1 loop =~ 1 ms

    {

    // Serial.println(loopy); // Debug

    delay(100); // keep the output voltage constant so Arduino doesn't feel buggy, loop runs every 100ms insted of 1ms

    if (loopy == 0) // Turns motor ON

    {

    digitalWrite(motorPIN, HIGH);

    }

    if (loopy == loopFINAL*INterval) // Turns motor OFF

    {

    digitalWrite(motorPIN, LOW);

    }

    loopy++;

    }
  • 0
    Took me 2 hours to make what would take 2 minutes for a pro to do...

    Leaned a looooot in this two hours the hard way

    1. Programs will be very buggy because Arduino takes 1 ms to run the loop function, plus the time to run the code. a delay(100); at the start of the loop refines code, voltage and signal inputs and outputs... to 100ms for action

    2. Many times the hardware and wiring is right, check the code

    3. The rest of the times I connected something wrong or a wire got loose
  • 1
    Don't use a 1N4007 as a freewheeling diode! It's build for mains rectification and too slow for that purpose. You should a schottky instead, as your voltage seems to be low enough.
  • 0
    @7400 Please explain??

    Yep, I noticed that there was some problem, and after I wired all the hardware it doesn't work... (I may have fucked the diode)

    Anyhow, I prefer to use ULN2003A to control up to 7 motors, but I'm still learning and wanted to used a IRF or tip :p

    Well, Going on vacation tomorow, Guess I'll continue there.

    Already have 3 cases with arduino stuff packed... Beach by day, soldering by night
  • 2
  • 1
    I actually don't understand anything about diodes... I used the 1N4007 because it's what I have for now (I have like 40 packages camming from China, one is 140 diodes, other 140 resistors or something like that)

    I'm just checking tutorials, mixing what I know and learning a little more, do a little more...

    Started from 0 maby 2 weeks ago?

    Already made a thermometer, checking chips, steppers and all the basic stuff. Especially because when I know I won't be using Arduino Unos on my projects... At maximum, I'll use a nano or micro, probably even an Arduino chip and crystal.
  • 6
    Personally I'd go with a picture of a schematic (preferably SVG), or a Gerber file for a PCB layout. And perhaps document it a bit in the README.md as well.
    Edit: needless to say, I'm assuming that sharing the sauce is done through Git.

    And yeah as @7400 mentioned, a 1N4007 is often used for low frequency rectification - the FOOOLLL BRIDGE RECTIFYA!!! It could also be used for DC reverse polarity protection, but the forward voltage drop (generally 0.7V) could be an issue. This diode is pretty much a standard component these days though, I've got a bunch of them as well and personally I'd probably use them too.

    So I'm not sure if a 1N4007 would be unsuitable for freewheeling.. I mean, unless some crackhead decides to mash that power button at super high frequency, this shouldn't be an issue. All it does is providing a return path for the collapsing magnetic field's energy anyway.
  • 1
    @Condor Yep, it's basically there to prevent burning the IRF :p

    Trying to learn tricks to protect my MCUs and chips before burning... tough I already have 3 arduinos and 4 or 5 are on the way
  • 1
    @GyroGearloose I just understood that that text after “Hardware” was supposed to be your schematic… switching high-side this way is a bit unusual, because won't work if the supply for the Arduino and the power circuit is the same. In your case they seem to be separate, so it should work, except for the last line which implies that you actually shorted the motor? Also, some note on the polarity of the diode would be helpful.

    In other words: Please just draw a schematic on a sheet of paper and make life a lot easier for us ;).
  • 2
    @Condor @GyroGearloose The diode's there not only there because of the motor winding' inductance, but also because of its rotstional energy. It won't suddenly stop turning when the transistor switches off so it will act as a generator. Both effects try to keep up the motor current and if that means the voltage has to rise until it can. The resulting voltage spike likely kills any MOSFET, so a diode is used, which will carry the current.

    The spike will appear pretty fast (basically limited only by MOSFET switching speed and parasitic capacitances), so a fast diode is needed. Imagine that a diode won't switch infinitely fast between its states of conducting (positive voltage across it) and non-conducting (negative). There's two parameters:

    The forward recovery time [1] is the time until it starts conducting, given a positive forward voltage. If it is long, the voltage spike will appear (in your case) before the diode would do anything.
  • 2
    The reverse recovery time is the time until it stops conducting. This is relevant for PWM, as it will essentially short your power supply via the MOSFET. That current spike can also kill the MOSFET, but mostly you see it just as increased losses and worse EMC.

    [1] You don't usually find that one specified in data sheets, though.
  • 2
    Another thing nobody has mentioned yet (unless I missed it) is that the IRF530 isn't a logic level MOSFET and unsuitably for being driven with 5 V only.
  • 6
    @7400 I didn't even know that diodes actually have a forward recovery time.. as far as I was aware the diode would instantly start conducting once its forward voltage is reached. The more you know!
  • 1
    @Condor yep, learned that now...
    @7400 Ive been watching tutorials to learn such mistakes before burning my stuff.
    The program was working, got the motor to work properly in the bread board. When I started making the module (I'll convert such projects in modules to use later) I connected some line wrong. I'll start again tomorrow.
    I'm doing everything by steps... This project :
    Recover a old Niko Rc car.
    Only left the down chassis, wheels and motor.
    I'm doing the project as follows.:
    Motor module. I'm using irf520 although I prefer chips like uln2003 for such job (even less danger to burn something). But a uln2003 is wasted, since It can drive up to 7 DC motors.
    Second: servo motor for direction
    Third : Bluetooth module + Android Rc control modular app (make one app to work with any of my projects)
    Forth: remove control with second arduino and Bluetooth.

    For the next droid project : trade BT for radio for longer range
  • 6
    @GyroGearloose ooh, nice!! Do keep us updated on its progress :D
  • 0
    I have a picture and a crude schematic... picture explains better.

    sorry for the dellay
Add Comment