![]() Once the sensor and LED are connected to the Arduino and the code is uploaded, tap the sensor on a table. This turns off the LED when the sensor outputs a high signal. If read has a high value, the else statement is executed and we digital write ledPin low. This turns the LED on for one second when the sensor outputs a low signal. The if statement says “if read is low, digital write the ledPin high and delay for 1,000 milliseconds. This tells the Arduino to detect the voltage at pin 10 and store the high or low value in the read variable. In the loop() section we declare a local int variable called read, and set it equal to the digital read of the sensorPin. The sensorPin variable is an INPUT and the ledPin is an OUTPUT. In the setup() section we set the pin modes for the pin variables. The LED is connected to Arduino pin 12, so ledPin is set equal to 12. The ledPin variable will store the high or low value used to turn the LED on or off. Since the signal pin is connected to Arduino pin 10, sensorPin is set equal to 10. ![]() The sensorPin variable will store the high or low value detected at the sensor’s signal pin. The sketch works for both shock sensors and knock sensors: int sensorPin = 10 Īt the top of the sketch, we declare two pin variables. The sketch below will turn the LED on for one second when the sensor is activated. Shock sensors and knock sensors both output a high signal at rest and a low signal when jolted. Shock sensors and knock sensors have the same pin connections, so the wiring diagram below can be followed for either sensor: How to Program Shock Sensors and Knock Sensors on the Arduino Any device controlled with a 5V signal can be activated by this circuit. LEDs are just an easy way to demonstrate the circuit. Let’s see how to connect shock sensors and knock sensors to the Arduino with an example project that lights up an LED when the sensor detects a knock. A strong jolt or vibration causes the spring to bend and make an electrical contact with the rod, which allows current to flow between the rod and the spring: How to Connect Shock Sensors and Knock Sensors to the Arduino Inside the black tube, a metal spring attached to ground wraps around a metal rod that is connected to a positive voltage. When the sensor receives a jolt, the coil flexes and contacts an electrode that allows current to flow between the coil and the electrode: How Shock Sensors Work Inside the plastic case is a conductive wire coil. The pin with the “S” is the signal pin, the pin with the “-” connects to ground, and the middle pin connects to 5V.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |