HOW To Determine The End Of A Thing
Description
The collision sensor uses a tact switch. This sensor is often used as a limit switch in 3D printers. In the experiment, we judge whether the sensor shrapnel is pressed down by reading the high and low levels of the S terminal on the module; and, we display the test results in the shell.
Working Principle
It mainly uses a tact switch. When the shrapnel of the tact switch is pressed, 2 and 3 are connected, the signal terminal S is low level, and the red LED on the module lights up; when the touch switch is not pressed, 2 and 3 are not connected, and 3 is pulled up to a high level by the 4.7K resistor R1, that is, the sensor signal terminal S is a high level, and the built-in red LED will be off at this time.
Components Required
Connection Diagram
Test Code
//*************************************************************************************
/*
* Description : Reading the value of the collision sensor
* Auther : http://www.keyestudio.com
*/
int val = 0;
void setup() {
Serial.begin(9600);//Set baud rate to 9600
pinMode(15, INPUT);//Set collision sensor pin 15 to input mode
}
void loop() {
val = digitalRead(15);//Read the value of the collision sensor
Serial.print(val);//Newline print
if (val == 0) {//Collision val is 0
Serial.print(" ");
Serial.println("The end of his!");
delay(100);
}
else {// No collision val is 1
Serial.print(" ");
Serial.println("All going well");
delay(100);
}
}
//*************************************************************************************
Test Result
Connect the wires according to the experimental wiring diagram, compile and upload the code to the ESP32. After uploading successfully,we will use a USB cable to power on,open the serial monitor and set the baud rate to 9600. The serial monitor will display the corresponding data and characters.
In the experiment, when the shrapnel on the sensor is pressed down, val is 0, the red LED of the module is on, and “0 The end of his!” is printed; when the shrapnel is released, the val is 1, the red LED of the module is off, and “1 All going well” is printed. !” character, as shown below.