How to Use Tilt and Vibration Sensors on the Arduino - Ultimate Guide to the Arduino #33

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] we're going to learn about tilt and knock sensors tilt sensors are commonly used in remote controlled vehicles to detect when an incline is too steep knock sensors can be used to detect closing doors or collisions in remote controlled vehicles tilt sensors output a high or low signal when the sensor is oriented at a certain angle they can only detect if the sensor is tilted or not they can't measure the exact tilt angle like accelerometers tilt sensors are made up of a plastic tube with two pieces of wire inside of it one wire is red for positive the other wire is black for negative and much shorter than the red wire there's also a metal ball inside the tube when the tube is tilted forward the ball rolls forward and only contacts the red wire foreign backwards the ball rolls back and contacts both wires this allows current to flow between the two wires we can detect this high or low signal with the Arduino to see if the sensor is tilted or not shock sensors and knock sensors are also pretty simple shock sensors output a brief high or low signal when a shock or jolt is detected inside the sensing element there's a stiff rod that's connected to positive represented by the red vertical line in this diagram then there's a spring around the rod that's attached to ground when the sensor is jolted the spring moves and contacts the rod this allows current to flow between the rod and the spring in this video we're going to be working with two common tilt sensors and two common shock sensors the two tilt sensors we'll be setting up are the keys ky020 of till switch and the keys ky017 Mercury tilt switch it's ky017 Mercury tilt switch the Mercury tilt switch has a small drop of liquid mercury inside a glass bulb Mercury is electrically conductive so when the sensor is tilted back the Mercury contacts two Terminals and allows current to flow between them when the sensor is tilted forward the Mercury loses contact with one of the terminals and stops the current flow Mercury switches are more sensitive and reliable than the metal ball tilt sensors but Mercury is toxic so don't use them in high impact situations where there's a risk of breaking the glass this is a keys ky020 tilt switch inside this blue chamber is a metal ball that rolls back and forth depending on the orientation of the sensor the ball causes two electrical contacts inside the chamber to be either open or closed all the Tilt and vibration sensors we're going to see have just three pins the pin with the S is the signal pin the next pin is the VCC pin and the pin with the minus sign is the ground pin some tilt sensors have the pins in different locations for example the Tilt and Mercury switches have the signal and ground pins reversed but they will always have just three pins signal VCC and ground the two vibration sensors we'll be using are the ky031 knock sensor and the ky002 shock sensor here's the knock sensor inside this plastic case is a conductive wire coil when the sensor receives the jolt the coil flexes and contacts and Electrode which allows current to flow between the coil and the electrode shock sensors work similarly but with this particular sensor there's a metal sleeve that wraps around the coil a jolt or vibration causes the coil to flex which creates an electrical contact between the coil and the metal sleeve okay now let's see how to set up tilt and shock sensors let's start by building a tilt sensor circuit that turns on an LED when the sensor is tilted forward and turns on another LED when it's tilted backwards here's how to connect a Mercury switch to the Arduino the ground pin connects to ground and the VCC pin connects to 5 volts the signal pin connects to Arduino pin 10. I also have a red LED connected to pin 12 via current limiting resistor this led will line up when the sensor is tilted forward then I have a green LED connected to pin 11 with another current limiting resistor this led will light up when the sensor is tilted backwards if you're using a metal ball tilt switch connect it like this Mercury and metal ball tilt switches have different pin layouts the only difference is the layout of the sensor pins ground still connects to ground VCC connects to 5 volts and the signal pin connects to Arduino pin 10. now let's take a look at the sketch in this sketch we're going to turn on the red LED when the sensor is tilted forward and turn on the green LED when the sensor is tilted backwards so let's declare a few pin variables one for the sensor pin pin 10 one for the forward LED pin 12. and one for the reverse LED pin 11. in the setup section we set the pin modes the sensor pin is an input and the forward and reverse LEDs are outputs in the loop section we take a digital read from the sensor pin and store it in a local variable called read next we use a couple if statements to control what happens when the sensor pin goes high and low Mercury tilt switches output a high signal when tilted forward and a low signal when tilted back so the first if statement says if read equals High digital write the forward LED High and digital write the reverse LED low the second if statement says if read equals low digital write the reverse LED High and digital write the forward LED low so this turns the forward LED on when the sensor is tilted forward and this turns on the reverse LED when the sensor is tilted back let's upload this and see if it works so tilting the sensor forward lights up the forward LED and tilting it back lights up the reverse LED s are just a good way to demonstrate what's happening with the arduino's output pins instead of an LED you could connect any other device that's activated by a high or low signal if you're using the metal ball type of tilt switch the sketch is similar But Metal ball tilt switches output a low signal when tilted forward and a high signal when tilted back the signals are reversed from the Mercury switch so we have to change the conditions of the if statements instead of writing the forward LED High when the reading from the sensor pin is high you need to write it High when the sensor pin is low so I'm going to change this to low also instead of writing the reverse LED High when the sensor pin is low you need to ride it high when the sensor pin is high okay let's see what this looks like tilting the sensor forward lights up the forward LED and tilting it backwards lights up the reverse LED you can see that this sensor is less responsive than the Mercury switch it needs more angle to get the ball to roll down okay now let's check out how to set up shock sensors and knock sensors I'm going to demonstrate with the circuit that lights up an LED when the sensor detects a knock these sensors both have the same pin connections they also both output a high signal lit rest and a low signal when they experience a shock for that reason I'm just going to show you how to wire and program the shock sensor the signal pin of the shock sensor connects to Arduino pin 10. the VCC and ground pins connect to five volts and ground have an LED connected to Arduino pin 12 via a current limiting resistor in this sketch we're going to light up the LED for one second when the sensor is activated these sensors output a high signal at rest and a low signal when jolted so the sketch uses an if else statement to digital write the LED High when the sensor pin goes low up top here we have pin variables for our sensor pin pin 10. and our LED pin pin 12. we set the pin modes in the setup section the sensor pin is an input and the LED pin is an output in the loop we take a digital read of the sensor pin and store the value in the read variable then we have an if else statement the condition says if read equals low then digital write the LED pin High and delay for 1000 milliseconds if read is not low in other words it's high then the else statement gets executed and we digital write the LED pin low so the if statement turns on the LED when the sensor outputs a low signal and the else statement turns off the LED when the sensor outputs a high signal so if I tap the shock sensor the LED switch is on for a second it's actually pretty sensitive here's the knock sensor running the same sketch it takes quite a bit more Force to activate this one I have to knock it pretty hard the way you position these sensors will have an impact on their sensitivity be sure to try different orientations to get the response you need in the next video we're going to take a look at ultrasonic rangefinders and how you can use them to measure the distance to an object [Music] thank you the three in one smart car and iot learning kit from Sun founder is a Hands-On all included Electronics kit that is perfect for anyone who wants to learn how to master the Arduino the kit comes with an Arduino 22 different sensors and modules thread boards jumper wires and everything else you need to build a bunch of fun and interesting projects learn about robotics by building a remote controlled smart car that can be controlled with an infrared remote controller or drive on its own and avoid obstacles or fall on the line learn about the internet of things with a project that lets you monitor the temperature humidity and light level of a room from an app on your smartphone [Music] and build a plant monitor that tracks the temperature humidity light intensity and soil moisture and displays it on your smartphone so you can keep your plants water remotely it's a super cool kit and I have lots of fun building all the projects in it so click the link in the description below to order the kit from some founder
Info
Channel: Circuit Basics
Views: 7,560
Rating: undefined out of 5
Keywords: arduino, raspberry pi, circuit projects, electronics projects, circuits, circuit basics, arduino projects, raspberry pi projects, IOT, robotics
Id: aWSj7CrA6l0
Channel Id: undefined
Length: 14min 17sec (857 seconds)
Published: Fri Jun 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.