The setup function initializes these pins, while the loop continuously measures distances using the Ultrasonic Sensor and turns an LED on or off based on the measured distance. This Arduino sketch defines pins for trigger, echo, and an LED. Serial.print(distance) //print the distance value on serial port Long duration, distance // variable to hold the duration and distance value for HC-SR04ĭigitalWrite(trigPin, LOW) // Write trigger pin is as lowĭelayMicroseconds(2) // Delay for 2microsecondsĭigitalWrite(trigPin, HIGH) //Write trigger pin is as highĭelayMicroseconds(10) // Delay for 10microsecondsĭigitalWrite(trigPin, LOW) //Write trigger pin is as lowĭuration = pulseIn(echoPin, HIGH) //Read the echo pinĭistance = (duration / 2) / 29.1 // calculate the distance PinMode(led, OUTPUT) //Define LED pin as output PinMode(echoPin, INPUT) //Define Echo pin as input Find zip file links for all Arduino boards and versions of Proteus library. PinMode(trigPin, OUTPUT) //Define Trigger pin as output Learn how to download Proteus library of Arduino modules, including Arduino UNO, Mega 2560, Mega 1280, Mini, Nano, Pro Mini and Library. Serial.begin (9600) //Begin serial monitor with baud rate 9600 #define led 12 //Initialize pin12 for LED #define echoPin 8 // Initialize pin8 for HC-SR04 Echo Arduino Sketch #define trigPin 9 // Initialize pin9 for HC-SR04 Trigger
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |