How to Control a 12V Motor with Arduino: Easy Wiring & Code Examples
ฝัง
- เผยแพร่เมื่อ 27 เม.ย. 2024
- Embark on a journey from a Arduino coding novice to a prop animation wizard with our comprehensive guide on wiring and controlling a 12V motor with Arduino! You'll be able to program just about any motor, including using Arduino to control a 12V wiper motor, one of the most popular motor choices for DIY motorized props.
This easy Arduino motor control tutorial is specially crafted for beginners who have never written a line of code but are eager to dive into the world of DIY motorized props and robots to program realistic motor movements. Whether you're planning a theatrical setup, a unique art installation, or your next Halloween prop character, this Arduino motor control tutorial is your perfect starting point.
🛠️ Step by Step Arduino Motor Control Guide:
- A beginner-friendly introduction to Arduino and its potential in motor control.
- A clear walkthrough of wiring a 12V motor to an Arduino using a motor driver module along with power requirements.
- Easy-to-understand code examples that you can use and modify for programming your own custom prop movements.
🔗 Full Tutorial with Code Examples: racheldebarros.com/arduino-mo...
🔗 Identify Wiper Motor Wires: racheldebarros.com/how-to-wir...
🔗 Join the Engineering Artists Community: racheldebarros.com/community
🎬 Video Chapters: How to Control a 12V Motor with Arduino
0:00 - Intro
1:01 - Why You Can't Directly Connect Motors to Arduino Microcontrollers
1:48 - Intro to Motor Driver Modules
3:40 - How to Choose a Motor Driver Module
4:30 - XY-160D Motor Driver Module Pinout
6:38 - Intro to the Arduino Uno Microcontroller
8:04 - How to Wire a Motor Driver Module to an Arduino Uno
10:25 - How to Wire a 12V Motor to a Motor Driver Module
11:40 - How to Wire a Power Supply to a Motor Driver Module
12:14 - Intro to the Arduino IDE
13:22 - Defining Arduino Pins
15:12 - Setting Pin Modes
17:39 - Code Example: Spin the Motor
20:02 - How PWM Determines Motor Speed
21:27 - Code Example: Change the Direction of Rotation
22:20 - Code Example: Set Different Speeds
23:27 - Code Example: Animate the Motor Directions
28:03 - Code Example: Animate the Motor Speed
37:07 - Code Example: Randomize the Motor Speed
39:52 - Code Example: Randomize Motor Speed & Direction
42:04 - Code Example: Randomize Motor Speed, Direction & Timing
43:15 - Wrap up
43:31 - Read the Full Tutorial with Code Examples
43:51 - Join the Community
Build Along:
🛒 12V Wiper Motor: amzn.to/3GfO1sQ
🛒 XY-160D Motor Driver Module: amzn.to/46rwGHT
🛒 Arduino Uno (or off-brand clone): amzn.to/49SfApx
🛒 Breadboard Jumper Wires (Dupont-style): amzn.to/3FUrZvn
🔗 Download the Arduino IDE (free): www.arduino.cc/en/software
💬 Have any questions or want to share your own experiences with coding Arduino motor movements? Drop a comment below - I love hearing from you!
👍 If you find this video helpful, please hit that like button and consider subscribing to my channel for more animatronics/robot tutorials, tips, and tricks. Your support helps me create more content like this!
Some links included here are to affiliate sites. If you purchase something through them, I may earn a small commission - which costs you nothing! I am very grateful for your support when you use my links to make a purchase.
#arduino #animatronics #halloweenanimatronics #dcmotor #arduinocode #livecoding
#arduino #arduinotutorial #motors #12Vmotors #halloweenanimatronics #halloweenprops - วิทยาศาสตร์และเทคโนโลยี
I have been a professional software engineer for 30+ years and I have done lots of Arduino programming as well. I watched your video because I am very interested in motor control. The way that you teach is just universally enjoyable and very well structured. Thanks Rachel!
Glad you enjoyed it! Let me know if there's any other topics you'd like me to cover.
Finally, Someone who talks 'How to do' rather than 'Just a do this' You have opened a few doors previously locked to me.
I like your idea, some these people, Why would anybody post here, VIDEO means showing stuff with sound ? Or am I think about this in the wrong way. WAKE-UP-PEOPLE
No doors were opened and youre annoying.
I am a retired Industrial Engineer. I have programmed PLC Controls for decades. I have good learning/Comprehension skills. I've been trying to learn C++ basics and concepts and keep running into "how to" videos from people who have no ability to Teach. Rachel is the first of MANY to break things down and cover EVERYTHING. You write on the screen and explain what "it does" and "why we do this" .......with others, I keep finding myself going out on the Web to find definitions and explanations for terminology and concepts they ignore....so frustrating. Thank you Rachel.
She really makes it fun to learn and putting everything together practically 👏and explaining step by step.
So much fun, I'm a coder and to listen how you explain complex stuff like variable types, program and pulse width modulation that clearly and quickly explains it is refreshing! Keep up the good work!
You explain everything in such a fantastic way. I have been struggling so much with coding because everyone only talks about doing it but you explain it with examples while doing it. Thank you!!!
Excellent explanation for dummies. You explain like a teacher, very understandable, good job.
I am an electronics engineer, I was a teacher a few years before I was retired, I like your style, keep it up.
My mind is completely blown at how easy you make this look! Thank you for your videos.
Hope it helps for your next motorized project!
Best Arduino demo I've seen so far. Thanks for the information, great teaching technique.
I like the way you speak in a language most of us can understand. Too many people use the very technical language just assuming we all speak electronics and computer code. Nice job.
All I can say is that I wish I had you teaching my first collage course Very interesting and easy to follow. Many or the tutorials on TH-cam leave so much out of the expiation You are teaching properly.
I could just never get it when it came to programing the Arduino, until now.
Thanks a lot for the easy and intuitive teaching style you have that really helped me understand what was going on.
Most other people just expect that you already know what your doing. I cant wait to get onto some projects now thanks heaps.
Wow, been trying to learn Arduino for many years by reading books and watching tutorials. She does such a fantastic job of using analogies and simple terms while starting from the simple and gradually move to the more complex. I finally get the big picture after just watching this ONE video! What a fantastic teacher….thanks for the video!
Thank You so much Ms. Rachel!! It was worth spending 45min here.
This is a fantastic approach to introducing motor controllers, power requirements and coding to folks 👍
Glad you liked it! Let me know if there's any topic you'd like to see.
Fabulous video. SO MUCH better than books. Your production background shows in the presentation. Much appreciated. I'm just starting to learn Arduino and how to control the pesky motors. The video is a great tutorial. I'll be coming back to this as a refresher. Subscribed.
I randomly came across this video by following the "TH-cam rabbit hole." I have absolutely zero experience with the Arduino- I've never even seen one in person. I only know of their existence because some people make projects with them in amateur radio (my main hobby). Plus, I'm in my early 50s, so I don't soak up information as well as younger people. However, there wasn't a single thing you presented in this video that I didn't fully understand. Your teaching style is outstanding and you made all the gears click in my brain- and some of those gears are pretty rusty! That's a subscribe for me!
I am so glad I came across your channel. I really appreciate your clear and concise instructions and explanations. Subscribed.
I just stumbled upon your channel and even though I don’t know if I’ll ever need this information I liked and subscribed. You’re simply a great teacher and I need those in my life!
I feel like Neo in The Matrix when he gets plugged in and learns jujitsu. Thank you so much for this. I have been wanting to add motion to my metal sculptures for years. ❤️
I have always been drawn way more to hardware then programming, but this integrates both. The industrial machines i worked on do this on a larger scale, so this is the path to greater machinery starting here.
First, great video and explanation!
Let me give a few remarks on coding:
1. as another commentator already stated, using #define constants instead of variables for "never changing" or "magic number" values, like pin associations, or the motor's low speed (magic number 70), is better.
2. inherited from C++ (Arduino language is basically a superset of C++), you can use the shortcut var += increment instead of var = var + increment. Works with all arithmetic operators (-=, *=, /=,...).
3. try to avoid delay(). The processor is literally delaying then, means it does nothing else. You'll get in trouble if you want to do something concurrently then.
4. I'd have the movement of the motor put into a function, like void turn( int direction, int speed ), so you reduce three lines of code into one. Same with stop().
Re my point 3: I've almost nobody seen to address this, albeit this is basic microprocessor programming knowledge I learned almost 40 years ago 😊(Yes I'm an Old White Man, and credits to Professor Reichert of Engineering school Aalen who was a great teacher). I wrote a tiny class that works as a timer and executes code after a specified delay or in specified intervals, or tells you if a delay is over instead of halting the program. Also you can break the granularity down from milliseconds to microseconds, because you can use the micros() function instead of the millis(). The caveat is that this method adds some imponderability to the programs, so you can't predict when exactly which function is executed, because that depends on if some function is busy longer than the time checking interval is.
But great explanation of the Arduino's power capabilities! Overloading ports is one reason why newbies fry their Arduinos and then end up frustrated.
Have been using Arduinos for over a decade now and it's great to see someone who is equally excited about them.
🤣 Arduino has allowed me to do so many cool projects!
Just built this and could not have done it without your excellent video!
Awesome! What project are you building with a motor?
These videos are just brilliant... Understanding electronics is one level but comprehending it like your videos is another!!!
Hi, Since years I wanted to start with arduino to drive a small wood prototype and your video is so clear and well explained and your positivity makes the video very enjoyable thank you very much !!
I’m so happy I found you. I’m just starting to watch your videos, but just in one video I learned a lot. My brain almost exploded just like my motor controllers have 😂
I wish i found this video when i started learning arduino, sensors, and controlers. Great work.
RACHEL !!! I am so glad I stumbled across your channel. Weird timing: I am teaching myself Arduino programming to control servo motors for some machining projects I'm trying out. Awesome job explaining what you're doing, and how the Arduino works! That helps me learn as a newbie at this stuff. I so miss seeing you on All Girls Garage! Subscribed now, so I can start getting my "Rachel" fix again !
Best arduino Video i have ever seen. I myself attached Joystick to uno R3 , L298n, two yellow motors. Next will be wlan, maybe esp32. Started 3 weeks ago. Thank you so much.
Hey! Just discovered your video and am falling in love with you already. You're such a great teacher. Wish I could say the same for my teacher. He sucks!!!! You're sooooooo clear and precise. Guess I'll be coming back for more.
Thanks for this fantastic video! It's exactly what I've been wanting to do and you really made it clear and fun. I'm totally new to all this and this all makes clear sense.
Keep me posted on your project and let me know if you'd like me to cover any other topics!
Just finished watching. great stuff. I know a a lot of people who are going to be your fans. thanks
I like the Drok motor controller. That's what I use. It uses FET's, and it doesn't have a heatsink, and from what I've experienced, it doesn't hardly get warm, even with heavy use. I think it has opto-isolation, and that's one reason why it has a separate ground. It seems to be a pretty trouble free controller. Also, the wire receptacles are removable. This makes it very easy to wire, if the board is already mounted in something. You can attach the wires to the receptacle, then plug it back in to the board. It also makes it easy to switch channels, if you want to test the other channel.
The power input receptacle is also removable too. If some accident happens to your board, you can easily swap it out, and the project remains wired - aside from the control wires. I like that there are lights for both the control section, and the drive section, so you can tell which sections have power. Also, the positive and negative pins, for the control section are joined, so you can daisy chain & power another board, with the free pins.
Awesome video, Im a sophomore Mechanical Engineering student and this video really saved my project.
Just found you, and you make it look easy, you are a good teacher, I been hearing this Arduino but kinda intimated, but seeing your video, I feel I can do a simple Arduino project, thanks a lot.😊
Rachel, you are a gem. Explanation is simple.
I like the way you explained it. I learned and at the same time I had a lot of fun. Your are the best.
Thank you! 😃
I'm taking up Arduino programming because of you. You simplify the jargon.
This is perfect! I plan on using one of these motors for my gate openers and this is the last part I need to install it.
Outstanding tutorial. Turning that motor on or off with a button or sensor would be a dream come true for me.
Great idea for a future workshop! In the meantime, I have an upcoming one about how to control a motor with a motion sensor later this month.
If everyone had this attitude we would have warp drive by now...your awesome thank you for all your effort.
Its fun listening to you and you're telling it as a story, and my brain thinks "oh, now its actually easy to understand". Thanks for this!
I love how arduino has opened up this kind of work to people without formal technical backgrounds. For a lot of things, if someone can make a spreadsheet, they can use common sense and online examples to do what they need. I love watching your stuff, since you embody that ethos perfectly. Keep it up, and good luck to creative people adding tech to their creations!
Basically this would be a good video if you show how to control the wiper motor at delay various speeds between low and high and then you have the shut-off circuits that make sure the wiper blades shut off and the blades are in the right position. This should be super great video with just with simple things like this, especially the time delay on an older wiper motor.
Hi-eee. Newbie to Arduino (obviously why I'm here) your channel. Was watching on my own, then my 8-year-old came to see why I was chuckling. She sat on the floor and I wanted to if she grasped any of this or was even interested. So she watched you because I was laughing at how you make some expressions to get the point across. A child being a child, she watched for the 1st.15 min, 12 minutes longer than I expected. So I think I'll show her more of your video in small doses. You know Baby steps. It's how we learn.
You have a great way of putting the information over I'm new to coding.
You teach very well and your personality makes the videos very easy and fun to watch.
Just sub's I have been interested in this for a long time just never found anything that at least explains it and was very helpful, you instructions made this really doable for the newbie. Great video
I was a controls engineer for 45+ years and also taught industrial controls and PLC's at the local community college. You have a very nice teaching style, good job. I just found your channel so what I'm about to say you might have already addressed in another video, but when using Arduino, I tell people to never use the delay() function because the delay() function literally stops the program from executing code. So...if you set a delay(3000) 3-seconds and there is code below the delay(3000) that needs to be serviced during that 3-sec. delay, it will not get serviced. I recommend using the millis() or micros() function and setting a duty cycle with a bit of math for current time and previous time. That way the program keeps "scanning' the loop() function even during the delay.
You're absolutely right! 👍 Except for simple programs, I usually stick with millis(). I do have a video dedicated to using millis and another one for using interrupts. But I can't deny that delay is an easy concept for beginners that have little to no coding experience to understand, so that's why you see them in all my beginner videos like this one. I have another millis video coming out using a PIR sensor to orchestrate LEDs, servos, motors and mp3 players in March! Stay tuned!
@@RachelDeBarrosLive Thanks for the reply Rachel. I found your millis() video and your interrupt video. Very well done. I'll be very interested in the mp3 player. I'm doing the research for animating a Moebius 1:6 scale Lost in Space robot and already have sound files so I'll be interested in how [you] do the mp3 player. I'm looking for one that I can chose what sound track I want to play. My plan is to have everything RC controlled. That's all new ground for me, but it's so much fun!!!! I'll be watching. Great job!!! I'm so glad I found your channel. 😀😀
For that last example if you want to avoid the case of double zeros or double ones stopping the motor, change the first 3 lines of your loop:
void loop() {
int spinDir = random(0,2);
digitalWrite(IN1pin, spinDir);
digitalWrite(IN2pin, 1-spinDir);
…
Now whatever spinDir is set to (0 or 1), the second pin will be set to 1 minus that value: so if it picks 0, the second pin will be 1 - 0 = 1. If it picks 1, second pin will be 1 - 1 = 0.
Any time you need to flip between 2 numbers but don’t want to waste time checking the current value, perhaps in an interrupt routine, just add the 2 numbers together and set the value to that sum minus the current variable. This will always work as long as you initialize the variable to one of the two values.
So if you need a variable K to flip between 13 or 77, just do:
…
int K = 13; // initialize variable
…
K = 90 - K; // flip to other value
…
So the first time it flips:
90 - 13 = 77
Second flip:
90 - 77 = 13
Etc…
This video managed to turn the light of understanding in my head to ( HIGH); thanks !
I'm so glad! 🤩
2:57 - you're definitely not the only one! (I'm embarrassed to even say how long ago some of my not-yet-completed, but-still-being-worked-on projects got their first starts...... 😲)
Nice tutorial!
You explain it simple. Make it easy to understand.
Brilliant. No reason you came to my feed that I know of. But so good that I have this for reference now. Really enjoyed your voluptuous style and clarity as bright as your eyes. If that made sense to anyone you have a better grasp of english than I do of Arduino! After Rachel De barros explained and demonstrated how to make it function for "You"!
Thank you super video. Have an arduino for nearly 10 years with post purchase blues. So didn’t get much done with it. Thank you you have energised my imagination hopefully I get a project done now
that was the best explanation of code on the arduino ive seen.
it is remarkable how similar Rachel's on-screen personality and "look"is like that of Angela Petrelli in her guitar lessons. Both high-energy girls well-versed in their craft, both very attractive, both no-nonsense! Are they the same person? You decide. I like them both, and their videos too
Squiggly Pins are the Boss. MOSFETs are clean and simple, but piggy backing four L293Ds to make a 4.8 Amp driver is top shelf hacking.
Good Show
😄
Great video on controlling motors! Super helpful for a quick start, especially if you have not used Arduino or motors before.
I have been wanting to add Arduino to my props for a couple of years but have been too scared to start, until now. Thanks!
Love your style of teaching, well done.❤
I love your accent. I need to get back to my arduino and raspberry pie projects. Thanks for the motivation.
Thank you for providing such a great video, you were a pleasure to watch and so very good at teaching!
Hi Rachel and thanks for a great video 🫡 I really like how your explained all the functions of the arduino so now i feel like starting to use mine that's been in my drawer for 5years 😂 Keep up the good work and you've got yourself a new subscriber from Sweden 👍😁
Everybody else on here has already stated it so i wont, oh hell yes i will, absolutely brilliant video/tutorial, the best i have ever seen on coding, thank you
Great pedagogy Rachel! Thank you! You earn a new sub!
Great video with superb step by step explanation. I am new to coding with arduino and have a complicted machine to program. This has help no end to help understand the environment , instruction set and the logic too. Huge thanks for doing these vidoes.
Beautiful! Finally explaining it all! Thank you!
Thanks, I can apply these ideas to controlling my 12v slotcar track!
I use 3 relay`s to control my wiper motor. One for power on and the other two for direction. The motor is used to cover my greenhouse up at night with a canopy. The arduino program memory and RTC lasts all winter outside in minus 10 sometimes.
Gostei da maneira de explicar a programação. Parabéns!
Estou estudando programação como hobby.👍👍
this is best explanation I have ever seen, Literally !
Love the way you describe it in my language not all scientifical. And your HOTT!
You are really good at explaining . Imma give this project a go. Just need a motor controller :)
i sat and watched your video last night in bed and though this video is gunna be a right bag of u know what, but bugger me turned out really good i actually learned summet that is going 2 be bloody usefull to me so big thumbs up and iv subscribed
Thank you for the tutorial. Subscribed and ready for more.
I think the best video I've seen ever on Arduino
Glad to hear it! Let me know if there's any other topics you'd like me to cover.
Really like how you're explaining things. I just subscribed
Great tutorial 👍
Disappointed the spider on a string was a no-show.
🤣 🕷
I definitely see you with your own show on DIY, Home improvement, Science show on cable TV!!!!!
Fantastic explanation on motor controls thank you.
im loving this channel
I'm in love, Glad I found this channel..
Great video! Lots of great imformation for beginners. Thx.
Very welcome!
Thank you for a clear explanation of pwm motor control.
Great tutorial - thank you for this!
hi...excellent video....now i know who to call when there is a lot of repetitive production work....
like heavy lifting...or navigation stuff...have fun out there...
awesome explanation, really appreciate your work on this
such a nice video, clear explanation, I wish youtube was just this
Thank you so much for sharing this!
Glad you enjoyed it! Let me know if there are any other topics you'd like to see covered.
@@RachelDeBarrosLive That's a perfect video for me to learn how to control a motor with Arduino. Would it be the same logic when dealing with a stepper motor?
learned so much....loooooove your teaching style
Great video+explanation+presentation! Thank you for sharing well done mate - NEW SUB 🙏🏻
Got to hand it to you.. perfectly explained.
Great job very informative for a old man like me, thanks.
Great tutorial, thanks for making this look simple
I love the way you teach as it sinks in,
I would like to have this project but have it reduced in size, meaning using an ATtiny54 or 85 chip to control the motor and to have only one power source and to turn it on using a push button trigger and upon release it stops,
It would be pefect for me to start a robot project that is manually controlled.
Iwill work out the coding by myselfas cant expect everthing to be done :)
Like your. passion and simplicity explanation, with visualization of real working materials ,that represented, very good!!!
absolute amazing video!
Rachel I came across your videos because I wantes to make a shooting game using a windshield wiper motor. This is great, what i need some help with is using a piezoelectric shock sensor to turn the motor clockwise?
That's a fun idea! I should be getting in some piezo sensors in soon so you'll see some videos on those soon!
Not sure if you mentioned it or not, but I believe if you bring both IN1 & IN2 high, that provides a braking function (pretty common with h-bridges). In fact, I think your random function can cause the motor to brake when both are high and you can see that behavior in your video.
Yep, indeed it does! I can't remember if it's in this edited down version of the live stream or not but that's a good point to include in the tutorial on the website for everyone. Thanks for the catch!
@@RachelDeBarrosLive Actually, I just re-watched it and you do mention that. Thanks! 😊