External Sensor with ESP32

Hello everyone, I'm back!! This week I'll explain about using an external sensor with ESP32. For this project, I'll be using an ultrasonic sensor. The sensor looks like this:


This sensor uses sonar to determine the distance of an object to the sensor. This is how the sensor works:

1. The ultrasound transmitter (trig pin) will emit a high-frequency sound (40 kHz)

2. If the sound finds an object, it will bounce back to the sensor.

3. The ultrasound receiver (echo pin) receives the reflected sound.

cr: randomnerdtutorials

Basically, the sensor sends sound waves and receives the echo. But how could we know its distance?

We can calculate the distance to an object by using this formula:

distance to an object = ((speed of sound in the air)*time)/2

Now, let's start experimenting with the sensor!

First, prepare the components needed:
1. ESP32
2. Ultrasonic sensor

3. Breadboard
4. Male-to-male jumpers
5. Micro USB
6. Laptop/PC


You also need to install the software needed. You can refer to this post.

Then, we'll arrange the circuitry. This is how I arrange the circuitry for today's project. I'll be using GPIO 27 to connect with the Trig pin and GPIO 26 to connect with the Echo pin. You can also use any other suitable pins.


Then we'll move on to the Arduino IDE. 
1. Open arduino.exe
2. For the code, I'm using this one from randomnerdtutorials.
/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-hc-sr04-ultrasonic-arduino/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*********/

const int trigPin = 5;
const int echoPin = 18;

//define sound speed in cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701

long duration;
float distanceCm;
float distanceInch;

void setup() {
  Serial.begin(115200); // Starts the serial communication
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}

void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  
  // Calculate the distance
  distanceCm = duration * SOUND_SPEED/2;
  
  // Convert to inches
  distanceInch = distanceCm * CM_TO_INCH;
  
  // Prints the distance in the Serial Monitor
  Serial.print("Distance (cm): ");
  Serial.println(distanceCm);
  Serial.print("Distance (inch): ");
  Serial.println(distanceInch);
  
  delay(1000);
}

3. Connect the ESP32 and the Laptop/PC using a micro USB cable. 

4. Upload the code to the board by clicking this button.


5. When you're done uploading, you'll see "Done uploading" in arduino.exe


6. Open Tools>Serial Monitor, set the baud rate to 115200 baud. You'll see the status of the closest object detected by the sensor over time.

Easy right?


Let's try a little experiment with the sensor.

1. Prepare some 5mm LEDs and 330 ohm resistors.



2. Arrange the circuitry like this.

3. Use this code, I just add a little this and that to the code before to set up the LEDs and their conditionals.

const int trigPin = 27;

const int echoPin = 26;

const int led1 = 25;

const int led2 = 33;

const int led3 = 32;

const int threshold1 = 4;

const int threshold2 = 10;


//define sound speed in cm/uS

#define SOUND_SPEED 0.034

#define CM_TO_INCH 0.393701


long duration;

float distanceCm;

float distanceInch;


void setup() {

  Serial.begin(115200); // Starts the serial communication

  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output

  pinMode(echoPin, INPUT); // Sets the echoPin as an Input

  pinMode(led1, OUTPUT);

  pinMode(led2, OUTPUT);

  pinMode(led3, OUTPUT);

}


void loop() {

  // Clears the trigPin

  digitalWrite(trigPin, LOW);

  delayMicroseconds(2);

  // Sets the trigPin on HIGH state for 10 micro seconds

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);

  

  // Reads the echoPin, returns the sound wave travel time in microseconds

  duration = pulseIn(echoPin, HIGH);

  

  // Calculate the distance

  distanceCm = duration * SOUND_SPEED/2;

  

  // Convert to inches

  distanceInch = distanceCm * CM_TO_INCH;

  

  // Prints the distance in the Serial Monitor

  Serial.print("Distance (cm): ");

  Serial.println(distanceCm);

  Serial.print("Distance (inch): ");

  Serial.println(distanceInch);


  // 

  if (distanceCm < threshold1 && distanceCm < threshold2) {

    Serial.println("so close!");

    digitalWrite(led1, HIGH);

    digitalWrite(led2, LOW);

    digitalWrite(led3, LOW);

  } else if (distanceCm > threshold1 && distanceCm < threshold2){

    Serial.println("close enough");

    digitalWrite(led1, LOW);

    digitalWrite(led2, HIGH);

    digitalWrite(led3, LOW);

  } else {

    Serial.println("closer!!");

    digitalWrite(led1, LOW);

    digitalWrite(led2, LOW);

    digitalWrite(led3, HIGH);

  }

  

  delay(1000);

}

4. Connect the ESP32 board with the laptop using jumper wires.

5. Upload the code to the board.

6. You can see the status in the Tools>Serial Monitor and set the baud rate to 115200 baud.



7. Voila! The LED will turn on according to how close the object is to the sensor.


That's it for this week~ Hope this helps!




Comments

Popular posts from this blog

ESP32 OLED Display and ESP32 PWM

Blinking LED on ESP32

ESP32 I2C Serial Communication