IRF520 PWM code > I have a functioning code I have written, need some advice.

Greetings,

I have read plenty of books and sketches, not grasping every concept yet, but getting there.

My project:
A gravity assisted mini grain/feed elevator/provider, grain flows down by itself, sometimes stops, this just moves it along "insuring" a more constant feed. impeller the motor is connected to is spring loaded and will keep a tension on the feed until feed is depleted and IR sensor detects no feed is present.

What I have:
IRF520 module, IR sensor, Arduino Nano, 9v battery* (which is a part of the problem)

My current simple code that works:

int digitalPin = 8; //IRF520 module signal pin
int sensorPin = 7; //Infrared sensor module signal pin
int IRGate = HIGH; // < need explanation on its usage? rem'ing it out give errors in compiling.

void setup() {
pinMode (digitalPin, OUTPUT);//IRF520 module signal pin
pinMode (sensorPin, INPUT);//Infrared sensor module signal pin
Serial.begin(9600);
}
void loop() {
IRGate = digitalRead(sensorPin);
if (IRGate == LOW) //if gate is blocked stop motor
{
Serial.println ("Grain is present");
digitalWrite(digitalPin, LOW); //stop motor
}
else
{
Serial.println("Feeding");//run motor
digitalWrite(digitalPin, HIGH);//send signal to IRF520 to run motor
}
delay(200);//not certain I need this at this location in the sketch... but I do need the motor to run a bit longer than instantly start/stop, like a 1/4 second to 1/2 a second or so.
}

I need to limit the speed of the motor, its too fast, I had analysed the initial non arduino board I am trying to replicate, under no motor connected the motor has ~7v at the motor leads (I dont have a scope to measure PWM at the moment), ~0.5Ah no load but running the impeller and 1Ah stall load, motor connected and under load ~3v, with a 9V it runs too fast and a simple 9V battery does not have the amount of mAh for long run times.

I can simply change the battery from a 9v to 4AA or a 4000mah 1S Lipo ... yes ... ok, but would like to know if I could write this better using smarter code.

My questions are:

  1. Id like to use PWN syntax/function with the IRF520 to change the % of volts feeding the motor, can this be done using digitalWrite/digitalRead? (possibly learning how to use PWM better in the process) or is it simpler to use AnalogWrite/read? (I was not successful yet in writhing this code with analogWrite).
  2. Could I add a Pot to the setup and have it read to modify the PWM input to the IRF520 module? I can write the % or V needed from the power source I would be using. do I need a specific motor library to run the IRF520 and be able to alter the voltage (or on time) the motor gets?
  3. Should I just shut up and run the lower voltage battery and keep everything as it is?

Thanks,

Matt.

Reading your post above, I have many questions. If you had read the forum guide in the sticky post before you posted, I would have had far fewer questions. Please read the guide and modify your post above to include code tags (not quotation tags), links to components/modules, schematic showing how everything is connected (hand drawn is fine)...

Couple of thoughts:

  1. 9V batteries are useless for anything but smoke alarms. Certainly not motors.
  2. IRF520 not suitable for use with Arduino's 5V digital signals. Use IRL520. The "L" means "logic-level" and is suitable for use with 5V signals.

Thanks, I will re-read the post and attempt a much better and clearer post.

I would not say 9V are useless, its only for testing, im aware of their low mah.

The IRF520 module (not just the transistor) is working really well right now connected to pin 7 (not A7), wondering why its working so fine then.

Thank you for the pointers.

Matt.

void loop() {
IRGate = digitalRead(sensorPin);
if (IRGate == LOW) //if gate is blocked stop motor
{
  Serial.println ("Grain is present");
  digitalWrite(digitalPin, LOW); //stop motor
}
else
{
  Serial.println("Feeding");//run motor
  digitalWrite(digitalPin, HIGH);//send signal to IRF520 to run motor
}
delay(200);//not certain I need this at this location in the sketch... but I do need the motor to run a bit longer than instantly start/stop, like a 1/4 second to 1/2 a second or so.
}

Wouldn't that delay be be better placed just before the stop motor line?

Even better, learn Arduino time code and un-delay that wait-for time. It's not needed for the sketch as is but if you decide to add more buttons/actions then it may be just what's needed.

Motor speed you can control with an H-bridge motor driver or you can have a wheel on the motor shaft turn a bigger wheel on the impeller, if the impeller is round, have the motor wheel turn that... yes with friction and a rubber wheel on the motor shaft.

If I want grain to slide down a channel I might put a shaker on the channel, something small.

  1. Id like to use PWN syntax/function with the IRF520 to change the % of volts feeding the motor, can this be done using digitalWrite/digitalRead? (possibly learning how to use PWM better in the process) or is it simpler to use AnalogWrite/read? (I was not successful yet in writhing this code with analogWrite).

No, you do not use digitalWrite() to create PWM.

  1. Could I add a Pot to the setup and have it read to modify the PWM input to the IRF520 module? I can write the % or V needed from the power source I would be using. do I need a specific motor library to run the IRF520 and be able to alter the voltage (or on time) the motor gets?

You could learn all you need to know about PWMby using the program “Analog in out serial” in the analog subsection of the ide’s example programs. Once it works with the led, substitute the mosfet & motor for the led and resistor and you’ll have a variable speed motor. You should be using a wall wart for power as 9v batteries are hideously expensive for a few minutes of run time.

  1. Should I just shut up and run the lower voltage battery and keep everything as it is?

No, that’s not productive or helping you learn something.

MATTS:
The IRF520 module (not just the transistor) is working really well right now connected to pin 7 (not A7), wondering why its working so fine then.

How hot does it get? FETs are efficient when fully open or fully closed. Your FET needs 10V to open full. At 5V on the gate, it's running about as hot as it can given the Watts you pull through it.

I have IRLZ44N's that can be gotten pretty cheap or you can pay > 30 cents each, like about a buck each so shop around!
They are best used to drain a circuit to ground, the switch is at the low end instead of the power supplying high end.

I would not say 9V are useless, its only for testing, im aware of their low mah.

Testing with 9V batteries is going to lead to some very strange and disappointing results! Its not simply the low capacity of these batteries. They also have a very high internal resistance. This means they can only give small currents. If you try to draw more than a small current, the voltage quickly drops to much less than the expected 9V, leading to all sorts of strange behaviour of your circuit. I'm not talking about flat or nearly flat batteries here, even fresh, good quality ones have this behavoir.

It probably puts less load on the Arduino 7805 when the 9V drops to 7.

True. But if it drops to 5.5V, the 7805 can only output around 4V, and the 16MHz AVR chip starts to misbehave...

Good evening,

IRLZ44N, 50cents CAD a piece online, pack of 20, thanks for the direction.
I also have some 7805* TO-220 here, I could try as well.

Rechargeable 9V lipo and Duracells, yes, internal resistance not so great, many good points, I know better... I just didn't bother, no more nonsense, the system will use a 1800mah 2S (8.4V) 45C Lipo pack to run while away from a wall wart. I have several 18650's 2300mah I can configure in 2S packs I can use instead of 9V batteries.

How hot does the transistor on the IRF520 get?, not very hot at all, I will actually abuse one to see the heat it can give off, its in use a very short amount of time to prime the feed which takes at most 1 maybe 2 second at most to prime the channel and then fractions of a second (like 100ms to 250ms) to re-prime as it goes every few seconds when in use as it is not constantly in use either, I will make myself a loop test jig and get some more mAh/temp/time data.

I will study "Analog in out serial"

Turning a bigger wheel is not possible, the impeller assembly is already geared down something like 10-12 to 1 via oring pulleys. miniature im telling you.

Thankyou all for your valuable inputs, very appreciated.

Matt.

PaulRB:
True. But if it drops to 5.5V, the 7805 can only output around 4V, and the 16MHz AVR chip starts to misbehave...

That's what I understand, the regulator needs about 1.5V over the desired output to regulate properly. I expect problems start not much below 7V.

When I got my first adjustable buck converter I set it up for 5V output and found that even when I fed it 5.2V the converter output metered 5.0V. I'm sure the output has ripples but they're small from what I've seen and most circuits put a bypass cap after the converter to flatten that more.

Just for interest there are small current converters that look like regulators, about the same size and pretty cheap. I grabbed 15 on a deal and since then they've gotten cheaper. If I need 3.3V, I won't use a couple of resistors to get there from 5V.

MATTS:
Turning a bigger wheel is not possible, the impeller assembly is already geared down something like 10-12 to 1 via oring pulleys. miniature im telling you.

Okay, the impeller assy is turned by a motor not built into that assy?

You can drive the motor straight off a FET but an H-bridge should do it more efficiently and you are running off batteries. What the H-bridge does is during the PWM off-phases when the motor coil fields collapse and drive current back out, in one H-bridge mode it directs the current back through the coils. The surge circles around instead of all dumping straight through protection diodes to ground.

H-bridge has other modes and on this site: H-Bridge Secrets | Modular Circuits
there's even more than I'm ready to go full taco with.

H-bridges – the Basics
Sign-Magnitude Drive
Lock Anti-Phase Drive
Asynchronous Sign-magnitude Drive
MOSFETs and Catch Diodes
H-Bridge Drivers
H-Bridge Control
Safety Features

Since you are going battery power, quit using the on-board 7805 (I understand that some Arduinos now have DC converters instead of lossy 78 series linear regulators) and look into both buck and boost converters.

Example: I have converters that can take as low as 0.9V up to 5V and they have USB ports for output, can charge a phone using "dead" batteries in series as long as they addup to 5V or less. A serial line of cells can be drained to 0.9V =total=, get the last of non-rechargeables. Thing is, those converters max around 1A at best according to take with salt specs, I wouldn't push more than half that. Search words: dc converter buck boost tutorial

Okay, the impeller assy is turned by a motor not built into that assy?

Yes, motor built into the gear/transmission assy, removable yes but mounted deep in it. Not going that route. Motor fits in the palm of my hand, it's a 130 size DC motor, yet my fluke tells me it pulls as I mentioned 0.5amps with assembly spinning freely (not feedback ng grain) to a maximum of 1Amp stall (previous controller errored out at that amp draw and required reset), replaced the motor and board with other spare ones that I have and same specs/results.

As for buckdown or up regulators, I had thought of that too, I have a few of them, I will review their specs and definitely try that.

Thanks again for all your inputs, such a great community.

That's why I asked, it's part of the assy and best kept in place.

Would you consider using a buck converter to find a voltage that would turn the motor at the right speed? Then no PWM or H-bridge would be needed at all, just a FET to switch that power on/off.