2023年9月6日 星期三

ESP32 Laser Indicator Ranging Box

Purpose:

Ultrasonic sensors measure distance by sending and receiving the ultrasonic wave and use Laser module 5mW 650nm to indicate the object then show the distance value on LCD module.

利用ESP32做一個雷射對標然後在HC-SR04 ultrasonic sensor模組的範圍內去measure物體所在的距離並且顯示距離在LCD面板上.

Use ESP32 to make a laser benchmark and then measure the distance of the object within the range of the HC-SR04 ultrasonic sensor module and display the distance on the LCD panel.

BOM(Bill of Material):

HC-SR04 ultrasonic sensor


Laser module 5mW 650nm:

ESP32 module
LCD module


Circuit:




Pin Definition:

YouTube Demo:

Code Introduce:
#include <LiquidCrystal.h>
//---LCD -----------
//LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
LiquidCrystal LCD(22,23,5,18,19,21);
//------LED------------------
#define LED_BUILTIN 2
//-----switch------------------
#define MODE_PIN    27
#define LASER_PIN   26
//-----hcsr04 sensor------------------
#define TRIGPIN_PIN  12
#define ECHO_PIN    14
long duration;
int distance;
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
//--------- Flag structure --------------------------------------
typedef struct _vFlag
{
  uint8_t HCSR04Flag = 0;
  uint8_t LEDFlag = 1;
} vFlag;
vFlag *flag_Ptr;
vFlag flag;
//-------------------------------------------------
void setup()
{
  Serial.begin(9600);
  Serial.println(F("init"));
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(TRIGPIN_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(LASER_PIN, OUTPUT);
  pinMode(MODE_PIN, INPUT_PULLUP);
  LCD.begin(20, 2);
  LCD.clear();
  LCD.setCursor(0, 0);
  LCD.print("Distance (cm) ");
  spinner();
  LCD.setCursor(0, 1);
  LCD.print("------------- ");
}
//-----------------------------------------
void loop()
{
  Serial.print(F("Main at core:"));
  Serial.println(xPortGetCoreID());
  while(1)
  {
    if(flag.LEDFlag == 1)
    {
      digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
      vTaskDelay(300);
      spinner();
      digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
      vTaskDelay(300);
    }
    if ((digitalRead(MODE_PIN) == LOW))
    {
      flag.HCSR04Flag = 1;
      flag.LEDFlag = 0;
      digitalWrite(LASER_PIN, HIGH);      
    }
    else if((digitalRead(MODE_PIN) == HIGH))
    {
      flag.HCSR04Flag = 0;
      flag.LEDFlag =1;
      digitalWrite(LASER_PIN, LOW);
      LCD.clear();
      LCD.setCursor(0, 0);
      LCD.print("Distance (cm) ");
      //spinner();
      LCD.setCursor(0, 1);
      LCD.print("------------- ");
    }
   
    if(flag.HCSR04Flag==1)
    {
      currentMillis = millis();
     
      pinMode(TRIGPIN_PIN, OUTPUT);
      digitalWrite(TRIGPIN_PIN, LOW);  //關閉超音波
      delayMicroseconds(2);  //sustain at least 10us HIGH pulse
      digitalWrite(TRIGPIN_PIN, HIGH); //啟動超音波
      delayMicroseconds(10);  //sustain at least 10us HIGH pulse
      digitalWrite(TRIGPIN_PIN, LOW);  //關閉超音波
      pinMode(ECHO_PIN, INPUT);
      duration= pulseIn(ECHO_PIN, HIGH);
      distance= duration/29/2;
      if (duration==0)
      {
        Serial.println("No pulse is from sensor");
      }
      else {
        Serial.print("Ultrasonic sensor is shown distance:");
        Serial.print(distance);
        Serial.println("cm");
        LCD.clear();
        LCD.setCursor(0, 0);
        LCD.print("Distance (cm) ");
        spinner();
        LCD.setCursor(0, 1);
        LCD.print(distance);
      }
    }
  }
}
//----------------------------------------------
void spinner()
{
  static int8_t counter = 0;
  const char* glyphs = "\xa1\xa5\xdb";
  LCD.setCursor(15, 0);
  LCD.print(glyphs[counter++]);
  if (counter == strlen(glyphs)) {
    counter = 0;
  }
}
//-----------------------------------------

沒有留言:

張貼留言