Arduino Tutorial 38: Using a Tilt Switch Cut Off With a DC Motor
Vložit
- čas přidán 10. 09. 2024
- You guys can help me out over at Patreon, and that will help me keep my gear updated, and help me keep this quality content coming:
/ paulmcwhorter
In today's lesson we demonstrate how to incorporate a tilt switch cutoff into a DC motor project. This is a useful method for cases where you would like to automatically turn your motors off in the case your project/device turns over or tips. This feature can be useful in a number of different projects and prototypes.
You can get the kit I am using for this series at the following link:
amzn.to/2I7N4Ek
Follow these lessons on our FREE web site:
toptechboy.com...
[Disclosure of Material Connection: I am a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com. This means if you visit the link and purchase the item, I will receive an affiliate commission. Regardless, I only recommend products or services I use personally and believe will add value to my readers.]
#Arduino
I did the homework on the last episode no problem and decided to use the knowledge Ive gained so far afterwards to combine my DC Motor, Servo, and Joystick , X controlling motor speed/direction , Y controlling servo position to point the fan in a desired direction. My longest code so far and was successful right away. Fantastic teaching I can’t thank you enough !
Great idea, Colten!
Did the homework! When you see people only viewing for 3 minutes I think you'll find it's people just revisiting sections of tutorials they have watched in entirety previously to help better understand the one they are currently working on. I have certainly done this on multiple occasions!
Great lesson! Here is the code I ended up having before watching the video:
void loop() {
bool tiltVal = !digitalRead(tiltPin);
digitalWrite(directionPin1, HIGH & tiltVal);
digitalWrite(directionPin2, LOW & tiltVal);
analogWrite(speedPin, mSpeed);
}
Did the homework. I used a while loop instead and it worked quite nicely. Had to take the delays out like you did. I was yelling at you about the analogWrite instead of the digitalWrite. My wife thinks I'm crazy.
Completed the homework under 30 minutes on my own. Thanks a lot for your teaching sir
Keep doing these videos. they may not be crazy popular, but you have no idea how much these videos help us.
This one was cake!! The only thing to this point that I’m struggling to conceptualize is the toggle switch. It gets easier the more I practice with it tho
Exactly man
I can agree
I absolutely agree 🙂
Yes
@@williamhandlebar9585 the toggle switch was very challenging
Did my homeworks, even managed to control speed and direction with a pot and a pushbutton (using the INPUT_PULLUP status given by one of your followers)... I'm so happy that 2 days ago I didn't even know how to use a component... now I just feel like understanding is at hand !
What I like the most is when you explain by physics and math how and why things work in a component. It's so helpfull !
I did my homework and had it working prior to watching this episode. Setting the motor speed to zero seemed logical, so that's what I did. The only mod I made was to put the tilt-switch on a couple of male/female wire extensions to make the tip-over testing easier. Thank you for another wonderful lesson. 👍
Haha, putting the tilt switch on extensions was a great idea, thanks!
My coding was slightly different initially, but still got it to work. Yours was definitely more streamlined. On to my next homework assignment.
Made it through the homework unscathed. Love the homework assignments!
Hi, Paul. Did the homework on my own successfully. Since the bread board is so big and I hate to have to tear it down and start over most times, I decided to see if I could combine lessons 35, 36, 37 and 38, both schematic and code, so I could run the whole shmear at once. So, now I have a reversible stepper motor driven by a toggle switch, a DC motor and fan running clockwise (so the air blows out/up from the blades), and both motors stop if the tilt switch goes to 1... with green and red LEDs to indicate when it is normal vs "tilted". I'm sure the battery doesn't love running 2 motors at once, but the PS does seem to handle it. My board is quite full, but I didn't run out of pins! Fun!
Finished the homework before watching this. I used an else statement instead of another if statement.
Thank God for you! God bless you sir!
I made it Mr. McWhoter. Great video as always. I will finish this course to the end. So far I haven't had any problem and I've managed to pull out every assignment before see in you doing it. I always watch your videos up until the last second. Thank for your hard work doing these videos.
love these. I used a nested if loop to give the motor the "kickstart" so I could initiate spinning at a lower RPM.
I did the homework with flawless victory! Can't wait to start on the joystick controls!
I did it myself. I also added a potentiometer to control the speed and a button to change direction. Off to do the joystick assignment now.
I am now able to add the tilt cut off with flashing red LED. Control the speed of the motor with a potentiometer. Turn the whole thing off and on and reset the tilt state with a button.
I just wanted to take the time before finishing this video to let thank you for this series!
You taught me how to do Fusion 360 and I'm so happy to continue to be able to learn from you.
Reply
Was able to do this, simple enough and added a active buzzer as part of the tilt.
The tilt switch homework was easy and ill be getting to work on the joystick HW in the morning as it is getting late. Thanks you Paul!
Did the homework and got it working just fine. This one was easy.
Hi Paul. Did my homework and very close to your solution. I also pointed out your baud rate error in my comments to Tutorial 37. Really enjoying these tutorials. Thanks
Still there watching all the way through to the very end Paul, as always a very interesting tutorial, thanks again!!
I did this after the previous lesson all on my own! Loving the videos!
int speedPin=5;
int dir1=4;
int dir2=3;
int tiltPin=2;
int tiltVal=0;
int mSpeed=255;
void setup() {
// put your setup code here, to run once:
pinMode(speedPin,OUTPUT);
pinMode(dir1,OUTPUT);
pinMode(dir2,OUTPUT);
pinMode(tiltPin,INPUT);
digitalWrite(tiltPin,HIGH);
pinMode(buttonPin,INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
tiltVal=digitalRead(tiltPin);
analogWrite(speedPin,mSpeed);
Serial.println(tiltVal);
if(tiltVal==1){
digitalWrite(dir1,LOW);
digitalWrite(dir2,LOW);
analogWrite(speedPin,mSpeed);
tiltVal=digitalRead(tiltPin);
Serial.println(tiltVal);
}
if(tiltVal==0){
digitalWrite(dir1,HIGH);
digitalWrite(dir2,LOW);
analogWrite(speedPin,mSpeed);
tiltVal=digitalRead(tiltPin);
Serial.println(tiltVal);
}
}
I got it before to watch the video, basically it was easy applying the concepts I learnt in the 2 previous videos . I am looking forward to doing the next homework with the joystick.
Great job!
I did it ;) (it was kind of easy) Thanks so much for all these AMAZING TUTORIALS!!
These homework assignments are so important I am so glad you do them!!
Did the homework successfully before this lesson. Did it two ways. 1/ like you did with the simple "if" on the tilt switch and 2/ with the state change as in lesson 29.
I did what you did except I inverted the read from the tilt switch so that when it was in the OK position I was reading "1" from the tilt switch, that way I could simply multiply the motor speed variable by the result of the tilt switch read and if the device is OK the motor would run and if the device were "tilted" the result "0" multiplied by the speed variable would, of course, result in "0" effectively shutting off the motor. I like to avoid using conditional statements when I can as it slows down program execution. Also I was a bit confused when you gave the assignment as to if you were expecting us to use the tilt switch and hard wire it to electrically break the circuit or use it as a sensor and use program control to shut it down. I opted for the later as I figured it was mostly a coding exercise. Thanks again. I'm learning a lot.
I put the tilt resistor in series with the fan power. Works great and doesn't require any change to the code. I know it's not in the spirit of the homework, but it's basically a 0ohm switch.
Yes, I did the homework and got it working before this episode. Used, basically, the same approach as in this episode.
I did the homework basically the same as you. The only difference I also put the motor directions in my if statements, which I realize now was unnecessary.
I was thinking about where a tilt switch is used and there is one in my portable heater that shuts it off to prevent fires. It also turns on a red light. Really useful information 👍
Yes I always do the homework and watch to the end :-) Great stuff incorporating earlier learned stuff.
As I tell my students, any homework I give you, I do myself. I did the homework because the only way to really learn is to try things on your own. Doing this is really helpful because a day will come when there is no one to guide you but yourself. Thanks for another great video.
Great job!
Completed homework on my own. Looking forward to the next tutorial. Thanks Paul!
My solution was essentially what you did, but with one difference: the motor is only turned on or off at most once per loop() iteration. That is, the motor is initially turned on in setup(). The logic in loop() uses logic similar to the logic you used in Tutorial 34 about using a pushbutton, where the previous button state is compared to the current button state to determine whether the button was toggled. Here we compare the previous tilt state to the current tilt state; if we went from not tilted to tilted, then turn the motor off; if we went from tilted to not tilted, then turn the motor on; otherwise, do nothing. Pseudocode for loop() follows.
***** Begin loop() pseudocode *****
read tiltVal
if (tiltValPrev == 0 && tiltVal == 1) then turn the motor off
else if (tiltValPrev == 1 && tiltVal == 0) then turn the motor on
endif
tiltValPrev=tiltVal
***** End loop() pseudocode *****
This avoids having to turn the motor on or off for every loop() iteration; however, it may violate "best practices" for Arduino coding.
For my own edification, I decided to jazz this up to add a pushbutton that, when toggled, turns the motor on or off; each toggle of the button either turns the motor on or turns it off. When tilted, toggling the button still keeps track of whether the motor is logically on or off, but does not actually turn it on or off. When the motor is no longer tilted, the motor is turned on or left off depending on (1) its state when it went from not tilted to tilted, and (2) how many motor state changes resulted from toggling the button while tilted. This mimics a small tabletop heater that has a "tilt detector" that shuts the heater off if it tilts over. One can turn the on/off switch as much as desired; when you the heater is eventually set upright, it will either be on or
off, depending on how the switch is set.
Way too much information! However, as a retiree, I am having a lot of fun following your tutorials. Keep up the good work!
Hi John, i am trying to do exactly what you have done but the code is getting me. only difference is i am using a limit switch instead of a tilt switch but i would think it would be the same. anychance you could share the code you used. very new to this.
I did the homework without a problem. Thanks once again Paul.
I completed the homework prior to watching this lesson. The logic was pretty straight forward. I used an If-Else statement to solve the logic. It worked great with very little issues. Thanks for the knowledge.
BOOOOOM!!! Did it all by myself, coded it a slightly different way. Tilt sensor wasn't working at first, narrowed it down to hardware issue through your excellent de-bugging training; moved it away from the columns occupied by L293D and giddy up!!!! Success!
Watched to the end, and your coding was almost exactly like mine! Thank you! Off to do more homework!
Excellent!
Paul Still only watching the complete videos. My goal is learning to READ arduino code. Future plan is to then return to the very first lesson and repeat each one in succession actually'
doing the activity. I do have the kit now and I am getting familiar with it as I watch. Normal mode is to watch to near the end and stop the video, depending on time available hold in that
state or go to the next one. Thank You, this is the first set of videos that I have stayed with.
Successfully completed the homework. Only difference is that I had the if statements only redefine the motor speed, then I still had the digitalWrite and analogWrite commands after.
Yes,I did the assignment myself, but make a mistake , finally figured it out in this video
Done and dusted. Added two motors due to the use of L298N board. Great tutorial yet again.
Nice work!
I'm definitely doing my homework. And I do watch all the way to the end of every episode .
Did my homework: A++ Up to the next one!
could do the HW from last episode no problem... I wanted to add more functions but my batteries die to fast , I ordered a lithium battery hopping this can help me get more creative.
I added a Servo and a joystick , I could control the speed and direction with the joystick .
very long code , I still cant believe it worked !!! thank you Mrs Paul.
Yes I did the homework in Lesson 37, but used the tilt sw to drive both IN1 and IN2 HIGH, to stop the motor.
Hey Mr.McWhorter, ,I did this homework on my own, worked perfectly! heres the code:
int dirPin1=4;
int dirPin2=3;
int mSpeed=255;
int tiltPin=2;
int tiltVal;
void setup() {
// put your setup code here, to run once:
pinMode(speedPin,OUTPUT);
pinMode(dirPin1,OUTPUT);
pinMode(dirPin2,OUTPUT);
Serial.begin(9600);
pinMode(tiltPin,INPUT);
digitalWrite(tiltPin,HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
tiltVal=digitalRead(tiltPin);
if(tiltVal==0){
analogWrite(speedPin,mSpeed);
digitalWrite(dirPin1,LOW);
digitalWrite(dirPin2,HIGH);
}
if(tiltVal==1){
analogWrite(speedPin,0);
digitalWrite(dirPin1,LOW);
digitalWrite(dirPin2,LOW);
}
Serial.println(tiltVal);
}
Brilliant!! I managed the tilt and stop homework on my own. The next project looks tricky though. Thanks.
Tried it hands on and was successful in completing my homework :) Thankyou so much!!!
I did my homework before watching the video worked great. Todays homework is going to be fun really like how you assign homework to test your viewers learning progression though these series of videos.
Great job!
Rose amd Ronald! I always watch all videos to the end and more than once!.
Did homework, but, simply put the tilt switch in series with Vcc. Thanks for another great lesson.
Did it on my own, sir. Thank you for all the lessons. 38 down. 20 more to goooooo!!!
*30 more to go lol. sorry for the typo.
I'm watching your videos up until the last second. Thank you so much for your work!
I did the HW before watching , and it was working, thanks to your great explanations about tilt function earlier! Thanks again, you are the perfect teacher.
Excellent!
I did my homework, added a offSpeed=0 for my if statement tiltVal==1. Maybe a little crude but it worked
Thank you Paul! I am happy to have you here!
Boom, would you look at that. Learning all things arduino with you is fun. Yes doin the home work, was delighted when the tilt I did in less lines. I did tiltPin,INPUT_PULLUP then one if (digitalRead tiltPin==HIGH){analogWrite (speedPin,0);} noooo don’t put numbers in the void.
Thank you from Ireland for all your hard work and expertise.
Assignment is completed!
Love your lessons and started to learn math because of your inspiration!
Thank you very much.
I used the tilt switch and also used the photo detector,. they work great together. The motor does slow down with low light and full speed at full light. Thank you Paul always a good day to watch your lessons.
yup, did homework and solved it......you are an excellent teacher, giddyup...
I did do the assignment and was successful. My code was like yours for the most part. One small difference.
I absolutely love these lessons. Lets have a "Yippy i yay" for Paul
Super challenging homework, but mostly was able to do it. Thanks for your great instruction!
Great job!
Homework done and now on to the next one! Thank you for the great lessons!
Yup. Did the hw and now going to do this one with the joystick. I'm so close to phase 3 which is to use all this and build a robot. I still love how you teach btw.
Easy - once I remembered to do the resistor trick on the tiltPin. :) I am very much enjoying this series.
Cool, thanks
Enjoyed, and yes I did do the Homework, not quite the same way you did, but still with success.
I connected the tilt switch to Arduino pin A0 to chip pin 5. If tilted the input becomes 0 and the motor stops. In real power motor applications, you would use a tilt tin in the control circuit, not direct power running the motor.
I realized that 512 was a starting point, so you would need two slopes, one positive, one negative. Two if statements.
We at starting in the middle of the X axis from 0 to 1023. 0 is the still, and 255 is the highest for mSpeed for the motor.
Wayne - - Moderately hard. Again having an old lesson open while working on a new lesson is better than from scratch.
Great teacher ! I have been watching your videos since a week and now I have reached this lesson. Wish you a great day for you and you granddaughter.
Using this to control an rc crawler thank you! Back to the servo and IR control lessons. Glad I did my homework and mapped my buttons.
Hi Paul, I looked the lesson until the end :-) ...and I made my homework! I appreciate your effort for teaching us very much! Thank you a lot!!!
I did my homework. I only added one if statement in my code, so that when tiltVal == 1, the command digitalWrite(speedPin, LOW) was executed. Thanks for the great lesson Paul!
I’m starting to think like you! As i was ending the homework for this lesson i thought, why not use a joystick to control the speed and maybe stick it on a servo so i can toggle the fan angle 😂….. this lesson ends and you say, now try using a joystick! Fantastic!
Loving these lessons! FYI, I noticed that Paul keeps referring to the trick of doing a digitalWrite to a digital input pin to have it activate a pullup resistor for that pin. I noticed that there is an easier way to do this and that the Arduino docs even recommends doing it this way:
When initializing the pin, simply use a command like this:
pinMode(3,INPUT_PULLUP);
This is effectively the same as specifying that the pin is an input pin AND doing the digitalWrite in a single command.
Interestingly, in the homework, I had my tilt pin set to output instead of input and it worked.
Of course i've done my homework. Always do! I even tried to go further including a joystick on my own but i'll have to wait for the next lecture to figure it out.
Hi Paul, I did the homework and got it to work. I wrote the same code as you created, but added two extra variables : mSpeedStart=250 & mSpeedStop=0 then I used these variables in the if statements, although mine worked, I wrote more unnecessary code. Thanks again Paul
Thank you again, I added a Red & Green LED to indicate Up or Down Status.
Great job!
My version was about 90% the same as your version including pin and variable names. I lean toward the if/else versus the if/if approach. Good lesson, getting more comfortable with the thought process for the design. Keep it up, thanks.
Yep, did the HWrk, and used 2 x If's (not an 'else') , Like you advised before... but used 'digitalRead' not 'analogRead' off pin 2. worked a treat ...
I did the homework and was yelling at you for the analogWrite but you ignored me. Haha. Only difference is that I use and else statement instead of a second if statement. Thanks again.
Excellent!
Boom, done already on my own from homework assignment. you are an excellent teacher. thanks so much.
Rose and Ronald, another good lesson thanks Paul.
Did my homework. It worked perfectly.
The word is rose. I listen to all luctures
Keyword Rose and Ronald
Another great lesson, I am planning to use a DC motor to drive my observatory dome using mirror tiles fitted every 10 degrees around the dome and for position control with infrared photo sensors. I have learnt a great deal so far
did the homework, love that I just know how to do it. It's all thanks to you, my good sir.
Hey Paul, I sure did the home work plus I added the green and red LED to show a visual warning if the fan tipped over.
I was able to complete the tilt switch stop homework, I used 1 while loop rather than 2 if commands like you did though.
Did the homework on my own👌. My code was a bit different but it worked the same! Used and if, else statement 👍🏻.
I did the homework but was unsuccessful on several levels. I had bad connections because of loose wires, wrong pin connections caused by troubleshooting the loose wires and then bad coding because of it all. In my own defense, my original code was very similar to what you did, however, my tilt switch did intermittent 1's and 0's, even when the tilt switch was removed from the board. I thought I had a defective switch but an Ohm test showed it was good. The final result was I had the switch plugged into the tX1 connection instead of pin 2. My vision isn't good enough to see these tiny numbers and wires. But hey, I don't have enough hair anymore to worry about pulling it out!
Yes sir.....I am able to do the homework....I am eagerly waiting for the next Tuesday at 9:30 pm in India....Thank you.
Yes did the homework and worked great. I like the fact you make mistakes (whether deliberate or not) as this makes us analyse the issue.
I did the homework before the lesson. A difference is that I didn't set the speed to zero to stop the fan, I set both direction pins to "LOW". Also, I added the LEDs from the tilt switch lesson. So, when the fan is running, the green LED also lights up, and when the fan is stopped, the red LED lights up.
I did the assignment after few corrections it work OK. After I put the decimal point everything work fine. Thanks again.
Did the homework between lessons. Got it working, after watching you code it, I had to look over my shoulder to see if you had watched me write the code, almost identical right down to variable names. 😀
Did the homework and got it to work. Only thing I did differently were which pins I used and instead of a analogRead I did a digitalRead.