Sonar HC-SR04 verification issue

Hello everyone.
HARDWARE:

  • Arduino Uno
  • HC-SR04 Sonar
    SOFTWARE:
  • Arduino 2.0.0-rc3

I have the following code however when I am trying to verify, i get the following error messages.

#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04


// defines variables
long duration = 0; // variable for the duration of sound wave travel
int distance  = 0; // variable for the distance measurement

void setup() {
  pinMode(trigPin,OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin,INPUT); // Sets the echoPin as an INPUT
  Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
  Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
  Serial.println("with Arduino UNO R3");
}

void loop() {
  // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
  // Displays the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
}

The errors I get:

/home/siou/Documents/Programming/Arduino/sonar_HC-SR04/sonar_HC-SR04/ultrasonic_sensor_hc_sr04_with_arduino_code_for_ranging_test.c: In function 'setup':
/home/siou/Documents/Programming/Arduino/sonar_HC-SR04/sonar_HC-SR04/ultrasonic_sensor_hc_sr04_with_arduino_code_for_ranging_test.c:13:20: error: 'OUTPUT' undeclared (first use in this function)
   pinMode(trigPin, OUTPUT);
                    ^~~~~~
/home/siou/Documents/Programming/Arduino/sonar_HC-SR04/sonar_HC-SR04/ultrasonic_sensor_hc_sr04_with_arduino_code_for_ranging_test.c:13:20: note: each undeclared identifier is reported only once for each function it appears in
/home/siou/Documents/Programming/Arduino/sonar_HC-SR04/sonar_HC-SR04/ultrasonic_sensor_hc_sr04_with_arduino_code_for_ranging_test.c:14:20: error: 'INPUT' undeclared (first use in this function); did you mean 'OUTPUT'?
   pinMode(echoPin, INPUT);
                    ^~~~~
                    OUTPUT
/home/siou/Documents/Programming/Arduino/sonar_HC-SR04/sonar_HC-SR04/ultrasonic_sensor_hc_sr04_with_arduino_code_for_ranging_test.c:17:3: error: 'Serial' undeclared (first use in this function)
   Serial.begin(9600);
   ^~~~~~
/home/siou/Documents/Programming/Arduino/sonar_HC-SR04/sonar_HC-SR04/ultrasonic_sensor_hc_sr04_with_arduino_code_for_ranging_test.c: In function 'loop':
/home/siou/Documents/Programming/Arduino/sonar_HC-SR04/sonar_HC-SR04/ultrasonic_sensor_hc_sr04_with_arduino_code_for_ranging_test.c:22:25: error: 'LOW' undeclared (first use in this function)
   digitalWrite(trigPin, LOW);
                         ^~~
/home/siou/Documents/Programming/Arduino/sonar_HC-SR04/sonar_HC-SR04/ultrasonic_sensor_hc_sr04_with_arduino_code_for_ranging_test.c:26:25: error: 'HIGH' undeclared (first use in this function)
   digitalWrite(trigPin, HIGH);
                         ^~~~
/home/siou/Documents/Programming/Arduino/sonar_HC-SR04/sonar_HC-SR04/ultrasonic_sensor_hc_sr04_with_arduino_code_for_ranging_test.c:36:3: error: 'Serial' undeclared (first use in this function)
   Serial.print("Distance = ");
   ^~~~~~
Compilation error: exit status 1}

code compiles OK for a UNO using IDE 1.8.13

have you set Tools>Board correctly?

1 Like

Yes I have set the correct board to Arduino UNO.
Port option is dev/ttyACM0.
I guess I will have to install previous version.
Thank you

Has 2.0.0 been ported to anything besides Windoze yet?

I guess it must be, otherwise @msiou could not be using it on Linux.

@msiou it was probably not wise to install 2.0.0-rc3 at this time. You need to be an experienced user and be familiar with using 1.8.x on Linux in order to be confident enough to try to use any release candidate before it is officially released.

I suggest you click the flag icon on your original post, select "other" and ask the forum moderators to move this topic to the forum section for 2.0.0 issues. Then maybe your issue will get noticed by the team that can fix it. Once you have done that, uninstall 2.0.0 and install 1.8.x.

1 Like

Compiles fine for me on Arduino 1.8.19.

Copy that Sir...
I installed later version.
Every is ok

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.