Ultrasonic sensor arduino test code12/5/2023 Here is the formula: Distance echo signal high time. ![]() The pulse width of the echo signal is proportional to the measured distance. When we provide a pulse trigger signal with more than 10uS through signal pin, the GroveUltrasonicRanger will issue 8 cycles of 40kHz cycle level and detect the echo. The only problem now is the redLed stays on the whole time. This Grove - Ultrasonic ranger is a non-contact distance measurement module which works at 40KHz. Ok i wrote a digitalWrite LOW to turn off the greenLed when i get less than 40 (which is correct, i also want the redLed to come on when the distance is less than 40 and the greenLed turns off) If (distance != 0) // less than 40 and exclude 0 readingsĪsk yourself: when the distance is > 40 does the red led needs to be on or off ? If it needs to be off, then you need to let the pin know. Lol, ok here is my new sketch, it compiles but now when i turn on the Arduino and put my hand to the ultrasonic sensor both led lights go on and stay on, im guessing that i am missing a digital write LOW, but im trying to follow your format. If (distance < 40 & distance != 0) // less than 40 and exclude 0 readings Serial.print(distance) // Send ping, get distance in cm and print result (0 = outside set distance range) ![]() 29ms should be the shortest delay between pings. Serial.begin(115200) // Open serial monitor at 115200 baud to see ping results.ĭelay(50) // Wait 50ms between pings (about 20 pings/sec). NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE) // NewPing setup of pins and maximum distance. Maximum sensor distance is rated at 400-500cm. #define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). #define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor. #define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor. The code actually works, and I am very surprised because I am not very good at modifying the code.Įven though it works, I am not sure that I wrote it the best way, can I have someone tell me if it looks ok? #include I have modified the code again to say that when the distance is above 40 turns on Green led as well. Hello, A while back I took code that measured the distance with the HC-Sr04 sensor and my Arduino Nana would activate a Red Led when the distance is less than > 40 and not equal (=) to zero.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |