ข้ามไปที่เนื้อหาหลัก

ส่งงานครั้งที่ 9

Gyro Sensor Interfacing

Gyro Sensor คือเซ็นเซอร์ที่มีไว้สำหรับตรวจจับลักษณะการหมุน โดยเป็นการตรวจจับแบบ แกน (3-Axes)


อุปกรณ์

  1. Arduino Uno
  2. MPU-6050
  3. 10K POT
  4. Jumper wire
  5. Breadboard
  6. USB cable
  7. Power supply
Diagram



Code

#include<LiquidCrystal.h> //ประกาศเมื่อใช้จอ
LiquidCrystal lcd(8,9,10,11,12,13); //ขาที่ต่อจอ
#include <Wire.h>
#include <MPU6050.h>

#define period 10000

MPU6050 mpu;

int count=0;
char okFlag=0;

byte degree[8] = {
  0b00000,
  0b00110,
  0b01111,
  0b00110,
  0b00000,
  0b00000,
  0b00000,
  0b00000
};

void setup() 
{
  lcd.begin(16,2);
  lcd.createChar(0, degree);
  Serial.begin(9600);
  Serial.println("Initialize MPU6050");
  while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
  {
    lcd.clear();
    lcd.print("Device not Found");
    Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
    delay(500);
  }
  count=0;

  mpu.calibrateGyro();
  mpu.setThreshold(3);
  
  lcd.clear();
  lcd.print("MPU6050 Interface");
  lcd.setCursor(0,1);
  lcd.print(" Circuit Digest");
  delay(2000);
  lcd.clear();
}

void loop()
{
    lcd.clear();
    lcd.print("Temperature");
    long st=millis();
    Serial.println("Temperature");
    while(millis()<st+period)
    {
      lcd.setCursor(0,1);
      tempShow();
    }
    
    lcd.clear();
    lcd.print("Gyro");
    delay(2000);
    st=millis();
    Serial.println("Gyro");
    while(millis()<st+period)
    {
      lcd.setCursor(0,1);
      gyroShow();
    }

    lcd.clear();
    lcd.print("Accelerometer");
    delay(2000);
    st=millis();
    Serial.println("Accelerometer");
    while(millis()<st+period)
    {
      lcd.setCursor(0,1);
      accelShow();
    }
}

void tempShow()
{
    float temp = mpu.readTemperature();
    Serial.print(" Temp = ");
    Serial.print(temp);
    Serial.println(" *C");
    lcd.clear();
    lcd.print("Temperature");
    lcd.setCursor(0,1);
    lcd.print(temp);
    lcd.write((byte)0);
    lcd.print("C");
    delay(400);
}

void gyroShow()
{
  //lcd.setCursor(0,0); //กำหนดตำแหน่งLCD
  lcd.clear();
  lcd.print(" X     Y     Z");
  Vector rawGyro = mpu.readRawGyro();
  Vector normGyro = mpu.readNormalizeGyro();
  lcd.setCursor(0,1);
  lcd.print(normGyro.XAxis,1);
  lcd.setCursor(6,1);
  lcd.print(normGyro.YAxis,1);
  lcd.setCursor(12,1);
  lcd.print(normGyro.ZAxis,1);
  Serial.print(" Xnorm = ");
  Serial.print(normGyro.XAxis);
  Serial.print(" Ynorm = ");
  Serial.print(normGyro.YAxis);
  Serial.print(" Znorm = ");
  Serial.println(normGyro.ZAxis);
  delay(200);
}

void accelShow()
{
 // lcd.setCursor(0,0); //กำหนดตำแหน่งLCD
  lcd.clear();
  lcd.print(" X     Y     Z");
  Vector rawAccel = mpu.readRawAccel();
  Vector normAccel = mpu.readNormalizeAccel();
  lcd.setCursor(0,1);
  lcd.print(normAccel.XAxis,1);
  lcd.setCursor(6,1);
  lcd.print(normAccel.YAxis,1);
  lcd.setCursor(12,1);
  lcd.print(normAccel.ZAxis,1);
  Serial.print(" Xnorm = ");
  Serial.print(normAccel.XAxis);
  Serial.print(" Ynorm = ");
  Serial.print(normAccel.YAxis);
  Serial.print(" Znorm = ");
  Serial.println(normAccel.ZAxis);
  delay(200);
}

Credit By : https://circuitdigest.com/microcontroller-projects/mpu6050-gyro-sensor-interfacing-with-arduino

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

ส่งงานครั้งที่ 17

Air Pollution Monitoring อุปกรณ์ MQ135 Gas sensor Arduino Uno Wi-Fi module ESP8266 16X2 LCD Breadboard 10K potentiometer 1K ohm resistors 220 ohm resistor Buzzer Diagram Code #include "MQ135.h" #include <SoftwareSerial.h> #define DEBUG true SoftwareSerial esp8266(9,10); // กำหนดขา 9 ของ Arduino เป็น RX pin และ ขา 10 ของ Arduino เป็น the TX pin const int sensorPin= 0; int air_quality; #include <LiquidCrystal.h> LiquidCrystal lcd(12,11, 5, 4, 3, 2); void setup() { pinMode(8, OUTPUT); lcd.begin(16,2); lcd.setCursor (0,0); lcd.print ("circuitdigest "); lcd.setCursor (0,1); lcd.print ("Sensor Warming "); delay(1000); Serial.begin(115200); esp8266.begin(115200);   sendData("AT+RST\r\n",2000,DEBUG); // reset module   sendData("AT+CWMODE=2\r\n",1000,DEBUG); // กำหนดค่า  access point   sendData("AT+CIFSR\r\n",1000,DEBUG); // รับ ip address   sendData("AT+CIPMUair_quali

ส่งงานครั้งที่ 18

12 V Battery Charger อุปกรณ์ Transformer 12V 1Amp IC LM317 (2) Diode Bridge W005 Connector Terminal Block (2) Capacitor 1000uF, 1uF Capacitor 0.1uF (5) Variable resistor 100R Resistor 1k (5) Resistor 10k Diode- Nn007  (3) LM358 – Opamp 0.05R - Shunt Resistor/wire LCD-16*2 (optional) Arduino Nano (optional) Diagram Code #include <LiquidCrystal.h>// ประกาศเมื่อใช้LCD LiquidCrystal lcd(11, 12, 10, 9, 8, 7); float voltage,current;  void setup() {   Serial.begin(9600);   //ตั้งค่าLCD   lcd.begin(16, 2);   // Print a message to the LCD.   lcd.setCursor(0, 0);   lcd.print("12V Charger");   lcd.setCursor(0, 1);   lcd.print("-Circuit Digest");   delay(2000);   lcd.clear();   lcd.setCursor(0, 0);   lcd.print("Voltage = ");   lcd.setCursor(0, 1);   lcd.print("Current = "); } void loop() {   voltage = (analogRead(A0)) * 0.0140625;   current = (analogRead(A1)) * 0.35;           lcd.setCursor(1

ส่งงานครั้งที่ 15

Arduino Solar Tracker อุปกรณ์ Servo Motor (sg90) Solar panel Arduino Uno LDR’s X 2 (Light Dependent Resistor) 10K resistors X 2 Battery (6 to 12V) Diagram Code #include <Servo.h>      //ประกาศเมื่อมีการใช้Servo Servo sg90;             //ตั้งค่าServoด้วยตัวแปรsg90 int initial_position = 90;   //ประกาศตัวแปรไว้ที่ค่า 90 int LDR1 = A0;          //ขาที่LDRต่อไว้ int LDR2 = A1;          //ขาที่LDR2ต่อไว้ int error = 5;          //ตั้งค่าเริ่มต้นของ error ที่ 5 int servopin=9; void setup() {    sg90.attach(servopin);  // ให้ servo ที่ขา 9   pinMode(LDR1, INPUT);   //ให้ขาที่ต่อกับLDRเป็นInput   pinMode(LDR2, INPUT);   sg90.write(initial_position);   //ขยับServoไว้ที่90องศา   delay(2000);            // กำหนดdelay2วินาที } void loop() {   int R1 = analogRead(LDR1); // อ่านค่า LDR 1   int R2 = analogRead(LDR2); // อ่านค่า LDR 2   int diff1= abs(R1 - R2);   // คำนวณค่าการเปลี่ยนแปลงของ LDR's   int diff2= abs(R2 - R1);   if((diff1 <=