Chow Liang Zhi
Published

Proximity sensor for the hearing-impaired

An Arduino proximity sensor that uses visual cues to display the proximity to an obstacle.

BeginnerFull instructions provided4 hours40
Proximity sensor for the hearing-impaired

Things used in this project

Hardware components

Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Espressif ESP32-CS-DevKitM-1
×1
5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Yellow
5 mm LED: Yellow
×1
5 mm LED: Green
5 mm LED: Green
×1
LED, Blue
LED, Blue
×1
High Brightness LED, White
High Brightness LED, White
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×10

Software apps and online services

Firebase
Google Firebase
Realtime database needed

Story

Read more

Custom parts and enclosures

Video demostration

Schematics

Circuit diagram

Code

arduino_proximity_sensor_egfile.ino

Arduino
The code connects the proximity sensor to the Firebase database.

Under this set of code at the beginning:

#define _SSID "SSID" // Your WiFi SSID
#define _PASSWORD "PASSWORD" // Your WiFi Password
#define API_KEY "API_KEY"
#define USER_EMAIL "USER_EMAIL"
#define USER_PASSWORD "USER_PASSWORD"
#define DATABASE_URL "DATABASE_URL" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app
#define DATABASE_SECRET "DATABASE_SECRET"

Insert the relevant details, such as your WiFi and real-time database details.


Since the proximity sensor has 5 levels of distance perception, the database contains and relays the distance between each of these levels.

For example, when the database has a value of '5', it means that the different levels of depth perception would be as shown:

Let x be the distance, in cm, of an obstacle or object from the ultrasonic sensor.

White LED lights up: x > 20
Yellow LED lights up: 15 <= x < 20
Green LED lights up: 10 <= x < 15
Blue LED lights up: 5 <= x < 10
Red LED lights up: x < 5

So if the value in the database is changed to 10, the new ranges would be as shown:

White LED lights up: x > 40
Yellow LED lights up: 30 <= x < 40
Green LED lights up: 20 <= x < 30
Blue LED lights up: 10 <= x < 20
Red LED lights up: x < 10

Hence, the extent of proximity perception can be altered via the database for different scales of application for the sensor. (e.g. remote-controlled drone vs car)
#include <WiFi.h>
#include <FirebaseESP32.h>

// Provide the token generation process info.
#include <addons/TokenHelper.h>

// Provide the RTDB payload printing info and other helper functions.
#include <addons/RTDBHelper.h>
#include <WiFiMulti.h>

#define _SSID "SSID"          // Your WiFi SSID
#define _PASSWORD "PASSWORD"      // Your WiFi Password
#define API_KEY "API_KEY"
#define USER_EMAIL "USER_EMAIL"
#define USER_PASSWORD "USER_PASSWORD"
#define DATABASE_URL "DATABASE_URL" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app
#define DATABASE_SECRET "DATABASE_SECRET"

/* 6. Define the Firebase Data object */
FirebaseData fbdo;

/* 7. Define the FirebaseAuth data for authentication data */
FirebaseAuth auth;

/* 8. Define the FirebaseConfig data for config data */
FirebaseConfig config;

#define TRIG_PIN 18 // ESP32 pin GPIO23 connected to Ultrasonic Sensor's TRIG pin
#define ECHO_PIN 19 // ESP32 pin GPIO22 connected to Ultrasonic Sensor's ECHO pin

const int WhitePin = 4;
const int YellowPin = 5;
const int GreenPin = 6;
const int BluePin = 7;
const int RedPin = 8;
float duration_us, distance_cm;

const int Distance_4 = 20;
const int Distance_3 = 15;
const int Distance_2 = 10;
const int Distance_1 = 5;


void setup() {
  Serial.begin(115200);

  // pinMode(LED_BUILTIN, OUTPUT);
  // digitalWrite(LED_BUILTIN, LOW);
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(1000);

  // Connect to WiFi
  Serial.println();
  Serial.println();
  Serial.print("Connecting to: ");
  Serial.println(_SSID);
  WiFi.begin(_SSID, _PASSWORD);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print("-");
  }

  Serial.println("");
  Serial.println("WiFi Connected");

  // Print the IP address
  Serial.print("IP Address: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
  // digitalWrite(LED_BUILTIN, HIGH);

//================================================================//
    Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);

    /* Assign the api key (required) */
    config.api_key = API_KEY;
        /* Assign the RTDB URL */
    config.database_url = DATABASE_URL;
        /* Assign the user sign in credentials */
    auth.user.email = USER_EMAIL;
    auth.user.password = USER_PASSWORD;

    // Comment or pass false value when WiFi reconnection will control by your code or third party library e.g. WiFiManager
    Firebase.reconnectNetwork(true);

    // Since v4.4.x, BearSSL engine was used, the SSL buffer need to be set.
    // Large data transmission may require larger RX buffer, otherwise connection issue or data read time out can be occurred.
    fbdo.setBSSLBufferSize(4096 /* Rx buffer size in bytes from 512 - 16384 */, 1024 /* Tx buffer size in bytes from 512 - 16384 */);

    fbdo.setResponseSize(4096);

    String base_path = "/UsersData/";    
    config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
    Firebase.begin(&config, &auth);
    String var = "$userId";
    String val = "($userId === auth.uid && auth.token.premium_account === true && auth.token.admin === true)";
    Firebase.setReadWriteRules(fbdo, base_path, var, val, val, DATABASE_SECRET);



//================================================================//


  pinMode (WhitePin, OUTPUT);
  pinMode (YellowPin, OUTPUT);
  pinMode (GreenPin, OUTPUT);
  pinMode (BluePin, OUTPUT);
  pinMode (RedPin, OUTPUT);
  // configure the trigger pin to output mode
  pinMode(TRIG_PIN, OUTPUT);
  // configure the echo pin to input mode
  pinMode(ECHO_PIN, INPUT);
  
}

void loop() {

  // Distance_4_red = Firebase.setNumber("Distance_4");
  // Serial.println(Distance_4_red);

  int data_distance = 0;
  Serial.printf("Get int ref... %s\n", Firebase.getInt(fbdo, F("/user/setDist"), &data_distance) ? String(data_distance).c_str() : fbdo.errorReason().c_str());
  Serial.print("Received Int:\t\t");
  Serial.println(data_distance);

  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW); 
  // measure duration of pulse from ECHO pin
  duration_us = pulseIn(ECHO_PIN, HIGH);
  // Serial.println(duration_us);
  // calculate the distance
  distance_cm = 0.017 * duration_us;

  // print the value to Serial Monitor
  // Serial.print("distance: ");
  // Serial.print(distance_cm);
  // Serial.println(" cm");
  if (distance_cm < data_distance) {
    Serial.print("distance: ");
    Serial.print(distance_cm);
    Serial.println(" cm");
    digitalWrite (WhitePin, HIGH); // turn on the LED
    digitalWrite (YellowPin, HIGH); // turn on the LED
    digitalWrite (GreenPin, HIGH); // turn on the LED
    digitalWrite (BluePin, HIGH); // turn on the LED
    digitalWrite (RedPin, HIGH); // turn on the LED
  }
  else if (distance_cm< data_distance*2) {
    Serial.print("distance: ");
    Serial.print(distance_cm);
    Serial.println(" cm");
    digitalWrite (WhitePin, HIGH); // turn on the LED
    digitalWrite (YellowPin, HIGH); // turn on the LED
    digitalWrite (GreenPin, HIGH); // turn on the LED
    digitalWrite (BluePin, HIGH); // turn on the LED
    digitalWrite (RedPin, LOW); // turn off the LED
  }
  else if (distance_cm<data_distance*3) {
    Serial.print("distance: ");
    Serial.print(distance_cm);
    Serial.println(" cm");
    digitalWrite (WhitePin, HIGH); // turn on the LED
    digitalWrite (YellowPin, HIGH); // turn on the LED
    digitalWrite (GreenPin, HIGH); // turn on the LED
    digitalWrite (BluePin, LOW); // turn off the LED
    digitalWrite (RedPin, LOW); // turn off the LED
  }
  else if (distance_cm<data_distance*4) {
    Serial.print("distance: ");
    Serial.print(distance_cm);
    Serial.println(" cm");
    digitalWrite (WhitePin, HIGH); // turn on the LED
    digitalWrite (YellowPin, HIGH); // turn on the LED
    digitalWrite (GreenPin, LOW); // turn on the LED
    digitalWrite (BluePin, LOW); // turn off the LED
    digitalWrite (RedPin, LOW); // turn off the LED
  }
    else if (distance_cm<data_distance*5) {
    Serial.print("distance: ");
    Serial.print(distance_cm);
    Serial.println(" cm");
    digitalWrite (WhitePin, HIGH); // turn on the LED
    digitalWrite (YellowPin, LOW); // turn on the LED
    digitalWrite (GreenPin, LOW); // turn on the LED
    digitalWrite (BluePin, LOW); // turn off the LED
    digitalWrite (RedPin, LOW); // turn off the LED
  }
}

Credits

Chow Liang Zhi

Chow Liang Zhi

1 project • 0 followers

Comments