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?