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
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
Post a Comment