External Sensors

BMP 280

These are the component required for the tutorial.

  1. ESP-32
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>#define BMP_SCK (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS (10)Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);void setup() {
while ( !Serial ) delay(100); // wait for native usb
Serial.println(F("BMP280 test"));
unsigned status;
//status = bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID);
status = bmp.begin(0x76);
if (!status) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
"try a different address!"));
Serial.print("SensorID was: 0x"); Serial.println(bmp.sensorID(),16);
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Serial.print(" ID of 0x61 represents a BME 680.\n");
while (1) delay(10);
}/* Default settings from datasheet. */
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}void loop() {
Serial.print(F("Temperature = "));
Serial.println(" *C");Serial.print(F("Pressure = "));
Serial.println(" Pa");Serial.print(F("Approx altitude = "));
Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
Serial.println(" m");Serial.println();

Variations of Ultrasonic Sensor HC-SR04

Let’s move on to the second tutorial, which is a variation on the use of the HC-SR04 ultrasonic sensor. The ultrasonic sensor type HCSR04 is a device used to measure the distance from an object. The range of the measurable distance is about 2–450 cm. These are the component required for the tutorial.

  1. Ultrasonic sensor HC-SR04
int led = 23; // set LED pinconst int trigPin = 26;const int echoPin = 27;const int buzz = 25; // set buzzer//define sound speed in cm/uS#define SOUND_SPEED 0.034long duration;float dist;void setup() {Serial.begin(115200); // Starts the serial communicationpinMode(trigPin, OUTPUT); // Sets the trigPin as an OutputpinMode(echoPin, INPUT); // Sets the echoPin as an InputpinMode(buzz, OUTPUT);pinMode(led, OUTPUT);}void loop() {// Clears the trigPindigitalWrite(trigPin, LOW);delayMicroseconds(2);// Sets the trigPin on HIGH state for 10 micro secondsdigitalWrite(trigPin, HIGH);delayMicroseconds(10);digitalWrite(trigPin, LOW);// Reads the echoPin, returns the sound wave travel time in microsecondsduration = pulseIn(echoPin, HIGH);// Calculate the distancedist = duration * SOUND_SPEED/2;Serial.print("Distance (cm): ");Serial.println(dist);// Prints the distance in the Serial Monitor use the output to indicate condition per distanceif (dist > 30){Serial.print("Distance (cm): ");Serial.println(dist);Serial.println("Nothing :(");digitalWrite(buzz, LOW);digitalWrite(led, LOW);}else if (dist <= 30 && dist > 20){Serial.print("Distance (cm): ");Serial.println(dist);Serial.println("Ada objek yang terdeteksi");digitalWrite(buzz, LOW);digitalWrite(led, HIGH);delay(1000);digitalWrite(led, LOW);delay(1000);}else if (dist <= 20 && dist > 10){Serial.print("Distance (cm): ");Serial.println(dist);Serial.println("Sudah dekat!");digitalWrite(buzz, LOW);digitalWrite(led, HIGH);delay(100);digitalWrite(led, LOW);delay(100);}else {Serial.print("Distance (cm): ");Serial.println(dist);Serial.println("terlalu dekat :(");digitalWrite(buzz, HIGH);digitalWrite(led, HIGH);}delay(500);}

So here is the end of my tutorial, hope you enjoy!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andreana Hartadi Suliman

Andreana Hartadi Suliman

If we are afraid of failure, it means we have limited our ability. — Hi, nice to see you here. Have a good day!