12/9/24, 9:52 a.m.
06-ultrasonic_copy-2 — Arduino Cloud Editor
                                                   SELECT DEVICE                                                 Serial Monitor
                               06-ultrasonic_copy-2.ino
                      1    /*
                      2         This code read the distance from an ultrasonic sensor and display it on
                      3         an LCD screen. It uses an Arduino Uno R4 board and a 16x2 Liquid Crystal
                      4         Display (LCD) connected via I2C. The ultrasonic sensor is connected to
                      5         pins 3 and 4 for echo and trigger, respectively.
                      6
                      7         Board: Arduino Uno R4
                      8         Component: Ultrasonic Module
                      9    */
                     10
                     11    #include <LiquidCrystal_I2C.h>
                     12
                     13    LiquidCrystal_I2C lcd(0x27, 16, 2);          // initialize the Liquid Crystal Display object with the
                           I2C address 0x27, 16 columns and 2 rows
                     14
                     15    // Define the pin numbers for the ultrasonic sensor
                     16    const int echoPin = 3;
                     17    const int trigPin = 4;
                     18
                     19    void setup() {
                     20         pinMode(echoPin, INPUT);      // Set echo pin as input
                     21         pinMode(trigPin, OUTPUT);     // Set trig pin as output
                     22
                     23         lcd.init();         // initialize the LCD
                     24         lcd.clear();        // clear the LCD display
                     25         lcd.backlight();    // Make sure backlight is on
                     26    }
                     27
                     28    void loop() {
                     29         float distance = readDistance();       // Call the function to read the sensor data and get the
                           distance
                     30
                     31         lcd.setCursor(0, 0);                //Place the cursor at Line 1, Column 1. From here the
                           characters are to be displayed
                     32         lcd.print("Distance:");            ////Print Distance: on the LCD
                     33         lcd.setCursor(0, 1);                //Set the cursor at Line 1, Column 0
                     34         lcd.print("                 ");    //Here is to leave some spaces after the characters so as to
                           clear the previous characters that may still remain.
                     35         lcd.setCursor(7, 1);                //Set the cursor at Line 1, Column 7.
                     36         lcd.print(distance);               // print on the LCD the value of the distance converted from
                           the time between ping sending and receiving.
                     37         lcd.setCursor(14, 1);               //Set the cursor at Line 1, Column 14.
                     38         lcd.print("cm");                   //print the unit "cm"
                     39
                     40         delay(800);    // Delay for 800 milliseconds before repeating the loop
                     41    }
                     42
                     43    // Function to read the sensor data and calculate the distance
                     44    float readDistance() {
                     45         digitalWrite(trigPin, LOW);        // Set trig pin to low to ensure a clean pulse
                     46         delayMicroseconds(2);             // Delay for 2 microseconds
                     47         digitalWrite(trigPin, HIGH);       // Send a 10 microsecond pulse by setting trig pin to high
                     48         delayMicroseconds(10);
                     49         digitalWrite(trigPin, LOW);       // Set trig pin back to low
                     50
                     51         // Measure the pulse width of the echo pin and calculate the distance value
                     52         float distance = pulseIn(echoPin, HIGH) / 58.00;        // Formula: (340m/s * 1us) / 2
                     53         return distance;
                     54    }
                     55
                     Console
https://app.arduino.cc/sketches/9dff8934-29b5-4872-8d5e-d24102ae7a73?ui=fs                                                         1/2
12/9/24, 9:52 a.m.                                             06-ultrasonic_copy-2 — Arduino Cloud Editor
https://app.arduino.cc/sketches/9dff8934-29b5-4872-8d5e-d24102ae7a73?ui=fs                                   2/2