/* this code will help to print the distance from the data to serial monitor, readings will be taken by ultrasonic sensor*/ #include // ULTRASONIC LIBRARY #include // include SOFTWERE SERIAL library above, with software serial any two I/O pins can be run as Rx, Tx SoftwareSerial Serial(0, 1); // RX, TX // to send and receive the data to serial port(Monitor) Ultrasonic ultrasonic(7, 8); // ultrasonic pins defined ( triger(5) and echo(4)), echo pin will send sound waves and triger will receive sound signals int distance; // defining variable void setup() { pinMode(2,OUTPUT); Serial.begin(115200); } void loop() { // Pass INC as a parameter to get the distance in inches distance = ultrasonic.read(); // reading values Serial.print("Distance in CM: "); Serial.println(distance); // printing distance on serial delay(1000); // if else condition, if condition is fulfilled then, led will High, if no it will be low. if(distance <= 50) { digitalWrite(2,HIGH); } else { digitalWrite(2,LOW); } }