Write the coding of on off motor
Answers
Honestly, the fun of working with this stuff is figuring it out for your self. Plus what you code depends on what you want to do. What kind of sensor you’re using, do you want to vary the speed of the motor, it’s direction, etc.
So no code for you! Go forth and learn, I’m sure you can find sample projects that do what you want and can use that code as a starting point. But I do want to offer a few tips that I know you’ll need to understand.
You can’t drive the motor directly from your arduinio. Firstly the voltage on the output pins is 5Vdc or maybe 3.3Vdc and your motor wants 24 Vdc, but (and this is important) even if you have a 5V motor you STILL shouldn’t try to drive it from an output pin. The microprocessor isn’t built to supply more than I think 30mA of current on any single pin, a motor may draw 100ma or 1000 or even more, especially when it first starts. So connecting a motor directly to your Arduinio is a really good way to burn up that output and maybe the whole microcontroller, that’s no fun.
What you need instead is a switch, something that can handle the higher voltage and current needed for your motor that your micro can switch on and off using it’s GPIO pins. What you need is a FET. Read up on mosfets to learn how they work and why they’re superb for your application and really easy to use (and cheap, which is also important). Once you get your circuit right it’s as simple as turning your pin wired to the gate of the FET on and off to turn the motor on and off.
Finally, you need a diode across the motor leads to snub transient voltage spikes. A motor is an inductive load, what this means is that once current starts flowing through the motor it can’t just stop on a dime if you turn if off. Imagine someone wads up a piece of paper and throws it at you and hits you in the head, kinda funny or maybe annoying, but not a big deal right? Now imagine instead they pick up a rock of similar size and throw it at you with the same speed, not so funny. The different is that the rock has way more mass and so inertia than a very light wad of paper traveling at the same speed. In electronics an inductive load is analogous to mass, and whatever your using to stop the current flow has to be able to take the abuse of stopping that flow, which typically shows up as a brief but potentially large spike in voltage capable of damaging sensitive semiconductors like your FET. When you put a diode across the motor (in the correct direction) then instead of trying to suddenly stop the current dead in it’s tracks you just divert it into a loop through the diode and back into your motor. Your motor will take a little longer to come to a stop than it would otherwise, but you don’t burn up your circuit. If you turn on your motor for the first time and the diode goes pop and smokes then you put it on backwards, just put the next one on the other way ’round and you should be good.
If you want to vary the speed of your motor then you should look into how to do PWM, pulse width modulation, on the arduino. It’s very simple and thoroughly documented so go for it.
If you want your code to be able to control the direction of the motor as well then you’re getting into some more sophisticated stuff. I’d say get the above right first before you get into that. The simplest way might be to have a relay that you can switch from the arduino (probably using another FET) that reverses the voltage to the motor so that it’ll spin the other way, you would want to make sure the motor was stopped before you switched it over. The more sophisticated and professional approach would be to build an H-bridge, which is probably a great beginner-intermediate project once you’ve got the basics down.
hope it helps you mark me as brainlist