I am still working on a long term project to control airflow to a cars oil cooler by using a stepper motor to control a butterfly flap in an air duct. I previously had this working using a servo motor but decided that a stepper motor would resist the torque of the airflow better.
I first wrote an extremely basic code to just continually spin the stepper motor so that I knew things were working. I then progressed to my more proper code.
The Toyota TPS is used on the other end of the flap shaft from the stepper motor as a position feedback. I know the ADC reading of the TPS in the open and closed position and I compare the commanded flap position (TPS ADC reading) to the actual.
The way my code is written (well my intention anyway) is that the Setup procedure moves the flap to the closed position which corresponds to a TPS ADC reading of around 120. After that it updates some information on the Nextion. I have currently commented out everything inside my Loop() so as to try to isolate the issue which exists within the Setup() and function moveStepper().
When I turn on the external power supply, I am getting very erratic and inconsistent movement. Sometimes the flap moves from the fully open position (its starting point) to fully closed then immediately opens fully and sits there making a horrendous vibrating noise as if it is trying to move the flap but cannot (however I can push the flap in either direction so it is not stuck). Other times the flap just moves nowhere and just makes that noise and vibrates. I just tried it again then and it worked correctly! Go figure.
What have I tried;
re-wired to remove as many connection points/joins as possible and to minimise the use of dupont cables
measured the supplied voltages
played with the BED current pot position
added as much serial debug outputs as I could
I have never played with a stepper before but assume that this erratic performance is not normal. Any suggestions as to the cause or other things I can try?
Maybe the stepper I have chosen does not have sufficient torque? The flap I am testing it on does not appear to need a huge amount of effort to rotate it wen I push on the flap but I guess the stepper does not have that leverage advantage...
I have not looked at your program - you seem to have 27k of code.
I suggest you learn to control the stepper motor with a short program - like 1k or less. If you know the limits of the ADC values you can simulate the flap sensor readings either with a manually operated potentiometer or just with a few fixed values. Keep the progam as short as possible.
It will be much easier to help you with a short program that has no extraneous code.
In general your code doesn't look that bad and the motor is probably overkill. There are several issues though. Without examining the commented lines, I can't see why you've included SPI. Regardless, there will be a conflict with SoftwareSerial being assigned pins 10 and 11 as SPI wants to use these. Actually there's a problem with SoftwareSerial and any other library that wants to use pin change interrupts. Anyhow, so you need to fix that, maybe just comment out SPI for the time being.
Now for the nuts. What do you know about pull-in torque? Thought so. How about Infinite Acceleration? Yep. Okay well, acceleration is defined as the change in velocity divided by the change in time (dV/dT). When starting from zero (or any constant speed) where the acceleration is zero, and you instantly want to attain some speed, the delta T is effectively zero so the acceleration is effectively infinite. Reality is however, that propagation delays of a few nanoseconds means that dT is not actually zero, but close enough to make acceleration huge. Kinda like trying to go from 0-60 in your car in less than a second, you just spin your wheels. Same thing with a stepper motor, you try to start to fast, the motor stalls. That's where the pull-in torque curve comes in. For any given motor, for a given voltage and current, there is a minimum speed (rotational velocity) you must already be going before you can move up to the next speed. The curve defined by the power calculations is the pull-in torque curve. For a Series 42 (NMEA17) motor such as yours, with about 12V and maybe 1 amp, the start-from-zero maximum speed-without-stalling is about 120 RPM, without load. Now, a magic formula you want to remember is 300,000/RPM. This result is a value in microseconds of the period of the pulse train necessary to drive a full step, 1.8 degree motor at whatever RPM. The smaller the microseconds, the higher to RPM. Using the 'about 120' figure divided into 300,000 you get 2,500 or 2.5ms.
Now look at your loop where you send pulses. You write the STP pin high and hold it there for 1ms (you only need 1us for the A4988) then you bring it low for another 1ms. Then with one analogRead, you're straight back at it. Total loop time => 2ms plus a bit equaling about 150RPM. Whoops! That humming sound you hear with the motor is typical sound of a stepper stalled.
So increase your delay after bringing STP low to maybe 10 (start slow, see what impact your load has) and report back your results.
@DKWatson - you are correct as I had no knowledge of the terms you described. I will get back on this project and make the change you suggest. I will report back in due course.
I changed the delay after the LOW setting to 10 as advised. Since doing that the erratic behaviour has ceased. I then changed it to 50 and all was still good. 50 produces a slower and 'nicer' movement so I shall keep the figure there.
The only issue that remains is that the majority of the time the motor has a high pitch whine (sometimes however it goes silent). When whining, if I every so gently push the flap, it stops. What would that be? Anything to be concerned about?
I have tried playing around with the current pot on the Big Easy Driver to adjust it to the minimum current that works as both the motor and the BED are getting hot. I realise this is probably normal but it is unnerving to say the least. I have bought a small heat sink. Would that be stuck directly onto the small square chip on the BED??
One last thing, I have 10v going into the BED. Is there some worth in reducing that seeing as the stepper motor is operating <5v? Will that help with heat?
Noise. Another reason that micro-stepping is used by some is to get rid of audible hum. Unfortunately, the range of frequencies that stepper motors operate in are within the band that many can hear.
Heat. Another unfortunate, for you. Stepper motors run coolest when running, hottest when stopped. It is entirely common for the motors to get hot, even to the point where they're untouchable. The only way to get around this is to vary the current/voltage combination for your motor to a lesser value that still works. I've managed NEMA17 to operate (no load) with 7V and only about 80mA. The other thing to think about, is switching the power off when not running, except you then lose your holding torque. Always going to be a trade-off, somewhere. As they say, good, fast, cheap, pick two
Just tried changing it to 1/16th steps. The noise is still there. Ultimately the motor will be under a car so the noise itself is not an issue. My concern was that it meant something was wrong but it appears not. It is just strange that it squeals for 10 minutes then randomly stops - no idea why.
The heat should be fine. I left it running (stationary) for 30 odd minutes and the motor was still fine to hold. The BED gets hot but not as hot as the motor. The airflow under the car will take care of the motor heat so really I only need to ensure the BED does not overheat (that will sit near the fuel tank )
Well I think so. It seems to be working consistently now.
The only last piece of the puzzle is why the humming/sqealing noise is there sometimes and not others and why it goes if I apply light pressure to the flap.