4_Arduino-ESP32_超聲波傳感器,MPU6050,bluetooth

1_超聲波傳感器


1.1_SR04+OLED-I2C-0.96(SSD1306)

下載並把庫放在C:\Program Files (x86)\Arduino\libraries
Adafruit-GFX-Library
Adafruit_SSD1306git

SCL=22
SDA=21github

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// defines pins numbers
const int trigPin = 2;
const int echoPin = 5;

// defines variables
long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
  display.clearDisplay();
  
}

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);

// Calculating the distance
distance= duration*0.034/2;

  display.clearDisplay();
  
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(40, 30);
  // Display static text
  display.print(distance);
  display.println("CM");
  display.display();
  
  delay(500);
  display.clearDisplay();
}
複製代碼

2_MPU6050


下載並把庫放在C:\Program Files (x86)\Arduino\libraries
MPU6050_tocknbash

VCC = 5V
SCL = 22
SDA = 21dom

#include <MPU6050_tockn.h>
#include <Wire.h>

MPU6050 mpu6050(Wire);

void setup() {
  Serial.begin(9600);
  Wire.begin();
  mpu6050.begin();
  mpu6050.calcGyroOffsets(true);
}

void loop() {
  mpu6050.update();
  Serial.print("angleX : ");
  Serial.print(mpu6050.getAngleX());
  Serial.print("\tangleY : ");
  Serial.print(mpu6050.getAngleY());
  Serial.print("\tangleZ : ");
  Serial.println(mpu6050.getAngleZ());
}
複製代碼

3_bluetooth


3.1_藍牙點燈

請參考這篇博客ide

請參考這個視頻oop

// Load libraries
#include "BluetoothSerial.h"

// Check if Bluetooth configs are enabled
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

// Bluetooth Serial object
BluetoothSerial SerialBT;

// GPIO where LED is connected to
const int pinout =4;

char inputdata = 0;  //Variable for storing received data


void setup() {
  pinMode(pinout, OUTPUT);
  Serial.begin(115200);
  // Bluetooth device name
  SerialBT.begin("ESP32");
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
    if(SerialBT.available() > 0)      // Send data only when you receive data:
   {
      inputdata = SerialBT.read();        //Read the incoming data & store into data
           
      if(inputdata == '1') 
      {
         digitalWrite(pinout, HIGH); 
         SerialBT.print("LED ON\n");
      }
         
      else if(inputdata == '0')  
      {      
         digitalWrite(pinout, LOW);  
         SerialBT.print("LED OFF\n");  
      }
   }
   
}
複製代碼

3.2_藍牙溫溼度

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include "BluetoothSerial.h"

// Check if Bluetooth configs are enabled
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

// Bluetooth Serial object
BluetoothSerial SerialBT;

#define DHTPIN 4 // Digital pin connected to the DHT sensor

// Uncomment the type of sensor in use:
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

char inputdata = 0; 


void setup() {
  Serial.begin(115200);
  dht.begin();
  // Bluetooth device name
  SerialBT.begin("ESP32");
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  
    if(SerialBT.available() > 0)     
   {
      inputdata = SerialBT.read();
           
      if(inputdata == '1') 
      {
         SerialBT.print(t);
      }
         
      else if(inputdata == '0')  
      {       
         SerialBT.print(h);  
      }
   }
   
}
複製代碼

相關文章
相關標籤/搜索