Firebeetle ESP32 vs Arduino Nano IoT 33 coding difference

Hi
I'm trying to use the code that worked with my Nano IoT 33 (Arduino Nano 33 IoT — Arduino Official Store) on the FireBeetle ESP32 (FireBeetle 2 ESP32-E IoT Microcontroller with Header - DFR0654-F | DFRobot Electronics). I'm using a 3,3V HC-SR04 sensor to measure distance, it works on the Nano, but not on the FireBeetle.

// Variables used for Distance monitoring
#define ULTRASONIC_TRIG_PIN D13 // pin TRIG is D11
#define ULTRASONIC_ECHO_PIN D11 // pin ECHO is D12
int trigPin = 13;    // Trigger
int echoPin = 11;    // Echo

// MAIN SETUP
void setup() {
  // set pin modes
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

// MAIN LOOP
void loop() {
  long duration, firebeetle_distance;
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  Serial.println("Duration is");
  Serial.println(duration);
  Serial.println("");

  // Convert the time into a distance
  firebeetle_distance = (duration / 2) / 29.1;   // Divide by 29.1 or multiply by 0.0343
  Serial.println("Distance is");
  Serial.print(firebeetle_distance);
  Serial.println("");

On the Nano I get correct results, but on the FireBeetle I get:
13:45:18.503 -> Duration is
13:45:18.503 -> 2
13:45:18.503 ->
13:45:18.503 -> Distance is
13:45:18.503 -> 0

or
13:32:46.965 -> Duration is
13:32:46.965 -> 1
13:32:46.965 ->
13:32:46.965 -> Distance is
13:32:46.965 -> 0

What am I doing wrong?

what is the result of (2/2)/29.1? 0.something right? and what happens if you assign this to long int? you lose the fraction part so the result is 0. What is not working?

no, it should actually be a full number, rounded up. so from the Nano I get 2, 10, whatever number of cm the distance is from the obstruction the sensor detects.
as to the long int, I thought this would be done above, check this line in the original code block:
long duration, firebeetle_distance;

Wait what? Never mind, I dont have patience, let someone else explain this to you.

Found this, ESP32 with HC-SR04 Ultrasonic Sensor with Arduino IDE | Random Nerd Tutorials. Here it has the distance variable set as float, otherwise everything should be fine.

Same exact code on the Nano produced this:
image

@killzone_kid I just realized I read over your (2/2)/29.1 <- I missed the first 2. The problem is that the distance is not 2 :slight_smile:
From the code point of view yes, it works. I was thinking maybe I'm making some other mistake, like defining variables on the Firebeetle or something similar.

You are printing distance, and it is what it is, in you case it is 2 and 1. pulsIn returns microseconds, right? So maybe you are reading the wrong pin

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